diff --git a/includes/list.h b/includes/list.h index 033b8ad..1ad3bf8 100644 --- a/includes/list.h +++ b/includes/list.h @@ -5,13 +5,19 @@ #include #include #include +#include "../includes/texture_font_cache.h" typedef struct { - unsigned int Capcity; + unsigned int Capacity; unsigned int Size; + unsigned int MaxStringLength; char** Strings; + SDL_FRect BoundingBox; + TextureFontCache* TextureCache; } List; -List* CreateList(void); +List* CreateList(SDL_FRect boundingBox); +void AddStringToList(char* text, List* list); +void RenderList(SDL_Renderer* renderer, SDL_FPoint motion, TTF_Font* font, unsigned short fontSize, List* list); #endif \ No newline at end of file diff --git a/includes/texture_font_cache.h b/includes/texture_font_cache.h new file mode 100644 index 0000000..9e69bb2 --- /dev/null +++ b/includes/texture_font_cache.h @@ -0,0 +1,24 @@ +#ifndef TEXTURE_FONT_CACHE_h +#define TEXTURE_FONT_CACHE_h + +#include +#include + +typedef struct { + unsigned short FontSize; + SDL_Rect TextSize; + SDL_Texture* Texture; +} TextureDetails; + +typedef struct { + unsigned int Capacity; + unsigned int Size; + TextureDetails** Textures; +} TextureFontCache; + +TextureFontCache* CreateTextureFontCache(); +TextureDetails* AddTextureToCache(SDL_Texture* texture, unsigned short fontSize, SDL_Rect textSize, TextureFontCache* cache); +TextureDetails* GetTextureFromCache(TextureFontCache* cache, unsigned int index); +void FreeTextureDetails(TextureDetails* details); + +#endif diff --git a/src/list.c b/src/list.c index bdb5e97..65601ae 100644 --- a/src/list.c +++ b/src/list.c @@ -1,16 +1,64 @@ #include "../includes/list.h" +#include +#include +#include +#include #include #define DEFAULTLISTSIZE 64 +#define MINWIDTH 100 +#define MINHEIGHT 50 -List* CreateList(void) { +List* CreateList(SDL_FRect boundingBox) { List* list = calloc(1, sizeof(List)); list->Strings = calloc(DEFAULTLISTSIZE, sizeof(char*)); + list->TextureCache = CreateTextureFontCache(); - list->Capcity = DEFAULTLISTSIZE; + list->Capacity = DEFAULTLISTSIZE; list->Size = 0; + list->BoundingBox = boundingBox; return list; } +void AddStringToList(char* text, List* list) { + if (list->Size + 1 > list->Capacity) { + char** newContent = realloc(list->Strings, list->Capacity * 2); + + list->Strings = newContent; + list->Capacity *= 2; + } + + if (list->MaxStringLength < strlen(text)) list->MaxStringLength = strlen(text); + + list->Strings[list->Size] = text; + list->Size++; +} + +void RenderList(SDL_Renderer* renderer, SDL_FPoint motion, TTF_Font* font, unsigned short fontSize, List* list) { + //SDL_Surface* surface = TTF_RenderText_Blended_Wrapped(font, list->Strings[0], (SDL_Color){255, 255, 255, 0}, 90); + TextureDetails* details = GetTextureFromCache(list->TextureCache, 0); + + if (!details || fontSize != details->FontSize) { + SDL_Rect textSize; + TTF_SizeUTF8(font, "List (Array)", &textSize.w, &textSize.h); + SDL_Surface* surface = TTF_RenderText_Shaded(font, "List (Array)", (SDL_Color){255, 255, 255, 0}, (SDL_Color){0, 0, 0, 0}); + + if (!details) FreeTextureDetails(details); + + SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surface); + + SDL_FreeSurface(surface); + + details = AddTextureToCache(tex, fontSize, textSize, list->TextureCache); + } + + SDL_FRect size; + size.x = list->BoundingBox.x; + size.y = list->BoundingBox.y; + size.h = details->TextSize.h; + size.w = details->TextSize.w; + + SDL_RenderCopyF(renderer, details->Texture, NULL, &size); +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index ce62b11..df2a1dc 100644 --- a/src/main.c +++ b/src/main.c @@ -18,6 +18,7 @@ #include #include #include "../includes/font_cache.h" +#include "../includes/list.h" const int SCREEN_WIDTH = 1280; const int SCREEN_HEIGHT = 720; @@ -118,6 +119,10 @@ int main(int argc, char** argv){ SDL_FreeSurface(surface); + SDL_FRect boundingBox = {650, 700, 300, 90}; + + List* list = CreateList(boundingBox); + // Uint64 start = 0; // Uint64 end = 0; // float frameCount = 0; @@ -161,6 +166,8 @@ int main(int argc, char** argv){ SDL_RenderCopyF(app.renderer, texture, NULL, &(SDL_FRect){rect2.x + XRel, rect2.y + YRel, textSize.w * ZoomLevel, textSize.h * ZoomLevel}); + RenderList(app.renderer, (SDL_FPoint) {0,0}, GetFont(app.DefaultFontSize * ZoomLevel, app.FontCache), app.DefaultFontSize * ZoomLevel, list); + SDL_RenderPresent(app.renderer); // end = SDL_GetPerformanceCounter(); diff --git a/src/texture_font_cache.c b/src/texture_font_cache.c new file mode 100644 index 0000000..766996c --- /dev/null +++ b/src/texture_font_cache.c @@ -0,0 +1,54 @@ +#include "../includes/texture_font_cache.h" +#include +#include + +TextureDetails* CreateTextureDetails(SDL_Texture* texture, unsigned short fontSize, SDL_Rect textSize) { + TextureDetails* details = calloc(1, sizeof(TextureDetails)); + + details->FontSize = fontSize; + details->Texture = texture; + details->TextSize = textSize; + + return details; +} + +TextureFontCache* CreateTextureFontCache() { + TextureFontCache* cache = calloc(1, sizeof(TextureFontCache)); + + cache->Textures = calloc(64, sizeof(TextureDetails*)); + cache->Capacity = 64; + + return cache; +} + +TextureDetails* GetTextureFromCache(TextureFontCache* cache, unsigned int index) { + if (!cache || index >= cache->Size) return NULL; + + TextureDetails* details = cache->Textures[index]; + + if (!details) return NULL; + + return details; +} + +TextureDetails* AddTextureToCache(SDL_Texture* texture, unsigned short fontSize, SDL_Rect textSize, TextureFontCache* cache) { + if (cache->Size + 1 > cache->Capacity) { + TextureDetails** newContent = realloc(cache->Textures, cache->Capacity * 2); + + cache->Textures = newContent; + cache->Capacity *= 2; + } + + cache->Textures[cache->Size] = CreateTextureDetails(texture, fontSize, textSize); + cache->Size++; + + return cache->Textures[cache->Size - 1]; +} + +void FreeTextureDetails(TextureDetails* details) { + if (!details) return; + + SDL_DestroyTexture(details->Texture); + + free(details); +} \ No newline at end of file