-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTTFTexture.cpp
85 lines (67 loc) · 1.9 KB
/
TTFTexture.cpp
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
#include "TTFTexture.h"
#include "TSettings.h"
extern TSettings mGlobalSettings;
int TTFTexture::loadTTFFromUTF8(std::string cTexText, SDL_Color textColor, TTF_Font *cFont){
if(TTFTex){
SDL_DestroyTexture(TTFTex);
TTFTex = NULL;
}
if(!cTexText.size()){
return 1;
}
mTextColor = textColor;
mTexText = cTexText;
SDL_Surface* textSurf = TTF_RenderUTF8_Blended(cFont, cTexText.c_str(), textColor);
if( textSurf != NULL ){
TTFTex = SDL_CreateTextureFromSurface(mGlobalSettings.TRenderer, textSurf);
if(TTFTex == NULL){
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return 1;
} else {
mTexWidth = textSurf->w;
mTexHeight = textSurf->h;
}
SDL_FreeSurface(textSurf);
}
else{
std::cout << "SDL_ttf Error: " << TTF_GetError() << std::endl;
return 1;
}
return 0;
}
int TTFTexture::loadTTFFromUTF8(std::string cTexText, SDL_Color textColor){
return loadTTFFromUTF8(cTexText, textColor, mGlobalSettings.mOverlayText.TFont);
}
int TTFTexture::loadTTFFromString(std::string cTexText, SDL_Color textColor)
{
if(TTFTex){
SDL_DestroyTexture(TTFTex);
TTFTex = NULL;
}
if(!cTexText.size()){
return 1;
}
mTexText = cTexText;
SDL_Surface* textSurf = TTF_RenderText_Blended(mGlobalSettings.mOverlayText.TFont, cTexText.c_str(), textColor);
if( textSurf != NULL ){
TTFTex = SDL_CreateTextureFromSurface(mGlobalSettings.TRenderer, textSurf);
if(TTFTex == NULL){
std::cout << "SDL Error: " << SDL_GetError() << std::endl;
return 1;
} else {
mTexWidth = textSurf->w;
mTexHeight = textSurf->h;
}
SDL_FreeSurface(textSurf);
}
else{
std::cout << "SDL_ttf Error: " << TTF_GetError() << std::endl;
return 1;
}
return 0;
}
SDL_Rect TTFTexture::render(int xpos, int ypos){
SDL_Rect renderQuad = {xpos, ypos, mTexWidth, mTexHeight};
if(TTFTex) SDL_RenderCopy( mGlobalSettings.TRenderer, TTFTex, NULL, &renderQuad);
return renderQuad;
}