aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-12-05 21:30:51 +0100
committerkdx <kikoodx@paranoici.org>2023-12-05 21:37:21 +0100
commit1546f59de4e15fd6653cce36b8719c1c9c0fdd8d (patch)
tree2fb07b49562512e5af5a8df39c7a8445a8ab9f62
parent9c9f90bd045013e5657bb7402a46e80448c65b67 (diff)
downloadtzr-1546f59de4e15fd6653cce36b8719c1c9c0fdd8d.tar.gz
configurable TZR_Log
-rw-r--r--compile_flags.txt1
-rw-r--r--headers/TZR.h4
-rw-r--r--sources/TZR_DestroyResource.c2
-rw-r--r--sources/TZR_DirectResourceLoad.c15
-rw-r--r--sources/TZR_DrawImage.c4
-rw-r--r--sources/TZR_Init.c9
-rw-r--r--sources/TZR_LoadResourceFromMemory.c2
-rw-r--r--sources/TZR_LoadResourceTyped.c6
-rw-r--r--sources/TZR_PlayMusic.c2
-rw-r--r--sources/TZR_PlaySound.c4
-rw-r--r--sources/TZR_PollEvent.c7
-rw-r--r--sources/TZR_ReloadResource.c5
12 files changed, 35 insertions, 26 deletions
diff --git a/compile_flags.txt b/compile_flags.txt
index 7077ba2..36e8aa8 100644
--- a/compile_flags.txt
+++ b/compile_flags.txt
@@ -3,3 +3,4 @@
-Wno-initializer-overrides
-std=c99
-Iheaders
+-DTZR_Log=printf
diff --git a/headers/TZR.h b/headers/TZR.h
index 17129c8..fab1c55 100644
--- a/headers/TZR.h
+++ b/headers/TZR.h
@@ -4,6 +4,10 @@
#include "TZR_events.h"
#include <SDL2/SDL_main.h>
+#ifndef TZR_Log
+#define TZR_Log SDL_Log
+#endif
+
/* TZR manages all loaded resources internally and tries to avoid duplicate.
* A resource can be loaded multiple times if asked, identification doesn't rely
* on filepath. In doubt, use TZR_LoadResource ONCE per asset and store the ID.
diff --git a/sources/TZR_DestroyResource.c b/sources/TZR_DestroyResource.c
index 7536017..4b9d651 100644
--- a/sources/TZR_DestroyResource.c
+++ b/sources/TZR_DestroyResource.c
@@ -25,7 +25,7 @@ TZR_DestroyResource(TZR_Resource *res, int free_path)
#endif
break;
default:
- fprintf(stderr, "unknown resource type %u\n", res->type);
+ TZR_Log("unknown resource type %u", res->type);
break;
}
}
diff --git a/sources/TZR_DirectResourceLoad.c b/sources/TZR_DirectResourceLoad.c
index 7b53a5e..16f0f64 100644
--- a/sources/TZR_DirectResourceLoad.c
+++ b/sources/TZR_DirectResourceLoad.c
@@ -4,6 +4,7 @@
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_rwops.h>
+#include <errno.h>
#include <string.h>
#ifdef TZR_STB_IMAGE
@@ -19,12 +20,12 @@ stbi_load_surface(void *src, int size)
const int pitch = (width * 4 + 3) & ~3;
if (data == NULL) {
- fprintf(stderr, "stbi: %s\n", stbi_failure_reason());
+ TZR_Log("stbi: %s", stbi_failure_reason());
return NULL;
}
if (chans != 4) {
- fprintf(stderr, "TZR only supports RGBA images\n");
+ TZR_Log("TZR only supports RGBA images");
free(data);
return NULL;
}
@@ -61,7 +62,7 @@ TZR_DirectResourceLoad(TZR_Resource *res, const void *data, int size)
res->raw.size = size;
res->raw.data = malloc(size);
if (res->raw.data == NULL) {
- perror("TZR_DirectResourceLoad");
+ TZR_Log("%s", strerror(errno));
return -1;
}
memcpy(res->raw.data, data, size);
@@ -97,17 +98,17 @@ TZR_DirectResourceLoad(TZR_Resource *res, const void *data, int size)
} break;
case TZR_RES_SOUND: {
if (!___tzr_config.mixer) {
- SDL_Log("audio mixer disabled, skip loading");
+ TZR_Log("audio mixer disabled, skip loading");
return -1;
}
#ifdef TZR_SOLOUD
Wav *wav = Wav_create();
if (wav == NULL) {
- SDL_Log("Wav_create failed");
+ TZR_Log("Wav_create failed");
return -1;
}
if (Wav_loadMemEx(wav, data, size, true, false)) {
- SDL_Log("Wav_loadMem failed");
+ TZR_Log("Wav_loadMem failed");
Wav_destroy(wav);
return -1;
}
@@ -124,7 +125,7 @@ TZR_DirectResourceLoad(TZR_Resource *res, const void *data, int size)
#endif
} break;
default:
- fprintf(stderr, "invalid type\n");
+ TZR_Log("invalid type");
return -1;
}
return 0;
diff --git a/sources/TZR_DrawImage.c b/sources/TZR_DrawImage.c
index 6d9ce1f..37193e7 100644
--- a/sources/TZR_DrawImage.c
+++ b/sources/TZR_DrawImage.c
@@ -10,12 +10,12 @@ int
_TZR_DrawImage(const TZR_DrawImageArgs *args)
{
if (args->id == 0) {
- fprintf(stderr, "args->id is 0\n");
+ TZR_Log("args->id is 0");
return -1;
}
if (TZR_GetResourceType(args->id) != TZR_RES_IMAGE) {
- fprintf(stderr, "%u isn't an image\n", args->id);
+ TZR_Log("%u isn't an image", args->id);
return -1;
}
TZR_Resource *const res = TZR_GetResourcePointer(args->id);
diff --git a/sources/TZR_Init.c b/sources/TZR_Init.c
index ac436b3..5c9241b 100644
--- a/sources/TZR_Init.c
+++ b/sources/TZR_Init.c
@@ -5,6 +5,7 @@
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_mouse.h>
#include <SDL2/SDL_ttf.h>
+#include <errno.h>
#include <unistd.h>
static int
@@ -25,7 +26,7 @@ _TZR_Init(const TZR_Config *config)
if (___tzr_config.mixer == TZR_MIXER_FLAC &&
Mix_Init(MIX_INIT_FLAC) != MIX_INIT_FLAC)
{
- SDL_Log("%s", Mix_GetError());
+ TZR_Log("%s", Mix_GetError());
___tzr_config.mixer = TZR_MIXER_OFF;
}
@@ -39,7 +40,7 @@ _TZR_Init(const TZR_Config *config)
} else
chdir_rv = chdir(___tzr_config.basepath);
if (chdir_rv < 0)
- return perror("TZR_Init"), TZR_Quit(), -1;
+ return TZR_Log("%s", strerror(errno)), TZR_Quit(), -1;
___tzr_window =
SDL_CreateWindow(config->title,
@@ -111,14 +112,14 @@ _TZR_Init(const TZR_Config *config)
#ifdef TZR_SOLOUD
___tzr_soloud = Soloud_create();
if (___tzr_soloud == NULL) {
- SDL_Log("Soloud_create failed");
+ TZR_Log("Soloud_create failed");
Mix_Quit();
___tzr_config.mixer = false;
break;
}
if (Soloud_init(___tzr_soloud)) {
- SDL_Log("Soloud_init failed");
+ TZR_Log("Soloud_init failed");
Soloud_destroy(___tzr_soloud);
___tzr_soloud = NULL;
Mix_Quit();
diff --git a/sources/TZR_LoadResourceFromMemory.c b/sources/TZR_LoadResourceFromMemory.c
index 13adf2a..b995b4c 100644
--- a/sources/TZR_LoadResourceFromMemory.c
+++ b/sources/TZR_LoadResourceFromMemory.c
@@ -15,7 +15,7 @@ TZR_LoadResourceFromMemory(TZR_ResourceType type, const void *data, int size)
&___tzr_resources_capacity,
sizeof(TZR_Resource)))
{
- fprintf(stderr, "failed to reserve for new ressource\n");
+ TZR_Log("failed to reserve for new ressource");
return 0;
}
___tzr_resources = vec;
diff --git a/sources/TZR_LoadResourceTyped.c b/sources/TZR_LoadResourceTyped.c
index 126737b..8a2c5ab 100644
--- a/sources/TZR_LoadResourceTyped.c
+++ b/sources/TZR_LoadResourceTyped.c
@@ -1,6 +1,8 @@
#include "TZR_resource.h"
#include "TZR_globals.h"
#include "drain.h"
+#include <SDL2/SDL_log.h>
+#include <errno.h>
#include <string.h>
#include <sys/stat.h>
@@ -13,7 +15,7 @@ TZR_LoadResourceTyped(TZR_ResourceType type, const char *path)
return i + 1;
FILE *const fp = fopen(path, "rb");
if (fp == NULL) {
- perror(path);
+ TZR_Log("%s: %s", path, strerror(errno));
return 0;
}
size_t size;
@@ -33,7 +35,7 @@ TZR_LoadResourceTyped(TZR_ResourceType type, const char *path)
res->path = malloc(path_len + 1);
res->mtime = 0;
if (res->path == NULL)
- perror("TZR_LoadResourceTyped");
+ TZR_Log("%s", strerror(errno));
else
strcpy(res->path, path);
return id;
diff --git a/sources/TZR_PlayMusic.c b/sources/TZR_PlayMusic.c
index 10725b6..854c50f 100644
--- a/sources/TZR_PlayMusic.c
+++ b/sources/TZR_PlayMusic.c
@@ -6,7 +6,7 @@ int
TZR_PlayMusic(const char *path, int loop)
{
if (!___tzr_config.mixer) {
- fprintf(stderr, "mixer is disabled\n");
+ TZR_Log("mixer is disabled");
return -1;
}
TZR_StopMusic();
diff --git a/sources/TZR_PlaySound.c b/sources/TZR_PlaySound.c
index aa0d862..ce75032 100644
--- a/sources/TZR_PlaySound.c
+++ b/sources/TZR_PlaySound.c
@@ -8,12 +8,12 @@ int
_TZR_PlaySound(const TZR_PlaySoundArgs *args)
{
if (args->id == 0) {
- SDL_Log("args->id is 0");
+ TZR_Log("args->id is 0");
return -1;
}
if (TZR_GetResourceType(args->id) != TZR_RES_SOUND) {
- SDL_Log("%u isn't a sound", args->id);
+ TZR_Log("%u isn't a sound", args->id);
return -1;
}
diff --git a/sources/TZR_PollEvent.c b/sources/TZR_PollEvent.c
index 826f0e0..a4a489d 100644
--- a/sources/TZR_PollEvent.c
+++ b/sources/TZR_PollEvent.c
@@ -58,8 +58,7 @@ TZR_PollEvent(TZR_Event *e)
&___tzr_joysticks_capacity,
sizeof(SDL_Joystick *)))
{
- fprintf(stderr, "failed to reserve"
- "for new joystick\n");
+ TZR_Log("failed to reserve for new joystick");
break;
}
___tzr_joysticks = vec;
@@ -74,7 +73,7 @@ TZR_PollEvent(TZR_Event *e)
}
memset(&___tzr_joysticks[i], 0, sizeof(___tzr_joysticks[i]));
___tzr_joysticks[i].ptr = jstick;
- fprintf(stderr, "opened joystick\n");
+ TZR_Log("opened joystick");
break;
}
case SDL_JOYDEVICEREMOVED:
@@ -87,7 +86,7 @@ TZR_PollEvent(TZR_Event *e)
break;
}
}
- fprintf(stderr, "didn't remove no joystick\n");
+ TZR_Log("didn't remove no joystick");
break;
default:
break;
diff --git a/sources/TZR_ReloadResource.c b/sources/TZR_ReloadResource.c
index 1b7ac33..dda5b58 100644
--- a/sources/TZR_ReloadResource.c
+++ b/sources/TZR_ReloadResource.c
@@ -1,5 +1,6 @@
#include "TZR_resource.h"
#include "drain.h"
+#include <errno.h>
#include <sys/stat.h>
int
@@ -7,12 +8,12 @@ TZR_ReloadResource(TZR_Uint id)
{
TZR_Resource *const res = TZR_GetResourcePointer(id);
if (res->path == NULL) {
- fprintf(stderr, "resource path of %u is NULL\n", id);
+ TZR_Log("resource path of %u is NULL", id);
return -1;
}
FILE *const fp = fopen(res->path, "rb");
if (fp == NULL) {
- perror(res->path);
+ TZR_Log("%s: %s", res->path, strerror(errno));
return -1;
}
size_t size;