-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCFont.cpp
More file actions
165 lines (142 loc) · 3.71 KB
/
Copy pathCFont.cpp
File metadata and controls
165 lines (142 loc) · 3.71 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Copyright 2016, Michael R. Hoopmann, Institute for Systems Biology
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "CFont.h"
CFont::CFont(){
fontSize=16;
font=NULL;
display = NULL;
int i, j, k;
for(i=0; i<21; i++){
for(j=0; j<20; j++){
for(k=0; k<128; k++){
texture[i][j][k]=NULL;
}
}
}
TTF_Init();
}
CFont::~CFont(){
display = NULL;
int i, j, k;
for(i=0; i<21; i++){
for(j=0; j<20; j++){
for(k=0; k<128; k++){
if(texture[i][j][k]!=NULL){
SDL_DestroyTexture(texture[i][j][k]);
texture[i][j][k]=NULL;
}
}
}
}
if(font!=NULL){
TTF_CloseFont(font);
font=NULL;
}
TTF_Quit();
}
int CFont::getFontHeight(){
return rect[fontSize][106].h;
}
int CFont::getStringWidth(char* str){
int i=0;
int index;
for(size_t j=0;j<strlen(str);j++){
index=(int)str[j];
i+=rect[fontSize][index].w;
}
return i;
}
int CFont::getStringWidth(string str){
return getStringWidth(&str[0]);
}
bool CFont::loadFont(char* fname){
int i,j,k;
for(j=0;j<display->txtColors.size();j++){
for(i=6; i<21; i++){
font = TTF_OpenFont(fname, i);
if(font==NULL) return false;
for(k=32; k<127; k++){
if(!setText((char)k, texture[i][j][k], j)) return false;
SDL_QueryTexture(texture[i][j][k], NULL, NULL, &rect[i][k].w, &rect[i][k].h);
}
TTF_CloseFont(font);
font=NULL;
#ifdef GCC
font = TTF_OpenFont("./Fonts/Carlito-alphabeta.ttf",i);
#else
font = TTF_OpenFont("Fonts\\Carlito-alphabeta.ttf",i);
#endif
setText(30, texture[i][j][30], j);
SDL_QueryTexture(texture[i][j][30], NULL, NULL, &rect[i][30].w, &rect[i][30].h);
setText(31, texture[i][j][31], j);
SDL_QueryTexture(texture[i][j][31], NULL, NULL, &rect[i][31].w, &rect[i][31].h);
TTF_CloseFont(font);
font = NULL;
}
}
return true;
}
void CFont::render(int x, int y, char* str, int color, bool rotate) {
size_t i;
int index;
int posX=x;
int posY=y;
SDL_Rect r;
SDL_Point p;
for(i=0; i<strlen(str); i++){
index=(int)str[i];
r=rect[fontSize][index];
r.x=posX;
r.y=posY;
if(rotate){
p.x=0;
p.y=0;
SDL_RenderCopyEx(display->renderer, texture[fontSize][color][index], NULL, &r, -90.0, &p, SDL_FLIP_NONE);
posY-=r.w;
} else {
SDL_RenderCopy(display->renderer, texture[fontSize][color][index], NULL, &r);
posX+=r.w;
}
}
}
void CFont::render(int x, int y, string s, int color, bool rotate) {
render(x,y,&s[0],color,rotate);
}
void CFont::setDisplay(CDisplay* d){
display = d;
}
void CFont::setFontSize(int sz){
if(sz<6) sz=6;
if(sz>20) sz=20;
fontSize=sz;
}
bool CFont::setText(char c, SDL_Texture*& dest, int color){
SDL_Color col;
char str[2];
if (c == 30) str[0]='a';
else if (c == 31) str[0] = 'b';
else str[0]=c;
str[1]='\0';
if(dest!=NULL) SDL_DestroyTexture(dest);
col.r=display->txtColors[color].r;
col.g=display->txtColors[color].g;
col.b=display->txtColors[color].b;
col.a=255;
SDL_Surface* surf;
surf = TTF_RenderText_Blended(font,str,col);
if(surf==NULL) return false;
dest = SDL_CreateTextureFromSurface(display->renderer,surf);
if(dest==NULL) return false;
SDL_FreeSurface(surf);
return true;
}