Updated the list rendering function to just blindly take textures from the cache and relying on a new function to handle generating new textures if needed.

This commit is contained in:
2022-11-02 19:15:57 -05:00
parent 3dfe60fcc6
commit 08643b8fc5
5 changed files with 55 additions and 45 deletions
+3 -2
View File
@@ -10,14 +10,15 @@
typedef struct { typedef struct {
unsigned int Capacity; unsigned int Capacity;
unsigned int Size; unsigned int Size;
unsigned int MaxStringLength;
char** Strings; char** Strings;
SDL_Rect MaxTestSize;
SDL_FRect BoundingBox; SDL_FRect BoundingBox;
TextureFontCache* TextureCache; TextureFontCache* TextureCache;
} List; } List;
List* CreateList(SDL_FRect boundingBox); List* CreateList(SDL_FRect boundingBox);
void AddStringToList(char* text, List* list); 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 #endif
+2 -2
View File
@@ -17,8 +17,8 @@ typedef struct {
} TextureFontCache; } TextureFontCache;
TextureFontCache* CreateTextureFontCache(); TextureFontCache* CreateTextureFontCache();
TextureDetails* AddTextureToCache(SDL_Texture* texture, unsigned short fontSize, SDL_Rect textSize, TextureFontCache* cache); TextureDetails* AddTextureToCache(SDL_Texture* texture, SDL_Rect textSize, TextureFontCache* cache);
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);
TextureDetails* GetTextureFromCache(TextureFontCache* cache, unsigned int index); TextureDetails* GetTextureFromCache(TextureFontCache* cache, unsigned int index);
void FreeTextureDetails(TextureDetails* details); void FreeTextureDetails(TextureDetails* details);
+41 -34
View File
@@ -30,51 +30,61 @@ void AddStringToList(char* text, List* list) {
list->Capacity *= 2; 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->Strings[list->Size] = text;
list->Size++; 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); //SDL_Surface* surface = TTF_RenderText_Blended_Wrapped(font, list->Strings[0], (SDL_Color){255, 255, 255, 0}, 90);
TextureDetails* details = GetTextureFromCache(list->TextureCache, 0); 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_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); 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++) { for (int i = 0; i < list->Size; i++) {
SDL_RenderDrawRectF(renderer, &contentsBox); SDL_RenderDrawRectF(renderer, &contentsBox);
TextureDetails* details = GetTextureFromCache(list->TextureCache, i + 1); 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; SDL_FRect textArea;
textArea.x = contentsBox.x + contentsBox.w * 0.50 - details->TextSize.w * 0.50; 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.w = details->TextSize.w;
textArea.h = details->TextSize.h; textArea.h = details->TextSize.h;
//contentsBox.x = xOrg + contentsBox.w - details->TextSize.w * 0.50;
SDL_RenderCopyF(renderer, details->Texture, NULL, &textArea); SDL_RenderCopyF(renderer, details->Texture, NULL, &textArea);
} }
+5 -1
View File
@@ -124,6 +124,8 @@ int main(int argc, char** argv){
List* list = CreateList(boundingBox); List* list = CreateList(boundingBox);
AddStringToList("Hello World!", list); AddStringToList("Hello World!", list);
ListFontSizeHasChanged(app.renderer, GetFont(app.DefaultFontSize, app.FontCache), list);
// Uint64 start = 0; // Uint64 start = 0;
// Uint64 end = 0; // Uint64 end = 0;
// float frameCount = 0; // float frameCount = 0;
@@ -163,11 +165,13 @@ int main(int argc, char** argv){
texture = SDL_CreateTextureFromSurface(app.renderer, surface); texture = SDL_CreateTextureFromSurface(app.renderer, surface);
SDL_FreeSurface(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}); 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); SDL_RenderPresent(app.renderer);
+4 -6
View File
@@ -2,10 +2,9 @@
#include <SDL2/SDL_render.h> #include <SDL2/SDL_render.h>
#include <stdlib.h> #include <stdlib.h>
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)); TextureDetails* details = calloc(1, sizeof(TextureDetails));
details->FontSize = fontSize;
details->Texture = texture; details->Texture = texture;
details->TextSize = textSize; details->TextSize = textSize;
@@ -31,7 +30,7 @@ TextureDetails* GetTextureFromCache(TextureFontCache* cache, unsigned int index)
return details; 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) { if (cache->Size + 1 > cache->Capacity) {
TextureDetails** newContent = realloc(cache->Textures, cache->Capacity * 2); TextureDetails** newContent = realloc(cache->Textures, cache->Capacity * 2);
@@ -39,13 +38,13 @@ TextureDetails* AddTextureToCache(SDL_Texture* texture, unsigned short fontSize,
cache->Capacity *= 2; cache->Capacity *= 2;
} }
cache->Textures[cache->Size] = CreateTextureDetails(texture, fontSize, textSize); cache->Textures[cache->Size] = CreateTextureDetails(texture, textSize);
cache->Size++; cache->Size++;
return cache->Textures[cache->Size - 1]; 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; if (index >= cache->Size) return;
TextureDetails* details = cache->Textures[index]; TextureDetails* details = cache->Textures[index];
@@ -53,7 +52,6 @@ void UpdateTextureDetails(SDL_Texture* texture, unsigned short fontSize, SDL_Rec
SDL_DestroyTexture(details->Texture); SDL_DestroyTexture(details->Texture);
details->Texture = texture; details->Texture = texture;
details->FontSize = fontSize;
details->TextSize = textSize; details->TextSize = textSize;
} }