-
Notifications
You must be signed in to change notification settings - Fork 1
/
gp2x.cpp
331 lines (298 loc) · 9.34 KB
/
gp2x.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//
// GP2X specific code
//
// by Pickle
//
#ifdef GP2X
#include "gp2x.h"
static unsigned int screenshot_count = 0;
static int volume = 30;
static int intUp = 0;
static int intDown = 0;
static int intLeft = 0;
static int intRight = 0;
static int intUpRight = 0;
static int intUpLeft = 0;
static int intDownRight = 0;
static int intDownLeft = 0;
static int intButtonR = 0;
static int intButtonL = 0;
static int intButtonA = 0;
static int intButtonB = 0;
static int intButtonX = 0;
static int intButtonY = 0;
static int intButtonSel = 0;
static int intButtonSrt = 0;
static int intButtonStick = 0;
void GP2X_Init()
{
GP2X_StartMMUHack();
GP2X_AdjustVolume(VOLUME_NOCHG);
}
void GP2X_Shutdown()
{
system("/sbin/rmmod mmuhack");
}
void GP2X_StartMMUHack()
{
system("/sbin/rmmod mmuhack");
system("/sbin/insmod mmuhack.o");
int mmufd = open("/dev/mmuhack", O_RDWR);
if(mmufd < 0)
{
printf("MMU hack failed\n");
}
else
{
printf("MMU hack loaded\n");
close(mmufd);
}
}
void GP2X_AdjustVolume( int direction )
{
if( volume <= 10 )
{
if( direction == VOLUME_UP ) volume += VOLUME_CHANGE_RATE/2;
if( direction == VOLUME_DOWN ) volume -= VOLUME_CHANGE_RATE/2;
}
else
{
if( direction == VOLUME_UP ) volume += VOLUME_CHANGE_RATE;
if( direction == VOLUME_DOWN ) volume -= VOLUME_CHANGE_RATE;
}
if( volume < VOLUME_MIN ) volume = VOLUME_MIN;
if( volume > VOLUME_MAX ) volume = VOLUME_MAX;
printf( "Volume Change: %i\n", volume );
unsigned long soundDev = open("/dev/mixer", O_RDWR);
if(soundDev)
{
int vol = ((volume << 8) | volume);
ioctl(soundDev, SOUND_MIXER_WRITE_PCM, &vol);
close(soundDev);
}
}
void GP2X_ButtonDown( int button )
{
switch( button )
{
case GP2X_BUTTON_UP: intUp = 1; break;
case GP2X_BUTTON_DOWN: intDown = 1; break;
case GP2X_BUTTON_RIGHT: intRight = 1; break;
case GP2X_BUTTON_LEFT: intLeft = 1; break;
case GP2X_BUTTON_UPRIGHT: intUpRight = 1; break;
case GP2X_BUTTON_UPLEFT: intUpLeft = 1; break;
case GP2X_BUTTON_DOWNRIGHT: intDownRight = 1; break;
case GP2X_BUTTON_DOWNLEFT: intDownLeft = 1; break;
case GP2X_BUTTON_SELECT: intButtonSel = 1; break;
case GP2X_BUTTON_START: intButtonSrt = 1; break;
case GP2X_BUTTON_X: intButtonX = 1; LastASCII = 'x'; break;
case GP2X_BUTTON_Y: intButtonY = 1; LastASCII = 'y'; break;
case GP2X_BUTTON_A: intButtonA = 1; LastASCII = 'a'; break;
case GP2X_BUTTON_B: intButtonB = 1; LastASCII = 'b'; break;
case GP2X_BUTTON_R: intButtonR = 1; break;
case GP2X_BUTTON_L: intButtonL = 1; break;
case GP2X_BUTTON_VOLUP: GP2X_AdjustVolume( VOLUME_UP ); break;
case GP2X_BUTTON_VOLDOWN: GP2X_AdjustVolume( VOLUME_DOWN ); break;
case GP2X_BUTTON_CLICK: intButtonStick = 1; break;
}
if( intButtonL & intButtonR )
{
// Status Bar
SetKeyboard( SDLK_TAB, KEY_DOWN );
// Music Player (doesnt work, it appears the event's arnt happening soon enough)
SetKeyboard( sc_M, KEY_DOWN );
SetKeyboard( SDLK_LALT, KEY_UP );
SetKeyboard( SDLK_LEFT, KEY_UP );
SetKeyboard( SDLK_RIGHT, KEY_UP );
}
else if( intButtonL & !intButtonR )
{
// Strafe Left
SetKeyboard( SDLK_LALT, KEY_DOWN );
SetKeyboard( SDLK_LEFT, KEY_DOWN );
}
else if( intButtonR & !intButtonL )
{
// Strafe Right
SetKeyboard( SDLK_LALT, KEY_DOWN );
SetKeyboard( SDLK_RIGHT, KEY_DOWN );
}
// Left Direction
if( intLeft | intDownLeft | intUpLeft )
{
// UNstrafe
SetKeyboard( SDLK_LALT, KEY_UP );
SetKeyboard( SDLK_RIGHT, KEY_UP );
// Turn
SetKeyboard( SDLK_LEFT, KEY_DOWN );
}
// Right Direction
if( intRight | intDownRight | intUpRight )
{
// UNstrafe
SetKeyboard( SDLK_LALT, KEY_UP );
SetKeyboard( SDLK_LEFT, KEY_UP );
// Turn
SetKeyboard( SDLK_RIGHT, KEY_DOWN );
}
// Up Direction
if( intUp | intUpRight | intUpLeft ) {
SetKeyboard( SDLK_UP, KEY_DOWN );
}
// Down Direction
if( intDown | intDownRight | intDownLeft ) {
SetKeyboard( SDLK_DOWN, KEY_DOWN );
}
if( intButtonSel & intButtonSrt ) {
// Pause
SetKeyboard( SDLK_PAUSE, KEY_DOWN );
}
else if( intButtonStick & intButtonSel ) {
fpscounter ^= 1; // Turn On FPS Counter
}
else if( intButtonStick & intButtonSrt ) {
Screenshot();
}
else if( intButtonSel & !intButtonSrt ) {
// Escape
SetKeyboard( SDLK_ESCAPE, KEY_DOWN );
}
else if( !intButtonSel & intButtonSrt ) {
// Enter
SetKeyboard( SDLK_RETURN, KEY_DOWN );
}
if( intButtonX ) {
// Shoot
SetKeyboard( SDLK_LCTRL, KEY_DOWN );
}
if( intButtonY ) {
// Yes
SetKeyboard( SDLK_y, KEY_DOWN );
if( gamestate.chosenweapon == gamestate.bestweapon )
{
SetKeyboard( SDLK_1, KEY_DOWN );
}
else
{
SetKeyboard( SDLK_1 + gamestate.chosenweapon + 1, KEY_DOWN );
}
}
if( intButtonA ) {
// Open
SetKeyboard( SDLK_SPACE, KEY_DOWN );
}
if( intButtonB ) {
// No
SetKeyboard( SDLK_n, KEY_DOWN );
// Run
SetKeyboard( SDLK_LSHIFT, KEY_DOWN );
}
}
void GP2X_ButtonUp( int button )
{
switch( button )
{
case GP2X_BUTTON_UP: intUp = 0; break;
case GP2X_BUTTON_DOWN: intDown = 0; break;
case GP2X_BUTTON_RIGHT: intRight = 0; break;
case GP2X_BUTTON_LEFT: intLeft = 0; break;
case GP2X_BUTTON_UPRIGHT: intUpRight = 0; break;
case GP2X_BUTTON_UPLEFT: intUpLeft = 0; break;
case GP2X_BUTTON_DOWNRIGHT: intDownRight = 0; break;
case GP2X_BUTTON_DOWNLEFT: intDownLeft = 0; break;
case GP2X_BUTTON_SELECT: intButtonSel = 0; break;
case GP2X_BUTTON_START: intButtonSrt = 0; break;
case GP2X_BUTTON_X: intButtonX = 0; break;
case GP2X_BUTTON_Y: intButtonY = 0; break;
case GP2X_BUTTON_A: intButtonA = 0; break;
case GP2X_BUTTON_B: intButtonB = 0; break;
case GP2X_BUTTON_R: intButtonR = 0; break;
case GP2X_BUTTON_L: intButtonL = 0; break;
case GP2X_BUTTON_CLICK: intButtonStick = 0; break;
}
if( !intButtonL | !intButtonR )
{
SetKeyboard( SDLK_TAB, KEY_UP );
SetKeyboard( sc_M, KEY_UP );
SetKeyboard( SDLK_LALT, KEY_UP );
}
if( !intLeft & !intDownLeft & !intUpLeft )
{
if( !intButtonL )
{
SetKeyboard( SDLK_LALT, KEY_UP );
SetKeyboard( SDLK_LEFT, KEY_UP );
}
if( intButtonR )
{
SetKeyboard( SDLK_LALT, KEY_DOWN );
SetKeyboard( SDLK_RIGHT, KEY_DOWN );
}
}
if( !intRight & !intDownRight & !intUpRight )
{
if( !intButtonR )
{
SetKeyboard( SDLK_LALT, KEY_UP );
SetKeyboard( SDLK_RIGHT, KEY_UP );
}
if( intButtonL )
{
SetKeyboard( SDLK_LALT, KEY_DOWN );
SetKeyboard( SDLK_LEFT, KEY_DOWN );
}
}
if( !intUp & !intUpRight & !intUpLeft ) {
SetKeyboard( SDLK_UP, KEY_UP );
}
if( !intDown & !intDownRight & !intDownLeft ) {
SetKeyboard( SDLK_DOWN, KEY_UP );
}
if( !intButtonSel & !intButtonSrt ) {
SetKeyboard( SDLK_PAUSE, KEY_UP );
}
if( !intButtonSel ) {
SetKeyboard( SDLK_ESCAPE, KEY_UP );
}
if( !intButtonSrt ) {
SetKeyboard( SDLK_RETURN, KEY_UP );
}
if( !intButtonX ) {
SetKeyboard( SDLK_LCTRL, KEY_UP );
}
if( !intButtonY ) {
SetKeyboard( SDLK_y, KEY_UP );
SetKeyboard( SDLK_1, KEY_UP );
SetKeyboard( SDLK_2, KEY_UP );
SetKeyboard( SDLK_3, KEY_UP );
SetKeyboard( SDLK_4, KEY_UP );
}
if( !intButtonA ) {
SetKeyboard( SDLK_SPACE, KEY_UP );
}
if( !intButtonB ) {
SetKeyboard( SDLK_n, KEY_UP );
SetKeyboard( SDLK_LSHIFT, KEY_UP );
}
}
void Screenshot( void )
{
char filename[32];
snprintf( filename, sizeof(filename), "Screenshot_%i.bmp", screenshot_count );
SDL_SaveBMP(screen, filename );
screenshot_count++;
}
void SetKeyboard( unsigned int key, int press )
{
// press = 1 = down, press = 0 = up
if( press )
{
LastScan = key;
Keyboard[key] = 1;
}
else
{
Keyboard[key] = 0;
}
}
#endif // GP2X