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