source: abuse/branches/lol/src/sdlport/event.cpp @ 726

Last change on this file since 726 was 726, checked in by Sam Hocevar, 8 years ago

sdlport: fix a few include paths.

File size: 10.0 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 2001 Anthony Kruize <trandor@labyrinth.net.au>
[724]4 *  Copyright (c) 2005-2013 Sam Hocevar <sam@hocevar.net>
[56]5 *
6 *  This program is free software; you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation; either version 2 of the License, or
9 *  (at your option) any later version.
10 *
11 *  This program is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with this program; if not, write to the Free Software Foundation,
18 *  Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 */
[2]20
[555]21#if defined HAVE_CONFIG_H
22#   include "config.h"
23#endif
[56]24
[726]25#if defined HAVE_SDL_SDL_H
26#   include <SDL/SDL.h>
27#else
28#   include <SDL.h>
29#endif
[2]30
[512]31#include "common.h"
32
[724]33#include "imlib/image.h"
34#include "imlib/palette.h"
35#include "imlib/video.h"
36#include "imlib/event.h"
37
[481]38#include "sprite.h"
39#include "game.h"
[2]40
[643]41extern int get_key_binding(char const *dir, int i);
[2]42short mouse_buttons[5] = { 0, 0, 0, 0, 0 };
43
[682]44void EventHandler::SysWarpMouse(ivec2 pos)
[652]45{
46    SDL_WarpMouse(pos.x, pos.y);
47}
48
[2]49//
[645]50// IsPending()
[2]51// Are there any events in the queue?
52//
[645]53int EventHandler::IsPending()
[2]54{
[645]55    if (!m_pending && SDL_PollEvent(NULL))
56        m_pending = 1;
57
58    return m_pending;
[2]59}
60
61//
62// Get and handle waiting events
63//
[656]64void EventHandler::SysEvent(Event &ev)
[2]65{
[645]66    // No more events
67    m_pending = 0;
68
69    // NOTE : that the mouse status should be known
70    // even if another event has occurred.
[652]71    ev.mouse_move.x = m_pos.x;
72    ev.mouse_move.y = m_pos.y;
73    ev.mouse_button = m_button;
[645]74
[647]75    // Gather next event
[645]76    SDL_Event sdlev;
[647]77    if (!SDL_PollEvent(&sdlev))
78        return; // This should not happen
[2]79
[647]80    // Sort the mouse out
[124]81    int x, y;
[647]82    uint8_t buttons = SDL_GetMouseState(&x, &y);
[701]83    /* FIXME: this should be somewhere else */
84    x = x * main_screen->Size().x / Video::GetSize().x;
85    y = y * main_screen->Size().y / Video::GetSize().y;
86    ev.mouse_move.x = lol::min(x, main_screen->Size().x - 1);
87    ev.mouse_move.y = lol::min(y, main_screen->Size().y - 1);
[124]88    ev.type = EV_MOUSE_MOVE;
[2]89
[124]90    // Left button
[643]91    if((buttons & SDL_BUTTON(1)) && !mouse_buttons[1])
[124]92    {
93        ev.type = EV_MOUSE_BUTTON;
94        mouse_buttons[1] = !mouse_buttons[1];
95        ev.mouse_button |= LEFT_BUTTON;
96    }
[643]97    else if(!(buttons & SDL_BUTTON(1)) && mouse_buttons[1])
[124]98    {
99        ev.type = EV_MOUSE_BUTTON;
100        mouse_buttons[1] = !mouse_buttons[1];
[643]101        ev.mouse_button &= (0xff - LEFT_BUTTON);
[124]102    }
[2]103
[124]104    // Middle button
[643]105    if((buttons & SDL_BUTTON(2)) && !mouse_buttons[2])
[124]106    {
107        ev.type = EV_MOUSE_BUTTON;
108        mouse_buttons[2] = !mouse_buttons[2];
109        ev.mouse_button |= LEFT_BUTTON;
110        ev.mouse_button |= RIGHT_BUTTON;
111    }
[643]112    else if(!(buttons & SDL_BUTTON(2)) && mouse_buttons[2])
[124]113    {
114        ev.type = EV_MOUSE_BUTTON;
115        mouse_buttons[2] = !mouse_buttons[2];
[643]116        ev.mouse_button &= (0xff - LEFT_BUTTON);
117        ev.mouse_button &= (0xff - RIGHT_BUTTON);
[124]118    }
[2]119
[124]120    // Right button
[643]121    if((buttons & SDL_BUTTON(3)) && !mouse_buttons[3])
[124]122    {
123        ev.type = EV_MOUSE_BUTTON;
124        mouse_buttons[3] = !mouse_buttons[3];
125        ev.mouse_button |= RIGHT_BUTTON;
126    }
[643]127    else if(!(buttons & SDL_BUTTON(3)) && mouse_buttons[3])
[124]128    {
129        ev.type = EV_MOUSE_BUTTON;
130        mouse_buttons[3] = !mouse_buttons[3];
[643]131        ev.mouse_button &= (0xff - RIGHT_BUTTON);
[124]132    }
[682]133    m_pos = ivec2(ev.mouse_move.x, ev.mouse_move.y);
[652]134    m_button = ev.mouse_button;
[647]135
136    // Sort out other kinds of events
137    switch(sdlev.type)
138    {
139    case SDL_QUIT:
140        exit(0);
141        break;
142    case SDL_MOUSEBUTTONUP:
143        switch(sdlev.button.button)
144        {
145        case 4:        // Mouse wheel goes up...
146            ev.key = get_key_binding("b4", 0);
147            ev.type = EV_KEYRELEASE;
148            break;
149        case 5:        // Mouse wheel goes down...
150            ev.key = get_key_binding("b3", 0);
151            ev.type = EV_KEYRELEASE;
152            break;
153        }
154        break;
155    case SDL_MOUSEBUTTONDOWN:
156        switch(sdlev.button.button)
157        {
158        case 4:        // Mouse wheel goes up...
159            ev.key = get_key_binding("b4", 0);
160            ev.type = EV_KEY;
161            break;
162        case 5:        // Mouse wheel goes down...
163            ev.key = get_key_binding("b3", 0);
164            ev.type = EV_KEY;
165            break;
166        }
167        break;
168    case SDL_KEYDOWN:
169    case SDL_KEYUP:
170        // Default to EV_SPURIOUS
171        ev.key = EV_SPURIOUS;
172        if(sdlev.type == SDL_KEYDOWN)
173        {
174            ev.type = EV_KEY;
175        }
176        else
177        {
178            ev.type = EV_KEYRELEASE;
179        }
180        switch(sdlev.key.keysym.sym)
181        {
182        case SDLK_DOWN:         ev.key = JK_DOWN; break;
183        case SDLK_UP:           ev.key = JK_UP; break;
184        case SDLK_LEFT:         ev.key = JK_LEFT; break;
185        case SDLK_RIGHT:        ev.key = JK_RIGHT; break;
186        case SDLK_LCTRL:        ev.key = JK_CTRL_L; break;
187        case SDLK_RCTRL:        ev.key = JK_CTRL_R; break;
188        case SDLK_LALT:         ev.key = JK_ALT_L; break;
189        case SDLK_RALT:         ev.key = JK_ALT_R; break;
190        case SDLK_LSHIFT:       ev.key = JK_SHIFT_L; break;
191        case SDLK_RSHIFT:       ev.key = JK_SHIFT_R; break;
192        case SDLK_NUMLOCK:      ev.key = JK_NUM_LOCK; break;
193        case SDLK_HOME:         ev.key = JK_HOME; break;
194        case SDLK_END:          ev.key = JK_END; break;
195        case SDLK_BACKSPACE:    ev.key = JK_BACKSPACE; break;
196        case SDLK_TAB:          ev.key = JK_TAB; break;
197        case SDLK_RETURN:       ev.key = JK_ENTER; break;
198        case SDLK_SPACE:        ev.key = JK_SPACE; break;
199        case SDLK_CAPSLOCK:     ev.key = JK_CAPS; break;
200        case SDLK_ESCAPE:       ev.key = JK_ESC; break;
201        case SDLK_F1:           ev.key = JK_F1; break;
202        case SDLK_F2:           ev.key = JK_F2; break;
203        case SDLK_F3:           ev.key = JK_F3; break;
204        case SDLK_F4:           ev.key = JK_F4; break;
205        case SDLK_F5:           ev.key = JK_F5; break;
206        case SDLK_F6:           ev.key = JK_F6; break;
207        case SDLK_F7:           ev.key = JK_F7; break;
208        case SDLK_F8:           ev.key = JK_F8; break;
209        case SDLK_F9:           ev.key = JK_F9; break;
210        case SDLK_F10:          ev.key = JK_F10; break;
211        case SDLK_INSERT:       ev.key = JK_INSERT; break;
212        case SDLK_KP0:          ev.key = JK_INSERT; break;
213        case SDLK_PAGEUP:       ev.key = JK_PAGEUP; break;
214        case SDLK_PAGEDOWN:     ev.key = JK_PAGEDOWN; break;
215        case SDLK_KP8:          ev.key = JK_UP; break;
216        case SDLK_KP2:          ev.key = JK_DOWN; break;
217        case SDLK_KP4:          ev.key = JK_LEFT; break;
218        case SDLK_KP6:          ev.key = JK_RIGHT; break;
219        case SDLK_F11:
220            // Only handle key down
221            if(ev.type == EV_KEY)
222            {
223                // Toggle fullscreen
224                SDL_WM_ToggleFullScreen(SDL_GetVideoSurface());
225            }
226            ev.key = EV_SPURIOUS;
227            break;
228        case SDLK_F12:
229            // Only handle key down
230            if(ev.type == EV_KEY)
231            {
232                // Toggle grab mouse
233                if(SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON)
234                {
235                    the_game->show_help("Grab Mouse: OFF\n");
236                    SDL_WM_GrabInput(SDL_GRAB_OFF);
237                }
238                else
239                {
240                    the_game->show_help("Grab Mouse: ON\n");
241                    SDL_WM_GrabInput(SDL_GRAB_ON);
242                }
243            }
244            ev.key = EV_SPURIOUS;
245            break;
246        case SDLK_PRINT:    // print-screen key
247            // Only handle key down
248            if(ev.type == EV_KEY)
249            {
250                // Grab a screenshot
251                SDL_SaveBMP(SDL_GetVideoSurface(), "screenshot.bmp");
252                the_game->show_help("Screenshot saved to: screenshot.bmp.\n");
253            }
254            ev.key = EV_SPURIOUS;
255            break;
256        default:
257            ev.key = (int)sdlev.key.keysym.sym;
258            // Need to handle the case of shift being pressed
259            // There has to be a better way
260            if((sdlev.key.keysym.mod & KMOD_SHIFT) != 0)
261            {
262                if(sdlev.key.keysym.sym >= SDLK_a &&
263                    sdlev.key.keysym.sym <= SDLK_z)
264                {
265                    ev.key -= 32;
266                }
267                else if(sdlev.key.keysym.sym >= SDLK_1 &&
268                         sdlev.key.keysym.sym <= SDLK_5)
269                {
270                    ev.key -= 16;
271                }
272                else
273                {
274                    switch(sdlev.key.keysym.sym)
275                    {
276                    case SDLK_6:
277                        ev.key = SDLK_CARET; break;
278                    case SDLK_7:
279                    case SDLK_9:
280                    case SDLK_0:
281                        ev.key -= 17; break;
282                    case SDLK_8:
283                        ev.key = SDLK_ASTERISK; break;
284                    case SDLK_MINUS:
285                        ev.key = SDLK_UNDERSCORE; break;
286                    case SDLK_EQUALS:
287                        ev.key = SDLK_PLUS; break;
288                    case SDLK_COMMA:
289                        ev.key = SDLK_LESS; break;
290                    case SDLK_PERIOD:
291                        ev.key = SDLK_GREATER; break;
292                    case SDLK_SLASH:
293                        ev.key = SDLK_QUESTION; break;
294                    case SDLK_SEMICOLON:
295                        ev.key = SDLK_COLON; break;
296                    case SDLK_QUOTE:
297                        ev.key = SDLK_QUOTEDBL; break;
298                    default:
299                        break;
300                    }
301                }
302            }
303            break;
304        }
305    }
[2]306}
[647]307
Note: See TracBrowser for help on using the repository browser.