rnd-20041129-1-src
[rocksndiamonds.git] / src / game_em / graphics.c
1 /* 2000-08-13T14:36:17Z
2  *
3  * graphics manipulation crap
4  */
5
6 #include "global.h"
7 #include "display.h"
8 #include "level.h"
9
10
11 unsigned int frame;             /* current screen frame */
12 unsigned int screen_x;          /* current scroll position */
13 unsigned int screen_y;
14
15 /* tiles currently on screen */
16 static unsigned int screentiles[MAX_BUF_YSIZE][MAX_BUF_XSIZE];
17 static unsigned int crumbled_state[MAX_BUF_YSIZE][MAX_BUF_XSIZE];
18
19
20 /* copy the entire screen to the window at the scroll position
21  *
22  * perhaps use mit-shm to speed this up
23  */
24
25 void blitscreen(void)
26 {
27   unsigned int x = screen_x % (MAX_BUF_XSIZE * TILEX);
28   unsigned int y = screen_y % (MAX_BUF_YSIZE * TILEY);
29
30   if (x < 2 * TILEX && y < 2 * TILEY)
31   {
32     BlitBitmap(screenBitmap, window, x, y,
33                SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
34   }
35   else if (x < 2 * TILEX && y >= 2 * TILEY)
36   {
37     BlitBitmap(screenBitmap, window, x, y,
38                SCR_FIELDX * TILEX, MAX_BUF_YSIZE * TILEY - y,
39                SX, SY);
40     BlitBitmap(screenBitmap, window, x, 0,
41                SCR_FIELDX * TILEX, y - 2 * TILEY,
42                SX, SY + MAX_BUF_YSIZE * TILEY - y);
43   }
44   else if (x >= 2 * TILEX && y < 2 * TILEY)
45   {
46     BlitBitmap(screenBitmap, window, x, y,
47                MAX_BUF_XSIZE * TILEX - x, SCR_FIELDY * TILEY,
48                SX, SY);
49     BlitBitmap(screenBitmap, window, 0, y,
50                x - 2 * TILEX, SCR_FIELDY * TILEY,
51                SX + MAX_BUF_XSIZE * TILEX - x, SY);
52   }
53   else
54   {
55     BlitBitmap(screenBitmap, window, x, y,
56                MAX_BUF_XSIZE * TILEX - x, MAX_BUF_YSIZE * TILEY - y,
57                SX, SY);
58     BlitBitmap(screenBitmap, window, 0, y,
59                x - 2 * TILEX, MAX_BUF_YSIZE * TILEY - y,
60                SX + MAX_BUF_XSIZE * TILEX - x, SY);
61     BlitBitmap(screenBitmap, window, x, 0,
62                MAX_BUF_XSIZE * TILEX - x, y - 2 * TILEY,
63                SX, SY + MAX_BUF_YSIZE * TILEY - y);
64     BlitBitmap(screenBitmap, window, 0, 0,
65                x - 2 * TILEX, y - 2 * TILEY,
66                SX + MAX_BUF_XSIZE * TILEX - x, SY + MAX_BUF_YSIZE * TILEY - y);
67   }
68 }
69
70
71 /* draw differences between game tiles and screen tiles
72  *
73  * implicitly handles scrolling and restoring background under the sprites
74  *
75  * perhaps use mit-shm to speed this up
76  */
77
78 static void animscreen(void)
79 {
80   unsigned int x, y, i;
81   unsigned int left = screen_x / TILEX;
82   unsigned int top = screen_y / TILEY;
83   static int xy[4][2] =
84   {
85     { 0, -1 },
86     { -1, 0 },
87     { +1, 0 },
88     { 0, +1 }
89   };
90
91   for (y = top; y < top + MAX_BUF_YSIZE; y++)
92   {
93     for (x = left; x < left + MAX_BUF_XSIZE; x++)
94     {
95       int dx = x % MAX_BUF_XSIZE;
96       int dy = y % MAX_BUF_YSIZE;    
97       int tile = Draw[y][x];
98       struct GraphicInfo_EM *g = &graphic_info_em_object[tile][frame];
99       unsigned int obj;
100       unsigned int crm = 0;
101
102 #if 1
103
104 #if 1
105       /* re-calculate crumbled state of this tile */
106       if (g->has_crumbled_graphics)
107       {
108         for (i = 0; i < 4; i++)
109         {
110           int xx = x + xy[i][0];
111           int yy = y + xy[i][1];
112           int tile_next;
113
114           if (xx < 0 || xx >= EM_MAX_CAVE_WIDTH ||
115               yy < 0 || yy >= EM_MAX_CAVE_HEIGHT)
116             continue;
117
118           tile_next = Draw[yy][xx];
119
120           if (!graphic_info_em_object[tile_next][frame].has_crumbled_graphics)
121             crm |= (1 << i);
122         }
123       }
124 #else
125       /* re-calculate crumbled state of this tile */
126       if (tile == Xgrass ||
127           tile == Xdirt ||
128           tile == Xfake_grass ||
129           tile == Xfake_grassB)
130       {
131         for (i = 0; i < 4; i++)
132         {
133           int xx = x + xy[i][0];
134           int yy = y + xy[i][1];
135           int tile2;
136
137           if (xx < 0 || xx >= EM_MAX_CAVE_WIDTH ||
138               yy < 0 || yy >= EM_MAX_CAVE_HEIGHT)
139             continue;
140
141           tile2 = Draw[yy][xx];
142
143           if (tile2 == Xgrass ||
144               tile2 == Xdirt ||
145               tile2 == Xfake_grass ||
146               tile2 == Xfake_grassB ||
147               tile2 == Ygrass_nB ||
148               tile2 == Ygrass_eB ||
149               tile2 == Ygrass_sB ||
150               tile2 == Ygrass_wB ||
151               tile2 == Ydirt_nB ||
152               tile2 == Ydirt_eB ||
153               tile2 == Ydirt_sB ||
154               tile2 == Ydirt_wB)
155             continue;
156
157           crm |= (1 << i);
158         }
159       }
160 #endif
161
162       /* create unique graphic identifier to decide if tile must be redrawn */
163       obj = g->unique_identifier;
164
165       if (screentiles[dy][dx] != obj || crumbled_state[dy][dx] != crm)
166       {
167         int dst_x = dx * TILEX;
168         int dst_y = dy * TILEY;
169
170         if (g->width != TILEX || g->height != TILEY)
171           ClearRectangle(screenBitmap, dst_x, dst_y, TILEX, TILEY);
172
173         if (g->width > 0 && g->height > 0)
174           BlitBitmap(g->bitmap, screenBitmap,
175                      g->src_x + g->src_offset_x, g->src_y + g->src_offset_y,
176                      g->width, g->height,
177                      dst_x + g->dst_offset_x, dst_y + g->dst_offset_y);
178
179 #if 1
180         /* add crumbling graphic, if needed */
181         if (crm)
182         {
183           for (i = 0; i < 4; i++)
184           {
185             if (crm & (1 << i))
186             {
187               int width, height, cx, cy;
188
189               if (i == 1 || i == 2)
190               {
191                 width = g->crumbled_border_size;
192                 height = TILEY;
193                 cx = (i == 2 ? TILEX - g->crumbled_border_size : 0);
194                 cy = 0;
195               }
196               else
197               {
198                 width = TILEX;
199                 height = g->crumbled_border_size;
200                 cx = 0;
201                 cy = (i == 3 ? TILEY - g->crumbled_border_size : 0);
202               }
203
204               if (width > 0 && height > 0)
205                 BlitBitmap(g->crumbled_bitmap, screenBitmap,
206                            g->crumbled_src_x + cx, g->crumbled_src_y + cy,
207                            width, height, dst_x + cx, dst_y + cy);
208             }
209           }
210         }
211 #else
212         /* add crumbling graphic, if needed */
213         if (crm)
214         {
215           int crumbled_border_size;
216
217           tile = (tile == Xgrass ? Ygrass_crumbled :
218                   tile == Xdirt ? Ydirt_crumbled :
219                   tile == Xfake_grass ? Yfake_grass_crumbled :
220                   tile == Xfake_grassB ? Yfake_grassB_crumbled : 0);
221           g = &graphic_info_em_object[tile][frame];
222           crumbled_border_size = g->border_size;
223
224           for (i = 0; i < 4; i++)
225           {
226             if (crm & (1 << i))
227             {
228               int width, height, cx, cy;
229
230               if (i == 1 || i == 2)
231               {
232                 width = crumbled_border_size;
233                 height = TILEY;
234                 cx = (i == 2 ? TILEX - crumbled_border_size : 0);
235                 cy = 0;
236               }
237               else
238               {
239                 width = TILEX;
240                 height = crumbled_border_size;
241                 cx = 0;
242                 cy = (i == 3 ? TILEY - crumbled_border_size : 0);
243               }
244
245               if (width > 0 && height > 0)
246                 BlitBitmap(g->bitmap, screenBitmap,
247                            g->src_x + cx, g->src_y + cy, width, height,
248                            dst_x + cx, dst_y + cy);
249             }
250           }
251         }
252 #endif
253
254         screentiles[dy][dx] = obj;
255         crumbled_state[dy][dx] = crm;
256       }
257 #else
258       obj = map_obj[frame][tile];
259
260       if (screentiles[dy][dx] != obj)
261       {
262         BlitBitmap(objBitmap, screenBitmap,
263                    (obj / 512) * TILEX, (obj % 512) * TILEY / 16,
264                    TILEX, TILEY, dx * TILEX, dy * TILEY);
265
266         screentiles[dy][dx] = obj;
267       }
268 #endif
269     }
270   }
271 }
272
273
274 /* blit players to the screen
275  *
276  * handles transparency and movement
277  */
278
279 static void blitplayer(struct PLAYER *ply)
280 {
281   int dx, dy;
282   int old_x, old_y, new_x, new_y;
283   int src_x, src_y, dst_x, dst_y;
284   unsigned int x1, y1, x2, y2;
285 #if 1
286   unsigned short spr;
287 #else
288   unsigned short obj, spr;
289 #endif
290
291   if (!ply->alive)
292     return;
293
294   /* some casts to "int" are needed because of negative calculation values */
295   dx = (int)ply->x - (int)ply->oldx;
296   dy = (int)ply->y - (int)ply->oldy;
297   old_x = (int)ply->oldx + (7 - (int)frame) * dx / 8;
298   old_y = (int)ply->oldy + (7 - (int)frame) * dy / 8;
299   new_x = old_x + SIGN(dx);
300   new_y = old_y + SIGN(dy);
301
302   /* x1/y1 are left/top and x2/y2 are right/down part of the player movement */
303   x1 = (frame * ply->oldx + (8 - frame) * ply->x) * TILEX / 8;
304   y1 = (frame * ply->oldy + (8 - frame) * ply->y) * TILEY / 8;
305   x2 = x1 + TILEX - 1;
306   y2 = y1 + TILEY - 1;
307
308   if ((unsigned int)(x2 - screen_x) < ((MAX_BUF_XSIZE - 1) * TILEX - 1) &&
309       (unsigned int)(y2 - screen_y) < ((MAX_BUF_YSIZE - 1) * TILEY - 1))
310   {
311     struct GraphicInfo_EM *g;
312     int tile;
313
314     spr = map_spr[ply->num][frame][ply->anim];
315     x1 %= MAX_BUF_XSIZE * TILEX;
316     y1 %= MAX_BUF_YSIZE * TILEY;
317     x2 %= MAX_BUF_XSIZE * TILEX;
318     y2 %= MAX_BUF_YSIZE * TILEY;
319
320 #if 1
321
322 #if 1
323     g = &graphic_info_em_player[ply->num][ply->anim][frame];
324
325     /* draw the player to current location */
326     BlitBitmap(g->bitmap, screenBitmap,
327                g->src_x, g->src_y, TILEX, TILEY,
328                x1, y1);
329
330     /* draw the player to opposite wrap-around column */
331     BlitBitmap(g->bitmap, screenBitmap,
332                g->src_x, g->src_y, TILEX, TILEY,
333                x1 - MAX_BUF_XSIZE * TILEX, y1);
334
335     /* draw the player to opposite wrap-around row */
336     BlitBitmap(g->bitmap, screenBitmap,
337                g->src_x, g->src_y, TILEX, TILEY,
338                x1, y1 - MAX_BUF_YSIZE * TILEY);
339 #else
340     /* draw the player to current location */
341     BlitBitmap(sprBitmap, screenBitmap,
342                (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
343                x1, y1);
344
345     /* draw the player to opposite wrap-around column */
346     BlitBitmap(sprBitmap, screenBitmap,
347                (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
348                x1 - MAX_BUF_XSIZE * TILEX, y1);
349
350     /* draw the player to opposite wrap-around row */
351     BlitBitmap(sprBitmap, screenBitmap,
352                (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
353                x1, y1 - MAX_BUF_YSIZE * TILEY);
354 #endif
355
356     /* draw the field the player is moving from (masked over the player) */
357 #if 0
358     obj = screentiles[y1 / TILEY][x1 / TILEX];
359     src_x = (obj / 512) * TILEX;
360     src_y = (obj % 512) * TILEY / 16;
361     dst_x = (x1 / TILEX) * TILEX;
362     dst_y = (y1 / TILEY) * TILEY;
363 #endif
364
365 #if 1
366     tile = Draw[old_y][old_x];
367     g = &graphic_info_em_object[tile][frame];
368
369     if (g->width > 0 && g->height > 0)
370     {
371       src_x = g->src_x + g->src_offset_x;
372       src_y = g->src_y + g->src_offset_y;
373       dst_x = old_x % MAX_BUF_XSIZE * TILEX + g->dst_offset_x;
374       dst_y = old_y % MAX_BUF_YSIZE * TILEY + g->dst_offset_y;
375
376       SetClipOrigin(g->bitmap, g->bitmap->stored_clip_gc,
377                     dst_x - src_x, dst_y - src_y);
378       BlitBitmapMasked(g->bitmap, screenBitmap,
379                        src_x, src_y, g->width, g->height, dst_x, dst_y);
380     }
381 #else
382     SetClipOrigin(objBitmap, objBitmap->stored_clip_gc,
383                   dst_x - src_x, dst_y - src_y);
384     BlitBitmapMasked(objBitmap, screenBitmap,
385                      src_x, src_y, TILEX, TILEY, dst_x, dst_y);
386 #endif
387
388     /* draw the field the player is moving to (masked over the player) */
389 #if 0
390     obj = screentiles[y2 / TILEY][x2 / TILEX];
391     src_x = (obj / 512) * TILEX;
392     src_y = (obj % 512) * TILEY / 16;
393     dst_x = (x2 / TILEX) * TILEX;
394     dst_y = (y2 / TILEY) * TILEY;
395 #endif
396
397 #if 1
398     tile = Draw[new_y][new_x];
399     g = &graphic_info_em_object[tile][frame];
400
401     if (g->width > 0 && g->height > 0)
402     {
403       src_x = g->src_x + g->src_offset_x;
404       src_y = g->src_y + g->src_offset_y;
405       dst_x = new_x % MAX_BUF_XSIZE * TILEX + g->dst_offset_x;
406       dst_y = new_y % MAX_BUF_YSIZE * TILEY + g->dst_offset_y;
407
408       SetClipOrigin(g->bitmap, g->bitmap->stored_clip_gc,
409                     dst_x - src_x, dst_y - src_y);
410       BlitBitmapMasked(g->bitmap, screenBitmap,
411                        src_x, src_y, g->width, g->height, dst_x, dst_y);
412     }
413 #else
414     SetClipOrigin(objBitmap, objBitmap->stored_clip_gc,
415                   dst_x - src_x, dst_y - src_y);
416     BlitBitmapMasked(objBitmap, screenBitmap,
417                      src_x, src_y, TILEX, TILEY, dst_x, dst_y);
418 #endif
419
420 #else
421
422     if (objmaskBitmap)
423     {
424       obj = screentiles[y1 / TILEY][x1 / TILEX];
425       XCopyArea(display, objmaskBitmap, spriteBitmap, spriteGC,
426                 (obj / 512) * TILEX, (obj % 512) * TILEY / 16, TILEX, TILEY,
427                 -(x1 % TILEX), -(y1 % TILEY));
428
429       obj = screentiles[y2 / TILEY][x2 / TILEX];
430       XCopyArea(display, objmaskBitmap, spriteBitmap, spriteGC,
431                 (obj / 512) * TILEX, (obj % 512) * TILEY / 16, TILEX, TILEY,
432                 (MAX_BUF_XSIZE * TILEX - x1) % TILEX,
433                 (MAX_BUF_YSIZE * TILEY - y1) % TILEY);
434     }
435     else if (sprmaskBitmap)
436     {
437       XCopyArea(display, sprmaskBitmap, spriteBitmap, spriteGC,
438                 (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY, 0, 0);
439     }
440     else
441     {
442       XFillRectangle(display, spriteBitmap, spriteGC, 0, 0, TILEX, TILEY);
443     }
444
445     SetClipMask(sprBitmap, sprBitmap->stored_clip_gc, spriteBitmap);
446
447     SetClipOrigin(sprBitmap, sprBitmap->stored_clip_gc, x, y);
448     BlitBitmapMasked(sprBitmap, screenBitmap,
449                      (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
450                      x1, y1);
451
452     SetClipOrigin(sprBitmap, sprBitmap->stored_clip_gc,
453                   x - MAX_BUF_XSIZE * TILEX, y);
454     BlitBitmapMasked(sprBitmap, screenBitmap,
455                      (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
456                      x1 - MAX_BUF_XSIZE * TILEX, y1);
457
458     SetClipOrigin(sprBitmap, sprBitmap->stored_clip_gc,
459                   x1, y1 - MAX_BUF_YSIZE * TILEY);
460     BlitBitmapMasked(sprBitmap, screenBitmap,
461                      (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
462                      x1, y1 - MAX_BUF_YSIZE * TILEY);
463
464     SetClipMask(sprBitmap, sprBitmap->stored_clip_gc, None);
465 #endif
466
467     /* mark screen tiles as dirty */
468     screentiles[y1 / TILEY][x1 / TILEX] = -1;
469     screentiles[y2 / TILEY][x2 / TILEX] = -1;
470   }
471 }
472
473 void game_initscreen(void)
474 {
475   unsigned int x,y;
476
477   frame = 6;
478   screen_x = 0;
479   screen_y = 0;
480
481   for (y = 0; y < MAX_BUF_YSIZE; y++)
482   {
483     for (x = 0; x < MAX_BUF_XSIZE; x++)
484     {
485       screentiles[y][x] = -1;
486       crumbled_state[y][x] = 0;
487     }
488   }
489
490   DrawGameDoorValues_EM(lev.required, ply1.dynamite, lev.score,
491                         DISPLAY_TIME(lev.time + 4));
492 }
493
494 void game_animscreen(void)
495 {
496   unsigned int x,y;
497
498   x = (frame * ply1.oldx + (8 - frame) * ply1.x) * TILEX / 8
499     + ((SCR_FIELDX - 1) * TILEX) / 2;
500   y = (frame * ply1.oldy + (8 - frame) * ply1.y) * TILEY / 8
501     + ((SCR_FIELDY - 1) * TILEY) / 2;
502
503   if (x > lev.width * TILEX)
504     x = lev.width * TILEX;
505   if (y > lev.height * TILEY)
506     y = lev.height * TILEY;
507
508   if (x < SCR_FIELDX * TILEX)
509     x = SCR_FIELDX * TILEY;
510   if (y < SCR_FIELDY * TILEY)
511     y = SCR_FIELDY * TILEY;
512
513   screen_x = x - (SCR_FIELDX - 1) * TILEX;
514   screen_y = y - (SCR_FIELDY - 1) * TILEY;
515
516   animscreen();
517   blitplayer(&ply1);
518   blitplayer(&ply2);
519   blitscreen();
520
521   FlushDisplay();
522 }