diff --git a/includes/list.h b/includes/list.h index 44cac82..0bfe08e 100644 --- a/includes/list.h +++ b/includes/list.h @@ -10,14 +10,15 @@ typedef struct { unsigned int Capacity; unsigned int Size; - unsigned int MaxStringLength; char** Strings; + SDL_Rect MaxTestSize; SDL_FRect BoundingBox; TextureFontCache* TextureCache; } List; List* CreateList(SDL_FRect boundingBox); void AddStringToList(char* text, List* list); -void RenderList(SDL_Renderer* renderer, SDL_Point motion, TTF_Font* font, unsigned short fontSize, List* list); +void RenderList(SDL_Renderer* renderer, SDL_Point motion, List* list); +void ListFontSizeHasChanged(SDL_Renderer* renderer, TTF_Font* font, List* list); #endif \ No newline at end of file diff --git a/includes/texture_font_cache.h b/includes/texture_font_cache.h index 74841f6..c0cefcf 100644 --- a/includes/texture_font_cache.h +++ b/includes/texture_font_cache.h @@ -17,8 +17,8 @@ typedef struct { } TextureFontCache; TextureFontCache* CreateTextureFontCache(); -TextureDetails* AddTextureToCache(SDL_Texture* texture, unsigned short fontSize, SDL_Rect textSize, TextureFontCache* cache); -void UpdateTextureDetails(SDL_Texture* texture, unsigned short fontSize, SDL_Rect textSize, int index, TextureFontCache* cache); +TextureDetails* AddTextureToCache(SDL_Texture* texture, SDL_Rect textSize, TextureFontCache* cache); +void UpdateTextureDetails(SDL_Texture* texture, SDL_Rect textSize, int index, TextureFontCache* cache); TextureDetails* GetTextureFromCache(TextureFontCache* cache, unsigned int index); void FreeTextureDetails(TextureDetails* details); diff --git a/src/list.c b/src/list.c index 8f02f34..10c36d9 100644 --- a/src/list.c +++ b/src/list.c @@ -30,51 +30,61 @@ void AddStringToList(char* text, List* list) { list->Capacity *= 2; } - if (list->MaxStringLength < strlen(text)) list->MaxStringLength = strlen(text); + //if (list->MaxStringLength < strlen(text)) list->MaxStringLength = strlen(text); list->Strings[list->Size] = text; list->Size++; } -void RenderList(SDL_Renderer* renderer, SDL_Point motion, TTF_Font* font, unsigned short fontSize, List* list) { +void ListFontSizeHasChanged(SDL_Renderer* renderer, TTF_Font* font, List* list) { + TextureDetails* details = GetTextureFromCache(list->TextureCache, 0); + + 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}); + + SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surface); + + SDL_FreeSurface(surface); + + if (details) UpdateTextureDetails(tex, textSize, 0, list->TextureCache); + else details = AddTextureToCache(tex, textSize, list->TextureCache); + + for (int i = 0; i < list->Size; i++) { + TextureDetails* details = GetTextureFromCache(list->TextureCache, i+1); + char* item = list->Strings[i]; + + SDL_Rect textSize; + + TTF_SizeUTF8(font, item, &textSize.w, &textSize.h); + SDL_Surface* surface = TTF_RenderText_Shaded(font, item, (SDL_Color){255, 255, 255, 0}, (SDL_Color){0, 0, 0, 0}); + + SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surface); + + SDL_FreeSurface(surface); + + if (details) UpdateTextureDetails(tex, textSize, i+1, list->TextureCache); + else details = AddTextureToCache(tex, textSize, list->TextureCache); + } +} + +void RenderStructHeader(SDL_Renderer* renderer, TTF_Font* font, unsigned short fontSize, SDL_FRect* cursor, TextureFontCache* cache) { + TextureDetails* details = GetTextureFromCache(cache, 0); + + +} + +void RenderList(SDL_Renderer* renderer, SDL_Point motion, 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); SDL_FRect contentsBox = { .x = list->BoundingBox.x + list->BoundingBox.w * .125, .y = list->BoundingBox.y, .w = list->BoundingBox.w * 0.75, .h = list->BoundingBox.h * 0.15 }; SDL_RenderDrawRectF(renderer, &list->BoundingBox); - 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); - - if (details) UpdateTextureDetails(tex, fontSize, textSize, 0, list->TextureCache); - else details = AddTextureToCache(tex, fontSize, textSize, list->TextureCache); - } - for (int i = 0; i < list->Size; i++) { SDL_RenderDrawRectF(renderer, &contentsBox); TextureDetails* details = GetTextureFromCache(list->TextureCache, i + 1); - char* item = list->Strings[i]; - - if (!details || fontSize != details->FontSize) { - SDL_Rect textSize; - TTF_SizeUTF8(font, item, &textSize.w, &textSize.h); - SDL_Surface* surface = TTF_RenderText_Shaded(font, item, (SDL_Color){255, 255, 255, 0}, (SDL_Color){0, 0, 0, 0}); - - SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surface); - - SDL_FreeSurface(surface); - - if (details) UpdateTextureDetails(tex, fontSize, textSize, i+1, list->TextureCache); - else details = AddTextureToCache(tex, fontSize, textSize, list->TextureCache); - } SDL_FRect textArea; textArea.x = contentsBox.x + contentsBox.w * 0.50 - details->TextSize.w * 0.50; @@ -82,9 +92,6 @@ void RenderList(SDL_Renderer* renderer, SDL_Point motion, TTF_Font* font, unsign textArea.w = details->TextSize.w; textArea.h = details->TextSize.h; - //contentsBox.x = xOrg + contentsBox.w - details->TextSize.w * 0.50; - - SDL_RenderCopyF(renderer, details->Texture, NULL, &textArea); } diff --git a/src/main.c b/src/main.c index 5f8dc44..b433abd 100644 --- a/src/main.c +++ b/src/main.c @@ -124,6 +124,8 @@ int main(int argc, char** argv){ List* list = CreateList(boundingBox); AddStringToList("Hello World!", list); + ListFontSizeHasChanged(app.renderer, GetFont(app.DefaultFontSize, app.FontCache), list); + // Uint64 start = 0; // Uint64 end = 0; // float frameCount = 0; @@ -163,11 +165,13 @@ int main(int argc, char** argv){ texture = SDL_CreateTextureFromSurface(app.renderer, surface); SDL_FreeSurface(surface); + + ListFontSizeHasChanged(app.renderer, GetFont(app.DefaultFontSize * ZoomLevel, app.FontCache), list); } SDL_RenderCopyF(app.renderer, texture, NULL, &(SDL_FRect){rect2.x + XRel, rect2.y + YRel, textSize.w * ZoomLevel, textSize.h * ZoomLevel}); - RenderList(app.renderer, (SDL_Point) {XRel,YRel}, GetFont(app.DefaultFontSize * ZoomLevel, app.FontCache), app.DefaultFontSize * ZoomLevel, list); + RenderList(app.renderer, (SDL_Point) {XRel,YRel}, list); SDL_RenderPresent(app.renderer); diff --git a/src/texture_font_cache.c b/src/texture_font_cache.c index 3eaa022..d47a04e 100644 --- a/src/texture_font_cache.c +++ b/src/texture_font_cache.c @@ -2,10 +2,9 @@ #include #include -TextureDetails* CreateTextureDetails(SDL_Texture* texture, unsigned short fontSize, SDL_Rect textSize) { +TextureDetails* CreateTextureDetails(SDL_Texture* texture, SDL_Rect textSize) { TextureDetails* details = calloc(1, sizeof(TextureDetails)); - details->FontSize = fontSize; details->Texture = texture; details->TextSize = textSize; @@ -31,7 +30,7 @@ TextureDetails* GetTextureFromCache(TextureFontCache* cache, unsigned int index) return details; } -TextureDetails* AddTextureToCache(SDL_Texture* texture, unsigned short fontSize, SDL_Rect textSize, TextureFontCache* cache) { +TextureDetails* AddTextureToCache(SDL_Texture* texture, SDL_Rect textSize, TextureFontCache* cache) { if (cache->Size + 1 > cache->Capacity) { TextureDetails** newContent = realloc(cache->Textures, cache->Capacity * 2); @@ -39,13 +38,13 @@ TextureDetails* AddTextureToCache(SDL_Texture* texture, unsigned short fontSize, cache->Capacity *= 2; } - cache->Textures[cache->Size] = CreateTextureDetails(texture, fontSize, textSize); + cache->Textures[cache->Size] = CreateTextureDetails(texture, textSize); cache->Size++; return cache->Textures[cache->Size - 1]; } -void UpdateTextureDetails(SDL_Texture* texture, unsigned short fontSize, SDL_Rect textSize, int index, TextureFontCache* cache) { +void UpdateTextureDetails(SDL_Texture* texture, SDL_Rect textSize, int index, TextureFontCache* cache) { if (index >= cache->Size) return; TextureDetails* details = cache->Textures[index]; @@ -53,7 +52,6 @@ void UpdateTextureDetails(SDL_Texture* texture, unsigned short fontSize, SDL_Rec SDL_DestroyTexture(details->Texture); details->Texture = texture; - details->FontSize = fontSize; details->TextSize = textSize; }