World of Might and Magic  0.2.0
Open reimplementation of Might and Magic 6 7 8 game engine
Sdl2Window.cpp
См. документацию.
1 #include "Platform/Sdl2Window.h"
2 
3 #include "SDL_syswm.h"
4 
6  switch (e.type) {
7  case SDL_QUIT: {
8  extern void Engine_DeinitializeAndTerminate(int exit_code);
10  } break;
11 
12  case SDL_APP_WILLENTERBACKGROUND: { // this is for mobile apps wrong event
13  gameCallback->OnDeactivated();
14  } break;
15 
16  case SDL_APP_WILLENTERFOREGROUND: { // this is for mobile apps wrong event
17  gameCallback->OnActivated();
18  } break;
19 
20  case SDL_KEYUP: {
21  if (e.key.keysym.sym == SDLK_LCTRL || e.key.keysym.sym == SDLK_RCTRL) {
22  extern bool _507B98_ctrl_pressed;
23  _507B98_ctrl_pressed = false;
24  } else if (e.key.keysym.sym == SDLK_PRINTSCREEN) {
25  gameCallback->OnScreenshot();
26  }
27  } break;
28 
29  case SDL_KEYDOWN: {
30  auto key = e.key.keysym.sym;
31  auto mods = e.key.keysym.mod;
32 
33  auto ch = SdlkToChar(key, (mods & KMOD_CAPS) != (mods & KMOD_SHIFT));
34  auto vkkey = SdlkToVk(key);
35  if (ch != -1) {
36  gameCallback->OnChar(ch);
37  } else if ( key == SDLK_RETURN || key == SDLK_ESCAPE || key == SDLK_TAB || key == SDLK_BACKSPACE ) {
38  if (!gameCallback->OnChar(vkkey))
39  gameCallback->OnVkDown(vkkey, 0);
40  } else {
41  gameCallback->OnVkDown(vkkey, 0);
42  }
43  }
44 
45  case SDL_MOUSEMOTION: {
46  gameCallback->OnMouseMove(
47  e.motion.x, e.motion.y,
48  e.motion.state & SDL_BUTTON_LMASK, e.motion.state & SDL_BUTTON_RMASK);
49  }
50 
51  case SDL_MOUSEBUTTONDOWN: {
52  int x = e.button.x;
53  int y = e.button.y;
54  int clicks = e.button.clicks;
55 
56  // if (e.button.state == SDL_PRESSED) {
57  if (e.button.button == SDL_BUTTON_LEFT) {
58  if (clicks == 1) {
59  gameCallback->OnMouseLeftClick(x, y);
60  } else if (clicks >= 2) {
61  gameCallback->OnMouseLeftDoubleClick(x, y);
62  }
63  } else if (e.button.button == SDL_BUTTON_RIGHT) {
64  if (clicks == 1) {
65  gameCallback->OnMouseRightClick(x, y);
66  } else if (clicks >= 2) {
67  gameCallback->OnMouseRightDoubleClick(x, y);
68  }
69  }
70  // }
71  } break;
72 
73  case SDL_MOUSEBUTTONUP: {
74  // if (e.button.state == SDL_RELEASED) {
75  if (e.button.button == SDL_BUTTON_LEFT) {
76  gameCallback->OnMouseLeftUp();
77  } else if (e.button.button == SDL_BUTTON_RIGHT) {
78  gameCallback->OnMouseRightUp();
79  }
80  // }
81  } break;
82 
83  case SDL_WINDOWEVENT: {
84  switch (e.window.event) {
86  gameCallback->OnPaint();
87  } break;
89  gameCallback->OnDeactivated();
90  } break;
92  gameCallback->OnActivated();
93  } break;
94  }
95  } break;
96  }
97 }
98 
100  SDL_Event e;
101  while (SDL_PollEvent(&e)) {
102  MessageProc(e);
103  }
104 }
105 
107  SDL_Event e;
108  if (SDL_PollEvent(&e)) {
109  MessageProc(e);
110  }
111 }
112 
114  SDL_SysWMinfo wmInfo;
115  SDL_VERSION(&wmInfo.version);
116 
117  SDL_GetWindowWMInfo(sdlWindow, &wmInfo);
118 #ifdef _WINDOWS
119  return (void *)wmInfo.info.win.window;
120 #else
121  return (void *)wmInfo.info.x11.window;
122 #endif
123 }
124 
125 
127 }
128 
129 void Sdl2Window::SetWindowedMode(int new_window_width, int new_window_height) {
130 }
131 
132 void Sdl2Window::SetCursor(const char *cursor_name) {
133 }
134 
135 int Sdl2Window::GetX() const {
136  int x;
137  SDL_GetWindowPosition(sdlWindow, &x, nullptr);
138 
139  return x;
140 }
141 
142 int Sdl2Window::GetY() const {
143  int y;
144  SDL_GetWindowPosition(sdlWindow, nullptr, &y);
145 
146  return y;
147 }
148 
149 unsigned int Sdl2Window::GetWidth() const {
150  int w;
151  SDL_GetWindowSize(sdlWindow, &w, nullptr);
152 
153  return w;
154 }
155 unsigned int Sdl2Window::GetHeight() const {
156  int h;
157  SDL_GetWindowSize(sdlWindow, nullptr, &h);
158 
159  return h;
160 }
161 
163  SDL_Point p;
164  SDL_GetMouseState(&p.x, &p.y);
165 
166  return Point(p.x, p.y);
167 }
168 
169 bool Sdl2Window::OnOSMenu(int item_id) {
170  return false;
171 }
172 
176 
177  PeekMessageLoop();
178 }
179 
182  return (SDL_GetWindowFlags(sdlWindow) & flags) == flags;
183 }
184 
187 
188  PeekMessageLoop();
189 }
190 
191 
192 int Sdl2Window::SdlkToChar(SDL_Keycode key, bool uppercase) const {
193  if (key >= SDLK_0 && key <= SDLK_9) {
194  return '0' + (key - SDLK_0);
195  }
196  if (key >= SDLK_a && key <= SDLK_z) {
197  if (uppercase) {
198  return 'A' + (key - SDLK_a);
199  } else {
200  return 'a' + (key - SDLK_a);
201  }
202  }
203  return -1;
204 }
205 
207 #ifdef _WINDOWS
208  // "Will be removed later"
209  if (key >= SDLK_F1 && key <= SDLK_F12) {
210  return VK_F1 + (key - SDLK_F1);
211  }
212 
213  static struct {
214  SDL_Keycode sdlk;
215  int vk;
216  }
217  sdlk2vk[] = {
218  { SDLK_LEFT, VK_LEFT },
219  { SDLK_RIGHT, VK_RIGHT },
220  { SDLK_UP, VK_UP },
221  { SDLK_DOWN, VK_DOWN },
222  { SDLK_PRINTSCREEN, VK_PRINT },
223  { SDLK_INSERT, VK_INSERT },
224  { SDLK_HOME, VK_HOME },
225  { SDLK_PAGEUP, VK_PRIOR },
226  { SDLK_DELETE, VK_DELETE },
227  { SDLK_END, VK_END },
228  { SDLK_PAGEDOWN, VK_NEXT },
229  };
230 
231  for (int i = 0; i < sizeof(sdlk2vk) / sizeof(*sdlk2vk); ++i) {
232  if (sdlk2vk[i].sdlk == key) {
233  return sdlk2vk[i].vk;
234  }
235  }
236 #endif
237 
238  return key & 0xFFFF;
239 }
240 
241 
243  // Use OpenGL 3.3 core - requires all fixed pipeline code to be modernised
244  // SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
245  // SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
246  // SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
247 
248 
249 
250  // Turn on double buffering with a 24bit Z buffer.
251  // You may need to change this to 16 or 32 for your system
254 
256  if (!sdlOpenGlContext) {
257  log->Warning(L"OpenGL failed: %S", SDL_GetError());
258  }
259 
260  // Use Vsync
261  if (SDL_GL_SetSwapInterval(1) < 0) {
262  log->Info(L"Unable to set VSync! SDL Error: %S\n", SDL_GetError());
263  }
264 }
265 
268 }
269 
Sdl2Window::sdlWindow
SDL_Window * sdlWindow
Definition: Sdl2Window.h:42
SDL_Event::type
Uint32 type
Definition: SDL_events.h:560
Sdl2Window::GetWinApiHandle
void * GetWinApiHandle() override
Definition: Sdl2Window.cpp:113
Sdl2Window::OpenGlCreate
void OpenGlCreate() override
Definition: Sdl2Window.cpp:242
SDL_GL_DEPTH_SIZE
@ SDL_GL_DEPTH_SIZE
Definition: SDL_video.h:205
SDL_SysWMinfo::win
struct SDL_SysWMinfo::@107::@108 win
SDL_MouseMotionEvent::y
Sint32 y
Definition: SDL_events.h:261
SDLK_HOME
@ SDLK_HOME
Definition: SDL_keycode.h:146
SDLK_INSERT
@ SDLK_INSERT
Definition: SDL_keycode.h:145
SDL_GetError
const DECLSPEC char *SDLCALL SDL_GetError(void)
SDLK_DELETE
@ SDLK_DELETE
Definition: SDL_keycode.h:148
SDL_Event::motion
SDL_MouseMotionEvent motion
Definition: SDL_events.h:567
SDL_Event::button
SDL_MouseButtonEvent button
Definition: SDL_events.h:568
Sdl2Window::Activate
void Activate() override
Definition: Sdl2Window.cpp:185
Sdl2Window::Focused
bool Focused() override
Definition: Sdl2Window.cpp:180
SDL_MouseButtonEvent::x
Sint32 x
Definition: SDL_events.h:279
SDLK_BACKSPACE
@ SDLK_BACKSPACE
Definition: SDL_keycode.h:56
SDL_WINDOWEVENT_FOCUS_LOST
@ SDL_WINDOWEVENT_FOCUS_LOST
Definition: SDL_video.h:165
SDL_KEYUP
@ SDL_KEYUP
Definition: SDL_events.h:97
SDL_SysWMinfo
Definition: SDL_syswm.h:201
SDLK_ESCAPE
@ SDLK_ESCAPE
Definition: SDL_keycode.h:55
Sdl2Window::sdlOpenGlContext
SDL_GLContext sdlOpenGlContext
Definition: Sdl2Window.h:44
SDL_GetWindowSize
DECLSPEC void SDLCALL SDL_GetWindowSize(SDL_Window *window, int *w, int *h)
Get the size of a window's client area.
SDL_MouseButtonEvent::clicks
Uint8 clicks
Definition: SDL_events.h:277
SDL_GL_CreateContext
DECLSPEC SDL_GLContext SDLCALL SDL_GL_CreateContext(SDL_Window *window)
Create an OpenGL context for use with an OpenGL window, and make it current.
Sdl2Window::SetFullscreenMode
void SetFullscreenMode() override
Definition: Sdl2Window.cpp:126
SDL_MOUSEBUTTONUP
@ SDL_MOUSEBUTTONUP
Definition: SDL_events.h:107
SDLK_END
@ SDLK_END
Definition: SDL_keycode.h:149
Sdl2Window::SdlkToChar
int SdlkToChar(SDL_Keycode key, bool uppercase) const
Definition: Sdl2Window.cpp:192
SDL_MouseMotionEvent::state
Uint32 state
Definition: SDL_events.h:259
SDLK_z
@ SDLK_z
Definition: SDL_keycode.h:125
w
GLubyte GLubyte GLubyte GLubyte w
Definition: SDL_opengl_glext.h:734
Sdl2Window::TransformCursorPos
Point TransformCursorPos(Point &pt) const override
Definition: Sdl2Window.cpp:162
SDL_KEYDOWN
@ SDL_KEYDOWN
Definition: SDL_events.h:96
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1949
SDL_Keycode
Sint32 SDL_Keycode
The SDL virtual key representation.
Definition: SDL_keycode.h:45
OSWindow::log
Log * log
Definition: OSWindow.h:44
SDLK_PAGEDOWN
@ SDLK_PAGEDOWN
Definition: SDL_keycode.h:150
SDL_WINDOW_INPUT_FOCUS
@ SDL_WINDOW_INPUT_FOCUS
Definition: SDL_video.h:108
SDL_APP_WILLENTERFOREGROUND
@ SDL_APP_WILLENTERFOREGROUND
Definition: SDL_events.h:79
SDLK_F12
@ SDLK_F12
Definition: SDL_keycode.h:140
Sdl2Window.h
y
EGLSurface EGLint EGLint y
Definition: SDL_egl.h:1596
OSWindow::gameCallback
GameWindowHandler * gameCallback
Definition: OSWindow.h:43
SDL_GL_SetSwapInterval
DECLSPEC int SDLCALL SDL_GL_SetSwapInterval(int interval)
Set the swap interval for the current OpenGL context.
SDL_MouseButtonEvent::button
Uint8 button
Definition: SDL_events.h:275
SDL_MOUSEMOTION
@ SDL_MOUSEMOTION
Definition: SDL_events.h:105
SDL_SysWMinfo::info
union SDL_SysWMinfo::@107 info
p
GLfloat GLfloat p
Definition: SDL_opengl_glext.h:11093
SDL_ShowWindow
DECLSPEC void SDLCALL SDL_ShowWindow(SDL_Window *window)
Show a window.
Sdl2Window::SetCursor
void SetCursor(const char *cursor_name) override
Definition: Sdl2Window.cpp:132
x
EGLSurface EGLint x
Definition: SDL_egl.h:1596
SDL_GetMouseState
DECLSPEC Uint32 SDLCALL SDL_GetMouseState(int *x, int *y)
Retrieve the current state of the mouse.
Log::Info
void Info(const wchar_t *pFormat,...)
Definition: Log.cpp:11
SDL_GL_SetAttribute
DECLSPEC int SDLCALL SDL_GL_SetAttribute(SDL_GLattr attr, int value)
Set an OpenGL window attribute before window creation.
SDLK_F1
@ SDLK_F1
Definition: SDL_keycode.h:129
SDL_QUIT
@ SDL_QUIT
Definition: SDL_events.h:60
SDLK_PRINTSCREEN
@ SDLK_PRINTSCREEN
Definition: SDL_keycode.h:142
Log::Warning
void Warning(const wchar_t *pFormat,...)
Definition: Log.cpp:28
SDL_WINDOWEVENT_EXPOSED
@ SDL_WINDOWEVENT_EXPOSED
Definition: SDL_video.h:150
SDL_Event::window
SDL_WindowEvent window
Definition: SDL_events.h:563
SDLK_a
@ SDLK_a
Definition: SDL_keycode.h:100
SDL_SysWMinfo::x11
struct SDL_SysWMinfo::@107::@110 x11
SDL_GetWindowPosition
DECLSPEC void SDLCALL SDL_GetWindowPosition(SDL_Window *window, int *x, int *y)
Get the position of a window.
SDL_Event::key
SDL_KeyboardEvent key
Definition: SDL_events.h:564
SDL_APP_WILLENTERBACKGROUND
@ SDL_APP_WILLENTERBACKGROUND
Definition: SDL_events.h:71
_507B98_ctrl_pressed
bool _507B98_ctrl_pressed
Definition: Mouse.cpp:256
Sdl2Window::OnOSMenu
bool OnOSMenu(int item_id) override
Definition: Sdl2Window.cpp:169
Sdl2Window::MessageProc
void MessageProc(const SDL_Event &e)
Definition: Sdl2Window.cpp:5
SDL_WINDOW_SHOWN
@ SDL_WINDOW_SHOWN
Definition: SDL_video.h:101
Sdl2Window::GetY
int GetY() const override
Definition: Sdl2Window.cpp:142
SDLK_0
@ SDLK_0
Definition: SDL_keycode.h:74
SDL_GetWindowFlags
DECLSPEC Uint32 SDLCALL SDL_GetWindowFlags(SDL_Window *window)
Get the window flags.
SDLK_LEFT
@ SDLK_LEFT
Definition: SDL_keycode.h:152
Engine_DeinitializeAndTerminate
void Engine_DeinitializeAndTerminate(int exitCode)
Definition: Engine.cpp:157
Sdl2Window::PeekSingleMessage
void PeekSingleMessage() override
Definition: Sdl2Window.cpp:106
SDLK_9
@ SDLK_9
Definition: SDL_keycode.h:83
SDL_RaiseWindow
DECLSPEC void SDLCALL SDL_RaiseWindow(SDL_Window *window)
Raise a window above other windows and set the input focus.
Sdl2Window::GetWidth
unsigned int GetWidth() const override
Definition: Sdl2Window.cpp:149
SDL_GL_DOUBLEBUFFER
@ SDL_GL_DOUBLEBUFFER
Definition: SDL_video.h:204
SDLK_RCTRL
@ SDLK_RCTRL
Definition: SDL_keycode.h:280
Sdl2Window::SetWindowedMode
void SetWindowedMode(int new_window_width, int new_window_height) override
Definition: Sdl2Window.cpp:129
SDL_MouseMotionEvent::x
Sint32 x
Definition: SDL_events.h:260
SDL_Point
The structure that defines a point (integer)
Definition: SDL_rect.h:48
SDL_SysWMinfo::version
SDL_version version
Definition: SDL_syswm.h:203
SDLK_RETURN
@ SDLK_RETURN
Definition: SDL_keycode.h:54
SDLK_PAGEUP
@ SDLK_PAGEUP
Definition: SDL_keycode.h:147
SDL_Keysym::sym
SDL_Keycode sym
Definition: SDL_keyboard.h:50
Sdl2Window::OpenGlSwapBuffers
void OpenGlSwapBuffers() override
Definition: Sdl2Window.cpp:266
flags
EGLSyncKHR EGLint flags
Definition: SDL_egl.h:898
SDL_MOUSEBUTTONDOWN
@ SDL_MOUSEBUTTONDOWN
Definition: SDL_events.h:106
SDLK_RIGHT
@ SDLK_RIGHT
Definition: SDL_keycode.h:151
Sdl2Window::Show
void Show() override
Definition: Sdl2Window.cpp:173
SDL_Keysym::mod
Uint16 mod
Definition: SDL_keyboard.h:51
SDL_WINDOW_MOUSE_FOCUS
@ SDL_WINDOW_MOUSE_FOCUS
Definition: SDL_video.h:109
SDL_WindowEvent::event
Uint8 event
Definition: SDL_events.h:200
SDL_Event
General event structure.
Definition: SDL_events.h:558
SDL_WINDOWEVENT
@ SDL_WINDOWEVENT
Definition: SDL_events.h:92
SDL_PollEvent
DECLSPEC int SDLCALL SDL_PollEvent(SDL_Event *event)
Polls for currently pending events.
Sdl2Window::SdlkToVk
int SdlkToVk(SDL_Keycode key) const
Definition: Sdl2Window.cpp:206
SDLK_LCTRL
@ SDLK_LCTRL
Definition: SDL_keycode.h:276
Point
Definition: Point.h:3
SDLK_UP
@ SDLK_UP
Definition: SDL_keycode.h:154
SDL_MouseButtonEvent::y
Sint32 y
Definition: SDL_events.h:280
SDL_WINDOWEVENT_FOCUS_GAINED
@ SDL_WINDOWEVENT_FOCUS_GAINED
Definition: SDL_video.h:164
SDL_GL_SwapWindow
DECLSPEC void SDLCALL SDL_GL_SwapWindow(SDL_Window *window)
Swap the OpenGL buffers for a window, if double-buffering is supported.
KMOD_CAPS
@ KMOD_CAPS
Definition: SDL_keycode.h:337
SDL_KeyboardEvent::keysym
SDL_Keysym keysym
Definition: SDL_events.h:220
Sdl2Window::GetHeight
unsigned int GetHeight() const override
Definition: Sdl2Window.cpp:155
SDL_GetWindowWMInfo
DECLSPEC SDL_bool SDLCALL SDL_GetWindowWMInfo(SDL_Window *window, SDL_SysWMinfo *info)
This function allows access to driver-dependent window information.
SDLK_DOWN
@ SDLK_DOWN
Definition: SDL_keycode.h:153
Sdl2Window::GetX
int GetX() const override
Definition: Sdl2Window.cpp:135
SDL_syswm.h
Sdl2Window::PeekMessageLoop
void PeekMessageLoop() override
Definition: Sdl2Window.cpp:99
SDLK_TAB
@ SDLK_TAB
Definition: SDL_keycode.h:57