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 struct LevelInfo_EM native_em_level;
33 unsigned short **Boom;
34 unsigned short **Cave;
35 unsigned short **Next;
36 unsigned short **Draw;
38 static unsigned short *Index[4][HEIGHT];
39 static unsigned short Array[4][HEIGHT][WIDTH];
42 static int input_pause;
43 static int input_refresh;
46 static struct timeval tv1;
47 static struct timeval tv2;
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)
156 if (filename != NULL)
160 player_level = level_nr;
165 if (!LoadNativeLevel_EM(filename))
171 else /* !!! SOON OBSOLETE !!! */
173 char name[MAXNAME+2];
176 snprintf(name, MAXNAME+2, "%s/lev%02d", EM_LVL_DIR, player_level);
179 snprintf_overflow("read a level in cave/");
181 if (cave_convert(name) != 0)
193 int game_menu_loop(boolean init, byte action)
195 static int temp = -1;
219 ply1.joy_n = ply1.joy_e = ply1.joy_s = ply1.joy_w = 0;
228 if (ply1.joy_e && player_level < 99)
234 if (ply1.joy_w && player_level > 0)
240 if (ply1.joy_n && pos > 0)
246 if (ply1.joy_s && pos < 4)
253 temp = (ply1.joy_n || ply1.joy_e || ply1.joy_s || ply1.joy_w ||
260 int game_play_loop(byte action)
264 if (input_die || input_esc)
274 frame = (frame - 1) & 7;
300 void game_menu_init(void)
306 for (i = 0; i < 7; i++)
307 drawmenu(i); /* display all lines */
312 game_menu_loop(1, 0);
315 int game_loop(byte action)
318 printf("::: action == 0x%02x\n", action);
321 if (em_game_status == EM_GAME_STATUS_MENU)
323 int result = game_menu_loop(0, action);
336 em_game_status = EM_GAME_STATUS_PLAY;
337 if (game_play_init(player_level, NULL) != 0)
338 em_game_status = EM_GAME_STATUS_MENU;
341 else if (em_game_status == EM_GAME_STATUS_PLAY)
343 if (game_play_loop(action) != 0)
350 em_game_status = EM_GAME_STATUS_MENU;
359 /* read input device for players */
361 void readjoy(byte action)
363 unsigned int north = 0, east = 0, south = 0, west = 0, fire = 0;
365 if (action & JOY_LEFT)
368 if (action & JOY_RIGHT)
374 if (action & JOY_DOWN)
377 if (action & JOY_BUTTON_1)
381 if (action & JOY_BUTTON_2)
385 ply1.joy_fire = fire;
386 if (ply1.joy_stick || (north | east | south | west))
396 /* handle events from x windows and block until the next frame */
398 void input_eventloop(void)
404 XSync(display, False); /* block until all graphics are drawn */
406 if (gettimeofday(&tv2, 0) == -1)
409 count = tv2.tv_usec + 1000000 - tv1.tv_usec;
410 if (count >= 1000000)
413 tv1.tv_usec = tv2.tv_usec;
417 tv2.tv_usec = 25000 - count;
419 select(0, 0, 0, 0, &tv2); /* sleep a bit */