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>
28 unsigned short **Boom;
29 unsigned short **Cave;
30 unsigned short **Next;
31 unsigned short **Draw;
33 static unsigned short *Index[4][HEIGHT];
34 static unsigned short Array[4][HEIGHT][WIDTH];
37 static int input_pause;
38 static int input_refresh;
41 static struct timeval tv1;
42 static struct timeval tv2;
44 static unsigned char keymatrix[32];
46 static int player_mode;
47 static char player_name[32];
48 static int player_level;
50 static void drawmenu(int pos)
56 case 0: strcpy(buffer, "quit emerald mine"); break;
57 case 1: strcpy(buffer, "single player"); break;
58 case 2: strcpy(buffer, "teamwork"); break;
60 title_string(4, 0, 40, buffer);
63 sprintf(buffer, "name: %s", player_name);
64 title_string(5, 0, 40, buffer);
67 sprintf(buffer, "level: %d", player_level);
68 title_string(6, 0, 40, buffer);
71 title_string(7, 0, 40, "highscores");
74 title_string(8, 0, 40, "start");
77 title_string(10, 1, 13, "played");
78 title_string(10, 14, 26, "score");
79 title_string(10, 27, 39, "won");
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);
92 /* bring it all together */
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;
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];
117 player_mode = 1; /* start off as single player */
118 strcpy(player_name, "dave");
129 for(temp = 0; temp < 7; temp++) drawmenu(temp); /* display all lines */
130 title_blitants(4 + pos);
136 printf("--> M1: xwindow == %ld\n", xwindow);
142 if(input_die) return(0);
143 if(input_refresh) blitscreen();
146 printf("--> M2: xwindow == %ld\n", xwindow);
150 title_blitants(4 + pos);
155 printf("--> M3: xwindow == %ld\n", xwindow);
158 ply1.joy_n = ply1.joy_e = ply1.joy_s = ply1.joy_w = 0;
163 if(ply1.joy_fire) break;
165 if(ply1.joy_e && player_level < 99) {
169 if(ply1.joy_w && player_level > 0) {
173 if(ply1.joy_n && pos > 0) {
176 title_blitants(4 + pos);
178 if(ply1.joy_s && pos < 4) {
181 title_blitants(4 + pos);
184 temp = (ply1.joy_n || ply1.joy_e || ply1.joy_s || ply1.joy_w || ply1.joy_fire);
189 printf("--> M4: xwindow == %ld\n", xwindow);
193 snprintf(name, MAXNAME+2, "%s/lev%02d", EM_LVL_DIR, player_level);
194 if(name[MAXNAME]) snprintf_overflow("read a level in cave/");
196 if(cave_convert(name)) continue;
199 printf("--> M5: xwindow == %ld\n", xwindow);
204 printf("--> M6: xwindow == %ld\n", xwindow);
208 printf("--> M7: xwindow == %ld\n", xwindow);
213 printf("--> M8: xwindow == %ld\n", xwindow);
218 if(input_die || input_esc) break;
219 if(input_refresh) blitscreen();
224 frame = (frame - 1) & 7;
242 /* read input device for players
247 unsigned int north = 0, east = 0, south = 0, west = 0, fire = 0;
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;
255 ply1.joy_fire = fire;
256 if(ply1.joy_stick || (north | east | south | west)) {
264 /* handle events from x windows and block until the next frame
266 void input_eventloop(void)
273 XPeekEvent(display, &event); /* block until an event arrives */
275 if(gettimeofday(&tv1, 0) == -1) tv1.tv_usec = 0;
277 XSync(display, False); /* block until all graphics are drawn */
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;
284 tv2.tv_usec = 25000 - count;
286 select(0, 0, 0, 0, &tv2); /* sleep a bit */
295 lastKeySym = NoSymbol;
296 while(XPending(display)) { /* drain the event queue */
297 XNextEvent(display, &event);
298 switch(event.xany.type) {
300 XLookupString(&event.xkey, (char *)&count, 1, &lastKeySym, 0);
303 if(event.xexpose.window == xwindow && event.xexpose.count == 0) input_refresh = 1;
306 if(event.xclient.window == xwindow && (Atom)event.xclient.data.l[0] == deleteAtom) input_die = 1;
309 if(event.xcrossing.window == xwindow) input_pause = 0;
312 if(event.xcrossing.window == xwindow) input_pause = 1;
316 XQueryKeymap(display, keymatrix); /* read the keyboard */
319 for(i = 0; i < 1; i++) if(keymatrix[escKeyCode[i] >> 3] & 1 << (escKeyCode[i] & 7)) input_esc = 1;