Loading...
Searching...
No Matches
text.c
Go to the documentation of this file.
#include "arc/graphics/text.h"
#include "text.h"
#include "arc/graphics/color.h"
#include "arc/math/point.h"
#include "arc/math/rectangle.h"
#include "arc/std/string.h"
#include <SDL2/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));
ARC_String_Copy(&(*text)->name, path);
(*text)->size = size;
(*text)->color = color;
(*text)->texture = NULL;
(*text)->bounds = (ARC_Rect){ 0, 0, 0, 0 };
//TODO: fix this
if(TTF_Init()==-1) {
printf("TTF_Init: %s\n", TTF_GetError());
exit(2);
}
}
void ARC_Text_Destroy(ARC_Text *font){
if(font->texture != NULL){
SDL_DestroyTexture(font->texture);
}
ARC_String_Destroy(font->name);
free(font);
}
void ARC_Text_SetString(ARC_Text *text, ARC_Renderer *renderer, ARC_String *string){
TTF_Font *ttfont = TTF_OpenFont(text->name->data, text->size);
SDL_Color textColor = (SDL_Color){ text->color.r, text->color.g, text->color.b, text->color.a };
SDL_Surface *surface = TTF_RenderText_Blended_Wrapped(ttfont, string->data, textColor, 0);
text->bounds.w = surface->w;
text->bounds.h = surface->h;
if(text->texture){
SDL_DestroyTexture(text->texture);
}
text->texture = SDL_CreateTextureFromSurface((SDL_Renderer *)renderer, surface);
SDL_FreeSurface(surface);
TTF_CloseFont(ttfont);
}
void ARC_Text_Render(ARC_Text *text, ARC_Renderer *renderer){
if(text->texture == NULL){
return;
}
SDL_Rect bounds = (SDL_Rect){ text->bounds.x, text->bounds.y, text->bounds.w, text->bounds.h };
SDL_RenderCopy((SDL_Renderer *)renderer, text->texture, NULL, &bounds);
}
void ARC_Text_SetPos(ARC_Text *text, ARC_Point pos){
text->bounds.x = pos.x;
text->bounds.y = pos.y;
}
color.h
ARC_Renderer
struct ARC_RendererType ARC_Renderer
Definition renderer.h:16
text.h
ARC_Text_SetString
void ARC_Text_SetString(ARC_Text *text, ARC_Renderer *renderer, ARC_String *string)
Definition text.c:34
ARC_Text_Create
void ARC_Text_Create(ARC_Text **text, ARC_String *path, int32_t size, ARC_Color color)
Definition text.c:11
ARC_Text_Destroy
void ARC_Text_Destroy(ARC_Text *font)
Definition text.c:26
ARC_Text_Render
void ARC_Text_Render(ARC_Text *text, ARC_Renderer *renderer)
Definition text.c:52
ARC_Text_SetPos
void ARC_Text_SetPos(ARC_Text *text, ARC_Point pos)
Definition text.c:61
rectangle.h
ARC_Rect
struct ARC_Rect ARC_Rect
text.h
point.h
string.h
ARC_String_Copy
void ARC_String_Copy(ARC_String **copy, ARC_String *original)
copy a ARC_String
Definition string.c:62
ARC_String_Destroy
void ARC_String_Destroy(ARC_String *string)
destroys ARC_String type
Definition string.c:52
ARC_Color
Definition color.h:10
ARC_Color::a
uint8_t a
Definition color.h:11
ARC_Color::r
uint8_t r
Definition color.h:11
ARC_Color::b
uint8_t b
Definition color.h:11
ARC_Color::g
uint8_t g
Definition color.h:11
ARC_Point
Definition point.h:10
ARC_Point::y
int32_t y
Definition point.h:12
ARC_Point::x
int32_t x
Definition point.h:11
ARC_Rect::x
int32_t x
Definition rectangle.h:13
ARC_Rect::w
int32_t w
Definition rectangle.h:15
ARC_Rect::y
int32_t y
Definition rectangle.h:14
ARC_Rect::h
int32_t h
Definition rectangle.h:16
ARC_String
substring position within a string
Definition string.h:14
ARC_String::data
char * data
Definition string.h:15
ARC_Text
Definition text.h:10
ARC_Text::name
ARC_String * name
Definition text.h:11
ARC_Text::texture
SDL_Texture * texture
Definition text.h:16
ARC_Text::bounds
ARC_Rect bounds
Definition text.h:17
ARC_Text::color
ARC_Color color
Definition text.h:14
ARC_Text::size
int32_t size
Definition text.h:12