diff options
author | KikooDX <kikoodx@paranoici.org> | 2022-08-31 15:47:54 +0200 |
---|---|---|
committer | KikooDX <kikoodx@paranoici.org> | 2022-08-31 15:47:54 +0200 |
commit | 8370726562342ba72efe32986e2ad9dff8472ca2 (patch) | |
tree | 243b5d4ed29fc29bccbb2a065d42f8439fad1557 | |
parent | db5dfef1521ebc695559ccaf365ba472ec5b0754 (diff) | |
download | scr16-8370726562342ba72efe32986e2ad9dff8472ca2.tar.gz |
get rid of libdx dependency
-rw-r--r-- | audio.c | 2 | ||||
-rw-r--r-- | dx_alloc.c | 17 | ||||
-rw-r--r-- | dx_free.c | 8 | ||||
-rw-r--r-- | dx_log.c | 40 | ||||
-rw-r--r-- | dx_log.h | 18 | ||||
-rw-r--r-- | dx_mem.h | 8 | ||||
-rw-r--r-- | main.c | 4 | ||||
-rw-r--r-- | render.c | 2 | ||||
-rw-r--r-- | screenshot.c | 2 | ||||
-rw-r--r-- | sdl_init.c | 2 | ||||
-rw-r--r-- | texture.c | 4 |
11 files changed, 99 insertions, 8 deletions
@@ -1,9 +1,9 @@ #include "audio.h" #include "cmixer.h" +#include "dx_log.h" #include <SDL2/SDL_audio.h> #include <SDL2/SDL_error.h> #include <SDL2/SDL_mutex.h> -#include <dx/log.h> static SDL_mutex *audio_mutex = NULL; static SDL_AudioDeviceID dev = 0; diff --git a/dx_alloc.c b/dx_alloc.c new file mode 100644 index 0000000..870d454 --- /dev/null +++ b/dx_alloc.c @@ -0,0 +1,17 @@ +#include "dx_log.h" +#include "dx_mem.h" +#include <stdlib.h> + +void *dx_alloc(size_t size) +{ + if (size == 0) { + dx_log_warn("size is 0"); + return NULL; + } + void *const ptr = calloc(1, size); + if (ptr == NULL) { + dx_log_error("calloc failed"); + return NULL; + } + return ptr; +} diff --git a/dx_free.c b/dx_free.c new file mode 100644 index 0000000..8b8c8ef --- /dev/null +++ b/dx_free.c @@ -0,0 +1,8 @@ +#include "dx_mem.h" +#include <stdlib.h> + +void dx_free(void *ptr) +{ + if (ptr != NULL) + free(ptr); +} diff --git a/dx_log.c b/dx_log.c new file mode 100644 index 0000000..130f957 --- /dev/null +++ b/dx_log.c @@ -0,0 +1,40 @@ +#include "dx_log.h" +#include <stdarg.h> +#include <stdio.h> +#include <time.h> + +static const char *level_color[] = { + [DX_LOG_TRACE] = "\x1b[34m", [DX_LOG_INFO] = "\x1b[32m", + [DX_LOG_WARN] = "\x1b[33m", [DX_LOG_ERROR] = "\x1b[35m", + [DX_LOG_FATAL] = "\x1b[31m", +}; +static const char *level_strings[] = { + [DX_LOG_TRACE] = "TRACE", [DX_LOG_INFO] = "INFO", [DX_LOG_WARN] = "WARN", + [DX_LOG_ERROR] = "ERROR", [DX_LOG_FATAL] = "FATAL", +}; +static int _log_level = 0; + +void dx_log_set_min_level(int level) +{ + _log_level = level; +} + +void dx_log_log(int level, const char *file, const char *func, int line, + const char *fmt, ...) +{ + if (level < _log_level) + return; + + /* format to string */ + va_list valist; + va_start(valist, fmt); + char buf[2048] = {0}; + buf[sizeof(buf) - 1] = '\0'; + vsnprintf(buf, sizeof(buf) - 1, fmt, valist); + va_end(valist); + + fputs(level_color[level], stdout); + printf("%-5s %s:%d:%s: %s", level_strings[level], file, line, func, + buf); + fputs("\x1b[0m\n", stdout); +} diff --git a/dx_log.h b/dx_log.h new file mode 100644 index 0000000..67eb86f --- /dev/null +++ b/dx_log.h @@ -0,0 +1,18 @@ +#pragma once + +enum { DX_LOG_TRACE, DX_LOG_INFO, DX_LOG_WARN, DX_LOG_ERROR, DX_LOG_FATAL }; + +#define dx_log_trace(...) \ + dx_log_log(DX_LOG_TRACE, __FILE__, __func__, __LINE__, __VA_ARGS__) +#define dx_log_info(...) \ + dx_log_log(DX_LOG_INFO, __FILE__, __func__, __LINE__, __VA_ARGS__) +#define dx_log_warn(...) \ + dx_log_log(DX_LOG_WARN, __FILE__, __func__, __LINE__, __VA_ARGS__) +#define dx_log_error(...) \ + dx_log_log(DX_LOG_ERROR, __FILE__, __func__, __LINE__, __VA_ARGS__) +#define dx_log_fatal(...) \ + dx_log_log(DX_LOG_FATAL, __FILE__, __func__, __LINE__, __VA_ARGS__) + +void dx_log_set_min_level(int level); +void dx_log_log(int level, const char *file, const char *func, int line, + const char *fmt, ...); diff --git a/dx_mem.h b/dx_mem.h new file mode 100644 index 0000000..c8a4fea --- /dev/null +++ b/dx_mem.h @@ -0,0 +1,8 @@ +#pragma once +#include <stddef.h> + +/* dx_alloc allocates size bytes and returns a pointer to the allocated memory. + * Allocated memory is set to 0. Returns NULL if creation failed. */ +void *dx_alloc(size_t size); + +void dx_free(void *ptr); @@ -1,13 +1,13 @@ #include "audio.h" #include "beep.h" #include "cmixer.h" +#include "dx_log.h" +#include "dx_mem.h" #include "fe.h" #include "input.h" #include "render.h" #include "screenshot.h" #include <SDL2/SDL_events.h> -#include <dx/log.h> -#include <dx/mem.h> #include <errno.h> #include <math.h> #include <stdio.h> @@ -1,9 +1,9 @@ #include "render.h" +#include "dx_log.h" #include "sdl_init.h" #include "texture.h" #include <SDL2/SDL.h> #include <SDL2/SDL_image.h> -#include <dx/log.h> #include <stdint.h> static SDL_Window *window = NULL; diff --git a/screenshot.c b/screenshot.c index 50fbf7f..85b58ef 100644 --- a/screenshot.c +++ b/screenshot.c @@ -1,7 +1,7 @@ #include "screenshot.h" +#include "dx_log.h" #include "render.h" #include "sfd.h" -#include <dx/log.h> #include <stddef.h> void screenshot(void) @@ -1,7 +1,7 @@ #include "sdl_init.h" +#include "dx_log.h" #include <SDL2/SDL.h> #include <SDL2/SDL_image.h> -#include <dx/log.h> int sdl_init(void) { @@ -1,8 +1,8 @@ #include "texture.h" +#include "dx_log.h" +#include "dx_mem.h" #include "render.h" #include <SDL2/SDL_render.h> -#include <dx/log.h> -#include <dx/mem.h> static void set_infos(struct Texture *); static void set_blend(struct Texture *); |