2025-03-22 23:25:21 -06:00
|
|
|
#include "arc/graphics/text.h"
|
|
|
|
|
|
2025-04-04 03:45:53 -06:00
|
|
|
#include "renderer.h"
|
2025-03-22 23:25:21 -06:00
|
|
|
#include "arc/graphics/color.h"
|
|
|
|
|
#include "arc/math/point.h"
|
|
|
|
|
#include "arc/math/rectangle.h"
|
|
|
|
|
#include "arc/std/string.h"
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
#include <SDL3_ttf/SDL_ttf.h>
|
|
|
|
|
|
|
|
|
|
void ARC_Text_Create(ARC_Text **text, ARC_String *path, int32_t size, ARC_Color color){
|
|
|
|
|
*text = (ARC_Text *)malloc(sizeof(ARC_Text));
|
2025-04-11 04:34:00 -06:00
|
|
|
|
2025-03-22 23:25:21 -06:00
|
|
|
ARC_String_Copy(&(*text)->name, path);
|
2025-04-11 04:34:00 -06:00
|
|
|
|
|
|
|
|
(*text)->fontSize = size;
|
|
|
|
|
(*text)->bounds = (ARC_FRect){ 0.0f, 0.0f, 0.0f, 0.0f };
|
|
|
|
|
(*text)->color = color;
|
|
|
|
|
(*text)->backendData = NULL;
|
2025-03-22 23:25:21 -06:00
|
|
|
|
|
|
|
|
//TODO: fix this
|
|
|
|
|
if(TTF_Init() == false) {
|
|
|
|
|
printf("TTF_Init: %s\n", SDL_GetError());
|
|
|
|
|
exit(2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ARC_Text_Destroy(ARC_Text *font){
|
2025-04-11 04:34:00 -06:00
|
|
|
if(font->backendData != NULL){
|
|
|
|
|
SDL_DestroyTexture((SDL_Texture *)font->backendData);
|
2025-03-22 23:25:21 -06:00
|
|
|
}
|
2025-04-11 04:34:00 -06:00
|
|
|
|
2025-03-22 23:25:21 -06:00
|
|
|
ARC_String_Destroy(font->name);
|
|
|
|
|
free(font);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ARC_Text_SetString(ARC_Text *text, ARC_Renderer *renderer, ARC_String *string){
|
2025-04-11 04:34:00 -06:00
|
|
|
TTF_Font *ttfont = TTF_OpenFont(text->name->data, text->fontSize);
|
2025-03-22 23:25:21 -06:00
|
|
|
SDL_Color textColor = (SDL_Color){ text->color.r, text->color.g, text->color.b, text->color.a };
|
|
|
|
|
|
|
|
|
|
SDL_Surface *surface = TTF_RenderText_Blended(ttfont, string->data, 0, textColor);
|
|
|
|
|
|
|
|
|
|
text->bounds.w = surface->w;
|
|
|
|
|
text->bounds.h = surface->h;
|
|
|
|
|
|
2025-04-11 04:34:00 -06:00
|
|
|
if(text->backendData != NULL){
|
|
|
|
|
SDL_DestroyTexture((SDL_Texture *)text->backendData);
|
2025-03-22 23:25:21 -06:00
|
|
|
}
|
2025-04-11 04:34:00 -06:00
|
|
|
text->backendData = (void *)SDL_CreateTextureFromSurface(renderer->renderer, surface);
|
2025-03-22 23:25:21 -06:00
|
|
|
|
|
|
|
|
SDL_DestroySurface(surface);
|
|
|
|
|
TTF_CloseFont(ttfont);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ARC_Text_Render(ARC_Text *text, ARC_Renderer *renderer){
|
2025-04-11 04:34:00 -06:00
|
|
|
if(text->backendData == NULL){
|
2025-03-22 23:25:21 -06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_FRect bounds = (SDL_FRect){ text->bounds.x, text->bounds.y, text->bounds.w, text->bounds.h };
|
2025-04-11 04:34:00 -06:00
|
|
|
SDL_RenderTexture(renderer->renderer, (SDL_Texture *)(text->backendData), NULL, &bounds);
|
2025-03-22 23:25:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ARC_Text_SetPos(ARC_Text *text, ARC_Point pos){
|
|
|
|
|
text->bounds.x = pos.x;
|
|
|
|
|
text->bounds.y = pos.y;
|
|
|
|
|
}
|