From 7018500555f6524091cde78136a80c5f865f4909 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Sat, 10 Jun 2023 18:33:09 -0600 Subject: [PATCH] added text to graphics, basic functionality in place --- include/arc/graphics/sdl/text.h | 23 ++++++++++++++ include/arc/graphics/text.h | 32 +++++++++++++++++++ src/graphics/sdl/text.c | 55 +++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 include/arc/graphics/sdl/text.h create mode 100644 include/arc/graphics/text.h create mode 100644 src/graphics/sdl/text.c diff --git a/include/arc/graphics/sdl/text.h b/include/arc/graphics/sdl/text.h new file mode 100644 index 0000000..6137f5e --- /dev/null +++ b/include/arc/graphics/sdl/text.h @@ -0,0 +1,23 @@ +#ifndef ARC_SDL_TEXT_H_ +#define ARC_SDL_TEXT_H_ + +#include "arc/std/string.h" +#include "arc/graphics/color.h" +#include "arc/math/rectangle.h" + +#ifdef ARC_SDL +#include + +typedef struct ARC_Text { + ARC_String *name; + int32_t size; + + ARC_Color color; + + SDL_Texture *texture; + ARC_Rect bounds; +} ARC_Text; + +#endif // !ARC_SDL + +#endif // !ARC_SDL_TEXT_H_ diff --git a/include/arc/graphics/text.h b/include/arc/graphics/text.h new file mode 100644 index 0000000..a73ab11 --- /dev/null +++ b/include/arc/graphics/text.h @@ -0,0 +1,32 @@ +#ifndef ARC_STD_TEXT_H_ +#define ARC_STD_TEXT_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include "arc/graphics/color.h" +#include "arc/graphics/renderer.h" +#include "arc/math/point.h" +#include "arc/std/string.h" + +void ARC_TTF_Init(); + +typedef struct ARC_Text ARC_Text; + +void ARC_Text_Create(ARC_Text **text, ARC_String *path, int32_t size, ARC_Color color); + +void ARC_Text_Destroy(ARC_Text *font); + +void ARC_Text_SetString(ARC_Text *text, ARC_Renderer *renderer, ARC_String *string); + +void ARC_Text_Render(ARC_Text *text, ARC_Renderer *renderer); + +void ARC_Text_SetPos(ARC_Text *text, ARC_Point pos); + +#ifdef __cplusplus +} +#endif + +#endif //ARC_STD_STRING_H_ \ No newline at end of file diff --git a/src/graphics/sdl/text.c b/src/graphics/sdl/text.c new file mode 100644 index 0000000..1868b8a --- /dev/null +++ b/src/graphics/sdl/text.c @@ -0,0 +1,55 @@ +#include "arc/graphics/text.h" +#include "arc/graphics/sdl/text.h" +#include "arc/graphics/color.h" +#include "arc/math/point.h" +#include "arc/math/rectangle.h" +#include "arc/std/string.h" + +#include + +void ARC_TTF_Init(){ + TTF_Init(); +} + +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 }; +} + +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(ttfont, string->data, textColor); + + TTF_SizeText(ttfont, string->data, &(text->bounds.w), &(text->bounds.h)); + 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; +} \ No newline at end of file