rnd-20040821-1-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       XSetClipMask(display, screenGC, spriteBitmap);
189       XSetClipOrigin(display, screenGC, x, y);
190       XCopyArea(display, sprPixmap, screenPixmap, screenGC,
191                 (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
192                 x, y);
193       XSetClipOrigin(display, screenGC, x - 22 * TILEX, y);
194       XCopyArea(display, sprPixmap, screenPixmap, screenGC,
195                 (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
196                 x - 22 * TILEX, y);
197       XSetClipOrigin(display, screenGC, x, y - 14 * TILEY);
198       XCopyArea(display, sprPixmap, screenPixmap, screenGC,
199                 (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
200                 x, y - 14 * TILEY);
201       XSetClipMask(display, screenGC, None);
202     }
203   }
204 }
205
206 void game_initscreen(void)
207 {
208   unsigned int x,y;
209
210   xdebug("game_initscreen");
211
212   frame = 6;
213   screen_x = 0;
214   screen_y = 0;
215
216   for (y = 0; y < 14; y++)
217     for (x = 0; x < 22; x++)
218       screentiles[y][x] = -1;
219
220   colour_shuffle();
221   colours[0] += 16;
222   colours[1] += 16;
223   colours[2] += 16;
224   colour_anim = 0;
225
226   ClearRectangle(scoreBitmap, 0, 0, 20 * TILEX, SCOREY);
227   BlitBitmap(botBitmap, scoreBitmap,
228              11 * SCOREX, colours[0] * SCOREY, 3 * SCOREX, SCOREY,
229              1 * SCOREX, 0);                            /* 0-63 time */
230   BlitBitmap(botBitmap, scoreBitmap,
231              18 * SCOREX, colours[0] * SCOREY, 6 * SCOREX, SCOREY,
232              15 * SCOREX, 0);                           /* 112-207 diamonds */
233   BlitBitmap(botBitmap, scoreBitmap,
234              14 * SCOREX, colours[0] * SCOREY, 4 * SCOREX, SCOREY,
235              32 * SCOREX, 0);                           /* 256-319 score */
236 }
237
238 void game_blitscore(void)
239 {
240   unsigned int i;
241
242   xdebug("game_blitscore");
243
244   i = (lev.time + 4) / 5;
245   BlitBitmap(botBitmap, scoreBitmap,
246              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
247              7 * SCOREX, 0); i /= 10;
248   BlitBitmap(botBitmap, scoreBitmap,
249              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
250              6 * SCOREX, 0); i /= 10;
251   BlitBitmap(botBitmap, scoreBitmap,
252              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
253              5 * SCOREX, 0); i /= 10;
254   BlitBitmap(botBitmap, scoreBitmap,
255              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
256              4 * SCOREX, 0);
257
258   i = lev.score;
259   BlitBitmap(botBitmap, scoreBitmap,
260              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
261              39 * SCOREX, 0); i /= 10;
262   BlitBitmap(botBitmap, scoreBitmap,
263              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
264              38 * SCOREX, 0); i /= 10;
265   BlitBitmap(botBitmap, scoreBitmap,
266              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
267              37 * SCOREX, 0); i /= 10;
268   BlitBitmap(botBitmap, scoreBitmap,
269              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
270              36 * SCOREX, 0);
271
272   if (lev.home == 0)
273   {
274     BlitBitmap(botBitmap, scoreBitmap,
275                12 * SCOREX, 24 * SCOREY, 12 * SCOREX, SCOREY,
276                14 * SCOREX, 0); /* relax */
277
278     goto done;
279   }
280
281   if (ply1.alive + ply2.alive >= lev.home && lev.required == 0)
282   {
283     BlitBitmap(botBitmap, scoreBitmap,
284                24 * SCOREX, colours[2] * SCOREY, 12 * SCOREX, SCOREY,
285                14 * SCOREX, 0); /* find the exit */
286
287     goto done;
288   }
289
290   if (ply1.alive + ply2.alive < lev.home)
291   {
292     if (++colour_anim > 11)
293       colour_anim = 0;
294
295     if (colour_anim < 6)
296     {
297       BlitBitmap(botBitmap, scoreBitmap,
298                  0, 24 * SCOREY, 12 * SCOREX, SCOREY,
299                  14 * SCOREX, 0); /* forget it */
300
301       goto done;
302     }
303
304     BlitBitmap(botBitmap, scoreBitmap,
305                18 * SCOREX, colours[0] * SCOREY, 6 * SCOREX, SCOREY,
306                15 * SCOREX, 0); /* diamonds */
307   }
308
309   i = lev.required;
310   BlitBitmap(botBitmap, scoreBitmap,
311              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
312              24 * SCOREX, 0); i /= 10;
313   BlitBitmap(botBitmap, scoreBitmap,
314              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
315              23 * SCOREX, 0); i /= 10;
316   BlitBitmap(botBitmap, scoreBitmap,
317              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
318              22 * SCOREX, 0); i /= 10;
319   BlitBitmap(botBitmap, scoreBitmap,
320              (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY,
321              21 * SCOREX, 0);
322
323  done:
324 }
325
326 void game_animscreen(void)
327 {
328   unsigned int x,y;
329
330   xdebug("game_animscreen");
331
332   x = (frame * ply1.oldx + (8 - frame) * ply1.x) * TILEX / 8
333     + (19 * TILEX) / 2;
334   y = (frame * ply1.oldy + (8 - frame) * ply1.y) * TILEY / 8
335     + (11 * TILEY) / 2;
336
337   if (x > lev.width * TILEX)
338     x = lev.width * TILEX;
339   if (y > lev.height * TILEY)
340     y = lev.height * TILEY;
341   if (x < 20 * TILEX)
342     x = 20 * TILEY;
343   if (y < 12 * TILEY)
344     y = 12 * TILEY;
345
346   screen_x = x - 19 * TILEX;
347   screen_y = y - 11 * TILEY;
348
349   animscreen();
350   blitplayer(&ply1);
351   blitplayer(&ply2);
352   blitscreen();
353
354   XFlush(display);
355
356   Random = Random * 129 + 1;
357 }
358
359 void title_initscreen(void)
360 {
361   xdebug("title_initscreen");
362
363   screen_x = 0;
364   screen_y = 0;
365
366   colour_shuffle();
367   colours[1] += 8;
368   colour_anim = 0;
369
370   XCopyArea(display, ttlPixmap, screenPixmap, screenGC,
371             0, 0, 20 * TILEX, 12 * TILEY, 0, 0);
372
373   if (botmaskBitmap)
374   {
375     XCopyArea(display, botPixmap, scorePixmap, scoreGC,
376               0, colours[1] * SCOREY, 20 * TILEX, SCOREY, 0, 0);
377     XSetClipMask(display, scoreGC, botmaskBitmap);
378     XSetClipOrigin(display, scoreGC, 0, 0 - colours[0] * SCOREY);
379   }
380
381   XCopyArea(display, botPixmap, scorePixmap, scoreGC,
382             0, colours[0] * SCOREY, 20 * TILEX, SCOREY, 0, 0);
383
384   if (botmaskBitmap)
385     XSetClipMask(display, scoreGC, None);
386 }
387
388 void title_blitscore(void)
389 {
390   unsigned int x, y, i;
391
392   xdebug("title_blitscore");
393
394   if (++colour_anim > 30)
395     colour_anim = 0;
396
397   i = colour_anim >= 16 ? 31 - colour_anim : colour_anim;
398   x = (i / 8 + 18) * 2 * SCOREX;
399   y = (i % 8 + 16) * SCOREY;
400
401   if (botmaskBitmap)
402   {
403     XCopyArea(display, botPixmap, scorePixmap, scoreGC,
404               32 * SCOREX, colours[1] * SCOREY, 2 * SCOREX, SCOREY,
405               32 * SCOREX, 0);
406     XSetClipMask(display, scoreGC, botmaskBitmap);
407     XSetClipOrigin(display, scoreGC, 32 * SCOREX - x, 0 - y);
408   }
409
410   XCopyArea(display, botPixmap, scorePixmap, scoreGC,
411             x, y, 2 * SCOREX, SCOREY, 32 * SCOREX, 0);
412
413   if (botmaskBitmap)
414     XSetClipMask(display, scoreGC, None);
415 }
416
417 void title_blitants(unsigned int y)
418 {
419   static const char ants_dashes[2] = { 8, 7 };
420
421   xdebug("title_blitants");
422
423   XSetDashes(display, antsGC, colour_anim, ants_dashes, 2);
424   XDrawRectangle(display, screenPixmap, antsGC,
425                  0, y * TILEY, 20 * TILEX - 1, TILEY - 1);
426 }
427
428 void title_animscreen(void)
429 {
430   blitscreen();
431   XFlush(display);
432
433   Random = Random * 129 + 1;
434 }
435
436 static int ttl_map[] =
437 {
438   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
439   -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
440   -1,0,-1,-1,-1,-1,-1,1,-1,-1,-1,-1,2,3,4,-1,      /* !',-. */
441   5,6,7,8,9,10,11,12,13,14,15,-1,-1,-1,-1,16,      /* 0123456789:? */
442   -1,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31, /* ABCDEFGHIJKLMNO */
443   32,33,34,35,36,37,38,39,40,41,42,-1,-1,-1,-1,-1, /* PQRSTUVWXYZ */
444   -1,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31, /* abcdefghijklmno */
445   32,33,34,35,36,37,38,39,40,41,42,-1,-1,-1,-1,-1  /* pqrstuvwxyz */
446 };
447
448 void title_string(unsigned int y, unsigned int left, unsigned int right,
449                   char *string)
450 {
451   int i;
452   unsigned int x;
453
454   xdebug("title_string");
455
456   y *= TILEY;
457   left *= SCOREX;
458   right *= SCOREX;
459
460   x = (left + right - strlen(string) * MENUFONTX) / 2;
461   if (x < left || x >= right) x = left;
462
463   /* restore background graphic where text will be drawn */
464   BlitBitmap(ttlBitmap, screenBitmap,
465              left, y, right - left, MENUFONTY, left, y);
466
467 #if 1
468 #else
469   if (ttlmaskBitmap)
470     XSetClipMask(display, screenGC, ttlmaskBitmap);
471 #endif
472
473   for (i = 0; string[i] && x < right; i++)
474   {
475     int ch_pos, ch_x, ch_y;
476
477     ch_pos = ttl_map[string[i] & 127];
478
479     if (ch_pos == -1 || ch_pos > 22 * 2)
480       continue;                         /* no graphic for this character */
481
482     ch_x = (ch_pos % 22) * GFXMENUFONTX;
483     ch_y = (ch_pos / 22 + 12) * TILEY;
484
485 #if 1
486     SetClipOrigin(ttlBitmap, ttlBitmap->stored_clip_gc,
487                   x - ch_x, y - ch_y);
488
489     BlitBitmapMasked(ttlBitmap, screenBitmap,
490                      ch_x, ch_y, MENUFONTX, MENUFONTY, x, y);
491 #else
492     if (ttlmaskBitmap)
493       XSetClipOrigin(display, screenGC, x - ch_x, y - ch_y);
494
495     XCopyArea(display, ttlPixmap, screenPixmap, screenGC,
496               ch_x, ch_y, MENUFONTX, MENUFONTY, x, y);
497 #endif
498
499     x += MENUFONTX;
500   }
501
502 #if 1
503 #else
504   XSetClipMask(display, screenGC, None);
505 #endif
506 }
507
508 #endif