rnd-20040816-1-src
[rocksndiamonds.git] / src / libem / 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 unsigned int frame; /* current frame */
16 unsigned int screen_x; /* current scroll position */
17 unsigned int screen_y;
18
19 static unsigned short screentiles[14][22]; /* tiles currently on screen */
20
21 static unsigned int colours[8];
22 static unsigned int colour_anim;
23
24 void xdebug(char *msg)
25 {
26 #if 0
27   XSync(display, False);
28   printf("EM DEBUG: %s\n", msg);
29 #endif
30 }
31
32 static void colour_shuffle(void)
33 {
34         unsigned int i, j, k;
35         for (i = 0; i < 8; i++) colours[i] = i;
36         for (i = 0; i < 8; i++) {
37                 Random = Random * 129 + 1;
38                 j = (Random >> 10) & 7;
39                 k = colours[i];
40                 colours[i] = colours[j];
41                 colours[j] = k;
42         }
43 }
44
45 /* copy the entire screen to the window at the scroll position
46  *
47  * perhaps use mit-shm to speed this up
48  */
49 void blitscreen(void)
50 {
51         unsigned int x = screen_x % (22 * TILEX);
52         unsigned int y = screen_y % (14 * TILEY);
53
54         xdebug("blitscreen");
55
56 #if 0
57         printf("::: %d, %d [%d, %d]\n", x, y, TILEX, TILEY);
58 #endif
59
60         if (x < 2 * TILEX && y < 2 * TILEY) {
61
62 #if 0
63           printf("!!! %ld, %ld, %ld, %ld\n",
64                  display, screenPixmap, xwindow, screenGC);
65 #endif
66
67                 XCopyArea(display, screenPixmap, xwindow, screenGC, x, y, 20 * TILEX, 12 * TILEY, 0, 0);
68         } else if (x < 2 * TILEX && y >= 2 * TILEY) {
69                 XCopyArea(display, screenPixmap, xwindow, screenGC, x, y, 20 * TILEX, 14 * TILEY - y, 0, 0);
70                 XCopyArea(display, screenPixmap, xwindow, screenGC, x, 0, 20 * TILEX, y - 2 * TILEY, 0, 14 * TILEY - y);
71         } else if (x >= 2 * TILEX && y < 2 * TILEY) {
72                 XCopyArea(display, screenPixmap, xwindow, screenGC, x, y, 22 * TILEX - x, 12 * TILEY, 0, 0);
73                 XCopyArea(display, screenPixmap, xwindow, screenGC, 0, y, x - 2 * TILEX, 12 * TILEY, 22 * TILEX - x, 0);
74         } else {
75                 XCopyArea(display, screenPixmap, xwindow, screenGC, x, y, 22 * TILEX - x, 14 * TILEY - y, 0, 0);
76                 XCopyArea(display, screenPixmap, xwindow, screenGC, 0, y, x - 2 * TILEX, 14 * TILEY - y, 22 * TILEX - x, 0);
77                 XCopyArea(display, screenPixmap, xwindow, screenGC, x, 0, 22 * TILEX - x, y - 2 * TILEY, 0, 14 * TILEY - y);
78                 XCopyArea(display, screenPixmap, xwindow, screenGC, 0, 0, x - 2 * TILEX, y - 2 * TILEY, 22 * TILEX - x, 14 * TILEY - y);
79         }
80
81         XCopyArea(display, scorePixmap, xwindow, scoreGC, 0, 0, 20 * TILEX, SCOREY, 0, 12 * TILEY);
82         XFlush(display);
83
84         xdebug("blitscreen - done");
85 }
86
87 /* draw differences between game tiles and screen tiles
88  *
89  * implicitly handles scrolling and restoring background under the sprites
90  *
91  * perhaps use mit-shm to speed this up
92  */
93 static void animscreen(void)
94 {
95         unsigned int x, y, dx, dy;
96         unsigned short obj;
97         unsigned int left = screen_x / TILEX;
98         unsigned int top = screen_y / TILEY;
99
100         xdebug("animscreen");
101
102         for (y = top; y < top + 14; y++) {
103                 dy = y % 14;
104                 for (x = left; x < left + 22; x++) {
105                         dx = x % 22;
106                         obj = map_obj[frame][Draw[y][x]];
107                         if (screentiles[dy][dx] != obj) {
108                                 screentiles[dy][dx] = obj;
109                                 XCopyArea(display, objPixmap, screenPixmap, screenGC, (obj / 512) * TILEX, (obj % 512) * TILEY / 16, TILEX, TILEY, dx * TILEX, dy * TILEY);
110                         }
111                 }
112         }
113 }
114
115 /* blit players to the screen
116  *
117  * handles transparency and movement
118  */
119 static void blitplayer(struct PLAYER *ply)
120 {
121   unsigned int x, y, dx, dy;
122   unsigned short obj, spr;
123
124   xdebug("blitplayer");
125
126   if (ply->alive)
127   {
128     x = (frame * ply->oldx + (8 - frame) * ply->x) * TILEX / 8;
129     y = (frame * ply->oldy + (8 - frame) * ply->y) * TILEY / 8;
130     dx = x + TILEX - 1;
131     dy = y + TILEY - 1;
132
133     if ((unsigned int)(dx - screen_x) < (21 * TILEX - 1) &&
134         (unsigned int)(dy - screen_y) < (13 * TILEY - 1))
135     {
136       spr = map_spr[ply->num][frame][ply->anim];
137       x %= 22 * TILEX;
138       y %= 14 * TILEY;
139       dx %= 22 * TILEX;
140       dy %= 14 * TILEY;
141
142       if (objmaskBitmap)
143       {
144         obj = screentiles[y / TILEY][x / TILEX];
145         XCopyArea(display, objmaskBitmap, spriteBitmap, spriteGC,
146                   (obj / 512) * TILEX, (obj % 512) * TILEY / 16, TILEX, TILEY,
147                   -(x % TILEX), -(y % TILEY));
148
149         obj = screentiles[dy / TILEY][dx / TILEX];
150         XCopyArea(display, objmaskBitmap, spriteBitmap, spriteGC,
151                   (obj / 512) * TILEX, (obj % 512) * TILEY / 16, TILEX, TILEY,
152                   (22 * TILEX - x) % TILEX, (14 * TILEY - y) % TILEY);
153       }
154       else if (sprmaskBitmap)
155       {
156         XCopyArea(display, sprmaskBitmap, spriteBitmap, spriteGC,
157                   (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY, 0, 0);
158       }
159       else
160       {
161         XFillRectangle(display, spriteBitmap, spriteGC, 0, 0, TILEX, TILEY);
162       }
163
164       screentiles[y / TILEY][x / TILEX] = -1; /* mark screen as dirty */
165       screentiles[dy / TILEY][dx / TILEX] = -1;
166
167       XSetClipMask(display, screenGC, spriteBitmap);
168       XSetClipOrigin(display, screenGC, x, y);
169       XCopyArea(display, sprPixmap, screenPixmap, screenGC,
170                 (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
171                 x, y);
172       XSetClipOrigin(display, screenGC, x - 22 * TILEX, y);
173       XCopyArea(display, sprPixmap, screenPixmap, screenGC,
174                 (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
175                 x - 22 * TILEX, y);
176       XSetClipOrigin(display, screenGC, x, y - 14 * TILEY);
177       XCopyArea(display, sprPixmap, screenPixmap, screenGC,
178                 (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY,
179                 x, y - 14 * TILEY);
180       XSetClipMask(display, screenGC, None);
181     }
182   }
183 }
184
185 void game_initscreen(void)
186 {
187         unsigned int x,y;
188
189         xdebug("game_initscreen");
190
191 #if 0
192         printf("--> M5.1: xwindow == %ld\n", xwindow);
193 #endif
194
195         frame = 6;
196         screen_x = 0;
197         screen_y = 0;
198
199 #if 0
200         printf("--> M5.2: &window == %ld\n", &window);
201         printf("--> M5.2: xwindow == %ld\n", xwindow);
202         printf("--> M5.2: &xwindow == %ld\n", &xwindow);
203         printf("--> M5.2: screen == %ld\n", screen);
204         printf("--> M5.2: &screentiles[0][0] == %ld\n", &screentiles[0][0]);
205 #endif
206
207         for (y = 0; y < 14; y++) {
208                 for (x = 0; x < 22; x++) {
209 #if 0
210                   printf("--> M5.2.A: xwindow == %ld [%d,%d]\n", xwindow, x,y);
211 #endif
212                         screentiles[y][x] = -1;
213 #if 0
214                   printf("--> M5.2.B: xwindow == %ld [%d,%d]\n", xwindow, x,y);
215 #endif
216                 }
217         }
218
219 #if 0
220         printf("--> M5.3: xwindow == %ld\n", xwindow);
221 #endif
222
223         colour_shuffle();
224         colours[0] += 16;
225         colours[1] += 16;
226         colours[2] += 16;
227         colour_anim = 0;
228
229 #if 0
230         printf("--> M5.4: xwindow == %ld\n", xwindow);
231 #endif
232
233         XFillRectangle(display, scorePixmap, scoreGC, 0, 0, 20 * TILEX, SCOREY);
234         XCopyArea(display, botPixmap, scorePixmap, scoreGC, 11 * SCOREX, colours[0] * SCOREY, 3 * SCOREX, SCOREY, 1 * SCOREX, 0); /* 0-63 time */
235         XCopyArea(display, botPixmap, scorePixmap, scoreGC, 18 * SCOREX, colours[0] * SCOREY, 6 * SCOREX, SCOREY, 15 * SCOREX, 0); /* 112-207 diamonds */
236         XCopyArea(display, botPixmap, scorePixmap, scoreGC, 14 * SCOREX, colours[0] * SCOREY, 4 * SCOREX, SCOREY, 32 * SCOREX, 0); /* 256-319 score */
237
238 #if 0
239         printf("--> M5.X: xwindow == %ld\n", xwindow);
240 #endif
241 }
242
243 void game_blitscore(void)
244 {
245         unsigned int i;
246
247         xdebug("game_blitscore");
248
249         i = (lev.time + 4) / 5;
250         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 7 * SCOREX, 0); i /= 10;
251         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 6 * SCOREX, 0); i /= 10;
252         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 5 * SCOREX, 0); i /= 10;
253         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 4 * SCOREX, 0);
254         i = lev.score;
255         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 39 * SCOREX, 0); i /= 10;
256         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 38 * SCOREX, 0); i /= 10;
257         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 37 * SCOREX, 0); i /= 10;
258         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 36 * SCOREX, 0);
259         if (lev.home == 0) {
260                 XCopyArea(display, botPixmap, scorePixmap, scoreGC, 12 * SCOREX, 24 * SCOREY, 12 * SCOREX, SCOREY, 14 * SCOREX, 0); /* relax */
261                 goto done;
262         }
263         if (ply1.alive + ply2.alive >= lev.home && lev.required == 0) {
264                 XCopyArea(display, botPixmap, scorePixmap, scoreGC, 24 * SCOREX, colours[2] * SCOREY, 12 * SCOREX, SCOREY, 14 * SCOREX, 0); /* find the exit */
265                 goto done;
266         }
267         if (ply1.alive + ply2.alive < lev.home) {
268                 if (++colour_anim > 11) colour_anim = 0;
269                 if (colour_anim < 6) {
270                         XCopyArea(display, botPixmap, scorePixmap, scoreGC, 0, 24 * SCOREY, 12 * SCOREX, SCOREY, 14 * SCOREX, 0); /* forget it */
271                         goto done;
272                 }
273                 XCopyArea(display, botPixmap, scorePixmap, scoreGC, 18 * SCOREX, colours[0] * SCOREY, 6 * SCOREX, SCOREY, 15 * SCOREX, 0); /* diamonds */
274         }
275         i = lev.required;
276         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 24 * SCOREX, 0); i /= 10;
277         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 23 * SCOREX, 0); i /= 10;
278         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 22 * SCOREX, 0); i /= 10;
279         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 21 * SCOREX, 0);
280 done:
281 }
282
283 void game_animscreen(void)
284 {
285         unsigned int x,y;
286
287         xdebug("game_animscreen");
288
289         x = (frame * ply1.oldx + (8 - frame) * ply1.x) * TILEX / 8 + (19 * TILEX) / 2;
290         y = (frame * ply1.oldy + (8 - frame) * ply1.y) * TILEY / 8 + (11 * TILEY) / 2;
291         if (x > lev.width * TILEX) x = lev.width * TILEX;
292         if (y > lev.height * TILEY) y = lev.height * TILEY;
293         if (x < 20 * TILEX) x = 20 * TILEY;
294         if (y < 12 * TILEY) y = 12 * TILEY;
295         screen_x = x - 19 * TILEX;
296         screen_y = y - 11 * TILEY;
297
298         animscreen();
299         blitplayer(&ply1);
300         blitplayer(&ply2);
301         blitscreen();
302         XFlush(display);
303
304         Random = Random * 129 + 1;
305 }
306
307 void title_initscreen(void)
308 {
309         xdebug("title_initscreen");
310
311         screen_x = 0;
312         screen_y = 0;
313
314         colour_shuffle();
315         colours[1] += 8;
316         colour_anim = 0;
317
318         XCopyArea(display, ttlPixmap, screenPixmap, screenGC, 0, 0, 20 * TILEX, 12 * TILEY, 0, 0);
319         if (botmaskBitmap) {
320                 XCopyArea(display, botPixmap, scorePixmap, scoreGC, 0, colours[1] * SCOREY, 20 * TILEX, SCOREY, 0, 0);
321                 XSetClipMask(display, scoreGC, botmaskBitmap);
322                 XSetClipOrigin(display, scoreGC, 0, 0 - colours[0] * SCOREY);
323         }
324         XCopyArea(display, botPixmap, scorePixmap, scoreGC, 0, colours[0] * SCOREY, 20 * TILEX, SCOREY, 0, 0);
325         if (botmaskBitmap) {
326                 XSetClipMask(display, scoreGC, None);
327         }
328 }
329
330 void title_blitscore(void)
331 {
332         unsigned int x, y, i;
333
334         xdebug("title_blitscore");
335
336         if (++colour_anim > 30) colour_anim = 0;
337         i = colour_anim >= 16 ? 31 - colour_anim : colour_anim;
338         x = (i / 8 + 18) * 2 * SCOREX;
339         y = (i % 8 + 16) * SCOREY;
340
341         if (botmaskBitmap) {
342                 XCopyArea(display, botPixmap, scorePixmap, scoreGC, 32 * SCOREX, colours[1] * SCOREY, 2 * SCOREX, SCOREY, 32 * SCOREX, 0);
343                 XSetClipMask(display, scoreGC, botmaskBitmap);
344                 XSetClipOrigin(display, scoreGC, 32 * SCOREX - x, 0 - y);
345         }
346         XCopyArea(display, botPixmap, scorePixmap, scoreGC, x, y, 2 * SCOREX, SCOREY, 32 * SCOREX, 0);
347         if (botmaskBitmap) {
348                 XSetClipMask(display, scoreGC, None);
349         }
350 }
351
352 void title_blitants(unsigned int y)
353 {
354         static const char ants_dashes[2] = { 8, 7 };
355
356         xdebug("title_blitants");
357
358         XSetDashes(display, antsGC, colour_anim, ants_dashes, 2);
359         XDrawRectangle(display, screenPixmap, antsGC, 0, y * TILEY, 20 * TILEX - 1, TILEY - 1);
360 }
361
362 void title_animscreen(void)
363 {
364         blitscreen();
365         XFlush(display);
366
367         Random = Random * 129 + 1;
368 }
369
370 void title_string(unsigned int y, unsigned int left, unsigned int right, char *string)
371 {
372         int i;
373         unsigned int x;
374
375         xdebug("title_string");
376
377         y *= TILEY; left *= SCOREX; right *= SCOREX;
378         x = (left + right - strlen(string) * 12) / 2;
379         if (x < left || x >= right) x = left;
380
381         XCopyArea(display, ttlPixmap, screenPixmap, screenGC, left, y, right - left, TILEY, left, y);
382         if (ttlmaskBitmap) XSetClipMask(display, screenGC, ttlmaskBitmap);
383         for (i = 0; string[i] && x < right; i++) {
384                 unsigned short ch_pos, ch_x, ch_y;
385                 ch_pos = map_ttl[string[i] & 127];
386                 if (ch_pos < 640) {
387                         ch_x = (ch_pos % 320);
388                         ch_y = (ch_pos / 320 + 12) * TILEY;
389                         if (ttlmaskBitmap) XSetClipOrigin(display, screenGC, x - ch_x, y - ch_y);
390                         XCopyArea(display, ttlPixmap, screenPixmap, screenGC, ch_x, ch_y, 12, TILEY, x, y);
391                 }
392                 x += 12;
393         }
394         XSetClipMask(display, screenGC, None);
395 }