rename 'error' to 'fatal', add 'error' which just logs

mainly because we sometimes would like to just write an error without
terminating the program
This commit is contained in:
2025-03-18 14:50:31 +01:00
parent 972aeb61e7
commit 6e6e05c48c
5 changed files with 18 additions and 11 deletions

View File

@@ -49,7 +49,13 @@ void warn(char const* fmt, ...) {
(void)fprintf(stderr, "\033[93mW: %s\033[0m\n", buf); (void)fprintf(stderr, "\033[93mW: %s\033[0m\n", buf);
} }
noreturn void error(gamestatus error_code, char const* fname, uint32_t ln, char const* fmt, ...) { void error(char const* fmt, ...) {
char buf[PRINT_BUFFER_SIZE] = {0};
write_args(buf, fmt);
(void)fprintf(stderr, "\033[mW: %s\033[0m", buf);
}
noreturn void fatal(gamestatus error_code, char const* fname, uint32_t ln, char const* fmt, ...) {
char buf1[PRINT_BUFFER_SIZE] = {0}; char buf1[PRINT_BUFFER_SIZE] = {0};
write_args(buf1, fmt); write_args(buf1, fmt);

View File

@@ -33,6 +33,7 @@ gamestatus get_gamestatus(void); // gets the current status of the game
void debug(char const*, ...); // prints a debug message to stdout if the DEBUG environment variable is set, otherwise the call is ignored. void debug(char const*, ...); // prints a debug message to stdout if the DEBUG environment variable is set, otherwise the call is ignored.
void info(char const*, ...); // prints an info message to stdout void info(char const*, ...); // prints an info message to stdout
void warn(char const*, ...); // prints a warning message to stderr void warn(char const*, ...); // prints a warning message to stderr
void error(char const*, ...); // prints an warning message to stderr
// prints an error message to stderr before exiting // prints an error message to stderr before exiting
noreturn void error(gamestatus, char const* file_name, uint32_t line, char const* fmt, ...); noreturn void fatal(gamestatus, char const* file_name, uint32_t line, char const* fmt, ...);

View File

@@ -17,7 +17,7 @@ static renderdata rdat;
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, __FILE_NAME__, __LINE__, "SDL could not initialize! SDL Error: %s", SDL_GetError()); fatal(ERROR_SDL_INIT, __FILE_NAME__, __LINE__, "SDL could not initialize! SDL Error: %s", SDL_GetError());
// initialize other game components // initialize other game components
gt = gametime_new(); gt = gametime_new();

View File

@@ -56,23 +56,23 @@ static void audio_cvt(audiodevice const* dev, SDL_AudioSpec const* spec, uint8_t
cvt.buf = realloc(*bufptr, cvt.len * cvt.len_mult); // grow the inputted buffer for the conversion cvt.buf = realloc(*bufptr, cvt.len * cvt.len_mult); // grow the inputted buffer for the conversion
if (cvt.buf == NULL) if (cvt.buf == NULL)
error(ERROR_STD_MEMORY, __FILE_NAME__, __LINE__, "something went wrong whilst growing the audio buffer whilst converting!"); fatal(ERROR_STD_MEMORY, __FILE_NAME__, __LINE__, "something went wrong whilst growing the audio buffer whilst converting!");
// converts the audio to the new format // converts the audio to the new format
if (!SDL_ConvertAudio(&cvt)) if (!SDL_ConvertAudio(&cvt))
error(ERROR_SDL_AUDIO_INIT, __FILE_NAME__, __LINE__, "something went wrong when loading/converting an audio buffer! SDL Error: %s", SDL_GetError()); fatal(ERROR_SDL_AUDIO_INIT, __FILE_NAME__, __LINE__, "something went wrong when loading/converting an audio buffer! SDL Error: %s", SDL_GetError());
*len = cvt.len; *len = cvt.len;
*bufptr = realloc(cvt.buf, cvt.len_cvt); *bufptr = realloc(cvt.buf, cvt.len_cvt);
if (*bufptr == NULL) if (*bufptr == NULL)
error(ERROR_STD_MEMORY, __FILE_NAME__, __LINE__, "something went wrong whilst shrinking the audio buffer whilst converting!"); fatal(ERROR_STD_MEMORY, __FILE_NAME__, __LINE__, "something went wrong whilst shrinking the audio buffer whilst converting!");
} }
audiodevice* audio_device_init(int32_t freq, SDL_AudioFormat fmt, uint8_t channels, uint16_t samples) { audiodevice* audio_device_init(int32_t freq, SDL_AudioFormat fmt, uint8_t channels, uint16_t samples) {
audiodevice* dev = malloc(sizeof(audiodevice)); audiodevice* dev = malloc(sizeof(audiodevice));
if (dev == NULL) if (dev == NULL)
error(ERROR_STD_INIT, __FILE_NAME__, __LINE__, "null pointer when allocating memory for the audio device!"); fatal(ERROR_STD_INIT, __FILE_NAME__, __LINE__, "null pointer when allocating memory for the audio device!");
// define the audio specification // define the audio specification
SDL_AudioSpec spec = {freq, fmt, channels, 0, samples, 0, 0, NULL, NULL}; SDL_AudioSpec spec = {freq, fmt, channels, 0, samples, 0, 0, NULL, NULL};
@@ -90,7 +90,7 @@ audiodevice* audio_device_init(int32_t freq, SDL_AudioFormat fmt, uint8_t channe
// 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, __FILE_NAME__, __LINE__, "audio device failed to open! SDL Error: %s", SDL_GetError()); fatal(ERROR_SDL_AUDIO_INIT, __FILE_NAME__, __LINE__, "audio device failed to open! SDL Error: %s", SDL_GetError());
// default state of the device is paused, so we unpause it here // default state of the device is paused, so we unpause it here
SDL_PauseAudioDevice(dev->id, 0); SDL_PauseAudioDevice(dev->id, 0);

View File

@@ -14,12 +14,12 @@
void render_init(renderdata* const rdat, gamedata const* const gdat) { void render_init(renderdata* const rdat, gamedata const* const gdat) {
SDL_Window* const window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 500, 500, SDL_WINDOW_SHOWN); SDL_Window* const window = SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 500, 500, SDL_WINDOW_SHOWN);
if (window == NULL) if (window == NULL)
error(ERROR_SDL_RENDERING_INIT, __FILE_NAME__, __LINE__, "failed to create a window. SDL Error: %s", SDL_GetError()); fatal(ERROR_SDL_RENDERING_INIT, __FILE_NAME__, __LINE__, "failed to create a window. SDL Error: %s", SDL_GetError());
// rendere using vsync to limit updates to the refresh rate of the monitor // render using vsync to limit updates to the refresh rate of the monitor
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) if (renderer == NULL)
error(ERROR_SDL_RENDERING_INIT, __FILE_NAME__, __LINE__, "failed to create a renderer. SDL Error: %s", SDL_GetError()); fatal(ERROR_SDL_RENDERING_INIT, __FILE_NAME__, __LINE__, "failed to create a renderer. SDL Error: %s", SDL_GetError());
*rdat = (renderdata){ *rdat = (renderdata){
window, window,