rnd-20040814-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 screen[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(screen[dy][dx] != obj) {
108                                 screen[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         unsigned int x, y, dx, dy;
121         unsigned short obj, spr;
122
123         xdebug("blitplayer");
124
125         if(ply->alive) {
126                 x = (frame * ply->oldx + (8 - frame) * ply->x) * TILEX / 8; dx = x + TILEX - 1;
127                 y = (frame * ply->oldy + (8 - frame) * ply->y) * TILEY / 8; dy = y + TILEY - 1;
128                 if((unsigned int)(dx - screen_x) < (21 * TILEX - 1) && (unsigned int)(dy - screen_y) < (13 * TILEY - 1)) {
129                         spr = map_spr[ply->num][frame][ply->anim];
130                         x %= 22 * TILEX; dx %= 22 * TILEX;
131                         y %= 14 * TILEY; dy %= 14 * TILEY;
132                         if(objmaskBitmap) {
133                                 obj = screen[y / TILEY][x / TILEX];
134                                 XCopyArea(display, objmaskBitmap, spriteBitmap, spriteGC, (obj / 512) * TILEX, (obj % 512) * TILEY / 16, TILEX, TILEY, -(x % TILEX), -(y % TILEY));
135                                 obj = screen[dy / TILEY][dx / TILEX];
136                                 XCopyArea(display, objmaskBitmap, spriteBitmap, spriteGC, (obj / 512) * TILEX, (obj % 512) * TILEY / 16, TILEX, TILEY, (22 * TILEX - x) % TILEX, (14 * TILEY - y) % TILEY);
137                         } else if(sprmaskBitmap) {
138                                 XCopyArea(display, sprmaskBitmap, spriteBitmap, spriteGC, (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY, 0, 0);
139                         } else {
140                                 XFillRectangle(display, spriteBitmap, spriteGC, 0, 0, TILEX, TILEY);
141                         }
142                         screen[y / TILEY][x / TILEX] = -1; /* mark screen as dirty */
143                         screen[dy / TILEY][dx / TILEX] = -1;
144                         XSetClipMask(display, screenGC, spriteBitmap);
145                         XSetClipOrigin(display, screenGC, x, y);
146                         XCopyArea(display, sprPixmap, screenPixmap, screenGC, (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY, x, y);
147                         XSetClipOrigin(display, screenGC, x - 22 * TILEX, y);
148                         XCopyArea(display, sprPixmap, screenPixmap, screenGC, (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY, x - 22 * TILEX, y);
149                         XSetClipOrigin(display, screenGC, x, y - 14 * TILEY);
150                         XCopyArea(display, sprPixmap, screenPixmap, screenGC, (spr / 8) * TILEX, (spr % 8) * TILEY, TILEX, TILEY, x, y - 14 * TILEY);
151                         XSetClipMask(display, screenGC, None);
152                 }
153         }
154 }
155
156 void game_initscreen(void)
157 {
158         unsigned int x,y;
159
160         xdebug("game_initscreen");
161
162 #if 0
163         printf("--> M5.1: xwindow == %ld\n", xwindow);
164 #endif
165
166         frame = 6;
167         screen_x = 0;
168         screen_y = 0;
169
170 #if 0
171         printf("--> M5.2: &window == %ld\n", &window);
172         printf("--> M5.2: xwindow == %ld\n", xwindow);
173         printf("--> M5.2: &xwindow == %ld\n", &xwindow);
174         printf("--> M5.2: screen == %ld\n", screen);
175         printf("--> M5.2: &screen[0][0] == %ld\n", &screen[0][0]);
176 #endif
177
178         for(y = 0; y < 14; y++) {
179                 for(x = 0; x < 22; x++) {
180 #if 0
181                   printf("--> M5.2.A: xwindow == %ld [%d,%d]\n", xwindow, x,y);
182 #endif
183                         screen[y][x] = -1;
184 #if 0
185                   printf("--> M5.2.B: xwindow == %ld [%d,%d]\n", xwindow, x,y);
186 #endif
187                 }
188         }
189
190 #if 0
191         printf("--> M5.3: xwindow == %ld\n", xwindow);
192 #endif
193
194         colour_shuffle();
195         colours[0] += 16;
196         colours[1] += 16;
197         colours[2] += 16;
198         colour_anim = 0;
199
200 #if 0
201         printf("--> M5.4: xwindow == %ld\n", xwindow);
202 #endif
203
204         XFillRectangle(display, scorePixmap, scoreGC, 0, 0, 20 * TILEX, SCOREY);
205         XCopyArea(display, botPixmap, scorePixmap, scoreGC, 11 * SCOREX, colours[0] * SCOREY, 3 * SCOREX, SCOREY, 1 * SCOREX, 0); /* 0-63 time */
206         XCopyArea(display, botPixmap, scorePixmap, scoreGC, 18 * SCOREX, colours[0] * SCOREY, 6 * SCOREX, SCOREY, 15 * SCOREX, 0); /* 112-207 diamonds */
207         XCopyArea(display, botPixmap, scorePixmap, scoreGC, 14 * SCOREX, colours[0] * SCOREY, 4 * SCOREX, SCOREY, 32 * SCOREX, 0); /* 256-319 score */
208
209 #if 0
210         printf("--> M5.X: xwindow == %ld\n", xwindow);
211 #endif
212 }
213
214 void game_blitscore(void)
215 {
216         unsigned int i;
217
218         xdebug("game_blitscore");
219
220         i = (lev.time + 4) / 5;
221         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 7 * SCOREX, 0); i /= 10;
222         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 6 * SCOREX, 0); i /= 10;
223         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 5 * SCOREX, 0); i /= 10;
224         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 4 * SCOREX, 0);
225         i = lev.score;
226         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 39 * SCOREX, 0); i /= 10;
227         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 38 * SCOREX, 0); i /= 10;
228         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 37 * SCOREX, 0); i /= 10;
229         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 36 * SCOREX, 0);
230         if(lev.home == 0) {
231                 XCopyArea(display, botPixmap, scorePixmap, scoreGC, 12 * SCOREX, 24 * SCOREY, 12 * SCOREX, SCOREY, 14 * SCOREX, 0); /* relax */
232                 goto done;
233         }
234         if(ply1.alive + ply2.alive >= lev.home && lev.required == 0) {
235                 XCopyArea(display, botPixmap, scorePixmap, scoreGC, 24 * SCOREX, colours[2] * SCOREY, 12 * SCOREX, SCOREY, 14 * SCOREX, 0); /* find the exit */
236                 goto done;
237         }
238         if(ply1.alive + ply2.alive < lev.home) {
239                 if(++colour_anim > 11) colour_anim = 0;
240                 if(colour_anim < 6) {
241                         XCopyArea(display, botPixmap, scorePixmap, scoreGC, 0, 24 * SCOREY, 12 * SCOREX, SCOREY, 14 * SCOREX, 0); /* forget it */
242                         goto done;
243                 }
244                 XCopyArea(display, botPixmap, scorePixmap, scoreGC, 18 * SCOREX, colours[0] * SCOREY, 6 * SCOREX, SCOREY, 15 * SCOREX, 0); /* diamonds */
245         }
246         i = lev.required;
247         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 24 * SCOREX, 0); i /= 10;
248         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 23 * SCOREX, 0); i /= 10;
249         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 22 * SCOREX, 0); i /= 10;
250         XCopyArea(display, botPixmap, scorePixmap, scoreGC, (i % 10) * SCOREX, colours[1] * SCOREY, SCOREX, SCOREY, 21 * SCOREX, 0);
251 done:
252 }
253
254 void game_animscreen(void)
255 {
256         unsigned int x,y;
257
258         xdebug("game_animscreen");
259
260         x = (frame * ply1.oldx + (8 - frame) * ply1.x) * TILEX / 8 + (19 * TILEX) / 2;
261         y = (frame * ply1.oldy + (8 - frame) * ply1.y) * TILEY / 8 + (11 * TILEY) / 2;
262         if(x > lev.width * TILEX) x = lev.width * TILEX;
263         if(y > lev.height * TILEY) y = lev.height * TILEY;
264         if(x < 20 * TILEX) x = 20 * TILEY;
265         if(y < 12 * TILEY) y = 12 * TILEY;
266         screen_x = x - 19 * TILEX;
267         screen_y = y - 11 * TILEY;
268
269         animscreen();
270         blitplayer(&ply1);
271         blitplayer(&ply2);
272         blitscreen();
273         XFlush(display);
274
275         Random = Random * 129 + 1;
276 }
277
278 void title_initscreen(void)
279 {
280         xdebug("title_initscreen");
281
282         screen_x = 0;
283         screen_y = 0;
284
285         colour_shuffle();
286         colours[1] += 8;
287         colour_anim = 0;
288
289         XCopyArea(display, ttlPixmap, screenPixmap, screenGC, 0, 0, 20 * TILEX, 12 * TILEY, 0, 0);
290         if(botmaskBitmap) {
291                 XCopyArea(display, botPixmap, scorePixmap, scoreGC, 0, colours[1] * SCOREY, 20 * TILEX, SCOREY, 0, 0);
292                 XSetClipMask(display, scoreGC, botmaskBitmap);
293                 XSetClipOrigin(display, scoreGC, 0, 0 - colours[0] * SCOREY);
294         }
295         XCopyArea(display, botPixmap, scorePixmap, scoreGC, 0, colours[0] * SCOREY, 20 * TILEX, SCOREY, 0, 0);
296         if(botmaskBitmap) {
297                 XSetClipMask(display, scoreGC, None);
298         }
299 }
300
301 void title_blitscore(void)
302 {
303         unsigned int x, y, i;
304
305         xdebug("title_blitscore");
306
307         if(++colour_anim > 30) colour_anim = 0;
308         i = colour_anim >= 16 ? 31 - colour_anim : colour_anim;
309         x = (i / 8 + 18) * 2 * SCOREX;
310         y = (i % 8 + 16) * SCOREY;
311
312         if(botmaskBitmap) {
313                 XCopyArea(display, botPixmap, scorePixmap, scoreGC, 32 * SCOREX, colours[1] * SCOREY, 2 * SCOREX, SCOREY, 32 * SCOREX, 0);
314                 XSetClipMask(display, scoreGC, botmaskBitmap);
315                 XSetClipOrigin(display, scoreGC, 32 * SCOREX - x, 0 - y);
316         }
317         XCopyArea(display, botPixmap, scorePixmap, scoreGC, x, y, 2 * SCOREX, SCOREY, 32 * SCOREX, 0);
318         if(botmaskBitmap) {
319                 XSetClipMask(display, scoreGC, None);
320         }
321 }
322
323 void title_blitants(unsigned int y)
324 {
325         static const char ants_dashes[2] = { 8, 7 };
326
327         xdebug("title_blitants");
328
329         XSetDashes(display, antsGC, colour_anim, ants_dashes, 2);
330         XDrawRectangle(display, screenPixmap, antsGC, 0, y * TILEY, 20 * TILEX - 1, TILEY - 1);
331 }
332
333 void title_animscreen(void)
334 {
335         blitscreen();
336         XFlush(display);
337
338         Random = Random * 129 + 1;
339 }
340
341 void title_string(unsigned int y, unsigned int left, unsigned int right, char *string)
342 {
343         int i;
344         unsigned int x;
345
346         xdebug("title_string");
347
348         y *= TILEY; left *= SCOREX; right *= SCOREX;
349         x = (left + right - strlen(string) * 12) / 2;
350         if(x < left || x >= right) x = left;
351
352         XCopyArea(display, ttlPixmap, screenPixmap, screenGC, left, y, right - left, TILEY, left, y);
353         if(ttlmaskBitmap) XSetClipMask(display, screenGC, ttlmaskBitmap);
354         for(i = 0; string[i] && x < right; i++) {
355                 unsigned short ch_pos, ch_x, ch_y;
356                 ch_pos = map_ttl[string[i] & 127];
357                 if(ch_pos < 640) {
358                         ch_x = (ch_pos % 320);
359                         ch_y = (ch_pos / 320 + 12) * TILEY;
360                         if(ttlmaskBitmap) XSetClipOrigin(display, screenGC, x - ch_x, y - ch_y);
361                         XCopyArea(display, ttlPixmap, screenPixmap, screenGC, ch_x, ch_y, 12, TILEY, x, y);
362                 }
363                 x += 12;
364         }
365         XSetClipMask(display, screenGC, None);
366 }