-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.h
75 lines (72 loc) · 2.02 KB
/
text.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once
#include "constants.h"
#include "texture.h"
#include <string>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
using namespace std;
string defaultFont = "assets/OpenSans-ExtraBold.TTF";
struct textTexture
{
textTexture(int x,int y,int size,string str,string fontPath,SDL_Color color);
~textTexture();
void render(SDL_Renderer *renderer);
void initWH();
int x,y;
int size;
int w,h;
string str;
SDL_Color color;
TTF_Font* font;
string fontPath;
SDL_Texture* texture;
};
textTexture::textTexture(int x,int y,int size,string str,string fontPath = defaultFont,SDL_Color color = {255,255,255,255})
{
this->x=x;
this->y=y;
this->size=size;
this->str=str;
this->fontPath=fontPath;
this->color=color;
this->font=TTF_OpenFont(fontPath.c_str(),size);
this->texture=NULL;
initWH();
}
textTexture::~textTexture()
{
if(texture!=NULL)
SDL_DestroyTexture(texture);
if(font!=NULL)
TTF_CloseFont(font);
}
void textTexture::initWH()
{
TTF_SizeText(font,str.c_str(),&w,&h);
}
void textTexture::render(SDL_Renderer *renderer)
{
if(font==NULL)
{
cout<<"Font not found!"<<'\n';
return ;
}
//std::cout<<"Rendering text..."<<str<<'\n';
SDL_Surface* surface=TTF_RenderText_Solid(font,str.c_str(),color);
texture=SDL_CreateTextureFromSurface(renderer,surface);
SDL_Rect rect={x,y,surface->w,surface->h};
SDL_RenderCopy(renderer,texture,NULL,&rect);
SDL_FreeSurface(surface);
}
void drawText(SDL_Renderer *renderer, int x, int y, int size, string s, SDL_Color color = {255,255,255,255}, string font = defaultFont)// boxSize for allign
{
textTexture t(x,y,size,s,font,color);
t.render(renderer);
}
void drawTextCenter(SDL_Renderer *renderer, int x, int y, int size, string s, int boxWidth, int boxHeight, SDL_Color color = {255,255,255,255}, string font = defaultFont)// boxSize for allign
{
textTexture t(x,y,size,s,font,color);
t.x+=(boxWidth-t.w)/2;
t.y+=(boxHeight-t.h)/2;
t.render(renderer);
}