aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-10-27 14:06:01 +0200
committerkdx <kikoodx@paranoici.org>2023-10-27 14:06:01 +0200
commit38428ea84cbc72bae05c0f7f7535af485d96bc89 (patch)
tree4e6624571734ce094172df627c57dbe70ec43ee4
parent9859bf702aa695264fc8b9a4155bf8d6932e5c70 (diff)
downloadtzr-38428ea84cbc72bae05c0f7f7535af485d96bc89.tar.gz
load font (fixed ptsize)
-rw-r--r--headers/TZR_types.h8
-rw-r--r--sources/TZR_DirectResourceLoad.c22
-rw-r--r--sources/TZR_Init.c7
-rw-r--r--sources/TZR_LoadResource.c3
-rw-r--r--sources/TZR_Quit.c4
5 files changed, 44 insertions, 0 deletions
diff --git a/headers/TZR_types.h b/headers/TZR_types.h
index 6261799..105ac38 100644
--- a/headers/TZR_types.h
+++ b/headers/TZR_types.h
@@ -8,6 +8,7 @@ enum TZR_ResourceType {
TZR_RES_RAW,
TZR_RES_IMAGE,
TZR_RES_SOUND,
+ TZR_RES_FONT,
___TZR_RES_COUNT
};
@@ -39,6 +40,7 @@ typedef enum TZR_ResourceType TZR_ResourceType;
typedef struct TZR_Raw TZR_Raw;
typedef struct TZR_Image TZR_Image;
typedef struct TZR_Sound TZR_Sound;
+typedef struct TZR_Font TZR_Font;
typedef struct TZR_Resource TZR_Resource;
typedef enum TZR_EventType TZR_EventType;
typedef struct TZR_Event TZR_Event;
@@ -103,6 +105,11 @@ struct TZR_Sound {
};
#endif
+struct TZR_Font {
+ void *ptr;
+ int ptsize;
+};
+
struct TZR_Resource {
TZR_ResourceType type;
char *path; /* allocated and freed by TZR; can be NULL */
@@ -111,6 +118,7 @@ struct TZR_Resource {
TZR_Raw raw; /* raw file data */
TZR_Image image;
TZR_Sound sound;
+ TZR_Font ttf;
};
};
diff --git a/sources/TZR_DirectResourceLoad.c b/sources/TZR_DirectResourceLoad.c
index 7b53a5e..e3579b1 100644
--- a/sources/TZR_DirectResourceLoad.c
+++ b/sources/TZR_DirectResourceLoad.c
@@ -6,6 +6,10 @@
#include <SDL2/SDL_rwops.h>
#include <string.h>
+#ifdef TZR_TTF
+#include <SDL2/SDL_ttf.h>
+#endif
+
#ifdef TZR_STB_IMAGE
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
@@ -123,6 +127,24 @@ TZR_DirectResourceLoad(TZR_Resource *res, const void *data, int size)
res->sound.ptr = chunk;
#endif
} break;
+ case TZR_RES_FONT:
+#ifdef TZR_TTF
+ {
+ const int ptsize = 20;
+ SDL_RWops *const rw = SDL_RWFromConstMem(data, size);
+ if (rw == NULL)
+ return sdl_error(-1);
+ TTF_Font *font = TTF_OpenFontRW(rw, 0, ptsize);
+ SDL_RWclose(rw);
+ if (font == NULL)
+ return sdl_error(-1);
+ res->ttf.ptr = font;
+ res->ttf.ptsize = ptsize;
+ } break;
+#else
+ SDL_Log("add -DTZR_TTF= to your compile flags");
+ return -1;
+#endif
default:
fprintf(stderr, "invalid type\n");
return -1;
diff --git a/sources/TZR_Init.c b/sources/TZR_Init.c
index a598df4..a80880e 100644
--- a/sources/TZR_Init.c
+++ b/sources/TZR_Init.c
@@ -4,6 +4,7 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_mouse.h>
+#include <SDL2/SDL_ttf.h>
#include <unistd.h>
static int
@@ -117,5 +118,11 @@ _TZR_Init(const TZR_Config *config)
}
#endif
} while (0);
+
+#ifdef TZR_TTF
+ /* Setup font system. */
+ if (TTF_Init())
+ return _sdl_error();
+#endif
return 0;
}
diff --git a/sources/TZR_LoadResource.c b/sources/TZR_LoadResource.c
index c1c73ce..f503bcb 100644
--- a/sources/TZR_LoadResource.c
+++ b/sources/TZR_LoadResource.c
@@ -15,6 +15,9 @@ deduce_type(const char *path)
return TZR_RES_IMAGE;
if (strcasecmp(path_extension, ".wav") == 0)
return TZR_RES_SOUND;
+ if (strcasecmp(path_extension, ".ttf") == 0 ||
+ strcasecmp(path_extension, ".otf") == 0)
+ return TZR_RES_FONT;
return TZR_RES_RAW;
}
diff --git a/sources/TZR_Quit.c b/sources/TZR_Quit.c
index d0d6550..7d819a3 100644
--- a/sources/TZR_Quit.c
+++ b/sources/TZR_Quit.c
@@ -2,6 +2,7 @@
#include "TZR_globals.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
+#include <SDL2/SDL_ttf.h>
void
TZR_Quit(void)
@@ -43,5 +44,8 @@ TZR_Quit(void)
Mix_CloseAudio();
Mix_Quit();
}
+#ifdef TZR_TTF
+ TTF_Quit();
+#endif
SDL_Quit();
}