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 int player_mode;
48 static char player_name[32];
49 static int player_level;
51 static void drawmenu(int pos)
61 strcpy(buffer, "quit emerald mine");
65 strcpy(buffer, "single player");
69 strcpy(buffer, "teamwork");
73 title_string(4, 0, 40, buffer);
77 sprintf(buffer, "name: %s", player_name);
78 title_string(5, 0, 40, buffer);
82 sprintf(buffer, "level: %d", player_level);
83 title_string(6, 0, 40, buffer);
87 title_string(7, 0, 40, "highscores");
91 title_string(8, 0, 40, "start");
95 title_string(10, 3, 13, "played");
96 title_string(10, 14, 26, "score");
97 title_string(10, 27, 37, "won");
101 sprintf(buffer, "%d", 0);
102 title_string(11, 3, 13, buffer);
103 sprintf(buffer, "%d", 0);
104 title_string(11, 14, 26, buffer);
105 sprintf(buffer, "%d", 0);
106 title_string(11, 27, 37, buffer);
111 void game_init_vars(void)
117 for (y = 0; y < HEIGHT; y++)
118 for (x = 0; x < WIDTH; x++)
119 Array[0][y][x] = ZBORDER;
120 for (y = 0; y < HEIGHT; y++)
121 for (x = 0; x < WIDTH; x++)
122 Array[1][y][x] = ZBORDER;
123 for (y = 0; y < HEIGHT; y++)
124 for (x = 0; x < WIDTH; x++)
125 Array[2][y][x] = ZBORDER;
126 for (y = 0; y < HEIGHT; y++)
127 for (x = 0; x < WIDTH; x++)
128 Array[3][y][x] = Xblank;
130 for (y = 0; y < HEIGHT; y++)
131 Index[0][y] = Array[0][y];
132 for (y = 0; y < HEIGHT; y++)
133 Index[1][y] = Array[1][y];
134 for (y = 0; y < HEIGHT; y++)
135 Index[2][y] = Array[2][y];
136 for (y = 0; y < HEIGHT; y++)
137 Index[3][y] = Array[3][y];
144 player_mode = 1; /* start off as single player */
145 strcpy(player_name, "dave");
151 int game_play_init(int level_nr, char *filename)
153 if (filename != NULL)
155 player_level = level_nr;
157 if (cave_convert(filename) != 0)
160 else /* !!! SOON OBSOLETE !!! */
162 char name[MAXNAME+2];
165 snprintf(name, MAXNAME+2, "%s/lev%02d", EM_LVL_DIR, player_level);
168 snprintf_overflow("read a level in cave/");
170 if (cave_convert(name) != 0)
181 int game_menu_loop(boolean init, byte action)
183 static int temp = -1;
207 ply1.joy_n = ply1.joy_e = ply1.joy_s = ply1.joy_w = 0;
216 if (ply1.joy_e && player_level < 99)
222 if (ply1.joy_w && player_level > 0)
228 if (ply1.joy_n && pos > 0)
234 if (ply1.joy_s && pos < 4)
241 temp = (ply1.joy_n || ply1.joy_e || ply1.joy_s || ply1.joy_w ||
248 int game_play_loop(byte action)
252 if (input_die || input_esc)
262 frame = (frame - 1) & 7;
288 void game_menu_init(void)
294 for (i = 0; i < 7; i++)
295 drawmenu(i); /* display all lines */
300 game_menu_loop(1, 0);
303 int game_loop(byte action)
306 printf("::: action == 0x%02x\n", action);
309 if (em_game_status == EM_GAME_STATUS_MENU)
311 int result = game_menu_loop(0, action);
324 em_game_status = EM_GAME_STATUS_PLAY;
325 if (game_play_init(player_level, NULL) != 0)
326 em_game_status = EM_GAME_STATUS_MENU;
329 else if (em_game_status == EM_GAME_STATUS_PLAY)
331 if (game_play_loop(action) != 0)
338 em_game_status = EM_GAME_STATUS_MENU;
347 /* read input device for players */
349 void readjoy(byte action)
351 unsigned int north = 0, east = 0, south = 0, west = 0, fire = 0;
353 if (action & JOY_LEFT)
356 if (action & JOY_RIGHT)
362 if (action & JOY_DOWN)
365 if (action & JOY_BUTTON_1)
369 if (action & JOY_BUTTON_2)
373 ply1.joy_fire = fire;
374 if (ply1.joy_stick || (north | east | south | west))
384 /* handle events from x windows and block until the next frame */
386 void input_eventloop(void)
392 XSync(display, False); /* block until all graphics are drawn */
394 if (gettimeofday(&tv2, 0) == -1)
397 count = tv2.tv_usec + 1000000 - tv1.tv_usec;
398 if (count >= 1000000)
401 tv1.tv_usec = tv2.tv_usec;
405 tv2.tv_usec = 25000 - count;
407 select(0, 0, 0, 0, &tv2); /* sleep a bit */