diff --git a/src/error.h b/src/error.h index 679e5d0..ede9321 100644 --- a/src/error.h +++ b/src/error.h @@ -22,6 +22,8 @@ enum gamestatus { ERROR_SDL_RENDERING_INIT = ERROR_SDL_INIT | 16, // rendering initialization error ERROR_SDL_AUDIO = ERROR_SDL | 8, // audio error ERROR_SDL_AUDIO_INIT = ERROR_SDL_INIT | 8, // audio initialization error + ERROR_SDL_FONT = ERROR_SDL | 4, // font error + ERROR_SDL_FONT_INIT = ERROR_SDL_INIT | 4, // font initialization error STATUS_RUNNING = -1, // clang-format on diff --git a/src/errors.c b/src/errors.c deleted file mode 100644 index 9285841..0000000 --- a/src/errors.c +++ /dev/null @@ -1,33 +0,0 @@ -#include "errors.h" - -#include -#include -#include -#include - -#define MAX_STR_LEN 128 - -void error(error_code const error_code, char const* const format, ...) { - char buffer[MAX_STR_LEN] = {0}; // contains the buffer of the final string - - va_list args = {0}; - va_start(args, format); - vsnprintf(buffer, MAX_STR_LEN, format, args); - va_end(args); - - fprintf(stderr, "\033[91mE\033[0m: %s\n", buffer); - SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "something went wrong! :O", buffer, NULL); - - exit(error_code); -} - -void warn(char const* const format, ...) { - char buffer[MAX_STR_LEN] = {0}; // contains the buffer of the final string - - va_list args = {0}; - va_start(args, format); - vsnprintf(buffer, MAX_STR_LEN, format, args); - va_end(args); - - fprintf(stderr, "\033[93mW\033[0m: %s\n", buffer); -} diff --git a/src/errors.h b/src/errors.h deleted file mode 100644 index 829378f..0000000 --- a/src/errors.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once - -typedef unsigned char error_code; -enum { - ERROR_MISC = -1, - - SUCCESS = 0, - ERROR_INIT = 1, - - // SDL errors - ERROR_SDL = 2, - ERROR_SDL_INIT = ERROR_SDL | ERROR_INIT, - - // renderer errors - 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 -void error(error_code error_code, char const* format, ...); -void warn(char const* format, ...); diff --git a/src/game/game.c b/src/game/game.c index 1444059..1e142fc 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -7,7 +7,7 @@ #include #include -#include "../main.h" +#include "../error.h" #include "../window/audio.h" #include "../window/colour8.h" #include "./tetromino/shapes.h" @@ -71,7 +71,7 @@ void game_init(game_data* const dat) { // called every time the game's state is updated void game_update(game_data* const dat, uint8_t const* const keys) { if (keys[SDL_SCANCODE_ESCAPE]) - stop(); + set_gamestatus(STATUS_SUCCESS); input_data move = MOVE_NONE; // contains the move data uint32_t ctime = SDL_GetTicks(); diff --git a/src/game/tetromino/placing.c b/src/game/tetromino/placing.c index 4ea95f1..ccd7c81 100644 --- a/src/game/tetromino/placing.c +++ b/src/game/tetromino/placing.c @@ -2,7 +2,7 @@ #include -#include "../../main.h" +#include "../../error.h" #include "../../window/colour8.h" #include "../game.h" #include "shapes.h" @@ -116,7 +116,7 @@ void place_update(game_data* const game_data, input_data const move) { next_shape(game_data); if (shape_intersects(game_data->rows, game_data->curr_idx, game_data->sel_x, game_data->sel_y)) - stop(); + set_gamestatus(STATUS_SUCCESS); return; } diff --git a/src/main.c b/src/main.c index 768f48d..a12dffe 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,3 @@ -#include "main.h" - #include #include #include @@ -12,12 +10,12 @@ #include #include "SDL_ttf.h" -#include "errors.h" +#include "error.h" #include "game/game.h" #include "window/renderer.h" #ifdef __EMSCRIPTEN__ // for web builds -#include +# include #endif @@ -31,9 +29,9 @@ game_data game_dat = {0}; static void init(void) { // initialize SDL if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) - error(ERROR_SDL_INIT, "SDL could not initialize! SDL Error: %s", SDL_GetError()); + fatal(ERROR_SDL_INIT, "SDL could not initialize! SDL Error: %s", SDL_GetError()); if (TTF_Init() != 0) - error(ERROR_SDL_FONT_INIT, "the TTF module of SDL could not initialize! TTF Error: %s", TTF_GetError()); + fatal(ERROR_SDL_FONT_INIT, "the TTF module of SDL could not initialize! TTF Error: %s", TTF_GetError()); // initialize units game_init(&game_dat); @@ -48,7 +46,7 @@ static void update(void) { while (SDL_PollEvent(&e)) { switch (e.type) { case SDL_QUIT: - exit(SUCCESS); + set_gamestatus(STATUS_SUCCESS); break; } } @@ -71,9 +69,7 @@ int main(int const argc, char const* const* const argv) { init(); - printf("target framerate: %u\n", 0); - - while (playing) { + while (get_gamestatus() == -1) { update(); } diff --git a/src/main.h b/src/main.h deleted file mode 100644 index 1369892..0000000 --- a/src/main.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -// stops execution of the game -void stop(void); diff --git a/src/window/audio.c b/src/window/audio.c index a014257..ec8c689 100644 --- a/src/window/audio.c +++ b/src/window/audio.c @@ -7,7 +7,7 @@ #include #include -#include "../errors.h" +#include "../error.h" // // audio mixing @@ -62,7 +62,7 @@ static void convert_audio(audio_device const* const dev, SDL_AudioSpec const wav // performs the conversion if (SDL_ConvertAudio(&cvt) != 0) - error(ERROR_SDL_AUDIO_INIT, "something went wrong when converting an audio buffer! SDL Error: %s", SDL_GetError()); + fatal(ERROR_SDL_AUDIO_INIT, "something went wrong when converting an audio buffer! SDL Error: %s", SDL_GetError()); // set the length to the new length *wav_len = cvt.len_cvt; @@ -70,7 +70,7 @@ static void convert_audio(audio_device const* const dev, SDL_AudioSpec const wav // reallocate the conversion buffer to match the new size *wav_buf = realloc(cvt.buf, cvt.len_cvt); if (*wav_buf == NULL) - error(ERROR_MISC, "null value when reallocating the audio buffer"); + fatal(ERROR_SDL_AUDIO, "null value when reallocating the audio buffer"); } @@ -112,7 +112,7 @@ audio_device* audio_device_init(int32_t const freq, SDL_AudioFormat const fmt, u // if the audio device isn't set, cause an error if (dev->id < 1) { - error(ERROR_SDL_AUDIO_INIT, "AudioDivice failed to open! SDL Error: %s", SDL_GetError()); + fatal(ERROR_SDL_AUDIO_INIT, "AudioDivice failed to open! SDL Error: %s", SDL_GetError()); return NULL; } diff --git a/src/window/renderer.c b/src/window/renderer.c index e173ee5..85b753c 100644 --- a/src/window/renderer.c +++ b/src/window/renderer.c @@ -11,7 +11,7 @@ #include #include -#include "../errors.h" +#include "../error.h" #include "../game/game.h" #include "../game/tetromino/shapes.h" #include "SDL_surface.h" @@ -22,13 +22,13 @@ void renderer_init(render_data* const render_dat, game_data const* const game_dat) { 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()); + if (window == NULL) fatal(ERROR_SDL_RENDERING_INIT, "Window failed to be created! SDL Error: %s", SDL_GetError()); SDL_Renderer* const renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED); - if (renderer == NULL) error(ERROR_SDL_RENDERER_INIT, "Renderer failed to be created! SDL Error: %s", SDL_GetError()); + if (renderer == NULL) fatal(ERROR_SDL_RENDERING_INIT, "Renderer failed to be created! SDL Error: %s", SDL_GetError()); TTF_Font* const font = TTF_OpenFont("pixeldroid_botic-regular.ttf", PX_DENS); - if (font == NULL) error(ERROR_SDL_FONT_INIT, "Failed to open font! TTF Error: %s", TTF_GetError()); + if (font == NULL) fatal(ERROR_SDL_FONT_INIT, "Failed to open font! TTF Error: %s", TTF_GetError()); // initialize the render data *render_dat = (render_data){