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, 3, 13, "played");
98 title_string(10, 14, 26, "score");
99 title_string(10, 27, 37, "won");
103 sprintf(buffer, "%d", 0);
104 title_string(11, 3, 13, buffer);
105 sprintf(buffer, "%d", 0);
106 title_string(11, 14, 26, buffer);
107 sprintf(buffer, "%d", 0);
108 title_string(11, 27, 37, buffer);
113 void game_init_vars(void)
119 for (y = 0; y < HEIGHT; y++)
120 for (x = 0; x < WIDTH; x++)
121 Array[0][y][x] = ZBORDER;
122 for (y = 0; y < HEIGHT; y++)
123 for (x = 0; x < WIDTH; x++)
124 Array[1][y][x] = ZBORDER;
125 for (y = 0; y < HEIGHT; y++)
126 for (x = 0; x < WIDTH; x++)
127 Array[2][y][x] = ZBORDER;
128 for (y = 0; y < HEIGHT; y++)
129 for (x = 0; x < WIDTH; x++)
130 Array[3][y][x] = Xblank;
132 for (y = 0; y < HEIGHT; y++)
133 Index[0][y] = Array[0][y];
134 for (y = 0; y < HEIGHT; y++)
135 Index[1][y] = Array[1][y];
136 for (y = 0; y < HEIGHT; y++)
137 Index[2][y] = Array[2][y];
138 for (y = 0; y < HEIGHT; y++)
139 Index[3][y] = Array[3][y];
146 player_mode = 1; /* start off as single player */
147 strcpy(player_name, "dave");
153 int game_play_init(int level_nr, char *filename)
155 if (filename != NULL)
157 player_level = level_nr;
159 if (cave_convert(filename) != 0)
162 else /* !!! SOON OBSOLETE !!! */
164 char name[MAXNAME+2];
167 snprintf(name, MAXNAME+2, "%s/lev%02d", EM_LVL_DIR, player_level);
170 snprintf_overflow("read a level in cave/");
172 if (cave_convert(name) != 0)
183 int game_menu_loop(boolean init, byte action)
185 static int temp = -1;
206 title_blitants(4 + pos);
210 ply1.joy_n = ply1.joy_e = ply1.joy_s = ply1.joy_w = 0;
219 if (ply1.joy_e && player_level < 99)
225 if (ply1.joy_w && player_level > 0)
231 if (ply1.joy_n && pos > 0)
235 title_blitants(4 + pos);
238 if (ply1.joy_s && pos < 4)
242 title_blitants(4 + pos);
246 temp = (ply1.joy_n || ply1.joy_e || ply1.joy_s || ply1.joy_w ||
253 int game_play_loop(byte action)
257 if (input_die || input_esc)
267 frame = (frame - 1) & 7;
293 void game_menu_init(void)
300 for (i = 0; i < 7; i++)
301 drawmenu(i); /* display all lines */
303 title_blitants(4 + pos);
307 game_menu_loop(1, 0);
310 int game_loop(byte action)
315 printf("::: action == 0x%02x\n", action);
318 if (em_game_status == EM_GAME_STATUS_MENU)
320 int result = game_menu_loop(0, action);
333 em_game_status = EM_GAME_STATUS_PLAY;
334 if (game_play_init(player_level, NULL) != 0)
335 em_game_status = EM_GAME_STATUS_MENU;
338 else if (em_game_status == EM_GAME_STATUS_PLAY)
340 if (game_play_loop(action) != 0)
347 em_game_status = EM_GAME_STATUS_MENU;
358 int result = game_menu_loop(0);
367 em_game_status = EM_GAME_STATUS_PLAY;
368 if (game_play_init(player_level) != 0)
373 if (game_play_loop() != 0)
377 em_game_status = EM_GAME_STATUS_MENU;
385 /* read input device for players
387 void readjoy(byte action)
389 unsigned int north = 0, east = 0, south = 0, west = 0, fire = 0;
393 if (action & JOY_LEFT)
396 if (action & JOY_RIGHT)
402 if (action & JOY_DOWN)
405 if (action & JOY_BUTTON_1)
408 if (action & JOY_BUTTON_2)
415 for (i = 0; i < 3; i++)
416 if (keymatrix[northKeyCode[i] >> 3] & 1 << (northKeyCode[i] & 7))
419 for (i = 0; i < 3; i++)
420 if (keymatrix[eastKeyCode[i] >> 3] & 1 << (eastKeyCode[i] & 7))
423 for (i = 0; i < 3; i++)
424 if (keymatrix[southKeyCode[i] >> 3] & 1 << (southKeyCode[i] & 7))
427 for (i = 0; i < 3; i++)
428 if (keymatrix[westKeyCode[i] >> 3] & 1 << (westKeyCode[i] & 7))
431 for (i = 0; i < 3; i++)
432 if (keymatrix[fireKeyCode[i] >> 3] & 1 << (fireKeyCode[i] & 7))
436 ply1.joy_fire = fire;
437 if (ply1.joy_stick || (north | east | south | west))
446 /* handle events from x windows and block until the next frame
448 void input_eventloop(void)
459 XPeekEvent(display, &event); /* block until an event arrives */
461 if (gettimeofday(&tv1, 0) == -1)
467 XSync(display, False); /* block until all graphics are drawn */
469 if (gettimeofday(&tv2, 0) == -1)
472 count = tv2.tv_usec + 1000000 - tv1.tv_usec;
473 if (count >= 1000000)
476 tv1.tv_usec = tv2.tv_usec;
480 tv2.tv_usec = 25000 - count;
482 select(0, 0, 0, 0, &tv2); /* sleep a bit */
491 lastKeySym = NoSymbol;
494 while (XPending(display))
496 /* drain the event queue */
497 XNextEvent(display, &event);
499 switch(event.xany.type)
502 XLookupString(&event.xkey, (char *)&count, 1, &lastKeySym, 0);
506 if (event.xexpose.window == xwindow && event.xexpose.count == 0)
511 if (event.xclient.window == xwindow &&
512 (Atom)event.xclient.data.l[0] == deleteAtom)
517 if (event.xcrossing.window == xwindow)
522 if (event.xcrossing.window == xwindow)
528 XQueryKeymap(display, keymatrix); /* read the keyboard */
532 for (i = 0; i < 1; i++)
533 if (keymatrix[escKeyCode[i] >> 3] & 1 << (escKeyCode[i] & 7))