rnd-20040918-1-src
[rocksndiamonds.git] / src / game_em / input.c
1 /* 2000-08-13T15:29:40Z
2  *
3  * handle input from x11 and keyboard and joystick
4  */
5
6 #include <X11/Xlib.h>
7 #include <X11/Xutil.h>
8 #include <X11/Xatom.h>
9 #include <X11/keysym.h>
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <errno.h>
17
18 #include "global.h"
19 #include "display.h"
20 #include "level.h"
21
22
23 #if defined(TARGET_X11)
24
25 unsigned long Random;
26
27 struct PLAYER ply1;
28 struct PLAYER ply2;
29 struct LEVEL lev;
30
31 struct LevelInfo_EM native_em_level;
32
33 unsigned short **Boom;
34 unsigned short **Cave;
35 unsigned short **Next;
36 unsigned short **Draw;
37
38 static unsigned short *Index[4][HEIGHT];
39 static unsigned short Array[4][HEIGHT][WIDTH];
40
41 void game_init_vars(void)
42 {
43   int x, y;
44
45   Random = 1684108901;
46
47   for (y = 0; y < HEIGHT; y++)
48     for (x = 0; x < WIDTH; x++)
49       Array[0][y][x] = ZBORDER;
50   for (y = 0; y < HEIGHT; y++)
51     for (x = 0; x < WIDTH; x++)
52       Array[1][y][x] = ZBORDER;
53   for (y = 0; y < HEIGHT; y++)
54     for (x = 0; x < WIDTH; x++)
55       Array[2][y][x] = ZBORDER;
56   for (y = 0; y < HEIGHT; y++)
57     for (x = 0; x < WIDTH; x++)
58       Array[3][y][x] = Xblank;
59
60   for (y = 0; y < HEIGHT; y++)
61     Index[0][y] = Array[0][y];
62   for (y = 0; y < HEIGHT; y++)
63     Index[1][y] = Array[1][y];
64   for (y = 0; y < HEIGHT; y++)
65     Index[2][y] = Array[2][y];
66   for (y = 0; y < HEIGHT; y++)
67     Index[3][y] = Array[3][y];
68
69   Cave = Index[0];
70   Next = Index[1];
71   Draw = Index[2];
72   Boom = Index[3];
73 }
74
75 void InitGameEngine_EM()
76 {
77   prepare_em_level();
78
79   game_initscreen();
80   game_animscreen();
81 }
82
83 void GameActions_EM(byte action)
84 {
85   input_eventloop();
86
87   game_animscreen();
88
89   Random = Random * 129 + 1;
90
91   frame = (frame - 1) & 7;
92
93   readjoy(action);
94
95   if (frame == 7)
96   {
97     synchro_1();
98     synchro_2();
99   }
100
101   if (frame == 6)
102   {
103     synchro_3();
104     sound_play();
105
106     DrawGameDoorValues_EM(lev.required, ply1.dynamite, lev.score,
107                           (lev.time + 4) / 5);
108   }
109 }
110
111
112 /* read input device for players */
113
114 void readjoy(byte action)
115 {
116   unsigned int north = 0, east = 0, south = 0, west = 0, fire = 0;
117
118   if (action & JOY_LEFT)
119     west = 1;
120
121   if (action & JOY_RIGHT)
122     east = 1;
123
124   if (action & JOY_UP)
125     north = 1;
126
127   if (action & JOY_DOWN)
128     south = 1;
129
130   if (action & JOY_BUTTON_1)
131     fire = 1;
132
133   ply1.joy_fire = fire;
134   if (ply1.joy_stick || (north | east | south | west))
135   {
136     ply1.joy_n = north;
137     ply1.joy_e = east;
138     ply1.joy_s = south;
139     ply1.joy_w = west;
140   }
141 }
142
143
144 /* handle events from x windows and block until the next frame */
145
146 void input_eventloop(void)
147 {
148   static struct timeval tv1 = { 0, 0 };
149   static struct timeval tv2 = { 0, 0 };
150   unsigned long count;
151
152   XSync(display, False); /* block until all graphics are drawn */
153
154   if (gettimeofday(&tv2, 0) == -1)
155     tv2.tv_usec = 0;
156
157   count = tv2.tv_usec + 1000000 - tv1.tv_usec;
158   if (count >= 1000000)
159     count -= 1000000;
160
161   tv1.tv_usec = tv2.tv_usec;
162   if (count < 25000)
163   {
164     tv2.tv_sec = 0;
165     tv2.tv_usec = 25000 - count;
166 #if 1
167     select(0, 0, 0, 0, &tv2); /* sleep a bit */
168 #else
169     usleep(tv2.tv_usec);
170 #endif
171   }
172 }
173
174 #endif