rnd-20041125-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 short screentiles[MAX_BUF_YSIZE][MAX_BUF_XSIZE];
17
18
19 /* copy the entire screen to the window at the scroll position
20  *
21  * perhaps use mit-shm to speed this up
22  */
23
24 void blitscreen(void)
25 {
26   unsigned int x = screen_x % (MAX_BUF_XSIZE * TILEX);
27   unsigned int y = screen_y % (MAX_BUF_YSIZE * TILEY);
28
29   if (x < 2 * TILEX && y < 2 * TILEY)
30   {
31     BlitBitmap(screenBitmap, window, x, y,
32                SCR_FIELDX * TILEX, SCR_FIELDY * TILEY, SX, SY);
33   }
34   else if (x < 2 * TILEX && y >= 2 * TILEY)
35   {
36     BlitBitmap(screenBitmap, window, x, y,
37                SCR_FIELDX * TILEX, MAX_BUF_YSIZE * TILEY - y,
38                SX, SY);
39     BlitBitmap(screenBitmap, window, x, 0,
40                SCR_FIELDX * TILEX, y - 2 * TILEY,
41                SX, SY + MAX_BUF_YSIZE * TILEY - y);
42   }
43   else if (x >= 2 * TILEX && y < 2 * TILEY)
44   {
45     BlitBitmap(screenBitmap, window, x, y,
46                MAX_BUF_XSIZE * TILEX - x, SCR_FIELDY * TILEY,
47                SX, SY);
48     BlitBitmap(screenBitmap, window, 0, y,
49                x - 2 * TILEX, SCR_FIELDY * TILEY,
50                SX + MAX_BUF_XSIZE * TILEX - x, SY);
51   }
52   else
53   {
54     BlitBitmap(screenBitmap, window, x, y,
55                MAX_BUF_XSIZE * TILEX - x, MAX_BUF_YSIZE * TILEY - y,
56                SX, SY);
57     BlitBitmap(screenBitmap, window, 0, y,
58                x - 2 * TILEX, MAX_BUF_YSIZE * TILEY - y,
59                SX + MAX_BUF_XSIZE * TILEX - x, SY);
60     BlitBitmap(screenBitmap, window, x, 0,
61                MAX_BUF_XSIZE * TILEX - x, y - 2 * TILEY,
62                SX, SY + MAX_BUF_YSIZE * TILEY - y);
63     BlitBitmap(screenBitmap, window, 0, 0,
64                x - 2 * TILEX, y - 2 * TILEY,
65                SX + MAX_BUF_XSIZE * TILEX - x, SY + MAX_BUF_YSIZE * TILEY - y);
66   }
67 }
68
69
70 /* draw differences between game tiles and screen tiles
71  *
72  * implicitly handles scrolling and restoring background under the sprites
73  *
74  * perhaps use mit-shm to speed this up
75  */
76
77 static void animscreen(void)
78 {
79   unsigned int x, y, dx, dy;
80   unsigned short obj;
81   unsigned int left = screen_x / TILEX;
82   unsigned int top = screen_y / TILEY;
83
84   for (y = top; y < top + MAX_BUF_YSIZE; y++)
85   {
86     dy = y % MAX_BUF_YSIZE;
87
88     for (x = left; x < left + MAX_BUF_XSIZE; x++)
89     {
90       int tile = Draw[y][x];
91
92       dx = x % MAX_BUF_XSIZE;
93       obj = map_obj[frame][tile];
94
95       if (screentiles[dy][dx] != obj)
96       {
97 #if 1
98         struct GraphicInfo_EM *g = &graphic_info_em[tile][frame];
99         int dst_x = dx * TILEX;
100         int dst_y = dy * TILEY;
101
102         if (g->width != TILEX || g->height != TILEY)
103           ClearRectangle(screenBitmap, dst_x, dst_y, TILEX, TILEY);
104
105         if (g->width > 0 && g->height > 0)
106           BlitBitmap(g->bitmap, screenBitmap,
107                      g->src_x + g->src_offset_x, g->src_y + g->src_offset_y,
108                      g->width, g->height,
109                      dst_x + g->dst_offset_x, dst_y + g->dst_offset_y);
110 #else
111         BlitBitmap(objBitmap, screenBitmap,
112                    (obj / 512) * TILEX, (obj % 512) * TILEY / 16,
113                    TILEX, TILEY, dx * TILEX, dy * TILEY);
114 #endif
115
116         screentiles[dy][dx] = obj;
117       }
118     }
119   }
120 }
121
122
123 /* blit players to the screen
124  *
125  * handles transparency and movement
126  */
127
128 static void blitplayer(struct PLAYER *ply)
129 {
130   unsigned int x, y, dx, dy;
131   unsigned short obj, spr;
132   int src_x, src_y, dst_x, dst_y;
133
134   if (!ply->alive)
135     return;
136
137   x = (frame * ply->oldx + (8 - frame) * ply->x) * TILEX / 8;
138   y = (frame * ply->oldy + (8 - frame) * ply->y) * TILEY / 8;
139   dx = x + TILEX - 1;
140   dy = y + TILEY - 1;
141
142   if ((unsigned int)(dx - screen_x) < ((MAX_BUF_XSIZE - 1) * TILEX - 1) &&
143       (unsigned int)(dy - screen_y) < ((MAX_BUF_YSIZE - 1) * TILEY - 1))
144   {
145     spr = map_spr[ply->num][frame][ply->anim];
146     x  %= MAX_BUF_XSIZE * TILEX;
147     y  %= MAX_BUF_YSIZE * TILEY;
148     dx %= MAX_BUF_XSIZE * TILEX;
149     dy %= MAX_BUF_YSIZE * TILEY;
150
151 #if 1
152     /* draw the player to current location */
153     BlitBitmap(sprBitmap, screenBitmap,
154                (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
155                x, y);
156     /* draw the player to opposite wrap-around column */
157     BlitBitmap(sprBitmap, screenBitmap,
158                (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
159                x - MAX_BUF_XSIZE * TILEX, y),
160     /* draw the player to opposite wrap-around row */
161     BlitBitmap(sprBitmap, screenBitmap,
162                (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
163                x, y - MAX_BUF_YSIZE * TILEY);
164
165     /* draw the field the player is moving from (masked over the player) */
166     obj = screentiles[y / TILEY][x / TILEX];
167     src_x = (obj / 512) * TILEX;
168     src_y = (obj % 512) * TILEY / 16;
169     dst_x = (x / TILEX) * TILEX;
170     dst_y = (y / TILEY) * TILEY;
171
172 #if 1
173 #if 0
174     {
175       int tile = Draw[ply->oldy][ply->oldx];
176       struct GraphicInfo_EM *g = &graphic_info_em[tile][frame];
177
178       if (g->width > 0 && g->height > 0)
179       {
180         src_x = g->src_x + g->src_offset_x;
181         src_y = g->src_y + g->src_offset_y;
182         dst_x = ply->oldx % MAX_BUF_XSIZE * TILEX + g->dst_offset_x;
183         dst_y = ply->oldy % MAX_BUF_YSIZE * TILEY + g->dst_offset_y;
184
185         SetClipOrigin(g->bitmap, g->bitmap->stored_clip_gc,
186                       dst_x - src_x, dst_y - src_y);
187         BlitBitmapMasked(g->bitmap, screenBitmap,
188                          src_x, src_y, g->width, g->height, dst_x, dst_y);
189       }
190     }
191 #else
192     SetClipOrigin(objBitmap, objBitmap->stored_clip_gc,
193                   dst_x - src_x, dst_y - src_y);
194     BlitBitmapMasked(objBitmap, screenBitmap,
195                      src_x, src_y, TILEX, TILEY, dst_x, dst_y);
196 #endif
197 #endif
198
199     /* draw the field the player is moving to (masked over the player) */
200     obj = screentiles[dy / TILEY][dx / TILEX];
201     src_x = (obj / 512) * TILEX;
202     src_y = (obj % 512) * TILEY / 16;
203     dst_x = (dx / TILEX) * TILEX;
204     dst_y = (dy / TILEY) * TILEY;
205
206 #if 1
207 #if 1
208     {
209       /*
210         d? == 1:
211           0-3: 0, 1
212           4-7: 0, 1
213
214         d? == 2:
215           0-3: 0, 1
216           4-7: 1, 2
217        */
218
219       int dx = (ply->x - ply->oldx);
220       int dy = (ply->y - ply->oldy);
221       int old_x = ply->oldx  ;
222       int old_y = ply->oldy  ;
223       int new_x = ply->oldx  ;
224       int new_y = ply->oldy  ;
225       int xxx = (frame * ply->oldx + (8 - frame) * ply->x) / 8;
226       int yyy = (frame * ply->oldy + (8 - frame) * ply->y) / 8;
227       int tileXXX = Draw[ply->y][ply->x];
228       int tile = Draw[ply->y][ply->x];
229       struct GraphicInfo_EM *g = &graphic_info_em[tile][frame];
230       int xx = ply->x - 1, yy = ply->y;
231
232       printf("::: %d: %d,%d -> %d,%d: %d, %d, %d, %d [%d,%d] <-> ",
233              frame, ply->oldx, ply->oldy, ply->x, ply->y,
234              src_x, src_y, dst_x, dst_y, xxx, yyy);
235
236       if (g->width > 0 && g->height > 0)
237       {
238         src_x = g->src_x + g->src_offset_x;
239         src_y = g->src_y + g->src_offset_y;
240         dst_x = ply->x % MAX_BUF_XSIZE * TILEX + g->dst_offset_x;
241         dst_y = ply->y % MAX_BUF_YSIZE * TILEY + g->dst_offset_y;
242
243         SetClipOrigin(g->bitmap, g->bitmap->stored_clip_gc,
244                       dst_x - src_x, dst_y - src_y);
245         BlitBitmapMasked(g->bitmap, screenBitmap,
246                          src_x, src_y, g->width, g->height, dst_x, dst_y);
247
248         printf("::: %d, %d, %d, %d\n",
249                src_x, src_y, dst_x, dst_y);
250       }
251     }
252 #else
253     SetClipOrigin(objBitmap, objBitmap->stored_clip_gc,
254                   dst_x - src_x, dst_y - src_y);
255     BlitBitmapMasked(objBitmap, screenBitmap,
256                      src_x, src_y, TILEX, TILEY, dst_x, dst_y);
257 #endif
258 #endif
259
260 #else
261
262     if (objmaskBitmap)
263     {
264       obj = screentiles[y / TILEY][x / TILEX];
265       XCopyArea(display, objmaskBitmap, spriteBitmap, spriteGC,
266                 (obj / 512) * TILEX, (obj % 512) * TILEY / 16, TILEX, TILEY,
267                 -(x % TILEX), -(y % TILEY));
268
269       obj = screentiles[dy / TILEY][dx / TILEX];
270       XCopyArea(display, objmaskBitmap, spriteBitmap, spriteGC,
271                 (obj / 512) * TILEX, (obj % 512) * TILEY / 16, TILEX, TILEY,
272                 (MAX_BUF_XSIZE * TILEX - x) % TILEX,
273                 (MAX_BUF_YSIZE * TILEY - y) % TILEY);
274     }
275     else if (sprmaskBitmap)
276     {
277       XCopyArea(display, sprmaskBitmap, spriteBitmap, spriteGC,
278                 (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY, 0, 0);
279     }
280     else
281     {
282       XFillRectangle(display, spriteBitmap, spriteGC, 0, 0, TILEX, TILEY);
283     }
284
285     SetClipMask(sprBitmap, sprBitmap->stored_clip_gc, spriteBitmap);
286
287     SetClipOrigin(sprBitmap, sprBitmap->stored_clip_gc, x, y);
288     BlitBitmapMasked(sprBitmap, screenBitmap,
289                      (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
290                      x, y);
291
292     SetClipOrigin(sprBitmap, sprBitmap->stored_clip_gc,
293                   x - MAX_BUF_XSIZE * TILEX, y);
294     BlitBitmapMasked(sprBitmap, screenBitmap,
295                      (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
296                      x - MAX_BUF_XSIZE * TILEX, y);
297
298     SetClipOrigin(sprBitmap, sprBitmap->stored_clip_gc,
299                   x, y - MAX_BUF_YSIZE * TILEY);
300     BlitBitmapMasked(sprBitmap, screenBitmap,
301                      (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
302                      x, y - MAX_BUF_YSIZE * TILEY);
303
304     SetClipMask(sprBitmap, sprBitmap->stored_clip_gc, None);
305 #endif
306
307     screentiles[y / TILEY][x / TILEX] = -1;     /* mark screen as dirty */
308     screentiles[dy / TILEY][dx / TILEX] = -1;
309   }
310 }
311
312 void game_initscreen(void)
313 {
314   unsigned int x,y;
315
316   frame = 6;
317   screen_x = 0;
318   screen_y = 0;
319
320   for (y = 0; y < MAX_BUF_YSIZE; y++)
321     for (x = 0; x < MAX_BUF_XSIZE; x++)
322       screentiles[y][x] = -1;
323
324   DrawGameDoorValues_EM(lev.required, ply1.dynamite, lev.score,
325                         DISPLAY_TIME(lev.time + 4));
326 }
327
328 void game_animscreen(void)
329 {
330   unsigned int x,y;
331
332   x = (frame * ply1.oldx + (8 - frame) * ply1.x) * TILEX / 8
333     + ((SCR_FIELDX - 1) * TILEX) / 2;
334   y = (frame * ply1.oldy + (8 - frame) * ply1.y) * TILEY / 8
335     + ((SCR_FIELDY - 1) * 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
342   if (x < SCR_FIELDX * TILEX)
343     x = SCR_FIELDX * TILEY;
344   if (y < SCR_FIELDY * TILEY)
345     y = SCR_FIELDY * TILEY;
346
347   screen_x = x - (SCR_FIELDX - 1) * TILEX;
348   screen_y = y - (SCR_FIELDY - 1) * TILEY;
349
350   animscreen();
351   blitplayer(&ply1);
352   blitplayer(&ply2);
353   blitscreen();
354
355   FlushDisplay();
356 }