Added a texture cache for fonts. Created a render function for the List, or at least got one started.
This commit is contained in:
+8
-2
@@ -5,13 +5,19 @@
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include "../includes/texture_font_cache.h"
|
||||
|
||||
typedef struct {
|
||||
unsigned int Capcity;
|
||||
unsigned int Capacity;
|
||||
unsigned int Size;
|
||||
unsigned int MaxStringLength;
|
||||
char** Strings;
|
||||
SDL_FRect BoundingBox;
|
||||
TextureFontCache* TextureCache;
|
||||
} List;
|
||||
|
||||
List* CreateList(void);
|
||||
List* CreateList(SDL_FRect boundingBox);
|
||||
void AddStringToList(char* text, List* list);
|
||||
void RenderList(SDL_Renderer* renderer, SDL_FPoint motion, TTF_Font* font, unsigned short fontSize, List* list);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef TEXTURE_FONT_CACHE_h
|
||||
#define TEXTURE_FONT_CACHE_h
|
||||
|
||||
#include <SDL2/SDL_rect.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
|
||||
typedef struct {
|
||||
unsigned short FontSize;
|
||||
SDL_Rect TextSize;
|
||||
SDL_Texture* Texture;
|
||||
} TextureDetails;
|
||||
|
||||
typedef struct {
|
||||
unsigned int Capacity;
|
||||
unsigned int Size;
|
||||
TextureDetails** Textures;
|
||||
} TextureFontCache;
|
||||
|
||||
TextureFontCache* CreateTextureFontCache();
|
||||
TextureDetails* AddTextureToCache(SDL_Texture* texture, unsigned short fontSize, SDL_Rect textSize, TextureFontCache* cache);
|
||||
TextureDetails* GetTextureFromCache(TextureFontCache* cache, unsigned int index);
|
||||
void FreeTextureDetails(TextureDetails* details);
|
||||
|
||||
#endif
|
||||
+50
-2
@@ -1,16 +1,64 @@
|
||||
#include "../includes/list.h"
|
||||
#include <SDL2/SDL_rect.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <SDL2/SDL_surface.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define DEFAULTLISTSIZE 64
|
||||
#define MINWIDTH 100
|
||||
#define MINHEIGHT 50
|
||||
|
||||
List* CreateList(void) {
|
||||
List* CreateList(SDL_FRect boundingBox) {
|
||||
List* list = calloc(1, sizeof(List));
|
||||
|
||||
list->Strings = calloc(DEFAULTLISTSIZE, sizeof(char*));
|
||||
list->TextureCache = CreateTextureFontCache();
|
||||
|
||||
list->Capcity = DEFAULTLISTSIZE;
|
||||
list->Capacity = DEFAULTLISTSIZE;
|
||||
list->Size = 0;
|
||||
list->BoundingBox = boundingBox;
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
void AddStringToList(char* text, List* list) {
|
||||
if (list->Size + 1 > list->Capacity) {
|
||||
char** newContent = realloc(list->Strings, list->Capacity * 2);
|
||||
|
||||
list->Strings = newContent;
|
||||
list->Capacity *= 2;
|
||||
}
|
||||
|
||||
if (list->MaxStringLength < strlen(text)) list->MaxStringLength = strlen(text);
|
||||
|
||||
list->Strings[list->Size] = text;
|
||||
list->Size++;
|
||||
}
|
||||
|
||||
void RenderList(SDL_Renderer* renderer, SDL_FPoint 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);
|
||||
TextureDetails* details = GetTextureFromCache(list->TextureCache, 0);
|
||||
|
||||
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);
|
||||
|
||||
details = AddTextureToCache(tex, fontSize, textSize, list->TextureCache);
|
||||
}
|
||||
|
||||
SDL_FRect size;
|
||||
size.x = list->BoundingBox.x;
|
||||
size.y = list->BoundingBox.y;
|
||||
size.h = details->TextSize.h;
|
||||
size.w = details->TextSize.w;
|
||||
|
||||
SDL_RenderCopyF(renderer, details->Texture, NULL, &size);
|
||||
}
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include "../includes/font_cache.h"
|
||||
#include "../includes/list.h"
|
||||
|
||||
const int SCREEN_WIDTH = 1280;
|
||||
const int SCREEN_HEIGHT = 720;
|
||||
@@ -118,6 +119,10 @@ int main(int argc, char** argv){
|
||||
|
||||
SDL_FreeSurface(surface);
|
||||
|
||||
SDL_FRect boundingBox = {650, 700, 300, 90};
|
||||
|
||||
List* list = CreateList(boundingBox);
|
||||
|
||||
// Uint64 start = 0;
|
||||
// Uint64 end = 0;
|
||||
// float frameCount = 0;
|
||||
@@ -161,6 +166,8 @@ int main(int argc, char** argv){
|
||||
|
||||
SDL_RenderCopyF(app.renderer, texture, NULL, &(SDL_FRect){rect2.x + XRel, rect2.y + YRel, textSize.w * ZoomLevel, textSize.h * ZoomLevel});
|
||||
|
||||
RenderList(app.renderer, (SDL_FPoint) {0,0}, GetFont(app.DefaultFontSize * ZoomLevel, app.FontCache), app.DefaultFontSize * ZoomLevel, list);
|
||||
|
||||
SDL_RenderPresent(app.renderer);
|
||||
|
||||
// end = SDL_GetPerformanceCounter();
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
#include "../includes/texture_font_cache.h"
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
TextureDetails* CreateTextureDetails(SDL_Texture* texture, unsigned short fontSize, SDL_Rect textSize) {
|
||||
TextureDetails* details = calloc(1, sizeof(TextureDetails));
|
||||
|
||||
details->FontSize = fontSize;
|
||||
details->Texture = texture;
|
||||
details->TextSize = textSize;
|
||||
|
||||
return details;
|
||||
}
|
||||
|
||||
TextureFontCache* CreateTextureFontCache() {
|
||||
TextureFontCache* cache = calloc(1, sizeof(TextureFontCache));
|
||||
|
||||
cache->Textures = calloc(64, sizeof(TextureDetails*));
|
||||
cache->Capacity = 64;
|
||||
|
||||
return cache;
|
||||
}
|
||||
|
||||
TextureDetails* GetTextureFromCache(TextureFontCache* cache, unsigned int index) {
|
||||
if (!cache || index >= cache->Size) return NULL;
|
||||
|
||||
TextureDetails* details = cache->Textures[index];
|
||||
|
||||
if (!details) return NULL;
|
||||
|
||||
return details;
|
||||
}
|
||||
|
||||
TextureDetails* AddTextureToCache(SDL_Texture* texture, unsigned short fontSize, SDL_Rect textSize, TextureFontCache* cache) {
|
||||
if (cache->Size + 1 > cache->Capacity) {
|
||||
TextureDetails** newContent = realloc(cache->Textures, cache->Capacity * 2);
|
||||
|
||||
cache->Textures = newContent;
|
||||
cache->Capacity *= 2;
|
||||
}
|
||||
|
||||
cache->Textures[cache->Size] = CreateTextureDetails(texture, fontSize, textSize);
|
||||
cache->Size++;
|
||||
|
||||
return cache->Textures[cache->Size - 1];
|
||||
}
|
||||
|
||||
void FreeTextureDetails(TextureDetails* details) {
|
||||
if (!details) return;
|
||||
|
||||
SDL_DestroyTexture(details->Texture);
|
||||
|
||||
free(details);
|
||||
}
|
||||
Reference in New Issue
Block a user