From 420cf2ac12fd1d84ef3850fcc17e1cffe7af4934 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Wed, 2 Nov 2022 20:25:47 -0500 Subject: [PATCH] A simple visualization of a flat list is done. --- src/list.c | 77 +++++++++++++++++++++++++++++++++--------------------- src/main.c | 8 +++++- 2 files changed, 54 insertions(+), 31 deletions(-) diff --git a/src/list.c b/src/list.c index 10c36d9..fd6f193 100644 --- a/src/list.c +++ b/src/list.c @@ -4,6 +4,7 @@ #include #include #include +#include #define DEFAULTLISTSIZE 64 #define MINWIDTH 100 @@ -38,11 +39,14 @@ void AddStringToList(char* text, List* list) { void ListFontSizeHasChanged(SDL_Renderer* renderer, TTF_Font* font, List* list) { TextureDetails* details = GetTextureFromCache(list->TextureCache, 0); + char buffer[100] = { 0 }; + + snprintf(buffer, sizeof(buffer), "Character Pointer Array Start: %p | Struct, List (Flat Array)", list->Strings[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}); + TTF_SizeUTF8(font, buffer, &textSize.w, &textSize.h); + SDL_Surface* surface = TTF_RenderText_Shaded(font, buffer, (SDL_Color){255, 255, 255, 0}, (SDL_Color){0, 0, 0, 0}); SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surface); @@ -52,13 +56,17 @@ void ListFontSizeHasChanged(SDL_Renderer* renderer, TTF_Font* font, List* list) else details = AddTextureToCache(tex, textSize, list->TextureCache); for (int i = 0; i < list->Size; i++) { + memset(buffer, '\0', 100); + TextureDetails* details = GetTextureFromCache(list->TextureCache, i+1); char* item = list->Strings[i]; + snprintf(buffer, sizeof(buffer), "%p: %s", item, item); + 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}); + TTF_SizeUTF8(font, buffer, &textSize.w, &textSize.h); + SDL_Surface* surface = TTF_RenderText_Shaded(font, buffer, (SDL_Color){255, 255, 255, 0}, (SDL_Color){0, 0, 0, 0}); SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surface); @@ -69,43 +77,52 @@ void ListFontSizeHasChanged(SDL_Renderer* renderer, TTF_Font* font, List* list) } } -void RenderStructHeader(SDL_Renderer* renderer, TTF_Font* font, unsigned short fontSize, SDL_FRect* cursor, TextureFontCache* cache) { - TextureDetails* details = GetTextureFromCache(cache, 0); - +void RenderStructHeader(SDL_Renderer* renderer, SDL_FRect* cursor, List* list) { + TextureDetails* details = GetTextureFromCache(list->TextureCache, 0); + SDL_FRect textPosition; + textPosition.x = list->BoundingBox.x + list->BoundingBox.w * .50 - details->TextSize.w * .50; + textPosition.y = details->TextSize.h * .50 + list->BoundingBox.y; + textPosition.w = details->TextSize.w; + textPosition.h = details->TextSize.h; + + SDL_FRect headerBounds = {list->BoundingBox.x, list->BoundingBox.y, list->BoundingBox.w, textPosition.h * 2}; + + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); + + SDL_RenderFillRectF(renderer, &headerBounds); + + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0); + + SDL_RenderDrawRectF(renderer, &headerBounds); + + SDL_RenderCopyF(renderer, details->Texture, NULL, &textPosition); + + cursor->y = headerBounds.h; } 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); + //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); + RenderStructHeader(renderer, &contentsBox, list); + + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 0); + + //SDL_RenderDrawRectF(renderer, &list->BoundingBox); for (int i = 0; i < list->Size; i++) { - SDL_RenderDrawRectF(renderer, &contentsBox); + //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; - textArea.y = contentsBox.h * ( i + 1 ); - textArea.w = details->TextSize.w; - textArea.h = details->TextSize.h; + //SDL_FRect textArea; + contentsBox.x = contentsBox.x + contentsBox.w * 0.50 - details->TextSize.w * 0.50; + contentsBox.w = details->TextSize.w; + contentsBox.h = details->TextSize.h; - SDL_RenderCopyF(renderer, details->Texture, NULL, &textArea); + SDL_RenderCopyF(renderer, details->Texture, NULL, &contentsBox); + + contentsBox.y += details->TextSize.h * 1.15; // text.h * ( i + 1 ); } - - SDL_FRect size; - - float y = list->BoundingBox.y + motion.y - details->TextSize.h; - - if (y < list->BoundingBox.y) y = 0; - else if (y > list->BoundingBox.h) y = list->BoundingBox.h - details->TextSize.h; - - size.x = contentsBox.x; - size.y = y; - size.h = details->TextSize.h; - size.w = details->TextSize.w; - - SDL_RenderCopyF(renderer, details->Texture, NULL, &size); } \ No newline at end of file diff --git a/src/main.c b/src/main.c index b433abd..64395f2 100644 --- a/src/main.c +++ b/src/main.c @@ -121,8 +121,14 @@ int main(int argc, char** argv){ SDL_FRect boundingBox = {SCREEN_WIDTH * 0.25, 0, SCREEN_WIDTH * 0.50, SCREEN_HEIGHT}; - List* list = CreateList(boundingBox); + List* list = CreateList((SDL_FRect) {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT}); AddStringToList("Hello World!", list); + AddStringToList("Hello World!1", list); + AddStringToList("Hello World!2", list); + AddStringToList("Hello World!3", list); + AddStringToList("Hello World! 4", list); + AddStringToList("Hello World! 4 more stings! all the entries", list); + AddStringToList("Hello World! 4 and a reallly long sting", list); ListFontSizeHasChanged(app.renderer, GetFont(app.DefaultFontSize, app.FontCache), list);