mirror of
https://github.com/thepigeongenerator/sdl_template.git
synced 2025-12-17 05:55:47 +01:00
implement / re-implement error-handling
This commit is contained in:
3
.vscode/launch.json
vendored
3
.vscode/launch.json
vendored
@@ -5,6 +5,9 @@
|
||||
"name": "(lldb) debug project",
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"env": {
|
||||
"DEBUG": "1"
|
||||
},
|
||||
"program": "",
|
||||
"linux": {
|
||||
"preLaunchTask": "(dbg) compile linux-x86_64",
|
||||
|
||||
54
src/error_handling.c
Normal file
54
src/error_handling.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#include "error_handling.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "SDL_messagebox.h"
|
||||
#include "main.h"
|
||||
|
||||
#define PRINT_BUFFER_SIZE 128 // defines the buffer size for printing
|
||||
|
||||
// writes the arguments to the specified buffer
|
||||
#define write_args(buf, fmt) \
|
||||
va_list args; \
|
||||
va_start(args, fmt); \
|
||||
(void)vsnprintf(buf, PRINT_BUFFER_SIZE, fmt, args); \
|
||||
va_end(args);
|
||||
|
||||
// sets the game status (in turn exiting (if not set to STATUS_RUNNING))
|
||||
static void stop(gamestatus status) {
|
||||
set_gamestatus(status);
|
||||
}
|
||||
|
||||
void debug(char const* fmt, ...) {
|
||||
char const* env = getenv("DEBUG");
|
||||
if (env == NULL || *env != '1')
|
||||
return;
|
||||
|
||||
char buf[PRINT_BUFFER_SIZE] = {0};
|
||||
write_args(buf, fmt);
|
||||
|
||||
(void)fprintf(stdout, "\033[95m%s\033[0m\n", buf);
|
||||
}
|
||||
|
||||
void info(char const* fmt, ...) {
|
||||
char buf[PRINT_BUFFER_SIZE] = {0};
|
||||
write_args(buf, fmt);
|
||||
(void)fprintf(stdout, "\033[0m%s\033[0m\n", buf); // write colour here for consistency
|
||||
}
|
||||
|
||||
void warn(char const* fmt, ...) {
|
||||
char buf[PRINT_BUFFER_SIZE] = {0};
|
||||
write_args(buf, fmt);
|
||||
(void)fprintf(stderr, "\033[93mW: %s\033[0m\n", buf);
|
||||
}
|
||||
|
||||
void error(gamestatus status, char const* fmt, ...) {
|
||||
char buf[PRINT_BUFFER_SIZE] = {0};
|
||||
write_args(buf, fmt);
|
||||
(void)fprintf(stderr, "\033[91mE: %s\033[0m\n", buf);
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "something went wrong! :O", buf, NULL);
|
||||
stop(status);
|
||||
}
|
||||
33
src/error_handling.h
Normal file
33
src/error_handling.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* defines statuses in the 0..127, any higher/negative values are POSIX-reserved.
|
||||
* The max value (or -1) shall mean the application is running, anything else shall mean an exit code of some kind */
|
||||
enum {
|
||||
// clang-format off
|
||||
STATUS_SUCCESS = 0, // 0; successful exit
|
||||
STATUS_ERROR = 1, // miscellaneous error
|
||||
ERROR_INIT = STATUS_ERROR | 2, // initialisation error
|
||||
|
||||
ERROR_STD = STATUS_ERROR | 64, // standard library error
|
||||
ERROR_STD_INIT = ERROR_INIT | 64, // standard library initialisation error
|
||||
ERROR_STD_MEMORY = ERROR_STD | 32, // memory error
|
||||
ERROR_STD_MEMORY_INIT = ERROR_STD_INIT | 32, // memory initialization error
|
||||
|
||||
ERROR_SDL = STATUS_ERROR | 32, // SDL error
|
||||
ERROR_SDL_INIT = ERROR_INIT | 32, // SDL initialization error
|
||||
ERROR_SDL_RENDERING = ERROR_SDL | 16, // rendering error
|
||||
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
|
||||
|
||||
STATUS_RUNNING = -1,
|
||||
// clang-format on
|
||||
};
|
||||
typedef int8_t gamestatus;
|
||||
|
||||
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(gamestatus, char const*, ...); // prints an error message to stderr
|
||||
15
src/main.c
15
src/main.c
@@ -1,8 +1,17 @@
|
||||
#include "main.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "error_handling.h"
|
||||
|
||||
static gamestatus status = STATUS_RUNNING;
|
||||
|
||||
void set_gamestatus(gamestatus new_status) {
|
||||
status = new_status;
|
||||
}
|
||||
|
||||
int32_t main(int32_t argc, char** argv) {
|
||||
(void)argc, (void)argv;
|
||||
printf("%s\n", "Hello, World!");
|
||||
return 0;
|
||||
debug("%s", "Hello, World!");
|
||||
return status;
|
||||
}
|
||||
|
||||
5
src/main.h
Normal file
5
src/main.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "error_handling.h"
|
||||
|
||||
void set_gamestatus(gamestatus); // sets the status of the game (allows for quitting the game)
|
||||
Reference in New Issue
Block a user