Added a basic font cache and got some basic text scaling on the go.
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
#ifndef FONTCACHE_H
|
||||||
|
#define FONTCACHE_H
|
||||||
|
|
||||||
|
#include <SDL2/SDL_ttf.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
unsigned int Capcity;
|
||||||
|
unsigned int Size;
|
||||||
|
char* FontFilePath;
|
||||||
|
TTF_Font** Fonts;
|
||||||
|
} FontCache;
|
||||||
|
|
||||||
|
FontCache* CreateFontCache(char* fontFilePath);
|
||||||
|
TTF_Font* GetFont(unsigned short fontSize, FontCache* cache);
|
||||||
|
void FreeFontCache(FontCache* cache);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
#include "../includes/font_cache.h"
|
||||||
|
#include <SDL2/SDL_ttf.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define DEFAULTCACHESIZE 32
|
||||||
|
|
||||||
|
FontCache* CreateFontCache(char* fontFilePath) {
|
||||||
|
FontCache* cache = calloc(1, sizeof(FontCache));
|
||||||
|
|
||||||
|
if (!cache) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
cache->Fonts = calloc(DEFAULTCACHESIZE, sizeof(TTF_Font*));
|
||||||
|
|
||||||
|
if (!cache->Fonts) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
cache->FontFilePath = fontFilePath;
|
||||||
|
cache->Capcity = DEFAULTCACHESIZE;
|
||||||
|
|
||||||
|
return cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
TTF_Font* GetFont(unsigned short fontSize, FontCache* cache) {
|
||||||
|
//TTF_OpenFont("/usr/share/fonts/TTF/Hack-Regular.ttf", 14);
|
||||||
|
|
||||||
|
TTF_Font* font = cache->Fonts[fontSize];
|
||||||
|
|
||||||
|
if (font) return font;
|
||||||
|
|
||||||
|
cache->Fonts[fontSize] = TTF_OpenFont(cache->FontFilePath, fontSize);
|
||||||
|
|
||||||
|
return cache->Fonts[fontSize];
|
||||||
|
}
|
||||||
|
|
||||||
|
void FreeFontCache(FontCache* cache) {
|
||||||
|
for (int i = 0; i < cache->Capcity; i++) {
|
||||||
|
if (cache->Fonts[i]) {
|
||||||
|
TTF_CloseFont(cache->Fonts[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+37
-7
@@ -17,6 +17,7 @@
|
|||||||
#include <SDL2/SDL_video.h>
|
#include <SDL2/SDL_video.h>
|
||||||
#include <SDL2/SDL_image.h>
|
#include <SDL2/SDL_image.h>
|
||||||
#include <SDL2/SDL_ttf.h>
|
#include <SDL2/SDL_ttf.h>
|
||||||
|
#include "../includes/font_cache.h"
|
||||||
|
|
||||||
const int SCREEN_WIDTH = 1280;
|
const int SCREEN_WIDTH = 1280;
|
||||||
const int SCREEN_HEIGHT = 720;
|
const int SCREEN_HEIGHT = 720;
|
||||||
@@ -26,7 +27,8 @@ float ZoomLevel = 1;
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
SDL_Window *window;
|
SDL_Window *window;
|
||||||
SDL_Renderer *renderer;
|
SDL_Renderer *renderer;
|
||||||
TTF_Font* font;
|
FontCache* FontCache;
|
||||||
|
unsigned short DefaultFontSize;
|
||||||
} App;
|
} App;
|
||||||
|
|
||||||
App app;
|
App app;
|
||||||
@@ -39,13 +41,14 @@ int XOrg, YOrg;
|
|||||||
void Cleanup(void) {
|
void Cleanup(void) {
|
||||||
SDL_DestroyWindow(app.window);
|
SDL_DestroyWindow(app.window);
|
||||||
SDL_DestroyRenderer(app.renderer);
|
SDL_DestroyRenderer(app.renderer);
|
||||||
TTF_CloseFont(app.font);
|
FreeFontCache(app.FontCache);
|
||||||
TTF_Quit();
|
TTF_Quit();
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv){
|
int main(int argc, char** argv){
|
||||||
memset(&app, 0, sizeof(app));
|
memset(&app, 0, sizeof(app));
|
||||||
|
app.DefaultFontSize = 14;
|
||||||
XRel = 0;
|
XRel = 0;
|
||||||
YRel = 0;
|
YRel = 0;
|
||||||
XOrg = 0;
|
XOrg = 0;
|
||||||
@@ -63,12 +66,14 @@ int main(int argc, char** argv){
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
app.font = TTF_OpenFont("/usr/share/fonts/TTF/Hack-Regular.ttf", 14);
|
app.FontCache = CreateFontCache("/usr/share/fonts/TTF/Hack-Regular.ttf");
|
||||||
|
|
||||||
if (app.font == NULL) {
|
// app.font = TTF_OpenFont("/usr/share/fonts/TTF/Hack-Regular.ttf", 14);
|
||||||
printf("Failed to load font\n");
|
|
||||||
return -1;
|
// if (app.font == NULL) {
|
||||||
}
|
// printf("Failed to load font\n");
|
||||||
|
// return -1;
|
||||||
|
// }
|
||||||
|
|
||||||
app.window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
app.window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
||||||
|
|
||||||
@@ -101,6 +106,17 @@ int main(int argc, char** argv){
|
|||||||
rect2.y = 250;// / SCREEN_HEIGHT;
|
rect2.y = 250;// / SCREEN_HEIGHT;
|
||||||
rect2.w = SCREEN_WIDTH / 4;
|
rect2.w = SCREEN_WIDTH / 4;
|
||||||
rect2.h = SCREEN_HEIGHT / 4;
|
rect2.h = SCREEN_HEIGHT / 4;
|
||||||
|
float previousZoom = ZoomLevel;
|
||||||
|
SDL_Texture* texture;
|
||||||
|
SDL_Rect textSize;
|
||||||
|
|
||||||
|
TTF_SizeUTF8(GetFont(app.DefaultFontSize * ZoomLevel, app.FontCache), "Hello, world!", &textSize.w, &textSize.h);
|
||||||
|
|
||||||
|
SDL_Surface* surface = TTF_RenderText_Blended(GetFont(app.DefaultFontSize * ZoomLevel, app.FontCache), "Hello, world!", (SDL_Color){0,0,0,0});
|
||||||
|
|
||||||
|
texture = SDL_CreateTextureFromSurface(app.renderer, surface);
|
||||||
|
|
||||||
|
SDL_FreeSurface(surface);
|
||||||
|
|
||||||
// Uint64 start = 0;
|
// Uint64 start = 0;
|
||||||
// Uint64 end = 0;
|
// Uint64 end = 0;
|
||||||
@@ -131,6 +147,20 @@ int main(int argc, char** argv){
|
|||||||
|
|
||||||
SDL_RenderFillRectF(app.renderer, &(SDL_FRect){rect2.x + XRel, rect2.y + YRel, rect2.w * ZoomLevel, rect2.h * ZoomLevel});
|
SDL_RenderFillRectF(app.renderer, &(SDL_FRect){rect2.x + XRel, rect2.y + YRel, rect2.w * ZoomLevel, rect2.h * ZoomLevel});
|
||||||
|
|
||||||
|
if (previousZoom != ZoomLevel) {
|
||||||
|
SDL_DestroyTexture(texture);
|
||||||
|
|
||||||
|
TTF_SizeUTF8(GetFont(app.DefaultFontSize * ZoomLevel, app.FontCache), "Hello, world!", &textSize.w, &textSize.h);
|
||||||
|
|
||||||
|
SDL_Surface* surface = TTF_RenderText_Blended(GetFont(app.DefaultFontSize * ZoomLevel, app.FontCache), "Hello, world!", (SDL_Color){0,0,0,0});
|
||||||
|
|
||||||
|
texture = SDL_CreateTextureFromSurface(app.renderer, surface);
|
||||||
|
|
||||||
|
SDL_FreeSurface(surface);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_RenderCopyF(app.renderer, texture, NULL, &(SDL_FRect){rect2.x + XRel, rect2.y + YRel, textSize.w * ZoomLevel, textSize.h * ZoomLevel});
|
||||||
|
|
||||||
SDL_RenderPresent(app.renderer);
|
SDL_RenderPresent(app.renderer);
|
||||||
|
|
||||||
// end = SDL_GetPerformanceCounter();
|
// end = SDL_GetPerformanceCounter();
|
||||||
|
|||||||
Reference in New Issue
Block a user