Fixed a memory corruption bug in the List's render function.

This commit is contained in:
2022-11-01 18:10:01 +00:00
parent a239e0eb4a
commit 10e606e2b6
4 changed files with 17 additions and 2 deletions
+1
View File
@@ -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);
+3 -2
View File
@@ -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;
+1
View File
@@ -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:
+12
View File
@@ -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;