From 10e606e2b6a61c0ab8950d1acb434614b4b72bf4 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Tue, 1 Nov 2022 18:10:01 +0000 Subject: [PATCH] Fixed a memory corruption bug in the List's render function. --- includes/texture_font_cache.h | 1 + src/list.c | 5 +++-- src/main.c | 1 + src/texture_font_cache.c | 12 ++++++++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/includes/texture_font_cache.h b/includes/texture_font_cache.h index 9e69bb2..74841f6 100644 --- a/includes/texture_font_cache.h +++ b/includes/texture_font_cache.h @@ -18,6 +18,7 @@ typedef struct { 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* GetTextureFromCache(TextureFontCache* cache, unsigned int index); void FreeTextureDetails(TextureDetails* details); diff --git a/src/list.c b/src/list.c index 65601ae..67e873a 100644 --- a/src/list.c +++ b/src/list.c @@ -45,13 +45,14 @@ void RenderList(SDL_Renderer* renderer, SDL_FPoint motion, TTF_Font* font, unsig 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); + //if (!details) FreeTextureDetails(details); SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surface); SDL_FreeSurface(surface); - details = AddTextureToCache(tex, fontSize, textSize, list->TextureCache); + if (details) UpdateTextureDetails(tex, fontSize, textSize, 0, list->TextureCache); + else details = AddTextureToCache(tex, fontSize, textSize, list->TextureCache); } SDL_FRect size; diff --git a/src/main.c b/src/main.c index df2a1dc..af76be3 100644 --- a/src/main.c +++ b/src/main.c @@ -186,6 +186,7 @@ void HandleInput(void) { Exit = 1; break; case SDL_MOUSEBUTTONDOWN: + if (event.button.button == SDL_BUTTON_RIGHT) Exit = 1; MouseDown = 1; break; case SDL_MOUSEBUTTONUP: diff --git a/src/texture_font_cache.c b/src/texture_font_cache.c index 766996c..3eaa022 100644 --- a/src/texture_font_cache.c +++ b/src/texture_font_cache.c @@ -45,6 +45,18 @@ TextureDetails* AddTextureToCache(SDL_Texture* texture, unsigned short fontSize, return cache->Textures[cache->Size - 1]; } +void UpdateTextureDetails(SDL_Texture* texture, unsigned short fontSize, SDL_Rect textSize, int index, TextureFontCache* cache) { + if (index >= cache->Size) return; + + TextureDetails* details = cache->Textures[index]; + + SDL_DestroyTexture(details->Texture); + + details->Texture = texture; + details->FontSize = fontSize; + details->TextSize = textSize; +} + void FreeTextureDetails(TextureDetails* details) { if (!details) return;