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:
+3
-2
@@ -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
|
||||
@@ -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);
|
||||
|
||||
|
||||
+26
-19
@@ -30,41 +30,33 @@ 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) {
|
||||
//SDL_Surface* surface = TTF_RenderText_Blended_Wrapped(font, list->Strings[0], (SDL_Color){255, 255, 255, 0}, 90);
|
||||
void ListFontSizeHasChanged(SDL_Renderer* renderer, TTF_Font* font, List* list) {
|
||||
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);
|
||||
}
|
||||
if (details) UpdateTextureDetails(tex, textSize, 0, list->TextureCache);
|
||||
else details = AddTextureToCache(tex, 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});
|
||||
|
||||
@@ -72,9 +64,27 @@ void RenderList(SDL_Renderer* renderer, SDL_Point motion, TTF_Font* font, unsign
|
||||
|
||||
SDL_FreeSurface(surface);
|
||||
|
||||
if (details) UpdateTextureDetails(tex, fontSize, textSize, i+1, list->TextureCache);
|
||||
else details = AddTextureToCache(tex, fontSize, textSize, list->TextureCache);
|
||||
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);
|
||||
|
||||
for (int i = 0; i < list->Size; i++) {
|
||||
SDL_RenderDrawRectF(renderer, &contentsBox);
|
||||
TextureDetails* details = GetTextureFromCache(list->TextureCache, i + 1);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
+5
-1
@@ -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);
|
||||
|
||||
|
||||
@@ -2,10 +2,9 @@
|
||||
#include <SDL2/SDL_render.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));
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user