From 1af73284f61c6a07ee04fb7f1e7874f31b42750f Mon Sep 17 00:00:00 2001 From: Emil Kosz Date: Sun, 8 Jun 2025 11:24:06 +0200 Subject: [PATCH] Switch to GLib logging --- game/src/gl/program.c | 20 ++++++++++---------- game/src/gl/program.h | 3 +-- game/src/graphics.c | 16 ++++++++-------- game/src/resources.c | 14 +++++++------- game/src/resources.h | 1 + game/src/ui/uielement.c | 4 ++-- game/src/ui/uielement.h | 2 +- game/src/util.h | 22 +++++++++++++--------- 8 files changed, 43 insertions(+), 39 deletions(-) diff --git a/game/src/gl/program.c b/game/src/gl/program.c index a912390..a9043d1 100644 --- a/game/src/gl/program.c +++ b/game/src/gl/program.c @@ -21,12 +21,12 @@ shader_fail (int shader) { char *info = malloc (sizeof (char) * info_len); glGetShaderInfoLog (shader, info_len, NULL, info); - WARN ("Compiling %s shader failed:\n%s", type, info); + g_warning ("Compiling %s shader failed:\n%s", type, info); free (info); } else { - WARN ("Compiling %s shader failed without information.", type); + g_warning ("Compiling %s shader failed without information.", type); } } @@ -40,12 +40,12 @@ program_fail (int program) { char *info = malloc (sizeof (char) * info_len); glGetProgramInfoLog (program, info_len, NULL, info); - WARN ("Linking program failed:\n%s", info); + g_warning ("Linking program failed:\n%s", info); free (info); } else { - WARN ("Linking program failed without information."); + g_warning ("Linking program failed without information."); } } @@ -72,7 +72,7 @@ program_create (const char *v_src, const char *f_src) glLinkProgram (program); glGetProgramiv (program, GL_LINK_STATUS, &success); if (!success) program_fail (program); - else INFO ("Compiled shaders and linked program %d successfuly.", program); + else g_info ("Compiled shaders and linked program %d successfuly.", program); glDeleteShader (vertex); glDeleteShader (fragment); @@ -144,7 +144,7 @@ void program_print (int program) /* This function assumes that `program' is the active program. */ { - INFO ("== Program %d ==", program); + g_info ("== Program %d ==", program); int attr_len; glGetProgramiv (program, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &attr_len); @@ -157,7 +157,7 @@ program_print (int program) for (GLint i = 0; i < attribute_no; i++) { glGetActiveAttrib (program, i, attr_len, NULL, NULL, &type, attr); - INFO ("Attribute #%d: %s %s", i, gl_type_to_string (type), attr); + g_info ("Attribute #%d: %s %s", i, gl_type_to_string (type), attr); } int uniform_len; @@ -176,16 +176,16 @@ program_print (int program) { case GL_FLOAT_VEC3: glGetUniformfv (program, glGetUniformLocation (program, uniform), vec); - INFO ("Uniform #%d: %s %s = (%5.1f,%5.1f,%5.1f)", i, gl_type_to_string (type), + g_info ("Uniform #%d: %s %s = (%5.1f,%5.1f,%5.1f)", i, gl_type_to_string (type), uniform, vec[0], vec[1], vec[2]); break; case GL_FLOAT_VEC4: glGetUniformfv (program, glGetUniformLocation (program, uniform), vec); - INFO ("Uniform #%d: %s %s = (%5.1f,%5.1f,%5.1f,%5.1f)", i, gl_type_to_string (type), + g_info ("Uniform #%d: %s %s = (%5.1f,%5.1f,%5.1f,%5.1f)", i, gl_type_to_string (type), uniform, vec[0], vec[1], vec[2], vec[3]); break; default: - INFO ("Uniform #%d: %s %s", i, gl_type_to_string (type), uniform); + g_info ("Uniform #%d: %s %s", i, gl_type_to_string (type), uniform); break; } } diff --git a/game/src/gl/program.h b/game/src/gl/program.h index bd81e52..61f2c01 100644 --- a/game/src/gl/program.h +++ b/game/src/gl/program.h @@ -2,8 +2,7 @@ #include #include - -#include "../util.h" +#include unsigned int program_use (int program); void shader_fail (int shader); diff --git a/game/src/graphics.c b/game/src/graphics.c index 3674f45..f555f51 100644 --- a/game/src/graphics.c +++ b/game/src/graphics.c @@ -20,14 +20,14 @@ graphics_init () { if (!SDL_Init (SDL_INIT_VIDEO)) { - FATAL ("Couldn't initialize SDL: %s", SDL_GetError ()); + g_error ("Couldn't initialize SDL: %s", SDL_GetError ()); } sdl_window = SDL_CreateWindow (GAME_NAME, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL); if (!sdl_window) { - FATAL ("Couldn't create window: %s", SDL_GetError ()); + g_error ("Couldn't create window: %s", SDL_GetError ()); } /* We use OpenGL ES 2.0. It should support everything, and it's more @@ -42,16 +42,16 @@ graphics_init () if (!gl_context) { - FATAL ("Couldn't create OpenGL context: %s", SDL_GetError ()); + g_error ("Couldn't create OpenGL context: %s", SDL_GetError ()); } SDL_GL_MakeCurrent (sdl_window, gl_context); SDL_SetWindowRelativeMouseMode (sdl_window, true); - INFO ("Renderer: %s", glGetString (GL_RENDERER)); - INFO ("OpenGL version: %s", glGetString (GL_VERSION)); - INFO ("GLSL version: %s", glGetString (GL_SHADING_LANGUAGE_VERSION)); - INFO ("Max vertex attributes: %d", GL_MAX_VERTEX_ATTRIBS); + g_info ("Renderer: %s", glGetString (GL_RENDERER)); + g_info ("OpenGL version: %s", glGetString (GL_VERSION)); + g_info ("GLSL version: %s", glGetString (GL_SHADING_LANGUAGE_VERSION)); + g_info ("Max vertex attributes: %d", GL_MAX_VERTEX_ATTRIBS); glViewport (0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); glClearColor (0, 0.3, 0.8, 1); @@ -119,7 +119,7 @@ graphics_render () avg_elapsed = 0; avg_counter = 0; last_check_perf = now; - INFO ("Dynamic render distance check: render distance %d, average ms: %.2f", render_distance, avg / 10000.0); + g_debug ("Dynamic render distance check: render distance %d, average ms: %.2f", render_distance, avg / 10000.0); } } diff --git a/game/src/resources.c b/game/src/resources.c index ad0b06e..b4784f4 100644 --- a/game/src/resources.c +++ b/game/src/resources.c @@ -12,7 +12,7 @@ resources_load_program (const char *v_path, const char *f_path) if (v_file == NULL) { - WARN ("File `%s' does not exist.", v_path); + g_warning ("File `%s' does not exist.", v_path); return 0; } @@ -20,13 +20,13 @@ resources_load_program (const char *v_path, const char *f_path) size_t v_len = ftell (v_file) + 1; char *v_src = calloc (v_len, sizeof (char)); rewind (v_file); - INFO ("Read %zu bytes from vertex source.", fread (v_src, sizeof (char), v_len, v_file)); + g_debug ("Read %zu bytes from vertex source.", fread (v_src, sizeof (char), v_len, v_file)); FILE *f_file = fopen (f_path, "r"); if (f_file == NULL) { - WARN ("File `%s' does not exist.", f_path); + g_warning ("File `%s' does not exist.", f_path); return 0; } @@ -34,7 +34,7 @@ resources_load_program (const char *v_path, const char *f_path) size_t f_len = ftell (f_file) + 1; char *f_src = calloc (f_len, sizeof (char)); rewind (f_file); - INFO ("Read %zu bytes from fragment source.", fread (f_src, sizeof (char), f_len, f_file)); + g_debug ("Read %zu bytes from fragment source.", fread (f_src, sizeof (char), f_len, f_file)); unsigned int program = program_create (v_src, f_src); free (v_src); @@ -49,7 +49,7 @@ resources_load_texture (const char *path) if (surface == NULL) { - WARN ("Failed to load texture from path %s: %s", path, SDL_GetError ()); + g_warning ("Failed to load texture from path %s: %s", path, SDL_GetError ()); return 0; } @@ -80,7 +80,7 @@ resources_get_program (char *key) { if (!g_hash_table_contains (programs, key)) { - WARN ("Program `%s' does not exist. Careful!", key); + g_warning ("Program `%s' does not exist. Careful!", key); } return GPOINTER_TO_INT (g_hash_table_lookup (programs, key)); @@ -91,7 +91,7 @@ resources_get_texture (char *key) { if (!g_hash_table_contains (textures, key)) { - WARN ("Texture `%s' does not exist. Careful!", key); + g_warning ("Texture `%s' does not exist. Careful!", key); } return GPOINTER_TO_INT (g_hash_table_lookup (textures, key)); diff --git a/game/src/resources.h b/game/src/resources.h index 4516815..647bca3 100644 --- a/game/src/resources.h +++ b/game/src/resources.h @@ -6,6 +6,7 @@ #include "gl/program.h" #include "gl/texture.h" +#include "util.h" #define RESPRG(key) (resources_get_program (key)) #define RESTEX(key) (resources_get_texture (key)) diff --git a/game/src/ui/uielement.c b/game/src/ui/uielement.c index 8c71ea8..9fb31b8 100644 --- a/game/src/ui/uielement.c +++ b/game/src/ui/uielement.c @@ -321,9 +321,9 @@ uicb_flag_switch (UIElement *element, void *userdata) } void -uicb_debug_print (UIElement *element, void *userdata) +uicb_debug (UIElement *element, void *userdata) { UNUSED (element); char *text = userdata; - INFO ("%s", text); + g_debug ("%s", text); } diff --git a/game/src/ui/uielement.h b/game/src/ui/uielement.h index 479f229..6bedb4b 100644 --- a/game/src/ui/uielement.h +++ b/game/src/ui/uielement.h @@ -85,4 +85,4 @@ void uicb_label_show_int (UIElement *element, void *userdata); void uicb_label_show_float (UIElement *element, void *userdata); void uicb_flag_switch (UIElement *element, void *userdata); -void uicb_debug_print (UIElement *element, void *userdata); +void uicb_debug (UIElement *element, void *userdata); diff --git a/game/src/util.h b/game/src/util.h index 25f2972..df242fa 100644 --- a/game/src/util.h +++ b/game/src/util.h @@ -6,16 +6,20 @@ #define GAME_NAME "Wschód" #define GAME_VERSION "0.2" -#define COLOR_BLUE "\x1B[34m" -#define COLOR_RED "\x1B[31m" -#define COLOR_YELLOW "\x1B[33m" -#define COLOR_RESET "\x1B[0m" - -#define INFO(fmt, args...) (fprintf (stderr, COLOR_BLUE "[I] %s:%d:%s(): " COLOR_RESET fmt "\n", __FILE__, __LINE__, __func__, ##args)) -#define WARN(fmt, args...) (fprintf (stderr, COLOR_YELLOW "[W] %s:%d:%s(): " COLOR_RESET fmt "\n", __FILE__, __LINE__, __func__, ##args)) -#define FATAL(fmt, args...) (fprintf (stderr, COLOR_RED "[!] %s:%d:%s(): " COLOR_RESET fmt "\n", __FILE__, __LINE__, __func__, ##args), exit (-1)) - #define UNUSED(...) (UNUSED_1 (__VA_ARGS__, 0)) #define UNUSED_1(arg, ...) (void) arg, UNUSED_2 (__VA_ARGS__, 0) #define UNUSED_2(arg, ...) (void) arg, UNUSED_3 (__VA_ARGS__, 0) #define UNUSED_3(arg, ...) (void) arg + +/* + + GLib log levels: + + g_debug + g_info + g_message + g_warning + g_critical + g_error + + */