-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfonts.cpp
More file actions
187 lines (168 loc) · 5 KB
/
fonts.cpp
File metadata and controls
187 lines (168 loc) · 5 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <algorithm>
using std::max;
using std::min;
#include <SDL.h>
#include "data/font22_white.h"
static const unsigned char rchanc[] = { 0xFF, 0, 0, 0 };
static const unsigned int rchan = *((unsigned long *)rchanc);
static const unsigned char gchanc[] = { 0, 0xFF, 0, 0 };
static const unsigned int gchan = *((unsigned long *)gchanc);
static const unsigned char bchanc[] = { 0, 0, 0xFF, 0 };
static const unsigned int bchan = *((unsigned long *)bchanc);
static const unsigned char achanc[] = { 0, 0, 0, 0xFF };
static const unsigned int achan = *((unsigned long *)achanc);
struct font {
SDL_Surface *img;
int off[128];
int len[128];
};
font *font_init() {
int xpos = 0;
font *f = new font;
for(int let=0; let<128; ++let) {
if(let == ' ') {
int pres = 0;
f->off[let] = 0;
while((!pres) && xpos < (int)font22_white_image.width) {
pres = 0;
for(int ctr=0; ctr < int(font22_white_image.height) && (!pres); ++ctr) {
if(font22_white_image.pixel_data[4*(font22_white_image.width*ctr+xpos)+3])
pres = 1;
}
++xpos;
}
--xpos;
f->len[let] = xpos-f->off[let];
}
else if(!isgraph(let)) { f->off[let] = xpos; f->len[let] = 0; }
else {
int pres = 1;
f->off[let] = xpos;
while(pres && xpos < (int)font22_white_image.width) {
pres = 0;
for(int ctr=0; ctr < int(font22_white_image.height) && (!pres); ++ctr) {
if(font22_white_image.pixel_data[4*(font22_white_image.width*ctr+xpos)+3])
{ pres = 1; }
}
++xpos;
}
f->len[let] = xpos-f->off[let];
while((!pres) && xpos < (int)font22_white_image.width) {
pres = 0;
for(int ctr=0; ctr < int(font22_white_image.height) && (!pres); ++ctr) {
if(font22_white_image.pixel_data[4*(font22_white_image.width*ctr+xpos)+3])
pres = 1;
}
++xpos;
}
--xpos;
}
}
SDL_Surface *tmp = SDL_CreateRGBSurfaceFrom(
(void*)font22_white_image.pixel_data,
font22_white_image.width, font22_white_image.height,
32, 4*font22_white_image.width,
rchan, gchan, bchan, achan);
f->img = SDL_DisplayFormatAlpha(tmp);
SDL_FreeSurface(tmp);
SDL_SetAlpha(f->img, SDL_RLEACCEL|SDL_SRCALPHA, SDL_ALPHA_OPAQUE);
return f;
}
int string_length(const char *str, font *f) {
int ret=0, ln=0;
for(int ctr=0; ctr<int(strlen(str)); ++ctr) {
if(str[ctr] == '\r') { ret = max(ret, ln); ln = 0; }
if(str[ctr] == '\t') { ln += 64; ln &= (~(63)); }
else { ln += f->len[int(str[ctr])]; }
}
return max(ret, ln);
}
int string_height(const char *str, font *f) {
int ret=22;
for(int ctr=0; ctr<int(strlen(str)); ++ctr) {
if(str[ctr] == '\n') ret += 24;
}
return ret;
}
SDL_Surface *get_string(font *f, const char *t) {
SDL_Rect sr, dr;
int xpos = 0;
sr.y = 0;
sr.h = 24;
dr.y = 0;
int xs = string_length(t, f);
int ys = string_height(t, f);
SDL_Surface *tmp = SDL_CreateRGBSurface(SDL_SWSURFACE|SDL_SRCALPHA,
xs, ys, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
SDL_FillRect(tmp, NULL, 0);
SDL_SetAlpha(f->img, 0, 255);
for(int ctr=0; ctr<int(strlen(t)); ++ctr) {
if(isgraph(t[ctr]) || t[ctr] == ' ') {
sr.x = f->off[int(t[ctr])];
sr.w = f->len[int(t[ctr])];
dr.x = xpos;
SDL_BlitSurface(f->img, &sr, tmp, &dr);
xpos += f->len[int(t[ctr])];
}
else if(t[ctr] == '\n') {
xpos = 0;
dr.y += 24;
}
else if(t[ctr] == '\t') {
if(t[ctr] == '\t') { xpos += 64; xpos &= (~(63)); }
}
}
SDL_SetAlpha(f->img, SDL_SRCALPHA|SDL_RLEACCEL, 255);
SDL_Surface *ret = SDL_DisplayFormatAlpha(tmp);
SDL_FreeSurface(tmp);
SDL_SetAlpha(ret, SDL_SRCALPHA|SDL_RLEACCEL, 255);
return ret;
}
void string_draw(SDL_Surface *s, int x, int y, font *f, const char *t) {
SDL_Rect fr = {x, y, 0, 0};
SDL_Surface *mes = get_string(f, t);
SDL_BlitSurface(mes, NULL, s, &fr);
SDL_FreeSurface(mes);
}
void string_drawc(SDL_Surface *s, int x, int y, font *f, const char *t) {
x -= string_length(t, f)/2;
string_draw(s, x, y, f, t);
}
void string_drawr(SDL_Surface *s, int x, int y, font *f, const char *t) {
x -= string_length(t, f);
string_draw(s, x, y, f, t);
}
font *font_colored(font *f, unsigned long c) {
font *ret = new font;
(*ret) = (*f);
ret->img = SDL_DisplayFormatAlpha(f->img);
SDL_LockSurface(ret->img);
// c = SDL_MapRGBA(ret->img->format,
// (c>>24)&0xFF, (c>>16)&0xFF, (c>>8)&0xFF, (c)&0xFF);
if(ret->img->format->BytesPerPixel != 4) {
fprintf(stderr, "Error: You need to be in 32-bit mode\n");
exit(0);
}
Uint32 *tmp = (Uint32 *)(ret->img->pixels);
int base=0;
for(int y=0; y<ret->img->h; ++y) {
for(int x=0; x<ret->img->w; ++x) {
tmp[base+x] &= c;
}
base += ret->img->pitch/4;
}
SDL_UnlockSurface(ret->img);
return ret;
}
font *font_backed(font *f, unsigned long c) {
font *ret = new font;
(*ret) = (*f);
ret->img = SDL_DisplayFormat(f->img);
SDL_FillRect(ret->img, NULL, 0);
SDL_BlitSurface(f->img, NULL, ret->img, NULL);
return ret;
}