alistair
3 years ago
8 changed files with 11 additions and 214 deletions
@ -1,65 +0,0 @@ |
|||||||
#include "garbo.h" |
|
||||||
|
|
||||||
static sdl_abstract cleanup_queue[100]; |
|
||||||
static int cleanup_queue_length = 0; |
|
||||||
|
|
||||||
void queue_for_cleanup(void * SDL_thing, sdl_types kind) { |
|
||||||
// add item to queue for later cleanup
|
|
||||||
// TODO: support multiple cleanup queues
|
|
||||||
|
|
||||||
sdl_abstract obj; |
|
||||||
obj.kind = kind; |
|
||||||
switch (kind) { |
|
||||||
case TEXTURE: |
|
||||||
obj.thing.texture = SDL_thing; |
|
||||||
break; |
|
||||||
case WINDOW: |
|
||||||
obj.thing.window = SDL_thing; |
|
||||||
break; |
|
||||||
case RENDERER: |
|
||||||
obj.thing.renderer = SDL_thing; |
|
||||||
break; |
|
||||||
case SURFACE: |
|
||||||
obj.thing.surface = SDL_thing; |
|
||||||
break; |
|
||||||
} |
|
||||||
|
|
||||||
cleanup_queue_length++;
|
|
||||||
cleanup_queue[cleanup_queue_length] = obj; |
|
||||||
} |
|
||||||
|
|
||||||
void cleanup(void * thing, sdl_types thing_type) { |
|
||||||
// Destroy an SDL object immediately
|
|
||||||
if (!thing) { |
|
||||||
logwrite(DEBUG, "Null passed to cleanup\n"); |
|
||||||
return; |
|
||||||
} |
|
||||||
switch(thing_type) { |
|
||||||
case SURFACE: |
|
||||||
SDL_FreeSurface(thing); |
|
||||||
logwrite(DEBUG, "Freed surface\n"); |
|
||||||
break; |
|
||||||
case TEXTURE: |
|
||||||
SDL_DestroyTexture(thing); |
|
||||||
logwrite(DEBUG, "Destroyed texture\n"); |
|
||||||
break; |
|
||||||
case RENDERER: |
|
||||||
SDL_DestroyRenderer(thing); |
|
||||||
logwrite(DEBUG, "Destroyed renderer\n"); |
|
||||||
break; |
|
||||||
case WINDOW: |
|
||||||
SDL_DestroyWindow(thing); |
|
||||||
logwrite(DEBUG, "Destroyed window\n"); |
|
||||||
break; |
|
||||||
case NO: |
|
||||||
return; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void empty_cleanup_queue(void) { |
|
||||||
// empty the queue of items scheduled for cleanup
|
|
||||||
for (; cleanup_queue_length > 0; cleanup_queue_length--) { |
|
||||||
sdl_abstract thing = cleanup_queue[cleanup_queue_length]; |
|
||||||
cleanup(thing.thing.generic, thing.kind); |
|
||||||
} |
|
||||||
} |
|
@ -1,41 +0,0 @@ |
|||||||
|
|
||||||
#ifndef _DEFGARBO |
|
||||||
#define _DEFGARBO |
|
||||||
|
|
||||||
#include <stdlib.h> |
|
||||||
#include <string.h> |
|
||||||
#include <SDL2/SDL.h> |
|
||||||
#include <string.h> |
|
||||||
#include "logger.h" |
|
||||||
#include <stdbool.h> |
|
||||||
|
|
||||||
|
|
||||||
/* Type definition for object cleanups */ |
|
||||||
typedef enum { |
|
||||||
NO, SURFACE, TEXTURE, RENDERER, WINDOW |
|
||||||
} sdl_types; |
|
||||||
|
|
||||||
typedef union { |
|
||||||
SDL_Window * window; |
|
||||||
SDL_Renderer * renderer; |
|
||||||
SDL_Surface * surface; |
|
||||||
SDL_Texture * texture; |
|
||||||
void * generic; |
|
||||||
} thing_for_cleanup ; |
|
||||||
|
|
||||||
typedef struct { |
|
||||||
sdl_types kind; |
|
||||||
thing_for_cleanup thing; |
|
||||||
} sdl_abstract; |
|
||||||
|
|
||||||
|
|
||||||
/* Function prototypes */ |
|
||||||
|
|
||||||
void queue_for_cleanup(void * SDL_thing, sdl_types kind);
|
|
||||||
|
|
||||||
void cleanup(void * thing, sdl_types thing_type);
|
|
||||||
|
|
||||||
void empty_cleanup_queue(void);
|
|
||||||
|
|
||||||
#endif |
|
||||||
|
|
@ -1,70 +0,0 @@ |
|||||||
#include "logger.h" |
|
||||||
|
|
||||||
// Defaults
|
|
||||||
|
|
||||||
loggerlevel LOGLEVEL = SILENT; |
|
||||||
loggerlevel STDOUTLEVEL = SILENT; |
|
||||||
|
|
||||||
void set_loglevel(loggerlevel echo, loggerlevel write) { |
|
||||||
LOGLEVEL = write; |
|
||||||
STDOUTLEVEL = echo; |
|
||||||
} |
|
||||||
|
|
||||||
int copy_str(char * out_string, char * string) { |
|
||||||
/* A drop in replacement for strcpy() from <string.h>*/ |
|
||||||
int len = 0; |
|
||||||
while (*(string + len) != '\0') { |
|
||||||
*(out_string + len) = *(string + len); |
|
||||||
len++; |
|
||||||
} |
|
||||||
*(out_string + len) = '\0'; |
|
||||||
return (1); |
|
||||||
} |
|
||||||
|
|
||||||
void logwrite(loggerlevel level, char message[]) { |
|
||||||
// write a string message
|
|
||||||
char time_str[100]; |
|
||||||
time_t current_time; |
|
||||||
time(¤t_time);
|
|
||||||
|
|
||||||
copy_str(time_str, asctime(localtime(¤t_time))); |
|
||||||
|
|
||||||
// delete the \n
|
|
||||||
time_str[24] = ' '; |
|
||||||
|
|
||||||
char prepend[150]; |
|
||||||
copy_str(prepend, time_str); |
|
||||||
|
|
||||||
|
|
||||||
switch (level) { |
|
||||||
case DEBUG: |
|
||||||
strcat(prepend, "DEBUG"); |
|
||||||
break; |
|
||||||
case INFO: |
|
||||||
strcat(prepend, "INFO "); |
|
||||||
break; |
|
||||||
case WARN: |
|
||||||
strcat(prepend, "WARN "); |
|
||||||
break; |
|
||||||
case ERROR: |
|
||||||
strcat(prepend, "ERROR"); |
|
||||||
break; |
|
||||||
case SILENT:
|
|
||||||
return; |
|
||||||
} |
|
||||||
|
|
||||||
strcat(prepend, ": "); |
|
||||||
|
|
||||||
if (level <= LOGLEVEL) { |
|
||||||
FILE * logfile = fopen("debug.log", "a"); |
|
||||||
fprintf(logfile, "%s", prepend); |
|
||||||
fprintf(logfile, message); |
|
||||||
fclose(logfile); |
|
||||||
} |
|
||||||
|
|
||||||
if (level <= STDOUTLEVEL) { |
|
||||||
printf("%s", prepend); |
|
||||||
printf(message); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -1,23 +0,0 @@ |
|||||||
#include <time.h> |
|
||||||
#include <string.h> |
|
||||||
#include <stdio.h> |
|
||||||
|
|
||||||
#ifndef _DEFLOGGER |
|
||||||
#define _DEFLOGGER |
|
||||||
|
|
||||||
|
|
||||||
typedef enum { |
|
||||||
// high-low priority
|
|
||||||
// low-high verbosity
|
|
||||||
SILENT, ERROR, WARN, INFO, DEBUG |
|
||||||
} loggerlevel; |
|
||||||
|
|
||||||
|
|
||||||
extern loggerlevel LOGLEVEL; |
|
||||||
extern loggerlevel STDOUTLEVEL; |
|
||||||
|
|
||||||
void logwrite(loggerlevel level, char message[]); |
|
||||||
|
|
||||||
void set_loglevel(loggerlevel echo, loggerlevel write); |
|
||||||
|
|
||||||
#endif |
|
Loading…
Reference in new issue