added text to graphics, basic functionality in place

This commit is contained in:
herbglitch 2023-06-10 18:33:09 -06:00
parent 4c172b6734
commit 7018500555
3 changed files with 110 additions and 0 deletions

View file

@ -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 <SDL.h>
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_

View file

@ -0,0 +1,32 @@
#ifndef ARC_STD_TEXT_H_
#define ARC_STD_TEXT_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#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_

55
src/graphics/sdl/text.c Normal file
View file

@ -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 <SDL2/SDL_ttf.h>
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;
}