From 6b1708693679e00086396ce4cd3ee3e362452e55 Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 4 Feb 2025 13:54:28 +0100 Subject: [PATCH] code cleanup + add font support --- makefile | 4 ++-- src/errors.c | 4 ++-- src/errors.h | 5 +++++ src/main.c | 24 ++++++++---------------- src/window/renderer.c | 40 +++++++++++++++++++++++++--------------- src/window/renderer.h | 7 +++++-- 6 files changed, 47 insertions(+), 37 deletions(-) diff --git a/makefile b/makefile index d737ca0..1477e1c 100644 --- a/makefile +++ b/makefile @@ -4,8 +4,8 @@ NAME := tetris_clone CC := clang STD := c17 LANG = c -CFLAGS := $(shell pkg-config --cflags sdl2) -Wall -Wextra -Wpedantic -Wno-pointer-arith -LDFLAGS := $(shell pkg-config --libs sdl2) -lm +CFLAGS := $(shell pkg-config --cflags sdl2 SDL2_ttf) -Wall -Wextra -Wpedantic -Wno-pointer-arith +LDFLAGS := $(shell pkg-config --libs sdl2 SDL2_ttf) -lm ifeq ($(DEBUG),1) CFLAGS += -DDEBUG -Og -g diff --git a/src/errors.c b/src/errors.c index 89a22aa..e517065 100644 --- a/src/errors.c +++ b/src/errors.c @@ -15,7 +15,7 @@ void error(ErrorCode const error_code, char const* const format, ...) { vsnprintf(buffer, MAX_STR_LEN, format, args); va_end(args); - printf("\033[91mE\033[0m: %s\n", buffer); + fprintf(stderr, "\033[91mE\033[0m: %s\n", buffer); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "something went wrong! :O", buffer, NULL); exit(error_code); @@ -29,5 +29,5 @@ void warn(char const* const format, ...) { vsnprintf(buffer, MAX_STR_LEN, format, args); va_end(args); - printf("\033[93mW\033[0m: %s\n", buffer); + fprintf(stderr, "\033[93mW\033[0m: %s\n", buffer); } diff --git a/src/errors.h b/src/errors.h index 540024e..959edc9 100644 --- a/src/errors.h +++ b/src/errors.h @@ -15,9 +15,14 @@ enum { ERROR_SDL_RENDERER = ERROR_SDL | 4, ERROR_SDL_RENDERER_INIT = ERROR_SDL_RENDERER | ERROR_INIT, + // audio errors ERROR_SDL_AUDIO = ERROR_SDL | 8, ERROR_SDL_AUDIO_INIT = ERROR_SDL_AUDIO | ERROR_INIT, + + // font errors + ERROR_SDL_FONT = ERROR_SDL | 16, + ERROR_SDL_FONT_INIT = ERROR_SDL_FONT | ERROR_INIT, }; // call when a fatal error has occurred, the program will immediately terminate when called diff --git a/src/main.c b/src/main.c index 930a60f..784e29e 100644 --- a/src/main.c +++ b/src/main.c @@ -4,8 +4,6 @@ #include #include #include -#include -#include #include #include #include @@ -13,6 +11,7 @@ #include #include "SDL_timer.h" +#include "SDL_ttf.h" #include "errors.h" #include "game/game.h" // #include "window/audio.h" @@ -25,27 +24,21 @@ bool playing = true; -SDL_Window* window = NULL; -SDL_Renderer* renderer = NULL; +RenderData render_data = {0}; GameData game_data = {0}; // handles game application initialisation static void init(void) { // initialize SDL - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) { + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) error(ERROR_SDL_INIT, "SDL could not initialize! SDL Error: %s", SDL_GetError()); - return; - } + if (TTF_Init() != 0) + error(ERROR_SDL_FONT_INIT, "the TTF module of SDL could not initialize! TTF Error: %s", TTF_GetError()); - // initialize the renderer - if (renderer_init(&window, &renderer) < 0) { - error(ERROR_SDL_RENDERER_INIT, SDL_GetError()); - return; - } - - // initialize the game + // initialize units game_init(&game_data); + renderer_init(&render_data, &game_data); // initialize audio // AudioDevice* audio_device = audio_device_init(32000, AUDIO_S16, 1, 4096); @@ -68,8 +61,7 @@ static void update(void) { // preform updates game_update(&game_data, SDL_GetKeyboardState(NULL)); - renderer_update(&(RenderData){ - window, renderer, &game_data}); + renderer_update(&render_data); } // handles game application quitting diff --git a/src/window/renderer.c b/src/window/renderer.c index f6fe09e..c08f4ea 100644 --- a/src/window/renderer.c +++ b/src/window/renderer.c @@ -11,26 +11,27 @@ #include "../errors.h" #include "../game/game.h" #include "../game/tetromino/shapes.h" +#include "SDL_ttf.h" #include "colour8.h" #include "renderer.h" -int renderer_init(SDL_Window** const window, SDL_Renderer** const renderer) { - // create a new window - *window = SDL_CreateWindow("tetris clone", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); - if (*window == NULL) { - error(ERROR_SDL_RENDERER_INIT, "Window failed to be created! SDL Error: %s", SDL_GetError()); - return -1; - } +void renderer_init(RenderData* const render_data, GameData const* const game_data) { + SDL_Window* const window = SDL_CreateWindow("tetris clone", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + if (window == NULL) error(ERROR_SDL_RENDERER_INIT, "Window failed to be created! SDL Error: %s", SDL_GetError()); - // create a renderer - *renderer = SDL_CreateRenderer(*window, -1, SDL_RENDERER_PRESENTVSYNC); - if (*renderer == NULL) { - error(ERROR_SDL_RENDERER_INIT, "Renderer failed to be created! SDL Error: %s", SDL_GetError()); - return -1; - } + SDL_Renderer* const renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC); + if (renderer == NULL) error(ERROR_SDL_RENDERER_INIT, "Renderer failed to be created! SDL Error: %s", SDL_GetError()); - return 0; + TTF_Font* const font = TTF_OpenFont("/usr/share/fonts/TTF/JetBrainsMono-Regular.ttf", 5); + if (font == NULL) error(ERROR_SDL_FONT_INIT, "Failed to open font! TTF Error: %s", TTF_GetError()); + + // initialize the render data + *render_data = (RenderData){ + game_data, + window, + renderer, + font}; } // draws a block at the specified position @@ -74,7 +75,7 @@ static void render_level(SDL_Renderer* const renderer, GameData const* const dat } } -void renderer_update(const RenderData* const render_data) { +void renderer_update(RenderData const* const render_data) { SDL_Renderer* const renderer = render_data->renderer; GameData const* const game_data = render_data->game_data; @@ -99,3 +100,12 @@ void renderer_update(const RenderData* const render_data) { SDL_RenderPresent(renderer); } + +void renderer_free(RenderData* const render_data) { + SDL_DestroyRenderer(render_data->renderer); + SDL_DestroyWindow(render_data->window); + TTF_CloseFont(render_data->font); + render_data->renderer = NULL; + render_data->window = NULL; + render_data->font = NULL; +} diff --git a/src/window/renderer.h b/src/window/renderer.h index a2759cc..3418096 100644 --- a/src/window/renderer.h +++ b/src/window/renderer.h @@ -1,5 +1,6 @@ #pragma once #include +#include #include #include @@ -15,10 +16,12 @@ #define BLOCK_HEIGHT (TET_HEIGHT / ROWS) // height of a block typedef struct { + GameData const* game_data; SDL_Window* window; SDL_Renderer* renderer; - GameData* game_data; + TTF_Font* font; } RenderData; -int renderer_init(SDL_Window** window, SDL_Renderer** renderer); +void renderer_init(RenderData* render_data, GameData const* game_data); void renderer_update(RenderData const* render_data); +void renderer_free(RenderData* render_data);