A simple visualization of a flat list is done.
This commit is contained in:
+47
-30
@@ -4,6 +4,7 @@
|
|||||||
#include <SDL2/SDL_surface.h>
|
#include <SDL2/SDL_surface.h>
|
||||||
#include <SDL2/SDL_ttf.h>
|
#include <SDL2/SDL_ttf.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#define DEFAULTLISTSIZE 64
|
#define DEFAULTLISTSIZE 64
|
||||||
#define MINWIDTH 100
|
#define MINWIDTH 100
|
||||||
@@ -38,11 +39,14 @@ void AddStringToList(char* text, List* list) {
|
|||||||
|
|
||||||
void ListFontSizeHasChanged(SDL_Renderer* renderer, TTF_Font* font, List* list) {
|
void ListFontSizeHasChanged(SDL_Renderer* renderer, TTF_Font* font, List* list) {
|
||||||
TextureDetails* details = GetTextureFromCache(list->TextureCache, 0);
|
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;
|
SDL_Rect textSize;
|
||||||
|
|
||||||
TTF_SizeUTF8(font, "List (Array)", &textSize.w, &textSize.h);
|
TTF_SizeUTF8(font, buffer, &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_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);
|
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);
|
else details = AddTextureToCache(tex, textSize, list->TextureCache);
|
||||||
|
|
||||||
for (int i = 0; i < list->Size; i++) {
|
for (int i = 0; i < list->Size; i++) {
|
||||||
|
memset(buffer, '\0', 100);
|
||||||
|
|
||||||
TextureDetails* details = GetTextureFromCache(list->TextureCache, i+1);
|
TextureDetails* details = GetTextureFromCache(list->TextureCache, i+1);
|
||||||
char* item = list->Strings[i];
|
char* item = list->Strings[i];
|
||||||
|
|
||||||
|
snprintf(buffer, sizeof(buffer), "%p: %s", item, item);
|
||||||
|
|
||||||
SDL_Rect textSize;
|
SDL_Rect textSize;
|
||||||
|
|
||||||
TTF_SizeUTF8(font, item, &textSize.w, &textSize.h);
|
TTF_SizeUTF8(font, buffer, &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_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);
|
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) {
|
void RenderStructHeader(SDL_Renderer* renderer, SDL_FRect* cursor, List* list) {
|
||||||
TextureDetails* details = GetTextureFromCache(cache, 0);
|
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) {
|
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);
|
RenderStructHeader(renderer, &contentsBox, list);
|
||||||
|
|
||||||
|
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 0);
|
||||||
|
|
||||||
|
//SDL_RenderDrawRectF(renderer, &list->BoundingBox);
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
SDL_FRect textArea;
|
//SDL_FRect textArea;
|
||||||
textArea.x = contentsBox.x + contentsBox.w * 0.50 - details->TextSize.w * 0.50;
|
contentsBox.x = contentsBox.x + contentsBox.w * 0.50 - details->TextSize.w * 0.50;
|
||||||
textArea.y = contentsBox.h * ( i + 1 );
|
contentsBox.w = details->TextSize.w;
|
||||||
textArea.w = details->TextSize.w;
|
contentsBox.h = details->TextSize.h;
|
||||||
textArea.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);
|
|
||||||
}
|
}
|
||||||
+7
-1
@@ -121,8 +121,14 @@ int main(int argc, char** argv){
|
|||||||
|
|
||||||
SDL_FRect boundingBox = {SCREEN_WIDTH * 0.25, 0, SCREEN_WIDTH * 0.50, SCREEN_HEIGHT};
|
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!", 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);
|
ListFontSizeHasChanged(app.renderer, GetFont(app.DefaultFontSize, app.FontCache), list);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user