diff --git a/src/error.c b/src/error.c index f84d282..b180d93 100644 --- a/src/error.c +++ b/src/error.c @@ -49,7 +49,13 @@ void warn(char const* fmt, ...) { (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}; write_args(buf1, fmt); diff --git a/src/error.h b/src/error.h index 6506126..e19d500 100644 --- a/src/error.h +++ b/src/error.h @@ -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 info(char const*, ...); // prints an info message to stdout 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 -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, ...); diff --git a/src/main.c b/src/main.c index e4243ea..c8d6372 100644 --- a/src/main.c +++ b/src/main.c @@ -17,7 +17,7 @@ static renderdata rdat; static void init(void) { // initialize SDL 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 gt = gametime_new(); diff --git a/src/window/audio.c b/src/window/audio.c index 6df6d93..7c26875 100644 --- a/src/window/audio.c +++ b/src/window/audio.c @@ -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 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 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; *bufptr = realloc(cvt.buf, cvt.len_cvt); 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* dev = malloc(sizeof(audiodevice)); 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 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 (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 SDL_PauseAudioDevice(dev->id, 0); diff --git a/src/window/render.c b/src/window/render.c index 33ea568..4b88e46 100644 --- a/src/window/render.c +++ b/src/window/render.c @@ -14,12 +14,12 @@ 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); 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); 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){ window,