From 6b5b45322f9f07cb0cc777f10e1dfd67c4504e66 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Sun, 30 Oct 2022 13:53:46 -0500 Subject: [PATCH] Added a basic font cache and got some basic text scaling on the go. --- includes/font_cache.h | 17 +++++++++++++++++ src/font_cache.c | 44 +++++++++++++++++++++++++++++++++++++++++++ src/main.c | 44 ++++++++++++++++++++++++++++++++++++------- 3 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 includes/font_cache.h create mode 100644 src/font_cache.c diff --git a/includes/font_cache.h b/includes/font_cache.h new file mode 100644 index 0000000..fb3604f --- /dev/null +++ b/includes/font_cache.h @@ -0,0 +1,17 @@ +#ifndef FONTCACHE_H +#define FONTCACHE_H + +#include + +typedef struct { + unsigned int Capcity; + unsigned int Size; + char* FontFilePath; + TTF_Font** Fonts; +} FontCache; + +FontCache* CreateFontCache(char* fontFilePath); +TTF_Font* GetFont(unsigned short fontSize, FontCache* cache); +void FreeFontCache(FontCache* cache); + +#endif \ No newline at end of file diff --git a/src/font_cache.c b/src/font_cache.c new file mode 100644 index 0000000..72ddded --- /dev/null +++ b/src/font_cache.c @@ -0,0 +1,44 @@ +#include "../includes/font_cache.h" +#include +#include + +#define DEFAULTCACHESIZE 32 + +FontCache* CreateFontCache(char* fontFilePath) { + FontCache* cache = calloc(1, sizeof(FontCache)); + + if (!cache) { + return NULL; + } + + cache->Fonts = calloc(DEFAULTCACHESIZE, sizeof(TTF_Font*)); + + if (!cache->Fonts) { + return NULL; + } + + cache->FontFilePath = fontFilePath; + cache->Capcity = DEFAULTCACHESIZE; + + return cache; +} + +TTF_Font* GetFont(unsigned short fontSize, FontCache* cache) { + //TTF_OpenFont("/usr/share/fonts/TTF/Hack-Regular.ttf", 14); + + TTF_Font* font = cache->Fonts[fontSize]; + + if (font) return font; + + cache->Fonts[fontSize] = TTF_OpenFont(cache->FontFilePath, fontSize); + + return cache->Fonts[fontSize]; +} + +void FreeFontCache(FontCache* cache) { + for (int i = 0; i < cache->Capcity; i++) { + if (cache->Fonts[i]) { + TTF_CloseFont(cache->Fonts[i]); + } + } +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index 9bded46..ce62b11 100644 --- a/src/main.c +++ b/src/main.c @@ -17,6 +17,7 @@ #include #include #include +#include "../includes/font_cache.h" const int SCREEN_WIDTH = 1280; const int SCREEN_HEIGHT = 720; @@ -26,7 +27,8 @@ float ZoomLevel = 1; typedef struct { SDL_Window *window; SDL_Renderer *renderer; - TTF_Font* font; + FontCache* FontCache; + unsigned short DefaultFontSize; } App; App app; @@ -39,13 +41,14 @@ int XOrg, YOrg; void Cleanup(void) { SDL_DestroyWindow(app.window); SDL_DestroyRenderer(app.renderer); - TTF_CloseFont(app.font); + FreeFontCache(app.FontCache); TTF_Quit(); SDL_Quit(); } int main(int argc, char** argv){ memset(&app, 0, sizeof(app)); + app.DefaultFontSize = 14; XRel = 0; YRel = 0; XOrg = 0; @@ -63,12 +66,14 @@ int main(int argc, char** argv){ return -1; } - app.font = TTF_OpenFont("/usr/share/fonts/TTF/Hack-Regular.ttf", 14); + app.FontCache = CreateFontCache("/usr/share/fonts/TTF/Hack-Regular.ttf"); - if (app.font == NULL) { - printf("Failed to load font\n"); - return -1; - } + // app.font = TTF_OpenFont("/usr/share/fonts/TTF/Hack-Regular.ttf", 14); + + // if (app.font == NULL) { + // printf("Failed to load font\n"); + // return -1; + // } app.window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); @@ -101,6 +106,17 @@ int main(int argc, char** argv){ rect2.y = 250;// / SCREEN_HEIGHT; rect2.w = SCREEN_WIDTH / 4; rect2.h = SCREEN_HEIGHT / 4; + float previousZoom = ZoomLevel; + SDL_Texture* texture; + SDL_Rect textSize; + + TTF_SizeUTF8(GetFont(app.DefaultFontSize * ZoomLevel, app.FontCache), "Hello, world!", &textSize.w, &textSize.h); + + SDL_Surface* surface = TTF_RenderText_Blended(GetFont(app.DefaultFontSize * ZoomLevel, app.FontCache), "Hello, world!", (SDL_Color){0,0,0,0}); + + texture = SDL_CreateTextureFromSurface(app.renderer, surface); + + SDL_FreeSurface(surface); // Uint64 start = 0; // Uint64 end = 0; @@ -131,6 +147,20 @@ int main(int argc, char** argv){ SDL_RenderFillRectF(app.renderer, &(SDL_FRect){rect2.x + XRel, rect2.y + YRel, rect2.w * ZoomLevel, rect2.h * ZoomLevel}); + if (previousZoom != ZoomLevel) { + SDL_DestroyTexture(texture); + + TTF_SizeUTF8(GetFont(app.DefaultFontSize * ZoomLevel, app.FontCache), "Hello, world!", &textSize.w, &textSize.h); + + SDL_Surface* surface = TTF_RenderText_Blended(GetFont(app.DefaultFontSize * ZoomLevel, app.FontCache), "Hello, world!", (SDL_Color){0,0,0,0}); + + texture = SDL_CreateTextureFromSurface(app.renderer, surface); + + SDL_FreeSurface(surface); + } + + SDL_RenderCopyF(app.renderer, texture, NULL, &(SDL_FRect){rect2.x + XRel, rect2.y + YRel, textSize.w * ZoomLevel, textSize.h * ZoomLevel}); + SDL_RenderPresent(app.renderer); // end = SDL_GetPerformanceCounter();