Merge branch 'master' of ssh.marcbarbier.fr:Marc/gamejam-polytech

This commit is contained in:
Marc
2023-01-20 22:01:15 +01:00
3 changed files with 84 additions and 1 deletions
+1 -1
View File
@@ -27,6 +27,6 @@ set(SRC
# add the executable # add the executable
add_executable(FLOPPE_FION ${SRC}) add_executable(FLOPPE_FION ${SRC})
target_link_libraries(FLOPPE_FION SDL2 SDL_image pthread m) target_link_libraries(FLOPPE_FION SDL2 SDL_image SDL_ttf pthread m)
set_property(TARGET FLOPPE_FION PROPERTY C_STANDARD 23) set_property(TARGET FLOPPE_FION PROPERTY C_STANDARD 23)
+65
View File
@@ -0,0 +1,65 @@
#include "text.h"
#include <SDL2/SDL_log.h>
#define FONT_CACHE_SIZE 100
#define MAX_TEXT_SIZE 100
typedef struct FontCacheObject {
char* file;
int size;
TTF_Font* font;
} FontCacheObject_t;
FontCacheObject_t fontCache[FONT_CACHE_SIZE];
unsigned fontCacheSize = 0;
void text_freeFontCache(void){
for(unsigned i = 0; i < fontCacheSize; ++i){
free(fontCache[i].file);
TTF_CloseFont(fontCache[i].font);
}
}
int text_renderText(RenderObject_t* renderObject, Position position, SDL_Color color, const char* fontFilename, int fontSize, char* format, ...){
TTF_Font* font = NULL;
for(unsigned i = 0; i < fontCacheSize; ++i){
if(strcmp(fontCache[i].file, fontFilename) && fontCache[i].size == fontSize){
font = fontCache[i].font;
break;
}
}
if(font == NULL) {
font = TTF_OpenFont(fontFilename, fontSize);
if(font == NULL) {
SDL_LogError(SDL_LOG_PRIORITY_ERROR, "TTF_OpenFont");
return -1;
}
FontCacheObject_t fontCacheObject;
fontCacheObject.file = (char*)malloc(strlen(fontFilename) + 1);
strcpy(fontCacheObject.file, fontFilename);
fontCacheObject.size = fontSize;
fontCacheObject.font = font;
fontCache[fontCacheSize] = fontCacheObject;
++fontCacheSize;
}
char text[MAX_TEXT_SIZE];
va_list args;
memset(&text, '\0', sizeof(text));
va_start(args, format);
vsprintf(text, format, args);
va_end(args);
SDL_Surface* surface = TTF_RenderText_Blended(font, text, color);
renderObject->x = position.x;
renderObject->y = position.y;
renderObject->z = position.z;
renderObject->surface = surface;
return 0;
}
+18
View File
@@ -0,0 +1,18 @@
#ifndef __TEXT_H__
#define __TEXT_H__
#include <SDL/SDL_ttf.h>
#include "renderer.h"
typedef struct {
int x;
int y;
int z;
} Position;
int text_renderText(RenderObject_t* renderObject, Position position, SDL_Color color, const char* fontFilename, int fontSize, char* format, ...);
void text_freeFontCache(void);
#endif