use new error files

This commit is contained in:
2025-03-22 12:08:19 +01:00
parent fccf1c6276
commit 7fb624311f
9 changed files with 20 additions and 89 deletions

View File

@@ -22,6 +22,8 @@ enum gamestatus {
ERROR_SDL_RENDERING_INIT = ERROR_SDL_INIT | 16, // rendering initialization error ERROR_SDL_RENDERING_INIT = ERROR_SDL_INIT | 16, // rendering initialization error
ERROR_SDL_AUDIO = ERROR_SDL | 8, // audio error ERROR_SDL_AUDIO = ERROR_SDL | 8, // audio error
ERROR_SDL_AUDIO_INIT = ERROR_SDL_INIT | 8, // audio initialization 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, STATUS_RUNNING = -1,
// clang-format on // clang-format on

View File

@@ -1,33 +0,0 @@
#include "errors.h"
#include <SDL_messagebox.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#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);
}

View File

@@ -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, ...);

View File

@@ -7,7 +7,7 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include "../main.h" #include "../error.h"
#include "../window/audio.h" #include "../window/audio.h"
#include "../window/colour8.h" #include "../window/colour8.h"
#include "./tetromino/shapes.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 // called every time the game's state is updated
void game_update(game_data* const dat, uint8_t const* const keys) { void game_update(game_data* const dat, uint8_t const* const keys) {
if (keys[SDL_SCANCODE_ESCAPE]) if (keys[SDL_SCANCODE_ESCAPE])
stop(); set_gamestatus(STATUS_SUCCESS);
input_data move = MOVE_NONE; // contains the move data input_data move = MOVE_NONE; // contains the move data
uint32_t ctime = SDL_GetTicks(); uint32_t ctime = SDL_GetTicks();

View File

@@ -2,7 +2,7 @@
#include <stdint.h> #include <stdint.h>
#include "../../main.h" #include "../../error.h"
#include "../../window/colour8.h" #include "../../window/colour8.h"
#include "../game.h" #include "../game.h"
#include "shapes.h" #include "shapes.h"
@@ -116,7 +116,7 @@ void place_update(game_data* const game_data, input_data const move) {
next_shape(game_data); next_shape(game_data);
if (shape_intersects(game_data->rows, game_data->curr_idx, game_data->sel_x, game_data->sel_y)) if (shape_intersects(game_data->rows, game_data->curr_idx, game_data->sel_x, game_data->sel_y))
stop(); set_gamestatus(STATUS_SUCCESS);
return; return;
} }

View File

@@ -1,5 +1,3 @@
#include "main.h"
#include <SDL.h> #include <SDL.h>
#include <SDL_error.h> #include <SDL_error.h>
#include <SDL_events.h> #include <SDL_events.h>
@@ -12,7 +10,7 @@
#include <wchar.h> #include <wchar.h>
#include "SDL_ttf.h" #include "SDL_ttf.h"
#include "errors.h" #include "error.h"
#include "game/game.h" #include "game/game.h"
#include "window/renderer.h" #include "window/renderer.h"
@@ -31,9 +29,9 @@ game_data game_dat = {0};
static void init(void) { static void init(void) {
// initialize SDL // 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()); fatal(ERROR_SDL_INIT, "SDL could not initialize! SDL Error: %s", SDL_GetError());
if (TTF_Init() != 0) 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 // initialize units
game_init(&game_dat); game_init(&game_dat);
@@ -48,7 +46,7 @@ static void update(void) {
while (SDL_PollEvent(&e)) { while (SDL_PollEvent(&e)) {
switch (e.type) { switch (e.type) {
case SDL_QUIT: case SDL_QUIT:
exit(SUCCESS); set_gamestatus(STATUS_SUCCESS);
break; break;
} }
} }
@@ -71,9 +69,7 @@ int main(int const argc, char const* const* const argv) {
init(); init();
printf("target framerate: %u\n", 0); while (get_gamestatus() == -1) {
while (playing) {
update(); update();
} }

View File

@@ -1,4 +0,0 @@
#pragma once
// stops execution of the game
void stop(void);

View File

@@ -7,7 +7,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "../errors.h" #include "../error.h"
// //
// audio mixing // audio mixing
@@ -62,7 +62,7 @@ static void convert_audio(audio_device const* const dev, SDL_AudioSpec const wav
// performs the conversion // performs the conversion
if (SDL_ConvertAudio(&cvt) != 0) 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 // set the length to the new length
*wav_len = cvt.len_cvt; *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 // reallocate the conversion buffer to match the new size
*wav_buf = realloc(cvt.buf, cvt.len_cvt); *wav_buf = realloc(cvt.buf, cvt.len_cvt);
if (*wav_buf == NULL) 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 the audio device isn't set, cause an error
if (dev->id < 1) { 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; return NULL;
} }

View File

@@ -11,7 +11,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "../errors.h" #include "../error.h"
#include "../game/game.h" #include "../game/game.h"
#include "../game/tetromino/shapes.h" #include "../game/tetromino/shapes.h"
#include "SDL_surface.h" #include "SDL_surface.h"
@@ -22,13 +22,13 @@
void renderer_init(render_data* const render_dat, game_data const* const game_dat) { 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); 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); 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); 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 // initialize the render data
*render_dat = (render_data){ *render_dat = (render_data){