From 161d6c1ba718c7bfa6ca77cecdfcc434f741cda7 Mon Sep 17 00:00:00 2001 From: historic_bruno Date: Fri, 5 Apr 2013 02:39:30 +0000 Subject: [PATCH] Improves SDL 2.0 support This was SVN commit r13343. --- source/ps/Hotkey.cpp | 12 ++-- source/ps/Joystick.cpp | 24 +++++++- source/ps/TouchInput.cpp | 6 +- source/ps/VideoMode.cpp | 69 ++++++++++++++++------ source/tools/atlas/AtlasUI/Misc/KeyMap.cpp | 33 ++++++++++- 5 files changed, 112 insertions(+), 32 deletions(-) diff --git a/source/ps/Hotkey.cpp b/source/ps/Hotkey.cpp index 22ab9f6a30..4828de57e9 100644 --- a/source/ps/Hotkey.cpp +++ b/source/ps/Hotkey.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2010 Wildfire Games. +/* Copyright (C) 2013 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -200,12 +200,12 @@ InReaction HotkeyInputHandler( const SDL_Event_* ev ) #if SDL_VERSION_ATLEAST(2, 0, 0) case SDL_MOUSEWHEEL: - if (ev->ev.wheel.y < 0) + if (ev->ev.wheel.y > 0) { keycode = MOUSE_WHEELUP; break; } - else if (ev->ev.wheel.y > 0) + else if (ev->ev.wheel.y < 0) { keycode = MOUSE_WHEELDOWN; break; @@ -248,7 +248,7 @@ InReaction HotkeyInputHandler( const SDL_Event_* ev ) } #if SDL_VERSION_ATLEAST(2, 0, 0) else if( ( keycode == SDLK_LGUI ) || ( keycode == SDLK_RGUI ) ) -#else +#else // SDL 1.2 else if( ( keycode == SDLK_LSUPER ) || ( keycode == SDLK_RSUPER ) || ( keycode == SDLK_LMETA ) || ( keycode == SDLK_RMETA) ) #endif { @@ -279,7 +279,11 @@ InReaction HotkeyInputHandler( const SDL_Event_* ev ) // matching the conditions (i.e. the event with the highest number of auxiliary // keys, providing they're all down) +#if SDL_VERSION_ATLEAST(2, 0, 0) + bool typeKeyDown = ( ev->ev.type == SDL_KEYDOWN ) || ( ev->ev.type == SDL_MOUSEBUTTONDOWN ) || (ev->ev.type == SDL_MOUSEWHEEL); +#else bool typeKeyDown = ( ev->ev.type == SDL_KEYDOWN ) || ( ev->ev.type == SDL_MOUSEBUTTONDOWN ); +#endif // -- KEYDOWN SECTION -- diff --git a/source/ps/Joystick.cpp b/source/ps/Joystick.cpp index 1300d7d758..9d1fb1abdf 100644 --- a/source/ps/Joystick.cpp +++ b/source/ps/Joystick.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2012 Wildfire Games. +/* Copyright (C) 2013 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -41,7 +41,7 @@ void CJoystick::Initialise() if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0) { - LOGERROR(L"InitInput: failed to initialise joystick"); + LOGERROR(L"CJoystick::Initialise failed to initialise joysticks (\"%hs\")", SDL_GetError()); return; } @@ -50,7 +50,23 @@ void CJoystick::Initialise() LOGMESSAGE(L"Found %d joystick(s)", numJoysticks); for (int i = 0; i < numJoysticks; ++i) - LOGMESSAGE(L"Joystick %d: %hs", i, SDL_JoystickName(i)); + { +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_Joystick* stick = SDL_JoystickOpen(i); + if (!stick) + { + LOGERROR(L"CJoystick::Initialise failed to open joystick %d (\"%hs\")", i, SDL_GetError()); + continue; + } + const char* name = SDL_JoystickName(stick); +#else // SDL 1.2 + const char* name = SDL_JoystickName(i); +#endif + LOGMESSAGE(L"Joystick %d: %hs", i, name); +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_JoystickClose(stick); +#endif + } if (numJoysticks) { @@ -58,6 +74,8 @@ void CJoystick::Initialise() // Always pick the first joystick, and assume that's the right one m_Joystick = SDL_JoystickOpen(0); + if (!m_Joystick) + LOGERROR(L"CJoystick::Initialise failed to open joystick (\"%hs\")", SDL_GetError()); } } diff --git a/source/ps/TouchInput.cpp b/source/ps/TouchInput.cpp index c9f2b80c01..bcfb7f20b0 100644 --- a/source/ps/TouchInput.cpp +++ b/source/ps/TouchInput.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2012 Wildfire Games. +/* Copyright (C) 2013 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -267,11 +267,11 @@ InReaction CTouchInput::HandleEvent(const SDL_Event_* ev) case SDL_FINGERMOTION: { // Map finger events onto the mouse, for basic testing - debug_printf(L"finger %s tid=%lld fid=%lld s=%d x=%d y=%d dx=%d dy=%d p=%d\n", + debug_printf(L"finger %s tid=%lld fid=%lld x=%d y=%d dx=%d dy=%d p=%d\n", ev->ev.type == SDL_FINGERDOWN ? "down" : ev->ev.type == SDL_FINGERUP ? "up" : ev->ev.type == SDL_FINGERMOTION ? "motion" : "?", - ev->ev.tfinger.touchId, ev->ev.tfinger.fingerId, ev->ev.tfinger.state, + ev->ev.tfinger.touchId, ev->ev.tfinger.fingerId, ev->ev.tfinger.x, ev->ev.tfinger.y, ev->ev.tfinger.dx, ev->ev.tfinger.dy, ev->ev.tfinger.pressure); if (ev->ev.type == SDL_FINGERDOWN) diff --git a/source/ps/VideoMode.cpp b/source/ps/VideoMode.cpp index e8d7f6fe52..084da4cf75 100644 --- a/source/ps/VideoMode.cpp +++ b/source/ps/VideoMode.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2012 Wildfire Games. +/* Copyright (C) 2013 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -66,34 +66,65 @@ void CVideoMode::ReadConfig() bool CVideoMode::SetVideoMode(int w, int h, int bpp, bool fullscreen) { #if SDL_VERSION_ATLEAST(2, 0, 0) + Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; if (fullscreen) - flags |= SDL_WINDOW_FULLSCREEN; + flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; else flags |= SDL_WINDOW_RESIZABLE; - m_Window = SDL_CreateWindow("0 A.D.", 0, 0, w, h, flags); if (!m_Window) { - LOGERROR(L"SetVideoMode failed in SDL_CreateWindow: %dx%d:%d %d (\"%hs\")", - w, h, bpp, fullscreen ? 1 : 0, SDL_GetError()); - return false; - // TODO: fall back to windowed mode - } + m_Window = SDL_CreateWindow("0 A.D.", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags); + if (!m_Window) + { + // If fullscreen fails, try windowed mode + if (fullscreen) + { + LOGWARNING(L"Failed to set the video mode to fullscreen for the chosen resolution " + L"%dx%d:%d (\"%hs\"), falling back to windowed mode", + w, h, bpp, SDL_GetError()); + // Using default size for the window for now, as the attempted setting + // could be as large, or larger than the screen size. + return SetVideoMode(DEFAULT_WINDOW_W, DEFAULT_WINDOW_H, bpp, false); + } + else + { + LOGERROR(L"SetVideoMode failed in SDL_CreateWindow: %dx%d:%d %d (\"%hs\")", + w, h, bpp, fullscreen ? 1 : 0, SDL_GetError()); + return false; + } + } - if (SDL_SetWindowDisplayMode(m_Window, NULL) < 0) - { - LOGERROR(L"SetVideoMode failed in SDL_SetWindowDisplayMode: %dx%d:%d %d (\"%hs\")", - w, h, bpp, fullscreen ? 1 : 0, SDL_GetError()); - return false; - } + if (SDL_SetWindowDisplayMode(m_Window, NULL) < 0) + { + LOGERROR(L"SetVideoMode failed in SDL_SetWindowDisplayMode: %dx%d:%d %d (\"%hs\")", + w, h, bpp, fullscreen ? 1 : 0, SDL_GetError()); + return false; + } - SDL_GLContext context = SDL_GL_CreateContext(m_Window); - if (!context) + SDL_GLContext context = SDL_GL_CreateContext(m_Window); + if (!context) + { + LOGERROR(L"SetVideoMode failed in SDL_GL_CreateContext: %dx%d:%d %d (\"%hs\")", + w, h, bpp, fullscreen ? 1 : 0, SDL_GetError()); + return false; + } + } + else { - LOGERROR(L"SetVideoMode failed in SDL_GL_CreateContext: %dx%d:%d %d (\"%hs\")", - w, h, bpp, fullscreen ? 1 : 0, SDL_GetError()); - return false; + if (m_IsFullscreen != fullscreen) + { + if (SDL_SetWindowFullscreen(m_Window, flags) < 0) + { + LOGERROR(L"SetVideoMode failed in SDL_SetWindowFullscreen: %dx%d:%d %d (\"%hs\")", + w, h, bpp, fullscreen ? 1 : 0, SDL_GetError()); + return false; + } + } + + if (!fullscreen) + SDL_SetWindowSize(m_Window, w, h); } // Grab the current video settings diff --git a/source/tools/atlas/AtlasUI/Misc/KeyMap.cpp b/source/tools/atlas/AtlasUI/Misc/KeyMap.cpp index ab6d3015ec..cc9f9a8c81 100644 --- a/source/tools/atlas/AtlasUI/Misc/KeyMap.cpp +++ b/source/tools/atlas/AtlasUI/Misc/KeyMap.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2010 Wildfire Games. +/* Copyright (C) 2013 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -19,7 +19,12 @@ #include "KeyMap.h" -#include "SDL_keysym.h" +#include "SDL_version.h" +#if SDL_VERSION_ATLEAST(2, 0, 0) +# include "SDL_keycode.h" +#else // SDL 1.2 +# include "SDL_keysym.h" +#endif int GetSDLKeyFromWxKeyCode(int wxkey) { @@ -32,6 +37,18 @@ int GetSDLKeyFromWxKeyCode(int wxkey) switch (wxkey) { +#if SDL_VERSION_ATLEAST(2, 0, 0) + case WXK_NUMPAD0: return SDLK_KP_0; + case WXK_NUMPAD1: return SDLK_KP_1; + case WXK_NUMPAD2: return SDLK_KP_2; + case WXK_NUMPAD3: return SDLK_KP_3; + case WXK_NUMPAD4: return SDLK_KP_4; + case WXK_NUMPAD5: return SDLK_KP_5; + case WXK_NUMPAD6: return SDLK_KP_6; + case WXK_NUMPAD7: return SDLK_KP_7; + case WXK_NUMPAD8: return SDLK_KP_8; + case WXK_NUMPAD9: return SDLK_KP_9; +#else // SDL 1.2 case WXK_NUMPAD0: return SDLK_KP0; case WXK_NUMPAD1: return SDLK_KP1; case WXK_NUMPAD2: return SDLK_KP2; @@ -42,6 +59,7 @@ int GetSDLKeyFromWxKeyCode(int wxkey) case WXK_NUMPAD7: return SDLK_KP7; case WXK_NUMPAD8: return SDLK_KP8; case WXK_NUMPAD9: return SDLK_KP9; +#endif case WXK_NUMPAD_DECIMAL: return SDLK_KP_PERIOD; case WXK_NUMPAD_DIVIDE: return SDLK_KP_DIVIDE; case WXK_NUMPAD_MULTIPLY: return SDLK_KP_MULTIPLY; @@ -76,9 +94,14 @@ int GetSDLKeyFromWxKeyCode(int wxkey) case WXK_F14: return SDLK_F14; case WXK_F15: return SDLK_F15; +#if SDL_VERSION_ATLEAST(2, 0, 0) + case WXK_NUMLOCK: return SDLK_NUMLOCKCLEAR; + case WXK_SCROLL: return SDLK_SCROLLLOCK; +#else // SDL 1.2 case WXK_NUMLOCK: return SDLK_NUMLOCK; -// case WXK_: return SDLK_CAPSLOCK; case WXK_SCROLL: return SDLK_SCROLLOCK; +#endif +// case WXK_: return SDLK_CAPSLOCK; case WXK_SHIFT: return SDLK_RSHIFT; // case WXK_: return SDLK_LSHIFT; case WXK_CONTROL: return SDLK_RCTRL; @@ -93,7 +116,11 @@ int GetSDLKeyFromWxKeyCode(int wxkey) // case WXK_: return SDLK_COMPOSE; case WXK_HELP: return SDLK_HELP; +#if SDL_VERSION_ATLEAST(2, 0, 0) + case WXK_PRINT: return SDLK_PRINTSCREEN; +#else // SDL 1.2 case WXK_PRINT: return SDLK_PRINT; +#endif // case WXK_: return SDLK_SYSREQ; // case WXK_: return SDLK_BREAK; case WXK_MENU: return SDLK_MENU;