font.c
9.01 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
Example of FONTS
*/
#include <PR/mbi.h>
#include <PR/sp.h>
#include "font.h"
#include "font_ext.h"
#include "letters_img.h"
static char letters_string[] = "abcdefhiuvsnorm.klxz4321567890`!gjpqytw,EFGHKLNOPRSUVXZ@JM:WIYABCD'T\"+-=Q_/?()[]";
static Bitmap letters_bms[] = {
{ 8, 64, 0*8, 0*12, letters_img, 12,0 },
{ 8, 64, 1*8, 0*12, letters_img, 12,0 },
{ 8, 64, 2*8, 0*12, letters_img, 12,0 },
{ 8, 64, 3*8, 0*12, letters_img, 12,0 },
{ 8, 64, 4*8, 0*12, letters_img, 12,0 },
{ 8, 64, 5*8, 0*12, letters_img, 12,0 },
{ 8, 64, 6*8, 0*12, letters_img, 12,0 },
{ 7, 64, 7*8, 0*12, letters_img, 12,0 },
{ 8, 64, 0*8, 1*12, letters_img, 12,0 },
{ 8, 64, 1*8, 1*12, letters_img, 12,0 },
{ 8, 64, 2*8, 1*12, letters_img, 12,0 },
{ 8, 64, 3*8, 1*12, letters_img, 12,0 },
{ 8, 64, 4*8, 1*12, letters_img, 12,0 },
{ 8, 64, 5*8, 1*12, letters_img, 12,0 },
{ 9, 64, 6*8, 1*12, letters_img, 12,0 },
{ 6, 64, 7*8+1, 1*12, letters_img, 12,0 },
{ 8, 64, 0*8, 2*12, letters_img, 12,0 },
{ 8, 64, 1*8, 2*12, letters_img, 12,0 },
{ 8, 64, 2*8, 2*12, letters_img, 12,0 },
{ 8, 64, 3*8, 2*12, letters_img, 12,0 },
{ 8, 64, 4*8, 2*12, letters_img, 12,0 },
{ 8, 64, 5*8, 2*12, letters_img, 12,0 },
{ 8, 64, 6*8, 2*12, letters_img, 12,0 },
{ 8, 64, 7*8, 2*12, letters_img, 12,0 },
{ 8, 64, 0*8, 3*12, letters_img, 12,0 },
{ 8, 64, 1*8, 3*12, letters_img, 12,0 },
{ 8, 64, 2*8, 3*12, letters_img, 12,0 },
{ 8, 64, 3*8, 3*12, letters_img, 12,0 },
{ 8, 64, 4*8, 3*12, letters_img, 12,0 },
{ 8, 64, 5*8, 3*12, letters_img, 12,0 },
{ 7, 64, 6*8, 3*12, letters_img, 12,0 },
{ 7, 64, 7*8, 3*12, letters_img, 12,0 },
{ 8, 64, 0*8, 4*12, letters_img, 16,0 },
{ 8, 64, 1*8, 4*12, letters_img, 16,0 },
{ 8, 64, 2*8, 4*12, letters_img, 16,0 },
{ 8, 64, 3*8, 4*12, letters_img, 16,0 },
{ 8, 64, 4*8, 4*12, letters_img, 16,0 },
{ 8, 64, 5*8, 4*12, letters_img, 16,0 },
{ 9, 64, 6*8, 4*12, letters_img, 16,0 },
{ 6, 64, 7*8+1, 4*12, letters_img, 16,0 },
{ 8, 64, 0*8, 64+0*12, letters_img, 12,0 },
{ 8, 64, 1*8, 64+0*12, letters_img, 12,0 },
{ 8, 64, 2*8, 64+0*12, letters_img, 12,0 },
{ 8, 64, 3*8, 64+0*12, letters_img, 12,0 },
{ 8, 64, 4*8, 64+0*12, letters_img, 12,0 },
{ 8, 64, 5*8, 64+0*12, letters_img, 12,0 },
{ 8, 64, 6*8, 64+0*12, letters_img, 12,0 },
{ 8, 64, 7*8, 64+0*12, letters_img, 12,0 },
{ 8, 64, 0*8, 64+1*12, letters_img, 12,0 },
{ 8, 64, 1*8, 64+1*12, letters_img, 12,0 },
{ 8, 64, 2*8, 64+1*12, letters_img, 12,0 },
{ 8, 64, 3*8, 64+1*12, letters_img, 12,0 },
{ 8, 64, 4*8, 64+1*12, letters_img, 12,0 },
{ 8, 64, 5*8, 64+1*12, letters_img, 12,0 },
{ 8, 64, 6*8, 64+1*12, letters_img, 12,0 },
{ 8, 64, 7*8, 64+1*12, letters_img, 12,0 },
{ 7, 64, 0*8, 64+2*12, letters_img, 12,0 },
{ 9, 64, 1*8-1, 64+2*12, letters_img, 12,0 },
{ 6, 64, 2*8, 64+2*12, letters_img, 12,0 },
{ 9, 64, 3*8-1, 64+2*12, letters_img, 12,0 },
{ 7, 64, 4*8, 64+2*12, letters_img, 12,0 },
{ 9, 64, 5*8-1, 64+2*12, letters_img, 12,0 },
{ 8, 64, 6*8, 64+2*12, letters_img, 12,0 },
{ 8, 64, 7*8, 64+2*12, letters_img, 12,0 },
{ 8, 64, 0*8, 64+3*12, letters_img, 12,0 },
{ 8, 64, 1*8, 64+3*12, letters_img, 12,0 },
{ 7, 64, 2*8, 64+3*12, letters_img, 12,0 },
{ 9, 64, 3*8-1, 64+3*12, letters_img, 12,0 },
{ 7, 64, 4*8, 64+3*12, letters_img, 12,0 },
{ 9, 64, 5*8-1, 64+3*12, letters_img, 12,0 },
{ 8, 64, 6*8, 64+3*12, letters_img, 12,0 },
{ 8, 64, 7*8, 64+3*12, letters_img, 12,0 },
{ 8, 64, 0*8, 64+4*12+1, letters_img, 16,0 },
{ 8, 64, 1*8, 64+4*12+1, letters_img, 16,0 },
{ 8, 64, 2*8, 64+4*12+1, letters_img, 16,0 },
{ 8, 64, 3*8, 64+4*12+1, letters_img, 16,0 },
{ 8, 64, 4*8, 64+4*12+1, letters_img, 16,0 },
{ 8, 64, 5*8, 64+4*12+1, letters_img, 16,0 },
{ 8, 64, 6*8, 64+4*12+1, letters_img, 16,0 },
{ 8, 64, 7*8, 64+4*12+1, letters_img, 16,0 },
};
Font letters_font = { letters_string, letters_bms, letters_img };
char *my_strchr( char *, char );
void
text_sprite( Sprite *txt, char *str, Font *fnt, int xlen, int ylen )
{
int i, ci;
int x;
int y;
char *indx;
Bitmap *bm;
txt->width = xlen * 8;
txt->height = ylen * 16;
bm = txt->bitmap;
i = 0;
ci = 0;
for(y=0; y<ylen; y++) {
for(x=0; x<xlen; x++, i++, ci++) {
if( str[ci] == '\0' ) {
bm[i] = fnt->bitmaps[0];
bm[i].width = -1;
txt->nbitmaps = i;
return;
};
if( str[ci] == '\n' ) {
bm[i] = fnt->bitmaps[0];
bm[i].buf = NULL;
ci--;
continue;
};
if( (indx = my_strchr(fnt->index_string, str[ci])) != NULL ) {
bm[i] = fnt->bitmaps[indx-fnt->index_string];
} else {
bm[i] = fnt->bitmaps[0];
bm[i].buf = NULL;
};
};
if( str[ci] == '\n' )
ci++;
};
txt->nbitmaps = i;
return;
}
char *
my_strchr( char *str, char target )
{
while( *str && (*str != target) )
str++;
if( *str )
return( str );
return( NULL );
}
#define DYN_SPRITE_HACK
#define NUM_template_BMS (24*40)
static Bitmap template_bm[NUM_template_BMS];
static Gfx template_dl[NUM_DL(NUM_template_BMS)];
Sprite template_sprite = {
0,0, /* Position: x,y */
0,0, /* Sprite size in texels (x,y) */
1.0,1.0, /* Sprite Scale: x,y */
0,0, /* Explosion (x,y) */
/*SP_TRANSPARENT, /* Sprite Attributes */
SP_EXTERN, /* Sprite Attributes */
0x1234, /* Sprite Depth: Z */
255,255,255,255, /* Sprite Coloration: RGBA */
0,0,NULL, /* Color LookUp Table: start_index, length, address */
0,1, /* Sprite Bitmap index: start index, step increment */
NUM_template_BMS, /* Number of bitmaps */
NUM_DL(NUM_template_BMS), /* Number of display list locations allocated */
15, 128, /* Sprite Bitmap Height: Used_height, physical height */
G_IM_FMT_I, /* Sprite Bitmap Format */
G_IM_SIZ_4b, /* Sprite Bitmap Texel Size */
template_bm, /* Pointer to bitmaps */
template_dl, /* Display list memory */
NULL, /* HACK: dynamic_dl pointer */
};
static unsigned char font_red = 255;
static unsigned char font_grn = 255;
static unsigned char font_blu = 255;
static unsigned char font_alf = 255;
static int font_xpos = 0;
static int font_ypos = 0;
static int font_win_width = 40;
static int font_win_height = 1;
static double font_xscale = 1.0;
static double font_yscale = 1.0;
/* Initialize Font local variables and prepare for drawing sprites */
void
font_init( Gfx **glistp )
{
Gfx *gxp;
gxp = *glistp;
spInit( &gxp );
template_sprite.rsp_dl_next = template_sprite.rsp_dl;
*glistp = gxp;
font_red = 255;
font_grn = 255;
font_blu = 255;
font_alf = 255;
font_xpos = 0;
font_ypos = 0;
font_win_width = 40;
font_win_height = 1;
font_xscale = 1.0;
font_yscale = 1.0;
}
/* Call spFinish() to clean up when done using Sprites */
void
font_finish( Gfx **glistp )
{
Gfx *gxp;
gxp = *glistp;
spFinish( &gxp );
*glistp = (gxp-1); /* Don't use final EndDisplayList() */
}
/* Set text window area (units are characters) */
void
font_set_win( int width, int height )
{
font_win_width = width;
font_win_height = height;
}
/* Set text window position (upper left corner) */
void
font_set_pos( int xpos, int ypos )
{
font_xpos = xpos;
font_ypos = ypos;
}
/* Set text size */
void
font_set_scale( double xscale, double yscale )
{
font_xscale = xscale;
font_yscale = yscale;
}
/* Set text color */
void
font_set_color( unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha )
{
font_red = red;
font_grn = green;
font_blu = blue;
font_alf = alpha;
}
void
font_set_transparent( int flag )
{
if( flag )
spSetAttribute( &template_sprite, SP_TRANSPARENT | SP_CUTOUT );
else
spClearAttribute( &template_sprite, SP_TRANSPARENT | SP_CUTOUT );
}
/* Convert the string to a sprite with the propler bitmaps
assembled from the basic font texture. */
void
font_show_string( Gfx **glistp, char *val_str )
{
Sprite *sp;
static Gfx gx[10000];
Gfx *gxp, *dl;
gxp = *glistp;
sp = &template_sprite;
sp->width = font_win_width*8 + 8;
sp->height = font_win_height*16 + 8;
text_sprite( sp, val_str, &letters_font, font_win_width, font_win_height );
spMove( sp, font_xpos, font_ypos );
spColor( sp, font_red, font_grn, font_blu, font_alf );
spScale( sp, font_xscale, font_yscale );
dl = spDraw( sp ); /* */
gSPDisplayList( gxp++, dl );
*glistp = gxp;
}