1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #if defined USE_SDL |
---|
16 | # include <SDL.h> |
---|
17 | #endif |
---|
18 | |
---|
19 | #include "core.h" |
---|
20 | #include "sdlinput.h" |
---|
21 | |
---|
22 | namespace lol |
---|
23 | { |
---|
24 | |
---|
25 | /* |
---|
26 | * SDL Input implementation class |
---|
27 | */ |
---|
28 | |
---|
29 | class SdlInputData |
---|
30 | { |
---|
31 | friend class SdlInput; |
---|
32 | |
---|
33 | private: |
---|
34 | static ivec2 GetMousePos(); |
---|
35 | }; |
---|
36 | |
---|
37 | /* |
---|
38 | * Public SdlInput class |
---|
39 | */ |
---|
40 | |
---|
41 | SdlInput::SdlInput() |
---|
42 | : data(new SdlInputData()) |
---|
43 | { |
---|
44 | #if defined USE_SDL |
---|
45 | SDL_Init(SDL_INIT_TIMER); |
---|
46 | |
---|
47 | gamegroup = GAMEGROUP_BEFORE; |
---|
48 | #endif |
---|
49 | } |
---|
50 | |
---|
51 | void SdlInput::TickGame(float deltams) |
---|
52 | { |
---|
53 | #if defined USE_SDL |
---|
54 | Entity::TickGame(deltams); |
---|
55 | |
---|
56 | /* Handle mouse input */ |
---|
57 | ivec2 mouse = SdlInputData::GetMousePos();; |
---|
58 | Input::SetMousePos(mouse); |
---|
59 | |
---|
60 | /* Handle keyboard and WM events */ |
---|
61 | SDL_Event event; |
---|
62 | while (SDL_PollEvent(&event)) |
---|
63 | { |
---|
64 | switch (event.type) |
---|
65 | { |
---|
66 | case SDL_QUIT: |
---|
67 | Ticker::Shutdown(); |
---|
68 | break; |
---|
69 | #if 0 |
---|
70 | case SDL_KEYDOWN: |
---|
71 | Input::KeyPressed(event.key.keysym.sym, deltams); |
---|
72 | break; |
---|
73 | #endif |
---|
74 | case SDL_MOUSEBUTTONDOWN: |
---|
75 | case SDL_MOUSEBUTTONUP: |
---|
76 | { |
---|
77 | ivec2 newmouse = SdlInputData::GetMousePos(); |
---|
78 | if (newmouse != mouse) |
---|
79 | Input::SetMousePos(mouse = newmouse); |
---|
80 | if (event.type == SDL_MOUSEBUTTONDOWN) |
---|
81 | Input::SetMouseButton(event.button.button - 1); |
---|
82 | else |
---|
83 | Input::UnsetMouseButton(event.button.button - 1); |
---|
84 | break; |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | /* Send the whole keyboard state to the input system */ |
---|
90 | #if 0 |
---|
91 | Uint8 *keystate = SDL_GetKeyState(NULL); |
---|
92 | for (int i = 0; i < 256; i++) |
---|
93 | if (keystate[i]) |
---|
94 | Input::KeyPressed(i, deltams); |
---|
95 | #endif |
---|
96 | #endif |
---|
97 | } |
---|
98 | |
---|
99 | SdlInput::~SdlInput() |
---|
100 | { |
---|
101 | delete data; |
---|
102 | } |
---|
103 | |
---|
104 | ivec2 SdlInputData::GetMousePos() |
---|
105 | { |
---|
106 | ivec2 ret(-1, -1); |
---|
107 | |
---|
108 | #if defined USE_SDL |
---|
109 | if (SDL_GetAppState() & SDL_APPMOUSEFOCUS) |
---|
110 | { |
---|
111 | SDL_GetMouseState(&ret.x, &ret.y); |
---|
112 | ret.y = Video::GetSize().y - 1 - ret.y; |
---|
113 | } |
---|
114 | #endif |
---|
115 | return ret; |
---|
116 | } |
---|
117 | |
---|
118 | } /* namespace lol */ |
---|
119 | |
---|