rnd-20040821-2-src
[rocksndiamonds.git] / src / game_em / graphics.c
1 /* 2000-08-13T14:36:17Z
2  *
3  * graphics manipulation crap
4  */
5
6 #include <X11/Xlib.h>
7 #include <X11/Xutil.h>
8
9 #include "global.h"
10 #include "display.h"
11 #include "level.h"
12
13 #include <stdio.h>
14
15
16 #if defined(TARGET_X11)
17
18 unsigned int frame; /* current frame */
19 unsigned int screen_x; /* current scroll position */
20 unsigned int screen_y;
21
22 static unsigned short screentiles[14][22]; /* tiles currently on screen */
23
24 static unsigned int colours[8];
25 static unsigned int colour_anim;
26
27 static void xdebug(char *msg)
28 {
29 #if 0
30   XSync(display, False);
31   printf("EM DEBUG: %s\n", msg);
32 #endif
33 }
34
35 static void colour_shuffle(void)
36 {
37   unsigned int i, j, k;
38
39   for (i = 0; i < 8; i++)
40     colours[i] = i;
41
42   for (i = 0; i < 8; i++)
43   {
44     Random = Random * 129 + 1;
45     j = (Random >> 10) & 7;
46     k = colours[i];
47     colours[i] = colours[j];
48     colours[j] = k;
49   }
50 }
51
52 /* copy the entire screen to the window at the scroll position
53  *
54  * perhaps use mit-shm to speed this up
55  */
56 void blitscreen(void)
57 {
58   unsigned int x = screen_x % (22 * TILEX);
59   unsigned int y = screen_y % (14 * TILEY);
60
61   xdebug("blitscreen");
62
63   if (x < 2 * TILEX && y < 2 * TILEY)
64   {
65     BlitBitmap(screenBitmap, window, x, y,
66                20 * TILEX, 12 * TILEY, SX, SY);
67   }
68   else if (x < 2 * TILEX && y >= 2 * TILEY)
69   {
70     BlitBitmap(screenBitmap, window, x, y,
71                20 * TILEX, 14 * TILEY - y, SX, SY);
72     BlitBitmap(screenBitmap, window, x, 0,
73                20 * TILEX, y - 2 * TILEY, SX, SY + 14 * TILEY - y);
74   }
75   else if (x >= 2 * TILEX && y < 2 * TILEY)
76   {
77     BlitBitmap(screenBitmap, window, x, y,
78                22 * TILEX - x, 12 * TILEY, SX, SY);
79     BlitBitmap(screenBitmap, window, 0, y,
80                x - 2 * TILEX, 12 * TILEY, SX + 22 * TILEX - x, SY);
81   }
82   else
83   {
84     BlitBitmap(screenBitmap, window, x, y,
85                22 * TILEX - x, 14 * TILEY - y, SX, SY);
86     BlitBitmap(screenBitmap, window, 0, y,
87                x - 2 * TILEX, 14 * TILEY - y, SX + 22 * TILEX - x, SY);
88     BlitBitmap(screenBitmap, window, x, 0,
89                22 * TILEX - x, y - 2 * TILEY, SX, SY + 14 * TILEY - y);
90     BlitBitmap(screenBitmap, window, 0, 0,
91                x - 2 * TILEX, y - 2 * TILEY,
92                SX + 22 * TILEX - x, SY + 14 * TILEY - y);
93   }
94
95   BlitBitmap(scoreBitmap, window, 0, 0,
96              20 * TILEX, SCOREY, SX, SY + 12 * TILEY);
97   XFlush(display);
98
99   xdebug("blitscreen - done");
100 }
101
102 /* draw differences between game tiles and screen tiles
103  *
104  * implicitly handles scrolling and restoring background under the sprites
105  *
106  * perhaps use mit-shm to speed this up
107  */
108 static void animscreen(void)
109 {
110   unsigned int x, y, dx, dy;
111   unsigned short obj;
112   unsigned int left = screen_x / TILEX;
113   unsigned int top = screen_y / TILEY;
114
115   xdebug("animscreen");
116
117   for (y = top; y < top + 14; y++)
118   {
119     dy = y % 14;
120     for (x = left; x < left + 22; x++)
121     {
122       dx = x % 22;
123       obj = map_obj[frame][Draw[y][x]];
124
125       if (screentiles[dy][dx] != obj)
126       {
127         screentiles[dy][dx] = obj;
128         BlitBitmap(objBitmap, screenBitmap,
129                    (obj / 512) * TILEX, (obj % 512) * TILEY / 16,
130                    TILEX, TILEY, dx * TILEX, dy * TILEY);
131       }
132     }
133   }
134 }
135
136 /* blit players to the screen
137  *
138  * handles transparency and movement
139  */
140 static void blitplayer(struct PLAYER *ply)
141 {
142   unsigned int x, y, dx, dy;
143   unsigned short obj, spr;
144
145   xdebug("blitplayer");
146
147   if (ply->alive)
148   {
149     x = (frame * ply->oldx + (8 - frame) * ply->x) * TILEX / 8;
150     y = (frame * ply->oldy + (8 - frame) * ply->y) * TILEY / 8;
151     dx = x + TILEX - 1;
152     dy = y + TILEY - 1;
153
154     if ((unsigned int)(dx - screen_x) < (21 * TILEX - 1) &&
155         (unsigned int)(dy - screen_y) < (13 * TILEY - 1))
156     {
157       spr = map_spr[ply->num][frame][ply->anim];
158       x %= 22 * TILEX;
159       y %= 14 * TILEY;
160       dx %= 22 * TILEX;
161       dy %= 14 * TILEY;
162
163       if (objmaskBitmap)
164       {
165         obj = screentiles[y / TILEY][x / TILEX];
166         XCopyArea(display, objmaskBitmap, spriteBitmap, spriteGC,
167                   (obj / 512) * TILEX, (obj % 512) * TILEY / 16, TILEX, TILEY,
168                   -(x % TILEX), -(y % TILEY));
169
170         obj = screentiles[dy / TILEY][dx / TILEX];
171         XCopyArea(display, objmaskBitmap, spriteBitmap, spriteGC,
172                   (obj / 512) * TILEX, (obj % 512) * TILEY / 16, TILEX, TILEY,
173                   (22 * TILEX - x) % TILEX, (14 * TILEY - y) % TILEY);
174       }
175       else if (sprmaskBitmap)
176       {
177         XCopyArea(display, sprmaskBitmap, spriteBitmap, spriteGC,
178                   (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY, 0, 0);
179       }
180       else
181       {
182         XFillRectangle(display, spriteBitmap, spriteGC, 0, 0, TILEX, TILEY);
183       }
184
185       screentiles[y / TILEY][x / TILEX] = -1; /* mark screen as dirty */
186       screentiles[dy / TILEY][dx / TILEX] = -1;
187
188 #if 1
189
190
191 #if 1
192
193       SetClipMask(sprBitmap, sprBitmap->stored_clip_gc, spriteBitmap);
194
195       SetClipOrigin(sprBitmap, sprBitmap->stored_clip_gc, x, y);
196       BlitBitmapMasked(sprBitmap, screenBitmap,
197                        (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
198                        x, y);
199
200       SetClipOrigin(sprBitmap, sprBitmap->stored_clip_gc, x - 22 * TILEX, y);
201       BlitBitmapMasked(sprBitmap, screenBitmap,
202                        (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
203                        x - 22 * TILEX, y);
204
205       SetClipOrigin(sprBitmap, sprBitmap->stored_clip_gc, x, y - 14 * TILEY);
206       BlitBitmapMasked(sprBitmap, screenBitmap,
207                        (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
208                        x, y - 14 * TILEY);
209
210       SetClipMask(sprBitmap, sprBitmap->stored_clip_gc, None);
211
212 #else
213
214       XSetClipMask(display, sprBitmap->stored_clip_gc, spriteBitmap);
215
216       XSetClipOrigin(display, sprBitmap->stored_clip_gc, x, y);
217       XCopyArea(display, sprBitmap->drawable, screenBitmap->drawable,
218                 sprBitmap->stored_clip_gc,
219                 (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
220                 x, y);
221
222       XSetClipOrigin(display, sprBitmap->stored_clip_gc, x - 22 * TILEX, y);
223       XCopyArea(display, sprBitmap->drawable, screenBitmap->drawable,
224                 sprBitmap->stored_clip_gc,
225                 (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
226                 x - 22 * TILEX, y);
227
228       XSetClipOrigin(display, sprBitmap->stored_clip_gc, x, y - 14 * TILEY);
229       XCopyArea(display, sprBitmap->drawable, screenBitmap->drawable,
230                 sprBitmap->stored_clip_gc,
231                 (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
232                 x, y - 14 * TILEY);
233
234       XSetClipMask(display, sprBitmap->stored_clip_gc, None);
235
236 #endif
237
238 #else
239
240       XSetClipMask(display, screenGC, spriteBitmap);
241       XSetClipOrigin(display, screenGC, x, y);
242       XCopyArea(display, sprPixmap, screenPixmap, screenGC,
243                 (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
244                 x, y);
245       XSetClipOrigin(display, screenGC, x - 22 * TILEX, y);
246       XCopyArea(display, sprPixmap, screenPixmap, screenGC,
247                 (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
248                 x - 22 * TILEX, y);
249       XSetClipOrigin(display, screenGC, x, y - 14 * TILEY);
250       XCopyArea(display, sprPixmap, screenPixmap, screenGC,
251                 (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
252                 x, y - 14 * TILEY);
253       XSetClipMask(display, screenGC, None);
254
255 #endif
256     }
257   }
258 }
259
260 void game_initscreen(void)
261 {
262   unsigned int x,y;
263
264   xdebug("game_initscreen");
265
266   frame = 6;
267   screen_x = 0;
268   screen_y = 0;
269
270   for (y = 0; y < 14; y++)
271     for (x = 0; x < 22; x++)
272       screentiles[y][x] = -1;
273
274   colour_shuffle();
275   colours[0] += 16;
276   colours[1] += 16;
277   colours[2] += 16;
278   colour_anim = 0;
279
280   ClearRectangle(scoreBitmap, 0, 0, 20 * TILEX, SCOREY);
281   BlitBitmap(botBitmap, scoreBitmap,
282              11 * SCOREX, colours[0] * SCOREY, 3 * SCOREX, SCOREY,
283              1 * SCOREX, 0);                            /* 0-63 time */
284   BlitBitmap(botBitmap, scoreBitmap,
285              18 * SCOREX, colours[0] * SCOREY, 6 * SCOREX, SCOREY,
286              15 * SCOREX, 0);                           /* 112-207 diamonds */
287   BlitBitmap(botBitmap, scoreBitmap,
288              14 * SCOREX, colours[0] * SCOREY, 4 * SCOREX, SCOREY,
289              32 * SCOREX, 0);                           /* 256-319 score */
290 }
291
292 void game_blitscore(void)
293 {
294   unsigned int i;
295
296   xdebug("game_blitscore");
297
298   i = (lev.time + 4) / 5;
299   BlitBitmap(botBitmap, scoreBitmap,
300              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
301              7 * SCOREX, 0); i /= 10;
302   BlitBitmap(botBitmap, scoreBitmap,
303              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
304              6 * SCOREX, 0); i /= 10;
305   BlitBitmap(botBitmap, scoreBitmap,
306              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
307              5 * SCOREX, 0); i /= 10;
308   BlitBitmap(botBitmap, scoreBitmap,
309              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
310              4 * SCOREX, 0);
311
312   i = lev.score;
313   BlitBitmap(botBitmap, scoreBitmap,
314              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
315              39 * SCOREX, 0); i /= 10;
316   BlitBitmap(botBitmap, scoreBitmap,
317              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
318              38 * SCOREX, 0); i /= 10;
319   BlitBitmap(botBitmap, scoreBitmap,
320              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
321              37 * SCOREX, 0); i /= 10;
322   BlitBitmap(botBitmap, scoreBitmap,
323              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
324              36 * SCOREX, 0);
325
326   if (lev.home == 0)
327   {
328     BlitBitmap(botBitmap, scoreBitmap,
329                12 * SCOREX, 24 * SCOREY, 12 * SCOREX, SCOREY,
330                14 * SCOREX, 0); /* relax */
331
332     goto done;
333   }
334
335   if (ply1.alive + ply2.alive >= lev.home && lev.required == 0)
336   {
337     BlitBitmap(botBitmap, scoreBitmap,
338                24 * SCOREX, colours[2] * SCOREY, 12 * SCOREX, SCOREY,
339                14 * SCOREX, 0); /* find the exit */
340
341     goto done;
342   }
343
344   if (ply1.alive + ply2.alive < lev.home)
345   {
346     if (++colour_anim > 11)
347       colour_anim = 0;
348
349     if (colour_anim < 6)
350     {
351       BlitBitmap(botBitmap, scoreBitmap,
352                  0, 24 * SCOREY, 12 * SCOREX, SCOREY,
353                  14 * SCOREX, 0); /* forget it */
354
355       goto done;
356     }
357
358     BlitBitmap(botBitmap, scoreBitmap,
359                18 * SCOREX, colours[0] * SCOREY, 6 * SCOREX, SCOREY,
360                15 * SCOREX, 0); /* diamonds */
361   }
362
363   i = lev.required;
364   BlitBitmap(botBitmap, scoreBitmap,
365              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
366              24 * SCOREX, 0); i /= 10;
367   BlitBitmap(botBitmap, scoreBitmap,
368              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
369              23 * SCOREX, 0); i /= 10;
370   BlitBitmap(botBitmap, scoreBitmap,
371              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
372              22 * SCOREX, 0); i /= 10;
373   BlitBitmap(botBitmap, scoreBitmap,
374              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
375              21 * SCOREX, 0);
376
377  done:
378 }
379
380 void game_animscreen(void)
381 {
382   unsigned int x,y;
383
384   xdebug("game_animscreen");
385
386   x = (frame * ply1.oldx + (8 - frame) * ply1.x) * TILEX / 8
387     + (19 * TILEX) / 2;
388   y = (frame * ply1.oldy + (8 - frame) * ply1.y) * TILEY / 8
389     + (11 * TILEY) / 2;
390
391   if (x > lev.width * TILEX)
392     x = lev.width * TILEX;
393   if (y > lev.height * TILEY)
394     y = lev.height * TILEY;
395   if (x < 20 * TILEX)
396     x = 20 * TILEY;
397   if (y < 12 * TILEY)
398     y = 12 * TILEY;
399
400   screen_x = x - 19 * TILEX;
401   screen_y = y - 11 * TILEY;
402
403   animscreen();
404   blitplayer(&ply1);
405   blitplayer(&ply2);
406   blitscreen();
407
408   XFlush(display);
409
410   Random = Random * 129 + 1;
411 }
412
413 void title_initscreen(void)
414 {
415   xdebug("title_initscreen");
416
417   screen_x = 0;
418   screen_y = 0;
419
420   colour_shuffle();
421   colours[1] += 8;
422   colour_anim = 0;
423
424 #if 1
425
426   BlitBitmap(ttlBitmap, screenBitmap,
427              0, 0, 20 * TILEX, 12 * TILEY, 0, 0);
428
429   if (botmaskBitmap)
430   {
431     BlitBitmap(botBitmap, scoreBitmap,
432               0, colours[1] * SCOREY, 20 * TILEX, SCOREY, 0, 0);
433
434     SetClipOrigin(botBitmap, botBitmap->stored_clip_gc,
435                   0, 0 - colours[0] * SCOREY);
436   }
437
438   BlitBitmapMasked(botBitmap, scoreBitmap,
439                    0, colours[0] * SCOREY, 20 * TILEX, SCOREY, 0, 0);
440
441 #else
442
443   XCopyArea(display, ttlPixmap, screenPixmap, screenGC,
444             0, 0, 20 * TILEX, 12 * TILEY, 0, 0);
445
446   if (botmaskBitmap)
447   {
448     XCopyArea(display, botPixmap, scorePixmap, scoreGC,
449               0, colours[1] * SCOREY, 20 * TILEX, SCOREY, 0, 0);
450     XSetClipMask(display, scoreGC, botmaskBitmap);
451     XSetClipOrigin(display, scoreGC, 0, 0 - colours[0] * SCOREY);
452   }
453
454   XCopyArea(display, botPixmap, scorePixmap, scoreGC,
455             0, colours[0] * SCOREY, 20 * TILEX, SCOREY, 0, 0);
456
457   if (botmaskBitmap)
458     XSetClipMask(display, scoreGC, None);
459
460 #endif
461 }
462
463 void title_blitscore(void)
464 {
465   unsigned int x, y, i;
466
467   xdebug("title_blitscore");
468
469   if (++colour_anim > 30)
470     colour_anim = 0;
471
472   i = colour_anim >= 16 ? 31 - colour_anim : colour_anim;
473   x = (i / 8 + 18) * 2 * SCOREX;
474   y = (i % 8 + 16) * SCOREY;
475
476 #if 1
477   if (botmaskBitmap)
478   {
479     BlitBitmap(botBitmap, scoreBitmap,
480                32 * SCOREX, colours[1] * SCOREY, 2 * SCOREX, SCOREY,
481                32 * SCOREX, 0);
482
483     SetClipOrigin(botBitmap, botBitmap->stored_clip_gc,
484                   32 * SCOREX - x, 0 - y);
485   }
486
487   BlitBitmapMasked(botBitmap, scoreBitmap,
488                    x, y, 2 * SCOREX, SCOREY, 32 * SCOREX, 0);
489
490 #else
491
492   if (botmaskBitmap)
493   {
494     XCopyArea(display, botPixmap, scorePixmap, scoreGC,
495               32 * SCOREX, colours[1] * SCOREY, 2 * SCOREX, SCOREY,
496               32 * SCOREX, 0);
497     XSetClipMask(display, scoreGC, botmaskBitmap);
498     XSetClipOrigin(display, scoreGC, 32 * SCOREX - x, 0 - y);
499   }
500
501   XCopyArea(display, botPixmap, scorePixmap, scoreGC,
502             x, y, 2 * SCOREX, SCOREY, 32 * SCOREX, 0);
503
504   if (botmaskBitmap)
505     XSetClipMask(display, scoreGC, None);
506 #endif
507 }
508
509 void title_blitants(unsigned int y)
510 {
511   static const char ants_dashes[2] = { 8, 7 };
512
513   xdebug("title_blitants");
514
515   XSetDashes(display, antsGC, colour_anim, ants_dashes, 2);
516   XDrawRectangle(display, screenPixmap, antsGC,
517                  0, y * TILEY, 20 * TILEX - 1, TILEY - 1);
518 }
519
520 void title_animscreen(void)
521 {
522   blitscreen();
523   XFlush(display);
524
525   Random = Random * 129 + 1;
526 }
527
528 static int ttl_map[] =
529 {
530   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
531   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
532   -1,0,-1,-1,-1,-1,-1,1,-1,-1,-1,-1,2,3,4,-1,      /* !',-. */
533   5,6,7,8,9,10,11,12,13,14,15,-1,-1,-1,-1,16,      /* 0123456789:? */
534   -1,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31, /* ABCDEFGHIJKLMNO */
535   32,33,34,35,36,37,38,39,40,41,42,-1,-1,-1,-1,-1, /* PQRSTUVWXYZ */
536   -1,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31, /* abcdefghijklmno */
537   32,33,34,35,36,37,38,39,40,41,42,-1,-1,-1,-1,-1  /* pqrstuvwxyz */
538 };
539
540 void title_string(unsigned int y, unsigned int left, unsigned int right,
541                   char *string)
542 {
543   int i;
544   unsigned int x;
545
546   xdebug("title_string");
547
548   y *= TILEY;
549   left *= SCOREX;
550   right *= SCOREX;
551
552   x = (left + right - strlen(string) * MENUFONTX) / 2;
553   if (x < left || x >= right) x = left;
554
555   /* restore background graphic where text will be drawn */
556   BlitBitmap(ttlBitmap, screenBitmap,
557              left, y, right - left, MENUFONTY, left, y);
558
559 #if 1
560 #else
561   if (ttlmaskBitmap)
562     XSetClipMask(display, screenGC, ttlmaskBitmap);
563 #endif
564
565   for (i = 0; string[i] && x < right; i++)
566   {
567     int ch_pos, ch_x, ch_y;
568
569     ch_pos = ttl_map[string[i] & 127];
570
571     if (ch_pos == -1 || ch_pos > 22 * 2)
572       continue;                         /* no graphic for this character */
573
574     ch_x = (ch_pos % 22) * GFXMENUFONTX;
575     ch_y = (ch_pos / 22 + 12) * TILEY;
576
577 #if 1
578     SetClipOrigin(ttlBitmap, ttlBitmap->stored_clip_gc,
579                   x - ch_x, y - ch_y);
580
581     BlitBitmapMasked(ttlBitmap, screenBitmap,
582                      ch_x, ch_y, MENUFONTX, MENUFONTY, x, y);
583 #else
584     if (ttlmaskBitmap)
585       XSetClipOrigin(display, screenGC, x - ch_x, y - ch_y);
586
587     XCopyArea(display, ttlPixmap, screenPixmap, screenGC,
588               ch_x, ch_y, MENUFONTX, MENUFONTY, x, y);
589 #endif
590
591     x += MENUFONTX;
592   }
593
594 #if 1
595 #else
596   XSetClipMask(display, screenGC, None);
597 #endif
598 }
599
600 #endif