1 /* 2000-08-13T15:29:40Z
3 * handle input from x11 and keyboard and joystick
9 #include <X11/keysym.h>
11 #include <sys/types.h>
23 #if defined(TARGET_X11)
31 unsigned short **Boom;
32 unsigned short **Cave;
33 unsigned short **Next;
34 unsigned short **Draw;
36 static unsigned short *Index[4][HEIGHT];
37 static unsigned short Array[4][HEIGHT][WIDTH];
40 static int input_pause;
41 static int input_refresh;
44 static struct timeval tv1;
45 static struct timeval tv2;
47 static unsigned char keymatrix[32];
49 static int player_mode;
50 static char player_name[32];
51 static int player_level;
53 static void drawmenu(int pos)
63 strcpy(buffer, "quit emerald mine");
67 strcpy(buffer, "single player");
71 strcpy(buffer, "teamwork");
75 title_string(4, 0, 40, buffer);
79 sprintf(buffer, "name: %s", player_name);
80 title_string(5, 0, 40, buffer);
84 sprintf(buffer, "level: %d", player_level);
85 title_string(6, 0, 40, buffer);
89 title_string(7, 0, 40, "highscores");
93 title_string(8, 0, 40, "start");
97 title_string(10, 1, 13, "played");
98 title_string(10, 14, 26, "score");
99 title_string(10, 27, 39, "won");
103 sprintf(buffer, "%d", 0);
104 title_string(11, 1, 13, buffer);
105 sprintf(buffer, "%d", 0);
106 title_string(11, 14, 26, buffer);
107 sprintf(buffer, "%d", 0);
108 title_string(11, 27, 39, buffer);
113 /* bring it all together */
117 char name[MAXNAME+2];
123 for (y = 0; y < HEIGHT; y++)
124 for (x = 0; x < WIDTH; x++)
125 Array[0][y][x] = ZBORDER;
126 for (y = 0; y < HEIGHT; y++)
127 for (x = 0; x < WIDTH; x++)
128 Array[1][y][x] = ZBORDER;
129 for (y = 0; y < HEIGHT; y++)
130 for (x = 0; x < WIDTH; x++)
131 Array[2][y][x] = ZBORDER;
132 for (y = 0; y < HEIGHT; y++)
133 for (x = 0; x < WIDTH; x++)
134 Array[3][y][x] = Xblank;
136 for (y = 0; y < HEIGHT; y++)
137 Index[0][y] = Array[0][y];
138 for (y = 0; y < HEIGHT; y++)
139 Index[1][y] = Array[1][y];
140 for (y = 0; y < HEIGHT; y++)
141 Index[2][y] = Array[2][y];
142 for (y = 0; y < HEIGHT; y++)
143 Index[3][y] = Array[3][y];
150 player_mode = 1; /* start off as single player */
151 strcpy(player_name, "dave");
162 for (temp = 0; temp < 7; temp++)
163 drawmenu(temp); /* display all lines */
165 title_blitants(4 + pos);
174 if (input_die) return(0);
175 if (input_refresh) blitscreen();
179 title_blitants(4 + pos);
183 ply1.joy_n = ply1.joy_e = ply1.joy_s = ply1.joy_w = 0;
192 if (ply1.joy_e && player_level < 99)
198 if (ply1.joy_w && player_level > 0)
204 if (ply1.joy_n && pos > 0)
208 title_blitants(4 + pos);
211 if (ply1.joy_s && pos < 4)
215 title_blitants(4 + pos);
219 temp = (ply1.joy_n ||
228 snprintf(name, MAXNAME+2, "%s/lev%02d", EM_LVL_DIR, player_level);
231 snprintf_overflow("read a level in cave/");
233 if (cave_convert(name))
244 if (input_die || input_esc)
254 frame = (frame - 1) & 7;
275 /* read input device for players
280 unsigned int north = 0, east = 0, south = 0, west = 0, fire = 0;
282 for (i = 0; i < 3; i++)
283 if (keymatrix[northKeyCode[i] >> 3] & 1 << (northKeyCode[i] & 7))
286 for (i = 0; i < 3; i++)
287 if (keymatrix[eastKeyCode[i] >> 3] & 1 << (eastKeyCode[i] & 7))
290 for (i = 0; i < 3; i++)
291 if (keymatrix[southKeyCode[i] >> 3] & 1 << (southKeyCode[i] & 7))
294 for (i = 0; i < 3; i++)
295 if (keymatrix[westKeyCode[i] >> 3] & 1 << (westKeyCode[i] & 7))
298 for (i = 0; i < 3; i++)
299 if (keymatrix[fireKeyCode[i] >> 3] & 1 << (fireKeyCode[i] & 7))
302 ply1.joy_fire = fire;
303 if (ply1.joy_stick || (north | east | south | west))
312 /* handle events from x windows and block until the next frame
314 void input_eventloop(void)
322 XPeekEvent(display, &event); /* block until an event arrives */
324 if (gettimeofday(&tv1, 0) == -1)
329 XSync(display, False); /* block until all graphics are drawn */
331 if (gettimeofday(&tv2, 0) == -1)
334 count = tv2.tv_usec + 1000000 - tv1.tv_usec;
335 if (count >= 1000000)
338 tv1.tv_usec = tv2.tv_usec;
342 tv2.tv_usec = 25000 - count;
344 select(0, 0, 0, 0, &tv2); /* sleep a bit */
353 lastKeySym = NoSymbol;
355 while (XPending(display))
357 /* drain the event queue */
358 XNextEvent(display, &event);
360 switch(event.xany.type)
363 XLookupString(&event.xkey, (char *)&count, 1, &lastKeySym, 0);
367 if (event.xexpose.window == xwindow && event.xexpose.count == 0)
372 if (event.xclient.window == xwindow &&
373 (Atom)event.xclient.data.l[0] == deleteAtom)
378 if (event.xcrossing.window == xwindow)
383 if (event.xcrossing.window == xwindow)
389 XQueryKeymap(display, keymatrix); /* read the keyboard */
392 for (i = 0; i < 1; i++)
393 if (keymatrix[escKeyCode[i] >> 3] & 1 << (escKeyCode[i] & 7))