Refactor texture code + start splitting networking into threads.

This commit is contained in:
2024-11-08 22:46:33 +08:00
parent 759a99edcf
commit 1d30dba681
3 changed files with 121 additions and 48 deletions
+36 -4
View File
@@ -1,11 +1,11 @@
#include "texture.h"
#include "log.h"
int text_texture_init(text_texture* text_texture, SDL_Renderer* renderer)
int text_texture_init(text_texture* text_texture, SDL_Renderer* renderer, int font_size)
{
text_texture->texture = NULL;
text_texture->renderer = renderer;
text_texture->font = TTF_OpenFont("FiraCode-Regular.ttf", 24);
text_texture->font = TTF_OpenFont("FiraCode-Regular.ttf", font_size);
if (!text_texture->font) {
log_message(LOG_ERROR, "Failed to load font! TTF_Error: %s", TTF_GetError());
return 0;
@@ -23,6 +23,7 @@ int text_texture_load(text_texture* text_texture, char* string)
}
SDL_Color text_color = { 255, 255, 255 };
if (strlen(string) == 0) string = " ";
SDL_Surface* text_surface = TTF_RenderText_Solid(text_texture->font, string, text_color);
if (!text_surface) {
log_message(LOG_ERROR, "Unable to render text surface! TTF_Error: %s", TTF_GetError());
@@ -35,7 +36,7 @@ int text_texture_load(text_texture* text_texture, char* string)
return 0;
}
text_texture->texture = SDL_CreateTextureFromSurface(text_texture->renderer, text_surface);
text_texture->texture = texture;
text_texture->width = text_surface->w;
text_texture->height = text_surface->h;
@@ -62,11 +63,42 @@ int text_texture_render(text_texture* text_texture, int x, int y)
return 1;
}
int text_texture_free(text_texture* text_texture)
int text_texture_destroy(text_texture* text_texture)
{
if (!text_texture->texture) {
return 0;
}
SDL_DestroyTexture(text_texture->texture);
TTF_CloseFont(text_texture->font);
return 1;
}
int text_texture_frame_init(text_texture_frame* text_texture_frame, int len, SDL_Renderer* renderer, int font_size)
{
text_texture_frame->textures = malloc(len*sizeof(text_texture));
for (int i = 0; i < len; i++) {
text_texture_init(&text_texture_frame->textures[i], renderer, font_size);
}
text_texture_frame->len = len;
return 1;
}
int text_texture_frame_render(text_texture_frame* text_texture_frame, int x, int y)
{
int text_y_offset_step = TTF_FontHeight(text_texture_frame->textures[0].font);
int text_y_offset = y;
for (int i = 0; i < text_texture_frame->len; i++) {
text_texture_render(&text_texture_frame->textures[i], x, text_y_offset);
text_y_offset += text_y_offset_step;
}
return 1;
}
int text_texture_frame_destroy(text_texture_frame* text_texture_frame)
{
for (int i = 0; i < text_texture_frame->len; i++) {
text_texture_destroy(&text_texture_frame->textures[i]);
}
free(text_texture_frame->textures);
return 1;
}