rnd-20040921-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     for (x = left; x < left + MAX_BUF_XSIZE; x++)
88     {
89       dx = x % MAX_BUF_XSIZE;
90       obj = map_obj[frame][Draw[y][x]];
91
92       if (screentiles[dy][dx] != obj)
93       {
94         screentiles[dy][dx] = obj;
95         BlitBitmap(objBitmap, screenBitmap,
96                    (obj / 512) * TILEX, (obj % 512) * TILEY / 16,
97                    TILEX, TILEY, dx * TILEX, dy * TILEY);
98       }
99     }
100   }
101 }
102
103
104 /* blit players to the screen
105  *
106  * handles transparency and movement
107  */
108
109 static void blitplayer(struct PLAYER *ply)
110 {
111   unsigned int x, y, dx, dy;
112   unsigned short obj, spr;
113   int src_x, src_y, dest_x, dest_y;
114
115   if (!ply->alive)
116     return;
117
118   x = (frame * ply->oldx + (8 - frame) * ply->x) * TILEX / 8;
119   y = (frame * ply->oldy + (8 - frame) * ply->y) * TILEY / 8;
120   dx = x + TILEX - 1;
121   dy = y + TILEY - 1;
122
123   if ((unsigned int)(dx - screen_x) < ((MAX_BUF_XSIZE - 1) * TILEX - 1) &&
124       (unsigned int)(dy - screen_y) < ((MAX_BUF_YSIZE - 1) * TILEY - 1))
125   {
126     spr = map_spr[ply->num][frame][ply->anim];
127     x %= MAX_BUF_XSIZE * TILEX;
128     y %= MAX_BUF_YSIZE * TILEY;
129     dx %= MAX_BUF_XSIZE * TILEX;
130     dy %= MAX_BUF_YSIZE * TILEY;
131
132 #if 1
133     /* draw the player to current location */
134     BlitBitmap(sprBitmap, screenBitmap,
135                (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
136                x, y);
137     /* draw the player to opposite wrap-around column */
138     BlitBitmap(sprBitmap, screenBitmap,
139                (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
140                x - MAX_BUF_XSIZE * TILEX, y),
141     /* draw the player to opposite wrap-around row */
142     BlitBitmap(sprBitmap, screenBitmap,
143                (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
144                x, y - MAX_BUF_YSIZE * TILEY);
145
146     /* draw the field the player is moving from (masked over the player) */
147     obj = screentiles[y / TILEY][x / TILEX];
148     src_x = (obj / 512) * TILEX;
149     src_y = (obj % 512) * TILEY / 16;
150     dest_x = (x / TILEX) * TILEX;
151     dest_y = (y / TILEY) * TILEY;
152
153     SetClipOrigin(objBitmap, objBitmap->stored_clip_gc,
154                   dest_x - src_x, dest_y - src_y);
155     BlitBitmapMasked(objBitmap, screenBitmap,
156                      src_x, src_y, TILEX, TILEY, dest_x, dest_y);
157
158     /* draw the field the player is moving to (masked over the player) */
159     obj = screentiles[dy / TILEY][dx / TILEX];
160     src_x = (obj / 512) * TILEX;
161     src_y = (obj % 512) * TILEY / 16;
162     dest_x = (dx / TILEX) * TILEX;
163     dest_y = (dy / TILEY) * TILEY;
164
165     SetClipOrigin(objBitmap, objBitmap->stored_clip_gc,
166                   dest_x - src_x, dest_y - src_y);
167     BlitBitmapMasked(objBitmap, screenBitmap,
168                      src_x, src_y, TILEX, TILEY, dest_x, dest_y);
169
170 #else
171
172     if (objmaskBitmap)
173     {
174       obj = screentiles[y / TILEY][x / TILEX];
175       XCopyArea(display, objmaskBitmap, spriteBitmap, spriteGC,
176                 (obj / 512) * TILEX, (obj % 512) * TILEY / 16, TILEX, TILEY,
177                 -(x % TILEX), -(y % TILEY));
178
179       obj = screentiles[dy / TILEY][dx / TILEX];
180       XCopyArea(display, objmaskBitmap, spriteBitmap, spriteGC,
181                 (obj / 512) * TILEX, (obj % 512) * TILEY / 16, TILEX, TILEY,
182                 (MAX_BUF_XSIZE * TILEX - x) % TILEX,
183                 (MAX_BUF_YSIZE * TILEY - y) % TILEY);
184     }
185     else if (sprmaskBitmap)
186     {
187       XCopyArea(display, sprmaskBitmap, spriteBitmap, spriteGC,
188                 (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY, 0, 0);
189     }
190     else
191     {
192       XFillRectangle(display, spriteBitmap, spriteGC, 0, 0, TILEX, TILEY);
193     }
194
195     SetClipMask(sprBitmap, sprBitmap->stored_clip_gc, spriteBitmap);
196
197     SetClipOrigin(sprBitmap, sprBitmap->stored_clip_gc, x, y);
198     BlitBitmapMasked(sprBitmap, screenBitmap,
199                      (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
200                      x, y);
201
202     SetClipOrigin(sprBitmap, sprBitmap->stored_clip_gc,
203                   x - MAX_BUF_XSIZE * TILEX, y);
204     BlitBitmapMasked(sprBitmap, screenBitmap,
205                      (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
206                      x - MAX_BUF_XSIZE * TILEX, y);
207
208     SetClipOrigin(sprBitmap, sprBitmap->stored_clip_gc,
209                   x, y - MAX_BUF_YSIZE * TILEY);
210     BlitBitmapMasked(sprBitmap, screenBitmap,
211                      (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
212                      x, y - MAX_BUF_YSIZE * TILEY);
213
214     SetClipMask(sprBitmap, sprBitmap->stored_clip_gc, None);
215 #endif
216
217     screentiles[y / TILEY][x / TILEX] = -1;     /* mark screen as dirty */
218     screentiles[dy / TILEY][dx / TILEX] = -1;
219   }
220 }
221
222 void game_initscreen(void)
223 {
224   unsigned int x,y;
225
226   frame = 6;
227   screen_x = 0;
228   screen_y = 0;
229
230   for (y = 0; y < MAX_BUF_YSIZE; y++)
231     for (x = 0; x < MAX_BUF_XSIZE; x++)
232       screentiles[y][x] = -1;
233
234   DrawGameDoorValues_EM(lev.required, ply1.dynamite, lev.score,
235                         (lev.time + 4) / 5);
236 }
237
238 void game_animscreen(void)
239 {
240   unsigned int x,y;
241
242   x = (frame * ply1.oldx + (8 - frame) * ply1.x) * TILEX / 8
243     + ((SCR_FIELDX - 1) * TILEX) / 2;
244   y = (frame * ply1.oldy + (8 - frame) * ply1.y) * TILEY / 8
245     + ((SCR_FIELDY - 1) * TILEY) / 2;
246
247   if (x > lev.width * TILEX)
248     x = lev.width * TILEX;
249   if (y > lev.height * TILEY)
250     y = lev.height * TILEY;
251
252   if (x < SCR_FIELDX * TILEX)
253     x = SCR_FIELDX * TILEY;
254   if (y < SCR_FIELDY * TILEY)
255     y = SCR_FIELDY * TILEY;
256
257   screen_x = x - (SCR_FIELDX - 1) * TILEX;
258   screen_y = y - (SCR_FIELDY - 1) * TILEY;
259
260   animscreen();
261   blitplayer(&ply1);
262   blitplayer(&ply2);
263   blitscreen();
264
265   FlushDisplay();
266 }