Repository navigation
Expand file tree
/
Copy pathSDL_DisplayText.cpp
More file actions
90 lines (74 loc) · 1.83 KB
/
Copy pathSDL_DisplayText.cpp
File metadata and controls
90 lines (74 loc) · 1.83 KB
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
76
77
78
79
80
81
82
83
84
//The software was written by me, with references ideas from :
//Reference source 1 : LazyFoo's Tutorial (initSDL, Game-Loop, Texture load, Player with SpriteSheet load, Musics, Text)
//Reference source 2 : Phattrienphanmem123az's Tutorial (Map algorithm, Collision Detection)
//Reference source 3 : Tran Thi Minh Chau's Tutorial (Way to organize, sort .h and .cpp files)
//05 - 2020
#include "stdafx.h"
#include "SDL_DisplayText.h"
#include "SDL_BaseObj.h"
TextShow::TextShow()
{
textColor.r = 255;
textColor.g = 255;
textColor.b = 255;
textTexture = NULL;
}
TextShow::~TextShow()
{
Free();
}
bool TextShow::LoadText(TTF_Font* font, SDL_Renderer* screen)
{
Free();
SDL_Surface* textSurface = TTF_RenderText_Solid(font, strVal.c_str(), textColor);
if (textSurface)
{
textTexture = SDL_CreateTextureFromSurface(screen, textSurface);
tWidth = textSurface->w;
tHeight = textSurface->h;
}
return textTexture != NULL;
}
void TextShow::Free()
{
if(textTexture != NULL)
{
SDL_DestroyTexture(textTexture);
textTexture = NULL;
}
}
void TextShow::SetColor(Uint8 red, Uint8 green, Uint8 blue)
{
textColor.r = red;
textColor.g = green;
textColor.b = blue;
}
void TextShow::SetColor(int type)
{
if (type == RED_TEXT)
{
SDL_Color color = { 255, 0, 0 };
textColor = color;
}
else if (type == WHITE_TEXT)
{
SDL_Color color = { 255, 255, 255 };
textColor = color;
}
else if (type == YELLOW_TEXT)
{
SDL_Color color = { 255, 255, 0 };
textColor = color;
}
}
void TextShow::RenderText(SDL_Renderer* screen, int xTPos, int yTPos, SDL_Rect* clip, double angle,
SDL_Point* center, SDL_RendererFlip flip)
{
SDL_Rect renderQuad = { xTPos, yTPos, tWidth, tHeight };
if (clip != NULL)
{
renderQuad.w = clip->w;
renderQuad.h = clip->h;
}
SDL_RenderCopyEx(screen, textTexture, clip, &renderQuad, angle, center, flip);
}