forked from new299/DS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspf.c
150 lines (111 loc) · 2.44 KB
/
spf.c
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
/* sdl_picofont
http://nurd.se/~noname/sdl_picofont
File authors:
Fredrik Hultin
License: GPLv2
*/
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include "SDL_picofont.h"
#include <string.h>
#define FNT_FONTHEIGHT 8
#define FNT_FONTWIDTH 8
typedef struct
{
int x;
int y;
}FNT_xy;
FNT_xy FNT_Generate(const char* text, unsigned int len, unsigned int w, unsigned char* pixels)
{
unsigned int i, x, y, col, row, stop;
unsigned char *fnt, chr;
FNT_xy xy;
fnt = FNT_GetFont();
xy.x = xy.y = col = row = stop = 0;
for(i = 0; i < len; i++){
switch(text[i]){
case '\n':
row++;
col = 0;
chr = 0;
break;
case '\r':
chr = 0;
break;
case '\t':
chr = 0;
col += 4 - col % 4;
break;
case '\0':
stop = 1;
chr = 0;
break;
default:
col++;
chr = text[i];
break;
}
if(stop){
break;
}
if((col + 1) * FNT_FONTWIDTH > (unsigned int)xy.x){
xy.x = col * FNT_FONTWIDTH;
}
if((row + 1) * FNT_FONTHEIGHT > (unsigned int)xy.y){
xy.y = (row + 1) * FNT_FONTHEIGHT;
}
if(chr != 0 && w != 0){
for(y = 0; y < FNT_FONTHEIGHT; y++){
for(x = 0; x < FNT_FONTWIDTH; x++){
if(fnt[text[i] * FNT_FONTHEIGHT + y] >> (7 - x) & 1){
pixels[((col - 1) * FNT_FONTWIDTH) + x + (y + row * FNT_FONTHEIGHT) * w] = 1;
}
}
}
}
}
return xy;
}
SDL_Surface* FNT_Render(const char* text, SDL_Color color)
{
return FNT_RenderMax(text, strlen(text), color);
}
SDL_Surface* FNT_RenderMax(const char* text, unsigned int len, SDL_Color color)
{
SDL_Surface* surface;
SDL_Color colors[2];
FNT_xy xy;
colors[0].r = (color.r + 0x7e) % 0xff;
colors[0].g = (color.g + 0x7e) % 0xff;
colors[0].b = (color.b + 0x7e) % 0xff;
colors[1] = color;
xy = FNT_Generate(text, len, 0, NULL);
surface = SDL_CreateRGBSurface(
SDL_SWSURFACE,
xy.x,
xy.y,
8,
0,
0,
0,
0
);
if(!surface){
return NULL;
}
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format, (color.r + 0x7e) % 0xff, (color.g + 0x7e) % 0xff, (color.b + 0x7e) % 0xff));
/*SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 0x0d, 0xea, 0xd0));*/
SDL_SetColors(surface, colors, 0, 2);
if(SDL_MUSTLOCK(surface)){
SDL_LockSurface(surface);
}
FNT_Generate(text, len, surface->w, (unsigned char*)surface->pixels);
if(SDL_MUSTLOCK(surface)){
SDL_UnlockSurface(surface);
}
return surface;
}
#ifdef __cplusplus
};
#endif /* __cplusplus */