-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont.c
368 lines (308 loc) · 7.55 KB
/
font.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
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
357
358
359
360
361
362
363
364
365
366
367
368
/* A very simple font blitter - uses SFont/BFont style proportional fonts.
* vim:set cin sm ts=8 sw=4 sts=4: - Sec <sec@42.org>
* $Id: font.c,v 1.11 2004/06/29 08:40:45 sec Exp $
*/
#include "brillion.h"
#include <SDL_image.h>
a_font* init_font(const char *file){
int x,y;
SDL_PixelFormat pix;
SDL_Surface *in,*tmp;
a_font *f;
Uint32 *p,ep,op; /* pixel, endpixel, oldpixel */
f=calloc(1,sizeof(a_font));
in=IMG_Load(file);
assert(in!=NULL);
pix.palette=NULL;
pix.BitsPerPixel=32;
pix.Rmask=0x000000ff;
pix.Gmask=0x0000ff00;
pix.Bmask=0x00ff0000;
pix.Amask=0xff000000;
tmp=SDL_ConvertSurface(in, &pix, SDL_SWSURFACE);
assert(tmp!=NULL);
free(in);
p=tmp->pixels;op=ep=p[0];y=0;
for(x=0;x<tmp->w;x++){
if(p[x]!=op){
op=p[x];
if(op==ep){
f->wid[y]=x-f->beg[y];
y++;
}else{
f->beg[y]=x;
};
};
assert(y<FONTLEN);
};
f->h=tmp->h;
f->lineh=f->h+(f->h/6);
f->len=y;
f->space=f->wid['-'-FONTBEG]; /* What is typographically correct here? */
SDL_SetColorKey(tmp, SDL_SRCCOLORKEY, p[1]);
f->font=SDL_DisplayFormat(tmp);
assert(f->font!=NULL);
free(tmp);
return(f);
}
void render_font(int x, int y,const char *txt){
render_font_to(x, y, txt, play->g->display);
}
void render_font_to(int x, int y,const char *txt,SDL_Surface *disp){
a_font *f=play->g->tfont;
SDL_Rect rs,rd;
const char *c;
rd.x=x;rd.y=y;
rs.y=0; rs.h=f->h-1;
for(c=txt;*c!=0;c++){
if(*c<FONTBEG){
rd.x+=f->space;;
continue;
};
/* printf("Rendering: %c (%d) ",*c,*c-FONTBEG); */
assert((*c-FONTBEG)<f->len);
rs.x=f->beg[*c-FONTBEG];
rs.w=f->wid[*c-FONTBEG];
/* printf("[%d/%d -> %d/%d]\n",rs.x,rs.y,rs.w,rs.h); */
SDL_BlitSurface(f->font, &rs, disp, &rd);
rd.x+=rs.w;
};
}
SDL_Rect *render_text(int x, int y,const char *txt,a_font *f,int maxw){
static SDL_Rect rv;
SDL_Rect rs,rd;
const char *c;
if(f == NULL)
f=play->g->tfont;
assert(f !=NULL);
rv.x=rd.x=x;
rv.y=rd.y=y;
rv.w=0;
rs.y=0; rv.h=rs.h=f->h-1;
for(c=txt;*c!=0;c++){
if(*c<FONTBEG){
rd.x+=f->space;
rv.w+=f->space;
continue;
};
/* printf("Rendering: %c (%d) ",*c,*c-FONTBEG); */
assert((*c-FONTBEG)<f->len);
rs.x=f->beg[*c-FONTBEG];
rs.w=f->wid[*c-FONTBEG];
rv.w+=rs.w;
if(maxw && (rv.w>maxw)){
/* printf("Break!\n"); */
break;
};
/* printf("[%d/%d -> %d/%d]\n",rs.x,rs.y,rs.w,rs.h); */
SDL_BlitSurface(f->font, &rs, play->g->display, &rd);
rd.x+=rs.w;
};
return &rv;
}
int numwidth(a_font *f){
const char *txt = "0123456789";
const char *c;
int w;
assert(f !=NULL);
w=0;
for(c=txt;*c!=0;c++){
if(*c<FONTBEG){
if(f->space>w)
w=f->space;
continue;
};
assert((*c-FONTBEG)<f->len);
if(f->wid[*c-FONTBEG]>w)
w=f->wid[*c-FONTBEG];
};
return w;
}
void render_num(int x, int y, int w,int num,a_font *f){
SDL_Rect rs,rd;
int c;
signed int dx=x;
assert(f !=NULL);
rd.y=y;
rs.y=0;
rs.h=f->h-1;
for(c=num;c>0||dx==x;c/=10){
dx-=w;
rs.x=f->beg[(c%10)+'0'-FONTBEG];
rs.w=f->wid[(c%10)+'0'-FONTBEG];
rd.x=dx+(w-rs.w)/2;
SDL_BlitSurface(f->font, &rs, play->g->display, &rd);
};
}
void render_fix(int x, int y, int w,const char *txt,a_font *f){
SDL_Rect rs,rd;
const char *c;
signed int dx=x;
assert(f !=NULL);
rd.y=y;
rs.y=0;
rs.h=f->h-1;
for(c=txt;*c!=0;c++){
if(*c>='0' && *c<='9'){
rs.x=f->beg[*c-FONTBEG];
rs.w=f->wid[*c-FONTBEG];
rd.x=dx+(w-rs.w)/2;
SDL_BlitSurface(f->font, &rs, play->g->display, &rd);
dx+=w;
}else if (*c == ' '){
dx+=f->space;
}else{
rs.x=f->beg[*c-FONTBEG];
rs.w=f->wid[*c-FONTBEG];
rd.x=dx;
SDL_BlitSurface(f->font, &rs, play->g->display, &rd);
dx+=rs.w;
};
};
}
#define ALIGN_LEFT 0
#define ALIGN_CENTER 1
#define ALIGN_RIGHT 2
#define ALIGN_MASK 3
#define LIMIT_NONE 0
#define LIMIT_SIZE 4
void nice_render_text(SDL_Rect *rv,const char *txt,a_font *f,int flags){
SDL_Rect rs,rd;
const char *c;
int maxw=rv->w;
assert(f !=NULL);
if(flags & ALIGN_MASK){
size_text(rv,txt,f);
if(flags & LIMIT_SIZE && rv->w > maxw)
flags|= ~ALIGN_MASK;
if((flags & ALIGN_MASK) == ALIGN_RIGHT)
rv->x+= maxw-rv->w;
else if ((flags & ALIGN_MASK) == ALIGN_CENTER)
rv->x+=(maxw-rv->w)/2;
};
rd.x=rv->x;
rd.y=rv->y;
rv->w=0;
rs.y=0; rv->h=rs.h=f->h-1;
for(c=txt;*c!=0;c++){
if(*c<FONTBEG){
rd.x+=f->space;
rv->w+=f->space;
continue;
};
assert((*c-FONTBEG)<f->len);
rs.x=f->beg[*c-FONTBEG];
rs.w=f->wid[*c-FONTBEG];
rv->w+=rs.w;
if(maxw && (rv->w>maxw)){
break;
};
SDL_BlitSurface(f->font, &rs, play->g->display, &rd);
rd.x+=rs.w;
};
}
void size_text(SDL_Rect *sr,const char *txt,a_font *f){
const char *c;
assert(f !=NULL);
sr->w=0;
sr->h=f->h-1;
for(c=txt;*c!=0;c++){
if(*c<FONTBEG){
sr->w+=f->space;
continue;
};
assert((*c-FONTBEG)<f->len);
sr->w+=f->wid[*c-FONTBEG];
};
}
/* Not reentrant, but who cares? :) */
char* input_text(SDL_Rect *in, a_font *f){
static char str[99];
SDL_Rect rd,rs;
SDL_Surface *copy,*s;
SDL_Event ev;
graphic* g=play->g;
int num=0;
int blink=0;
char ch;
s=play->g->display;
/* XXX: Perhaps only copy needed area?
I think, copying might be faster in the current form */
copy=SDL_CreateRGBSurface(SDL_HWSURFACE,
s->w, s->h, s->format->BitsPerPixel,
s->format->Rmask, s->format->Gmask,
s->format->Bmask, s->format->Amask);
SDL_BlitSurface(s,NULL,copy,NULL);
rd.x=in->x;
rd.y=in->y;
rs.y=0; rd.h=rs.h=f->h-1;
time_event(5,SDL_USER_BLINK);
SDL_EnableUNICODE(1);
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
#define CLEARBLINK do{rd.w=f->wid['_'-FONTBEG];SDL_BlitSurface(copy, &rd, play->g->display, &rd);UPDATE(rd);}while(0)
#define SETBLINK do{rs.x=f->beg['_'-FONTBEG];rs.w=f->wid['_'-FONTBEG];SDL_BlitSurface(f->font, &rs, play->g->display, &rd);UPDATE(rd);}while(0)
#define BEEP do{putc('\a',stdout);fflush(stdout);}while(0)
while(SDL_WaitEvent(&ev)){
if(ev.type==SDL_KEYDOWN){
if ( (ev.key.keysym.unicode & 0xFF80) == 0 ){
ch = ev.key.keysym.unicode & 0x7F;
if(blink) CLEARBLINK;
if(ch>=FONTBEG && (ch-FONTBEG)<f->len){
rs.x=f->beg[ch-FONTBEG];
rs.w=f->wid[ch-FONTBEG];
printf("%c is %d\n",ch,rs.w);
if(in->w > 0 && rd.x+rs.w > in->x+in->w){
BEEP;
}else{
SDL_BlitSurface(f->font, &rs, play->g->display, &rd);
UPDATE(rd);
rd.x+=rs.w;
str[num++]=ch;
};
}else if(ch==' '){
if(in->w > 0 && rd.x + f->space > in->x+in->w){
BEEP;
}else{
rd.x+=f->space;
str[num++]=ch;
};
}else if(ch == 8 && num>0){
ch=str[--num];
if(ch==' ')
rd.w=f->space;
else
rd.w=f->wid[ch-FONTBEG];
rd.x-=rd.w;
SDL_BlitSurface(copy, &rd, play->g->display, &rd);
UPDATE(rd);
}else if(ch==SDLK_ESCAPE||ch==SDLK_RETURN){
str[num]=0;
break;
}else if (ch == 0){
; /* Non-character key, like SHIFT */
}else{
BEEP;
};
if(blink) SETBLINK;
}else{
BEEP;
};
}else if (ev.type == SDL_USEREVENT && ev.user.code==SDL_USER_BLINK){
blink=1-blink;
if(blink)
SETBLINK;
else
CLEARBLINK;
time_event(5,SDL_USER_BLINK);
}else if (ev.type == SDL_QUIT){
exit(-1); /* XXX */
};
DISPLAY;
}
DISPLAY;
SDL_EnableUNICODE(0);
SDL_EnableKeyRepeat(0,0);
SDL_FreeSurface(copy);
printf("Input is: %s\n",str);
return str;
}