diff options
-rw-r--r-- | CMakeLists.txt | 6 | ||||
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | src/main/engine/engine.c | 19 | ||||
-rw-r--r-- | src/main/engine/ui/x11/ui.c | 33 | ||||
-rw-r--r-- | src/main/game/cell.c | 15 | ||||
-rw-r--r-- | src/main/game/game.c | 17 | ||||
-rw-r--r-- | src/main/util.c | 34 | ||||
-rw-r--r-- | src/main/util.h | 25 |
8 files changed, 75 insertions, 78 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 71d5913..5cc956a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,8 +30,6 @@ add_executable(tris src/main/main.c src/main/engine/ui/ui.h src/main/engine/ui/x11/ui.c - src/main/util.h - src/main/util.c src/main/engine/engine.h src/main/engine/engine.c src/main/game/game.h @@ -43,4 +41,6 @@ add_executable(tris src/main/game/cell.h src/main/game/cell.c) -target_link_libraries(tris PRIVATE X11 GL GLX m) +target_include_directories(tris PRIVATE log/src) + +target_link_libraries(tris PRIVATE X11 GL GLX m ${CMAKE_SOURCE_DIR}/log/liblog.a) @@ -29,8 +29,8 @@ OBJ = ${SRC:.c=.o} CFLAGS != pkg-config --cflags ${LIBS} LFLAGS != pkg-config --libs ${LIBS} -CFLAGS += -DX11 -LFLAGS += -lm +CFLAGS += -DX11 -Ilog/src +LFLAGS += -lm -Llog -l:liblog.a ${PROG}: ${OBJ} ${CC} ${OBJ} -o $@ ${LFLAGS} diff --git a/src/main/engine/engine.c b/src/main/engine/engine.c index 0a4d81d..ab19785 100644 --- a/src/main/engine/engine.c +++ b/src/main/engine/engine.c @@ -22,8 +22,8 @@ #include <time.h> #include <GL/gl.h> #include <math.h> +#include <log.h> #include "engine.h" -#include "../util.h" #include "ui/ui.h" #include "../util/list.h" #include "types.h" @@ -34,6 +34,15 @@ #define RANGE_GL 2.0f +static Log *_log = NULL; + +static void +init_log(void) +{ + if (_log != NULL) return; + _log = log_create("Engine"); +} + static int width; static int height; @@ -69,9 +78,11 @@ MouseButtonEvent *mouse_button_event; void engine_init(int w, int h) { + init_log(); + engine = malloc(sizeof(Engine)); if (engine == NULL) { - log_error("Error allocating memory for engine"); + log_error(_log, "Error allocating memory for engine"); exit(EXIT_FAILURE); } engine->circles = list_create(); @@ -266,9 +277,11 @@ mouse_button_press_event(UIMouseButtonPressed *mbp) void engine_set_mouse_button_listener(void (*event)(float x, float y, void *data), void *data) { + init_log(); + mouse_button_event = malloc(sizeof(MouseButtonEvent)); if (mouse_button_event == NULL) { - log_error("Error allocating mouse button event"); + log_error(_log, "Error allocating mouse button event"); exit(EXIT_FAILURE); } diff --git a/src/main/engine/ui/x11/ui.c b/src/main/engine/ui/x11/ui.c index cd35edf..b1086a9 100644 --- a/src/main/engine/ui/x11/ui.c +++ b/src/main/engine/ui/x11/ui.c @@ -24,7 +24,7 @@ #include <X11/Xlib.h> #include <unistd.h> #include <time.h> -#include "../../../util.h" +#include <log.h> #include "../types.h" static Display *display; @@ -40,12 +40,23 @@ void (*on_generic_event)(int); void cleanup(void); +static Log *log = NULL; + +static void +init_log(void) +{ + if (log != NULL) return; + log = log_create("UI"); +} + static Display * ui_open_display(void) { + init_log(); + Display *display = XOpenDisplay(NULL); if (!display) { - log_error("Can't open X11 display"); + log_error(log, "Can't open X11 display"); exit(1); } @@ -55,10 +66,12 @@ ui_open_display(void) static XVisualInfo * gl_choose_visual(int screen) { + init_log(); + GLint attribs[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None }; XVisualInfo *vi = glXChooseVisual(display, screen, attribs); if (!vi) { - log_error("No compatible Visual found"); + log_error(log, "No compatible Visual found"); exit(1); } @@ -96,9 +109,11 @@ ui_set_title(const char *title) UI * ui_init(int width, int height) { + init_log(); + UI *ui = malloc(sizeof(UI)); if (ui == NULL) { - log_error("Error allocating UI"); + log_error(log, "Error allocating UI"); exit(1); } @@ -113,7 +128,7 @@ ui_init(int width, int height) gl_context = glXCreateContext(display, vi, NULL, GL_TRUE); if (!glXMakeCurrent(display, window, gl_context)) { - log_error("Error on making GLX context"); + log_error(log, "Error on making GLX context"); exit(1); } @@ -135,11 +150,13 @@ ui_on_expose(XEvent event) static void ui_on_resize(XEvent event) { + init_log(); + if (event.type != ConfigureNotify) return; if (on_resize_event != NULL) { UIEventResize *er = malloc(sizeof(UIEventResize)); if (er == NULL) { - log_error("Error allocating UIEventResize"); + log_error(log, "Error allocating UIEventResize"); exit(EXIT_FAILURE); } er->width = event.xconfigure.width; @@ -168,11 +185,13 @@ ui_get_mouse_position(Display *display, Window window, int *x, int *y) { static void ui_on_mouse_press(XEvent event) { + init_log(); + if (event.type != ButtonPress) return; if (on_mouse_press_event != NULL) { UIMouseButtonPressed *mbp = malloc(sizeof(UIMouseButtonPressed)); if (mbp == NULL) { - log_error("Error allocating UIMouseButtonPressed"); + log_error(log, "Error allocating UIMouseButtonPressed"); exit(EXIT_FAILURE); } ui_get_mouse_position(display, window, &mbp->x, &mbp->y); diff --git a/src/main/game/cell.c b/src/main/game/cell.c index 892dd47..53d051a 100644 --- a/src/main/game/cell.c +++ b/src/main/game/cell.c @@ -19,15 +19,26 @@ #include <stdlib.h> #include <stdbool.h> +#include <log.h> #include "cell.h" -#include "../util.h" + +static Log *log = NULL; + +static void +init_log(void) +{ + if (log != NULL) return; + log = log_create("Cell"); +} Cell * cell_new(float left, float right, float top, float bottom) { + init_log(); + Cell *cell = malloc(sizeof(Cell)); if (cell == NULL) { - log_error("Error allocating memory for cell"); + log_error(log, "Error allocating memory for cell"); exit(EXIT_FAILURE); } cell->left = left; diff --git a/src/main/game/game.c b/src/main/game/game.c index e1a193a..7579c50 100644 --- a/src/main/game/game.c +++ b/src/main/game/game.c @@ -19,10 +19,10 @@ #include <stdio.h> #include <stdlib.h> +#include <log.h> #include "../engine/engine.h" #include "game.h" #include "cell.h" -#include "../util.h" #include "../engine/ui/ui.h" float xmin, xmax, ymin, ymax; @@ -43,6 +43,15 @@ int moves = 0; static void game_draw_field(TrisGame *game, float box_size, float x, float y); +static Log *log = NULL; + +static void +init_log(void) +{ + if (log != NULL) return; + log = log_create("Game"); +} + static void draw_sign(float x, float y) { @@ -100,15 +109,19 @@ game_mouse_button_pressed(float x, float y, void *data) } } } + + check_win(); } TrisGame * game_init(int width, int height) { + init_log(); + /* Init of the TrisGame */ TrisGame *game = malloc(sizeof(TrisGame)); if (game == NULL) { - log_error("Error allocating memory for the game"); + log_error(log, "Error allocating memory for the game"); exit(EXIT_FAILURE); } game->width = width; diff --git a/src/main/util.c b/src/main/util.c deleted file mode 100644 index 30492ea..0000000 --- a/src/main/util.c +++ /dev/null @@ -1,34 +0,0 @@ -/*- - * Copyright (C) 2025 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it> - * - * This file is part of Tris Game. - * - * Tris Game is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Tris Game is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Tris Game. If not, see <http://www.gnu.org/licenses/>. - */ - -#include <stdio.h> -#include <stdarg.h> - -void -log_error(const char *format, ...) -{ - va_list args; - va_start(args, format); - - fprintf(stderr, "ERROR: "); - vfprintf(stderr, format, args); - fprintf(stderr, "\n"); - - va_end(args); -} diff --git a/src/main/util.h b/src/main/util.h deleted file mode 100644 index 9113892..0000000 --- a/src/main/util.h +++ /dev/null @@ -1,25 +0,0 @@ -/*- - * Copyright (C) 2025 Alessandro Iezzi <aiezzi AT alessandroiezzi PERIOD it> - * - * This file is part of Tris Game. - * - * Tris Game is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Tris Game is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Tris Game. If not, see <http://www.gnu.org/licenses/>. - */ - -#ifndef __UTIL_H__ -#define __UTIL_H__ - -void log_error(const char *msg, ...); - -#endif /* __UTIL_H__ */ |