Subversion Repositories Games.Rick Dangerous

Rev

Rev 10 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 pmbaty 1
/*
2
 * src/sysevt.c
3
 *
4
 * Copyright (C) 1998-2002 BigOrno (bigorno@bigorno.net). All rights reserved.
5
 *
6
 * The use and distribution terms for this software are contained in the file
7
 * named README, which can be found in the root of this distribution. By
8
 * using this software in any fashion, you are agreeing to be bound by the
9
 * terms of this license.
10
 *
11
 * You must not remove this notice, or any other, from this software.
12
 */
13
 
11 pmbaty 14
#include <SDL2/SDL.h>
15
#include <string.h>
16
#include <ctype.h>
1 pmbaty 17
 
18
#include "system.h"
19
#include "game.h"
20
 
21
#include "control.h"
22
#include "draw.h"
23
 
24
#define SYSJOY_RANGE 3280
25
 
11 pmbaty 26
static char character_ringbuffer[16] = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
27
static int character_ringbuffer_index = 0;
28
extern U8 want_infinitelives;
29
extern U8 want_infiniteammo;
30
 
1 pmbaty 31
static SDL_Event event;
32
extern U8 want_filter;
33
extern U8 want_fullscreen;
10 pmbaty 34
extern U8 enable_endkey;
7 pmbaty 35
extern U8 recreate_screen;
1 pmbaty 36
 
37
 
11 pmbaty 38
static U8 has_accumulated (const char *str)
39
{
40
   int len = (int) strlen (str);
41
   for (int i = 0; i < len; i++)
42
      if (character_ringbuffer[(sizeof (character_ringbuffer) + character_ringbuffer_index - len + i) % 16] != str[i])
43
         return 0;
44
   return 1;
45
}
46
 
47
 
1 pmbaty 48
/*
49
 * Process an event
50
 */
51
static void processEvent (void)
52
{
53
   static U8 alt_pressed = FALSE;
54
 
7 pmbaty 55
   U16 key = event.key.keysym.scancode;
1 pmbaty 56
 
57
   switch (event.type)
58
   {
59
   case SDL_KEYDOWN:
7 pmbaty 60
      if (key == SDL_SCANCODE_LALT)
1 pmbaty 61
         alt_pressed = TRUE;
7 pmbaty 62
      else if (alt_pressed && key == SDL_SCANCODE_RETURN)
1 pmbaty 63
      {
64
         want_fullscreen ^= TRUE;
7 pmbaty 65
         recreate_screen = TRUE;
1 pmbaty 66
      }
7 pmbaty 67
      else if (alt_pressed && key == SDL_SCANCODE_F4)
1 pmbaty 68
      {
69
         control_status |= CONTROL_EXIT;
70
         control_last = CONTROL_EXIT;
71
      }
11 pmbaty 72
      else if ((key == SDL_SCANCODE_O || key == SDL_SCANCODE_UP) && !has_accumulated ("MAT") && !has_accumulated ("MATOSGRAT") && !has_accumulated (";QT") && !has_accumulated (";QTOSGRQT"))
1 pmbaty 73
      {
74
         control_status |= CONTROL_UP;
75
         control_last = CONTROL_UP;
76
      }
7 pmbaty 77
      else if (key == SDL_SCANCODE_K || key == SDL_SCANCODE_DOWN)
1 pmbaty 78
      {
79
         control_status |= CONTROL_DOWN;
80
         control_last = CONTROL_DOWN;
81
      }
7 pmbaty 82
      else if (key == SDL_SCANCODE_Z || key == SDL_SCANCODE_LEFT)
1 pmbaty 83
      {
84
         control_status |= CONTROL_LEFT;
85
         control_last = CONTROL_LEFT;
86
      }
7 pmbaty 87
      else if (key == SDL_SCANCODE_X || key == SDL_SCANCODE_RIGHT)
1 pmbaty 88
      {
89
         control_status |= CONTROL_RIGHT;
90
         control_last = CONTROL_RIGHT;
91
      }
11 pmbaty 92
      else if (((key == SDL_SCANCODE_PAUSE) || (key == SDL_SCANCODE_P)) && !has_accumulated ("MEME") && !has_accumulated (";E;E"))
1 pmbaty 93
      {
94
         control_status |= CONTROL_PAUSE;
95
         control_last = CONTROL_PAUSE;
96
      }
10 pmbaty 97
      else if ((key == SDL_SCANCODE_END) && enable_endkey)
1 pmbaty 98
      {
99
         control_status |= CONTROL_END;
100
         control_last = CONTROL_END;
101
      }
7 pmbaty 102
      else if (key == SDL_SCANCODE_ESCAPE)
1 pmbaty 103
      {
104
         control_status |= CONTROL_EXIT;
105
         control_last = CONTROL_EXIT;
106
      }
7 pmbaty 107
      else if (key == SDL_SCANCODE_SPACE)
1 pmbaty 108
      {
109
         control_status |= CONTROL_FIRE;
110
         control_last = CONTROL_FIRE;
111
      }
7 pmbaty 112
      else if (key == SDL_SCANCODE_F4)
1 pmbaty 113
         syssnd_toggleMute();
7 pmbaty 114
      else if (key == SDL_SCANCODE_F5)
1 pmbaty 115
         syssnd_vol(-1);
7 pmbaty 116
      else if (key == SDL_SCANCODE_F6)
1 pmbaty 117
         syssnd_vol(+1);
7 pmbaty 118
      else if (key == SDL_SCANCODE_F7)
1 pmbaty 119
         want_filter ^= TRUE;
7 pmbaty 120
      else if (key == SDL_SCANCODE_F10)
1 pmbaty 121
      {
122
         control_status &= ~CONTROL_EXIT;
123
         control_last = CONTROL_EXIT;
124
      }
125
      break;
126
   case SDL_KEYUP:
7 pmbaty 127
      if (key == SDL_SCANCODE_LALT)
1 pmbaty 128
         alt_pressed = FALSE;
7 pmbaty 129
      else if (key == SDL_SCANCODE_O || key == SDL_SCANCODE_UP)
1 pmbaty 130
      {
131
         control_status &= ~CONTROL_UP;
132
         control_last = CONTROL_UP;
133
      }
7 pmbaty 134
      else if (key == SDL_SCANCODE_K || key == SDL_SCANCODE_DOWN)
1 pmbaty 135
      {
136
         control_status &= ~CONTROL_DOWN;
137
         control_last = CONTROL_DOWN;
138
      }
7 pmbaty 139
      else if (key == SDL_SCANCODE_Z || key == SDL_SCANCODE_LEFT)
1 pmbaty 140
      {
141
         control_status &= ~CONTROL_LEFT;
142
         control_last = CONTROL_LEFT;
143
      }
7 pmbaty 144
      else if (key == SDL_SCANCODE_X || key == SDL_SCANCODE_RIGHT)
1 pmbaty 145
      {
146
         control_status &= ~CONTROL_RIGHT;
147
         control_last = CONTROL_RIGHT;
148
      }
10 pmbaty 149
      else if ((key == SDL_SCANCODE_PAUSE) || (key == SDL_SCANCODE_P))
1 pmbaty 150
      {
151
         control_status &= ~CONTROL_PAUSE;
152
         control_last = CONTROL_PAUSE;
153
      }
10 pmbaty 154
      else if ((key == SDL_SCANCODE_END) && enable_endkey)
1 pmbaty 155
      {
156
         control_status &= ~CONTROL_END;
157
         control_last = CONTROL_END;
158
      }
7 pmbaty 159
      else if (key == SDL_SCANCODE_ESCAPE)
1 pmbaty 160
      {
161
         control_status &= ~CONTROL_EXIT;
162
         control_last = CONTROL_EXIT;
163
      }
7 pmbaty 164
      else if (key == SDL_SCANCODE_SPACE)
1 pmbaty 165
      {
166
         control_status &= ~CONTROL_FIRE;
167
         control_last = CONTROL_FIRE;
168
      }
11 pmbaty 169
      // cheat codes:
170
      // 'MEMEPASMAL' -> infinite lives
171
      // 'MATOSGRATOS' -> infinite ammo
172
      {
173
         const char *keyname = SDL_GetScancodeName (key);
174
         character_ringbuffer[character_ringbuffer_index] = toupper (keyname[0]);
175
         character_ringbuffer_index = (character_ringbuffer_index + 1) % 16;
176
         if (has_accumulated ("MEMEPASMAL") || has_accumulated (";E;EPQS;QL"))
177
         {
178
            want_infinitelives = TRUE;
179
            game_lives = 6;
180
            syssnd_play (WAV_DIE, 1);
181
         }
182
         else if (has_accumulated ("MATOSGRATOS") || has_accumulated (";QTOSGRQTOS"))
183
         {
184
            want_infiniteammo = TRUE;
185
            game_bullets = 6;
186
            game_bombs = 6;
187
            syssnd_play (WAV_DIE, 1);
188
         }
189
      }
1 pmbaty 190
      break;
191
   case SDL_QUIT:
192
      /* player tries to close the window -- this is the same as pressing ESC */
193
      control_status |= CONTROL_EXIT;
194
      control_last = CONTROL_EXIT;
195
      break;
196
   case SDL_JOYAXISMOTION:
197
      if (event.jaxis.axis == 0)
198
      {
199
         /* left-right */
200
         if (event.jaxis.value < -SYSJOY_RANGE)
201
         {   /* left */
202
            control_status |= CONTROL_LEFT;
203
            control_status &= ~CONTROL_RIGHT;
204
         }
205
         else if (event.jaxis.value > SYSJOY_RANGE)
206
         {   /* right */
207
            control_status |= CONTROL_RIGHT;
208
            control_status &= ~CONTROL_LEFT;
209
         }
210
         else
211
         {   /* center */
212
            control_status &= ~CONTROL_RIGHT;
213
            control_status &= ~CONTROL_LEFT;
214
         }
215
      }
216
      if (event.jaxis.axis == 1)
217
      {
218
         /* up-down */
219
         if (event.jaxis.value < -SYSJOY_RANGE)
220
         {   /* up */
221
            control_status |= CONTROL_UP;
222
            control_status &= ~CONTROL_DOWN;
223
         }
224
         else if (event.jaxis.value > SYSJOY_RANGE)
225
         {   /* down */
226
            control_status |= CONTROL_DOWN;
227
            control_status &= ~CONTROL_UP;
228
         }
229
         else
230
         {   /* center */
231
            control_status &= ~CONTROL_DOWN;
232
            control_status &= ~CONTROL_UP;
233
         }
234
      }
235
      break;
236
   case SDL_JOYBUTTONDOWN:
237
      control_status |= CONTROL_FIRE;
238
      break;
239
   case SDL_JOYBUTTONUP:
240
      control_status &= ~CONTROL_FIRE;
241
      break;
242
   default:
243
      break;
244
   }
245
}
246
 
247
 
248
/*
249
 * Process events, if any, then return
250
 */
251
void sysevt_poll (void)
252
{
253
   while (SDL_PollEvent (&event))
254
      processEvent ();
255
}
256
 
257
 
258
/*
259
 * Wait for an event, then process it and return
260
 */
261
void sysevt_wait (void)
262
{
263
   SDL_WaitEvent (&event);
264
   processEvent ();
265
}