-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.c
570 lines (472 loc) · 13.9 KB
/
graphics.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/* Display the game background & field. Do animations, too.
* vim:set cin sm ts=8 sw=4 sts=4: - Sec <[email protected]>
* $Id: graphics.c,v 1.42 2005/12/08 12:38:48 sec Exp $
*/
#include "brillion.h"
#include <SDL_image.h>
#define QUAD 32 /* Size of Standard Block, must be divisible by 2 */
#define MX 640
#define MY 480
typedef struct {
Uint8 r;
Uint8 g;
Uint8 b;
Uint8 a;
} myc;
/* hue: color tone
* sat -> 0 = white (other colors increase)
* val -> 0 = black (primary color decreases)
* relative brightness: Red: 88, Green: 127, Blue: 40 :: Sum should be == 255
*/
#define BRED 88
#define BGREEN 127
#define BBLUE 40
myc mk_color(int color, int br, int reality){
myc c;
int t,sat,val;
switch(color){
case RED: t=BRED +20;break;
case CYAN: t= BGREEN+BBLUE+ 5;break;
case MAGENTA: t=BRED +BBLUE+10;break;
case GREEN: t= BGREEN - 5;break;
case BLUE: t= BBLUE+10;break;
case YELLOW: t=BRED+BGREEN ;break;
default: t=BRED+BGREEN+BBLUE;
};
if(!reality)
t=255;
/* Saturation is actually 255-saturation here */
if(br>t){
sat=(br-t)*256/(256-t);
val=255;
}else{
sat=0;
val=br*255/t;
};
switch(color){
case RED: c.r=val; c.g=sat; c.b=sat; c.a=0; break;
case CYAN: c.r=sat; c.g=val; c.b=val; c.a=0; break;
case MAGENTA: c.r=val; c.g=sat; c.b=val; c.a=0; break;
case GREEN: c.r=sat; c.g=val; c.b=sat; c.a=0; break;
case BLUE: c.r=sat; c.g=sat; c.b=val; c.a=0; break;
case YELLOW: c.r=val; c.g=val; c.b=sat; c.a=0; break;
default: c.r=sat; c.g=sat; c.b=sat; c.a=0; break;
};
return(c);
}
/* Seems like an ugly hack, but I know no sane way */
SDL_Surface* color_graphic(SDL_Surface* in, int color, int alpha){
graphic *g=play->g;
SDL_Surface* out;
SDL_Surface* tmp;
SDL_PixelFormat f;
myc* p4;
f.palette=NULL;
f.BitsPerPixel=32;
#if SDL_BYTEORDER == 1234
f.Rmask=0x000000ff;
f.Gmask=0x0000ff00;
f.Bmask=0x00ff0000;
f.Amask=0xff000000;
#else
f.Rmask=0xff000000;
f.Gmask=0x00ff0000;
f.Bmask=0x0000ff00;
f.Amask=0x000000ff;
#endif
tmp=SDL_ConvertSurface(in, &f, SDL_SWSURFACE);
assert(tmp!=NULL);
/* perhaps We should check the 'pitch' ? */
for(p4=tmp->pixels;p4<(myc*)tmp->pixels+(tmp->w*tmp->h);p4++){
if(alpha!=3){
if(p4->r == p4->g && p4->r == 0 ){
*p4=mk_color(color,p4->b,0);
};
}else{
if(p4->r == p4->g && p4->b == p4->g){
*p4=mk_color(color,p4->b,1);
};
};
};
out=SDL_DisplayFormat(tmp);
assert(out!=NULL);
if(alpha)
SDL_SetColorKey(out, SDL_SRCCOLORKEY, g->colors[BG]);
return(out);
}
void load_graphics(void){
SDL_Surface* pad;
int x;
graphic *g=play->g;
g->colors[RED]= SDL_MapRGB (g->display->format, 0xff, 0x00, 0x00);
g->colors[CYAN]= SDL_MapRGB (g->display->format, 0x00, 0xff, 0xff);
g->colors[MAGENTA]= SDL_MapRGB (g->display->format, 0xff, 0x00, 0xff);
g->colors[GREEN]= SDL_MapRGB (g->display->format, 0x00, 0xff, 0x00);
g->colors[BLUE]= SDL_MapRGB (g->display->format, 0x00, 0x00, 0xff);
g->colors[YELLOW]= SDL_MapRGB (g->display->format, 0xff, 0xff, 0x00);
g->colors[WHITE]= SDL_MapRGB (g->display->format, 0xff, 0xff, 0xff);
g->colors[BG]= SDL_MapRGB (g->display->format, 0xff, 0x7f, 0x00);
g->colors[FG]= SDL_MapRGB (g->display->format, 0xff, 0x7f, 0x7f);
g->colors[NONE]= SDL_MapRGBA(g->display->format,0,0,0,0);
/* Load blocks, and create all colors */
pad=IMG_Load("ball_grey.gif");
for(x=1;x<GAME_COLORS;x++)
g->ball[x]=color_graphic(pad,x,3);
SDL_FreeSurface(pad);
pad=IMG_Load("ballX.gif");
for(x=1;x<GAME_COLORS;x++)
g->ballx[x]=color_graphic(pad,x,2);
SDL_FreeSurface(pad);
pad=IMG_Load("block_blue.gif");
for(x=1;x<GAME_COLORS;x++)
g->block[x]=color_graphic(pad,x,0);
SDL_FreeSurface(pad);
pad=IMG_Load("blockX.gif");
for(x=1;x<GAME_COLORS;x++)
g->blockx[x]=color_graphic(pad,x,2);
SDL_FreeSurface(pad);
pad=IMG_Load("star_blue.gif");
for(x=1;x<GAME_COLORS;x++)
g->star[x]=color_graphic(pad,x,0);
SDL_FreeSurface(pad);
pad=IMG_Load("disk_blue.gif");
for(x=1;x<GAME_COLORS;x++)
g->disk[x]=color_graphic(pad,x,0);
SDL_FreeSurface(pad);
pad=IMG_Load("space.gif");
g->back=SDL_ConvertSurface(pad, g->display->format, SDL_HWSURFACE);
assert(g->back!=NULL);
SDL_FreeSurface(pad);
pad=IMG_Load("wall.gif");
g->wall=SDL_ConvertSurface(pad, g->display->format, SDL_HWSURFACE);
assert(g->wall!=NULL);
SDL_FreeSurface(pad);
pad=IMG_Load("death.gif");
g->death=SDL_ConvertSurface(pad, g->display->format, SDL_HWSURFACE);
assert(g->death!=NULL);
SDL_FreeSurface(pad);
/* XXX: do that somewhere else... */
g->border=IMG_Load("bkg.png");
assert(g->border!=NULL);
g->level.x=20;g->level.y=(g->display->h - (QUAD*11))/2; /* XXX read from somewhere... */
/* play->g->level.w=(play->f->w-1)*QUAD; play->g->level.h=(play->f->h-1)*QUAD; */
g->level.w=(15)*QUAD; g->level.h=(11)*QUAD; /* XXX^2 */
g->font=IMG_Load("numbers.png");
g->tfont=init_font("font.png");
}
graphic* init_graphic(void){
SDL_Surface* disp;
graphic* g;
if(SDL_InitSubSystem(SDL_INIT_VIDEO)){
fprintf(stderr,"Could not initialize SDL: %s.\n", SDL_GetError());
exit(-1);
};
/* HWPALETTE, optional FULLSCREEN? check before ANYFORMAT? */
if(!(disp=SDL_SetVideoMode(MX, MY, 32, SDL_ANYFORMAT|SDL_HWPALETTE))){
printf("Could not set videomode: %s.\n", SDL_GetError());
exit(-1);
};
assert(SDL_MUSTLOCK(disp)==0);
printf("SDL uses %d bytes/pixel Display format\n",disp->format->BytesPerPixel);
SDL_WM_SetCaption(PROG_NAME " " PROG_VERSION, PROG_NAME);
g=calloc(1,sizeof(graphic));
g->display=disp;
if(disp->format->BytesPerPixel == 1){
int q;
printf("*UGH* this is an Palette display\n");
printf("X11 uses %d colors\n",disp->format->palette->ncolors);
g->ncolors=disp->format->palette->ncolors;
g->palette=calloc(g->ncolors,sizeof(SDL_Color));
for(q=0;q<g->ncolors;q++){
g->palette[q].r=disp->format->palette->colors[q].r;
g->palette[q].g=disp->format->palette->colors[q].g;
g->palette[q].b=disp->format->palette->colors[q].b;
};
};
g->rects=calloc(MAXRECTS,sizeof(SDL_Rect));
g->numrects=0;
play->g=g;
load_graphics();
return g;
}
/* -------------------------------------------------------------------- */
/* Where is a Block */
#define POSX(z) (QUAD*(z-1)+g->level.x)
#define POSY(z) (QUAD*(z-1)+g->level.y)
/* Where is the Ball */
#define POS2X(z) (QUAD/2*(z-2)+g->level.x)
#define POS2Y(z) (QUAD/2*(z-2)+g->level.y)
void paint_level(void){
int x,y;
field *lvl=play->f;
for(y=1;y<lvl->h;y++)
for(x=1;x<lvl->w;x++)
paint_block(x,y);
}
void paint_ball(void){
/* Paint new Ball */
SDL_Rect rect;
graphic *g=play->g;
field *lvl=play->f;
rect.w=rect.h=QUAD/2;
rect.x=POS2X(lvl->x);
rect.y=POS2Y(lvl->y);
SDL_BlitSurface(g->ball[lvl->color], NULL, g->display, &rect);
UPDATE(rect);
return;
}
void blank_block(int x, int y){
SDL_Rect rect;
graphic *g=play->g;
rect.w=rect.h=QUAD;
rect.x=POSX(x);
rect.y=POSY(y);
SDL_BlitSurface(g->back, NULL, g->display, &rect);
UPDATE(rect);
}
void paint_block(int x, int y){
SDL_Rect rect;
graphic *g=play->g;
field *lvl=play->f;
rect.w=rect.h=QUAD;
rect.x=POSX(x);
rect.y=POSY(y);
switch(PIECE(x,y)){
case BLOCK:
SDL_BlitSurface(g->block[COLOR(x,y)], NULL, g->display, &rect);
break;
case DISK:
SDL_BlitSurface(g->disk[COLOR(x,y)], NULL, g->display, &rect);
break;
case STAR:
SDL_BlitSurface(g->star[COLOR(x,y)], NULL, g->display, &rect);
break;
case WALL:
SDL_BlitSurface(g->wall, NULL, g->display, &rect);
break;
case DEATH:
SDL_BlitSurface(g->death, NULL, g->display, &rect);
break;
case SPACE:
SDL_BlitSurface(g->back, NULL, g->display, &rect);
break;
default:
fprintf(stderr,"Cannot draw %d at %d/%d\n",PIECE(x,y),x,y);
};
UPDATE(rect);
}
void snapshot(void){
static int q=0;
char name[40];
FILE* pic;
unsigned char* x;
graphic *g=play->g;
if(g->display->format->BytesPerPixel!=4){
fprintf(stderr,"Das geht nicht\n");
return;
};
sprintf(name,"snap_%d.pnm",++q);
pic=fopen(name,"w");
fprintf(pic,"P6\n%d %d\n%d\n",g->display->w,g->display->h,255);
for (x=g->display->pixels;
x<(unsigned char*)g->display->pixels+ g->display->w *
g->display->h * g->display->format->BytesPerPixel;
x+=g->display->format->BytesPerPixel)
fprintf(pic,"%c%c%c",*(x+2),*(x+1),*(x+0));
fclose(pic);
}
char number_cache[100];
void clear_number_cache(void){
memset(number_cache,' ',99);
number_cache[99]=0;
}
void print_number(int cacheno, int num, coord co){
char *c,*d;
SDL_Rect dst,r;
graphic *g=play->g;
/* Font specifics ... */
char fudge=5;
char wid=25;
char hei=31;
r.w=dst.w=wid-fudge*2;
r.h=dst.h=hei;
r.y=0;
sprintf(number_cache,"%5d",num);
dst.y=co.y;
dst.x=co.x-(5*dst.w);
assert((cacheno+1)*8<100);
for(d=(c=number_cache)+(cacheno*8);*c!=0;c++,d++){
if(*c != *d){
SDL_BlitSurface(g->border, &dst, g->display, &dst);
*d=*c;
if(*c!=' '){
int xoff=*c-'0';
r.x=wid*xoff+fudge;
SDL_BlitSurface(g->font, &r, g->display, &dst);
};
UPDATE(dst);
};
dst.x+=dst.w;
}
}
void update_scoreboard(void){
a_layout *l=play->layout;
print_number(1, play->level+1, l->level);
print_number(2, play->points, l->pts);
print_number(3, play->f->blocks,l->blocks);
print_number(4, play->f->time, l->time);
print_number(5, play->lives, l->lives);
}
a_anim* init_anim(void){
a_anim *a;
a=calloc(MAX_ANIM,sizeof(a_anim));
return a;
}
void create_moveanim(anim_t type, int color, int ox, int oy, int nx, int ny){
a_anim* a=play->a;
graphic* g=play->g;
int v;
if(type!=A_BALL){ /* Ball is always index 0. */
a++;
while(a->type != A_NONE) a++; /* Search free anim space. */
};
a->duration=0;
a->color=color;
switch(a->type=type){
case A_BALL:
if(ox>nx) v=1; else v=0;
a->block[v].x=ox/2;
a->block[1-v].x=nx/2;
if(oy>ny) v=1; else v=0;
a->block[v].y=oy/2;
a->block[1-v].y=ny/2;
a->pixel[0].x=POS2X(ox);
a->pixel[0].y=POS2Y(oy);
a->pixel[1].x=POS2X(nx)-a->pixel[0].x;
a->pixel[1].y=POS2Y(ny)-a->pixel[0].y;
break;
case A_DISK:
if(ox>nx) v=1; else v=0;
a->block[v].x=ox;
a->block[1-v].x=nx;
if(oy>ny) v=1; else v=0;
a->block[v].y=oy;
a->block[1-v].y=ny;
a->pixel[0].x=POSX(ox);
a->pixel[1].x=POSX(nx)-a->pixel[0].x;
a->pixel[0].y=POSY(oy);
a->pixel[1].y=POSY(ny)-a->pixel[0].y;
break;
default:
a->type=A_NONE;
printf("Unknown type %d in create_moveanim\n",type);
break;
};
}
void create_staticanim(anim_t type, int color, int x, int y){
a_anim* a=play->a;
graphic* g=play->g;
a++; /* Index 0 is always the ball move. */
while(a->type != A_NONE) a++; /* Search free anim space. */
/* XXX: Don't run over the end. */
a->duration=0;
a->color=color;
switch(a->type=type){
case A_DIE:
a->block[0].x=x/2;
a->block[0].y=y/2;
a->pixel[0].x=POS2X(x);
a->pixel[0].y=POS2Y(y);
break;
case A_EXPLODE:
a->block[0].x=x;
a->block[0].y=y;
a->pixel[0].x=POSX(x);
a->pixel[0].y=POSY(y);
break;
default:
a->type=A_NONE;
printf("Unknown type %d in create_moveanim\n",type);
break;
};
}
void animate(int step){
SDL_Rect rect,srect;
int aidx=0;
int x1,y1;
graphic *g=play->g;
a_anim *a=play->a;
for (aidx=0;aidx < MAX_ANIM; aidx++){
switch(a[aidx].type){
case A_NONE:
/* printf("No animation here\n"); */
break;
case A_BALL:
for(x1=a[aidx].block[0].x;x1<=a[aidx].block[1].x;x1++)
for(y1=a[aidx].block[0].y;y1<=a[aidx].block[1].y;y1++)
blank_block(x1,y1);
/* Ball displaying is last, so the ball can fly through another
* anim (only one relevant is A_EXPLODE for now)
*/
break;
case A_DISK:
blank_block(a[aidx].block[0].x,a[aidx].block[0].y);
blank_block(a[aidx].block[1].x,a[aidx].block[1].y);
rect.w=rect.h=QUAD;
rect.x=a[aidx].pixel[0].x+(a[aidx].pixel[1].x*
(step+AFRAMES*a[aidx].duration)/(2*AFRAMES));
rect.y=a[aidx].pixel[0].y+(a[aidx].pixel[1].y*
(step+AFRAMES*a[aidx].duration)/(2*AFRAMES));
SDL_BlitSurface(g->disk[a[aidx].color], NULL, g->display, &rect);
if(step == AFRAMES){ /* Disk Animation is be 2 Frames long */
if(++a[aidx].duration==2){
a[aidx].type=A_NONE;
};
};
break;
case A_DIE:
blank_block(a[aidx].block[0].x,a[aidx].block[0].y);
rect.x=a[aidx].pixel[0].x;
rect.y=a[aidx].pixel[0].y;
srect.y=0;srect.x=(QUAD/2)*((a[aidx].duration*AFRAMES+step-1)>>1);
srect.w=srect.h=QUAD/2;
if(srect.x>g->ballx[a[aidx].color]->w){
a[aidx].type=A_NONE;
}else{
SDL_BlitSurface(g->ballx[a[aidx].color], &srect,
g->display, &rect);
};
if(step == AFRAMES)
a[aidx].duration++;
break;
case A_EXPLODE:
blank_block(a[aidx].block[0].x,a[aidx].block[0].y);
rect.x=a[aidx].pixel[0].x;
rect.y=a[aidx].pixel[0].y;
srect.y=0;srect.x=(QUAD)*((a[aidx].duration*AFRAMES+step-1)>>1);
srect.w=srect.h=QUAD;
if(srect.x>g->blockx[a[aidx].color]->w){
a[aidx].type=A_NONE;
}else{
SDL_BlitSurface(g->blockx[a[aidx].color], &srect,
g->display, &rect);
};
if(step == AFRAMES)
a[aidx].duration++;
break;
default:
printf("Step: %d, idx: %d, ",step,aidx);
printf("Unknown animate %d\n",a[aidx].type);
break;
};
};
if(a->type == A_BALL){
rect.w=rect.h=QUAD/2;
rect.x=a->pixel[0].x+(a->pixel[1].x*step/AFRAMES);
rect.y=a->pixel[0].y+(a->pixel[1].y*step/AFRAMES);
SDL_BlitSurface(g->ball[a->color], NULL, g->display, &rect);
if(step == AFRAMES){ /* Ball Animation is one Frame long */
a->type=A_NONE;
};
};
}