rnd-20040814-1-src
[rocksndiamonds.git] / src / libem / input.c
1 /* 2000-08-13T15:29:40Z
2  *
3  * handle input from x11 and keyboard and joystick
4  */
5
6 #include <X11/Xlib.h>
7 #include <X11/Xutil.h>
8 #include <X11/Xatom.h>
9 #include <X11/keysym.h>
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <errno.h>
17
18 #include "global.h"
19 #include "display.h"
20 #include "level.h"
21
22 unsigned long Random;
23
24 struct PLAYER ply1;
25 struct PLAYER ply2;
26 struct LEVEL lev;
27
28 unsigned short **Boom;
29 unsigned short **Cave;
30 unsigned short **Next;
31 unsigned short **Draw;
32
33 static unsigned short *Index[4][HEIGHT];
34 static unsigned short Array[4][HEIGHT][WIDTH];
35
36 static int input_die;
37 static int input_pause;
38 static int input_refresh;
39 static int input_esc;
40
41 static struct timeval tv1;
42 static struct timeval tv2;
43
44 static unsigned char keymatrix[32];
45
46 static int player_mode;
47 static char player_name[32];
48 static int player_level;
49
50 static void drawmenu(int pos)
51 {
52         char buffer[256];
53         switch(pos) {
54         case 0:
55                 switch(player_mode) {
56                 case 0: strcpy(buffer, "quit emerald mine"); break;
57                 case 1: strcpy(buffer, "single player"); break;
58                 case 2: strcpy(buffer, "teamwork"); break;
59                 }
60                 title_string(4, 0, 40, buffer);
61                 break;
62         case 1:
63                 sprintf(buffer, "name: %s", player_name);
64                 title_string(5, 0, 40, buffer);
65                 break;
66         case 2:
67                 sprintf(buffer, "level: %d", player_level);
68                 title_string(6, 0, 40, buffer);
69                 break;
70         case 3:
71                 title_string(7, 0, 40, "highscores");
72                 break;
73         case 4:
74                 title_string(8, 0, 40, "start");
75                 break;
76         case 5:
77                 title_string(10, 1, 13, "played");
78                 title_string(10, 14, 26, "score");
79                 title_string(10, 27, 39, "won");
80                 break;
81         case 6:
82                 sprintf(buffer, "%d", 0);
83                 title_string(11, 1, 13, buffer);
84                 sprintf(buffer, "%d", 0);
85                 title_string(11, 14, 26, buffer);
86                 sprintf(buffer, "%d", 0);
87                 title_string(11, 27, 39, buffer);
88                 break;
89         }
90 }
91
92 /* bring it all together */
93 int game_start(void)
94 {
95         int x,y;
96         char name[MAXNAME+2];
97         int temp;
98         int pos;
99
100         Random = 1684108901;
101
102         for(y = 0; y < HEIGHT; y++) for(x = 0; x < WIDTH; x++) Array[0][y][x] = ZBORDER;
103         for(y = 0; y < HEIGHT; y++) for(x = 0; x < WIDTH; x++) Array[1][y][x] = ZBORDER;
104         for(y = 0; y < HEIGHT; y++) for(x = 0; x < WIDTH; x++) Array[2][y][x] = ZBORDER;
105         for(y = 0; y < HEIGHT; y++) for(x = 0; x < WIDTH; x++) Array[3][y][x] = Xblank;
106
107         for(y = 0; y < HEIGHT; y++) Index[0][y] = Array[0][y];
108         for(y = 0; y < HEIGHT; y++) Index[1][y] = Array[1][y];
109         for(y = 0; y < HEIGHT; y++) Index[2][y] = Array[2][y];
110         for(y = 0; y < HEIGHT; y++) Index[3][y] = Array[3][y];
111
112         Cave = Index[0];
113         Next = Index[1];
114         Draw = Index[2];
115         Boom = Index[3];
116
117         player_mode = 1; /* start off as single player */
118         strcpy(player_name, "dave");
119         player_level = 0;
120
121         input_pause = 1;
122
123         for(;;) {
124
125                 pos = 4;
126
127                 title_initscreen();
128
129                 for(temp = 0; temp < 7; temp++) drawmenu(temp); /* display all lines */
130                 title_blitants(4 + pos);
131
132                 title_blitscore();
133                 title_animscreen();
134
135 #if 0
136                 printf("--> M1: xwindow == %ld\n", xwindow);
137 #endif
138
139                 temp = 1;
140                 for(;;) {
141                         input_eventloop();
142                         if(input_die) return(0);
143                         if(input_refresh) blitscreen();
144
145 #if 0
146                         printf("--> M2: xwindow == %ld\n", xwindow);
147 #endif
148
149                         if(!input_pause) {
150                                 title_blitants(4 + pos);
151                                 title_blitscore();
152                                 title_animscreen();
153
154 #if 0
155                                 printf("--> M3: xwindow == %ld\n", xwindow);
156 #endif
157
158                                 ply1.joy_n = ply1.joy_e = ply1.joy_s = ply1.joy_w = 0;
159
160                                 readjoy();
161
162                                 if(temp == 0) {
163                                         if(ply1.joy_fire) break;
164
165                                         if(ply1.joy_e && player_level < 99) {
166                                                 player_level++;
167                                                 drawmenu(2);
168                                         }
169                                         if(ply1.joy_w && player_level > 0) {
170                                                 player_level--;
171                                                 drawmenu(2);
172                                         }
173                                         if(ply1.joy_n && pos > 0) {
174                                                 drawmenu(pos);
175                                                 pos--;
176                                                 title_blitants(4 + pos);
177                                         }
178                                         if(ply1.joy_s && pos < 4) {
179                                                 drawmenu(pos);
180                                                 pos++;
181                                                 title_blitants(4 + pos);
182                                         }
183                                 }
184                                 temp = (ply1.joy_n || ply1.joy_e || ply1.joy_s || ply1.joy_w || ply1.joy_fire);
185                         }
186                 }
187
188 #if 0
189                 printf("--> M4: xwindow == %ld\n", xwindow);
190 #endif
191
192                 name[MAXNAME] = 0;
193                 snprintf(name, MAXNAME+2, "%s/lev%02d", EM_LVL_DIR, player_level);
194                 if(name[MAXNAME]) snprintf_overflow("read a level in cave/");
195
196                 if(cave_convert(name)) continue;
197
198 #if 0
199                 printf("--> M5: xwindow == %ld\n", xwindow);
200 #endif
201
202                 game_initscreen();
203 #if 0
204                 printf("--> M6: xwindow == %ld\n", xwindow);
205 #endif
206                 game_blitscore();
207 #if 0
208                 printf("--> M7: xwindow == %ld\n", xwindow);
209 #endif
210                 game_animscreen();
211
212 #if 0
213                 printf("--> M8: xwindow == %ld\n", xwindow);
214 #endif
215
216                 for(;;) {
217                         input_eventloop();
218                         if(input_die || input_esc) break;
219                         if(input_refresh) blitscreen();
220
221                         if(!input_pause) {
222                                 game_animscreen();
223
224                                 frame = (frame - 1) & 7;
225
226                                 readjoy();
227
228                                 if(frame == 7) {
229                                         synchro_1();
230                                         synchro_2();
231                                 }
232                                 if(frame == 6) {
233                                         synchro_3();
234                                         sound_play();
235                                         game_blitscore();
236                                 }
237                         }
238                 }
239         }
240 }
241
242 /* read input device for players
243  */
244 void readjoy(void)
245 {
246         unsigned int i;
247         unsigned int north = 0, east = 0, south = 0, west = 0, fire = 0;
248
249         for(i = 0; i < 3; i++) if(keymatrix[northKeyCode[i] >> 3] & 1 << (northKeyCode[i] & 7)) north = 1;
250         for(i = 0; i < 3; i++) if(keymatrix[eastKeyCode[i] >> 3] & 1 << (eastKeyCode[i] & 7)) east = 1;
251         for(i = 0; i < 3; i++) if(keymatrix[southKeyCode[i] >> 3] & 1 << (southKeyCode[i] & 7)) south = 1;
252         for(i = 0; i < 3; i++) if(keymatrix[westKeyCode[i] >> 3] & 1 << (westKeyCode[i] & 7)) west = 1;
253         for(i = 0; i < 3; i++) if(keymatrix[fireKeyCode[i] >> 3] & 1 << (fireKeyCode[i] & 7)) fire = 1;
254
255         ply1.joy_fire = fire;
256         if(ply1.joy_stick || (north | east | south | west)) {
257                 ply1.joy_n = north;
258                 ply1.joy_e = east;
259                 ply1.joy_s = south;
260                 ply1.joy_w = west;
261         }
262 }
263
264 /* handle events from x windows and block until the next frame
265  */
266 void input_eventloop(void)
267 {
268         XEvent event;
269         unsigned int i;
270         unsigned long count;
271
272         if(input_pause) {
273                 XPeekEvent(display, &event); /* block until an event arrives */
274
275                 if(gettimeofday(&tv1, 0) == -1) tv1.tv_usec = 0;
276         } else {
277                 XSync(display, False); /* block until all graphics are drawn */
278
279                 if(gettimeofday(&tv2, 0) == -1) tv2.tv_usec = 0;
280                 count = tv2.tv_usec + 1000000 - tv1.tv_usec; if(count >= 1000000) count -= 1000000;
281                 tv1.tv_usec = tv2.tv_usec;
282                 if(count < 25000) {
283                         tv2.tv_sec = 0;
284                         tv2.tv_usec = 25000 - count;
285 #if 1
286                         select(0, 0, 0, 0, &tv2); /* sleep a bit */
287 #else
288                         usleep(tv2.tv_usec);
289 #endif
290                 }
291         }
292
293         input_die = 0;
294         input_refresh = 0;
295         lastKeySym = NoSymbol;
296         while(XPending(display)) { /* drain the event queue */
297                 XNextEvent(display, &event);
298                 switch(event.xany.type) {
299                 case KeyPress:
300                         XLookupString(&event.xkey, (char *)&count, 1, &lastKeySym, 0);
301                         break;
302                 case Expose:
303                         if(event.xexpose.window == xwindow && event.xexpose.count == 0) input_refresh = 1;
304                         break;
305                 case ClientMessage:
306                         if(event.xclient.window == xwindow && (Atom)event.xclient.data.l[0] == deleteAtom) input_die = 1;
307                         break;
308                 case EnterNotify:
309                         if(event.xcrossing.window == xwindow) input_pause = 0;
310                         break;
311                 case LeaveNotify:
312                         if(event.xcrossing.window == xwindow) input_pause = 1;
313                         break;
314                 }
315         }
316         XQueryKeymap(display, keymatrix); /* read the keyboard */
317
318         input_esc = 0;
319         for(i = 0; i < 1; i++) if(keymatrix[escKeyCode[i] >> 3] & 1 << (escKeyCode[i] & 7)) input_esc = 1;
320 }