From a0034f8000c424ad5f4b2d709db4ee9e13d13a05 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Fri, 25 Sep 2015 21:26:49 +0200 Subject: [PATCH] Finalized SDL2 migration --- src/app/app.cpp | 41 ++++++++++----------- src/app/input.cpp | 22 +++++++----- src/common/event.cpp | 9 +++-- src/common/event.h | 58 +++++++++--------------------- src/common/image.cpp | 1 - src/common/key.h | 4 ++- src/common/restext.cpp | 15 ++++---- src/graphics/engine/text.cpp | 1 - src/graphics/opengl/gl21device.cpp | 2 -- src/graphics/opengl/gl33device.cpp | 2 -- src/graphics/opengl/gldevice.cpp | 2 -- 11 files changed, 62 insertions(+), 95 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index c57b6824..94976a72 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -579,8 +579,6 @@ bool CApplication::Create() } } - //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); //TODO: ? - // Don't generate joystick events SDL_JoystickEventState(SDL_IGNORE); @@ -1002,30 +1000,34 @@ Event CApplication::ProcessSystemEvent() } else if (m_private->currentEvent.type == SDL_WINDOWEVENT) { - if (m_private->currentEvent.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { + if (m_private->currentEvent.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) + { Gfx::DeviceConfig newConfig = m_deviceConfig; newConfig.size.x = m_private->currentEvent.window.data1; newConfig.size.y = m_private->currentEvent.window.data2; if (newConfig.size != m_deviceConfig.size) ChangeVideoConfig(newConfig); } - // TODO: EVENT_ACTIVE - /*{ - event.type = EVENT_ACTIVE; - auto data = MakeUnique(); + if (m_private->currentEvent.window.event == SDL_WINDOWEVENT_ENTER) + { + event.type = EVENT_MOUSE_ENTER; + } - if (m_private->currentEvent.active.type & SDL_APPINPUTFOCUS) - data->flags |= ACTIVE_INPUT; - if (m_private->currentEvent.active.type & SDL_APPMOUSEFOCUS) - data->flags |= ACTIVE_MOUSE; - if (m_private->currentEvent.active.type & SDL_APPACTIVE) - data->flags |= ACTIVE_APP; + if (m_private->currentEvent.window.event == SDL_WINDOWEVENT_LEAVE) + { + event.type = EVENT_MOUSE_LEAVE; + } - data->gain = m_private->currentEvent.active.gain == 1; + if (m_private->currentEvent.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) + { + event.type = EVENT_FOCUS_GAINED; + } - event.data = std::move(data); - }*/ + if (m_private->currentEvent.window.event == SDL_WINDOWEVENT_FOCUS_LOST) + { + event.type = EVENT_FOCUS_LOST; + } } else if ( (m_private->currentEvent.type == SDL_KEYDOWN) || (m_private->currentEvent.type == SDL_KEYUP) ) @@ -1190,13 +1192,6 @@ void CApplication::LogEvent(const Event &event) l->Trace(" button = %d\n", data->button); break; } - case EVENT_ACTIVE: - { - auto data = event.GetData(); - l->Trace(" flags = 0x%x\n", data->flags); - l->Trace(" gain = %s\n", data->gain ? "true" : "false"); - break; - } default: break; } diff --git a/src/app/input.cpp b/src/app/input.cpp index 32400dca..1ffecf84 100644 --- a/src/app/input.cpp +++ b/src/app/input.cpp @@ -330,6 +330,7 @@ InputSlot CInput::FindBinding(unsigned int key) void CInput::SaveKeyBindings() { std::stringstream key; + CConfigFile::GetInstancePointer()->SetStringProperty("Keybindings", "_Version", "SDL2"); for (int i = 0; i < INPUT_SLOT_MAX; i++) { InputBinding b = GetInputBinding(static_cast(i)); @@ -355,19 +356,22 @@ void CInput::LoadKeyBindings() { std::stringstream skey; std::string keys; - for (int i = 0; i < INPUT_SLOT_MAX; i++) + if (CConfigFile::GetInstancePointer()->GetStringProperty("Keybindings", "_Version", keys) && keys == "SDL2") // Keybindings from SDL1.2 are incompatible with SDL2 !! { - InputBinding b; + for (int i = 0; i < INPUT_SLOT_MAX; i++) + { + InputBinding b; - if (!CConfigFile::GetInstancePointer()->GetStringProperty("Keybindings", m_keyTable[static_cast(i)], keys)) - continue; - skey.clear(); - skey.str(keys); + if (!CConfigFile::GetInstancePointer()->GetStringProperty("Keybindings", m_keyTable[static_cast(i)], keys)) + continue; + skey.clear(); + skey.str(keys); - skey >> b.primary; - skey >> b.secondary; + skey >> b.primary; + skey >> b.secondary; - SetInputBinding(static_cast(i), b); + SetInputBinding(static_cast(i), b); + } } for (int i = 0; i < JOY_AXIS_SLOT_MAX; i++) diff --git a/src/common/event.cpp b/src/common/event.cpp index de326bb2..5fce71b7 100644 --- a/src/common/event.cpp +++ b/src/common/event.cpp @@ -52,17 +52,20 @@ void InitializeEventTypeTexts() EVENT_TYPE_TEXT[EVENT_MOUSE_BUTTON_UP] = "EVENT_MOUSE_BUTTON_UP"; EVENT_TYPE_TEXT[EVENT_MOUSE_WHEEL] = "EVENT_MOUSE_WHEEL"; EVENT_TYPE_TEXT[EVENT_MOUSE_MOVE] = "EVENT_MOUSE_MOVE"; + EVENT_TYPE_TEXT[EVENT_MOUSE_MOVE] = "EVENT_MOUSE_ENTER"; + EVENT_TYPE_TEXT[EVENT_MOUSE_MOVE] = "EVENT_MOUSE_LEAVE"; + EVENT_TYPE_TEXT[EVENT_KEY_DOWN] = "EVENT_KEY_DOWN"; EVENT_TYPE_TEXT[EVENT_KEY_UP] = "EVENT_KEY_UP"; - - EVENT_TYPE_TEXT[EVENT_ACTIVE] = "EVENT_ACTIVE"; - EVENT_TYPE_TEXT[EVENT_TEXT_INPUT] = "EVENT_TEXT_INPUT"; EVENT_TYPE_TEXT[EVENT_JOY_AXIS] = "EVENT_JOY_AXIS"; EVENT_TYPE_TEXT[EVENT_JOY_BUTTON_DOWN] = "EVENT_JOY_BUTTON_DOWN"; EVENT_TYPE_TEXT[EVENT_JOY_BUTTON_UP] = "EVENT_JOY_BUTTON_UP"; + EVENT_TYPE_TEXT[EVENT_FOCUS_GAINED] = "EVENT_FOCUS_GAINED"; + EVENT_TYPE_TEXT[EVENT_FOCUS_LOST] = "EVENT_FOCUS_LOST"; + EVENT_TYPE_TEXT[EVENT_UPDINTERFACE] = "EVENT_UPDINTERFACE"; EVENT_TYPE_TEXT[EVENT_WIN] = "EVENT_WIN"; diff --git a/src/common/event.h b/src/common/event.h index d7230b07..0da15182 100644 --- a/src/common/event.h +++ b/src/common/event.h @@ -62,23 +62,29 @@ enum EventType EVENT_MOUSE_WHEEL = 5, //! Event sent after moving the mouse EVENT_MOUSE_MOVE = 7, + //! Event sent when mouse enters the window + EVENT_MOUSE_ENTER = 8, + //! Event sent when mouse leaves the window + EVENT_MOUSE_LEAVE = 9, + //! Event sent after pressing a key - EVENT_KEY_DOWN = 8, + EVENT_KEY_DOWN = 10, //! Event sent after releasing a key - EVENT_KEY_UP = 9, - - //! Event sent when application window loses/gains focus - EVENT_ACTIVE = 10, - + EVENT_KEY_UP = 11, //! Event sent when user inputs some character - EVENT_TEXT_INPUT = 11, + EVENT_TEXT_INPUT = 12, //! Event sent after moving joystick axes - EVENT_JOY_AXIS = 12, + EVENT_JOY_AXIS = 13, //! Event sent after pressing a joystick button - EVENT_JOY_BUTTON_DOWN = 13, + EVENT_JOY_BUTTON_DOWN = 14, //! Event sent after releasing a joystick button - EVENT_JOY_BUTTON_UP = 14, + EVENT_JOY_BUTTON_UP = 15, + + //! Event sent when the app winddow gains focus + EVENT_FOCUS_GAINED = 16, + //! Event sent when the app winddow loses focus + EVENT_FOCUS_LOST = 17, //!< Maximum value of system events EVENT_SYS_MAX, @@ -678,38 +684,6 @@ struct JoyButtonEventData : public EventData unsigned char button = 0; }; -/** - * \enum ActiveEventFlags - * \brief Type of focus gained/lost - */ -enum ActiveEventFlags -{ - //! Application window focus - ACTIVE_APP = 0x01, - //! Input focus - ACTIVE_INPUT = 0x02, - //! Mouse focus - ACTIVE_MOUSE = 0x04 - -}; - -/** - * \struct ActiveEventData - * \brief Additional data for active event - */ -struct ActiveEventData : public EventData -{ - std::unique_ptr Clone() const override - { - return MakeUnique(*this); - } - - //! Flags (bitmask of enum values ActiveEventFlags) - unsigned char flags = 0; - //! True if the focus was gained; false otherwise - bool gain = false; -}; - /** * \struct Event * \brief Event sent by system, interface or game diff --git a/src/common/image.cpp b/src/common/image.cpp index 845eb064..dd57b616 100644 --- a/src/common/image.cpp +++ b/src/common/image.cpp @@ -238,7 +238,6 @@ void CImage::ConvertToRGBA() void CImage::BlitToNewRGBASurface(int width, int height) { - //m_data->surface->flags &= (~SDL_SRCALPHA); //TODO: ? SDL_Surface* convertedSurface = SDL_CreateRGBSurface(0, width, height, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000); assert(convertedSurface != nullptr); diff --git a/src/common/key.h b/src/common/key.h index a0d66587..d57dd098 100644 --- a/src/common/key.h +++ b/src/common/key.h @@ -26,7 +26,9 @@ #include -#define SDLK_LAST (SDLK_SCANCODE_MASK << 1) //TODO + +// TODO: This is a bit ugly hack +#define SDLK_LAST (SDLK_SCANCODE_MASK << 1) /* Key definitions are specially defined here so that it is clear in other parts of the code that these are used. It is to avoid having SDL-related enum values or #defines lying around diff --git a/src/common/restext.cpp b/src/common/restext.cpp index 40d17aba..1c1a27fe 100644 --- a/src/common/restext.cpp +++ b/src/common/restext.cpp @@ -866,18 +866,17 @@ bool GetResource(ResType type, unsigned int num, std::string& text) } else { - // TODO: fix signed/unsigned comparations - if (num == KEY_INVALID) + if (num == static_cast(KEY_INVALID)) text.clear(); - else if (num == VIRTUAL_KMOD_CTRL) + else if (num == static_cast(VIRTUAL_KMOD(CTRL))) text = "Ctrl"; - else if (num == VIRTUAL_KMOD_SHIFT) + else if (num == static_cast(VIRTUAL_KMOD(SHIFT))) text = "Shift"; - else if (num == VIRTUAL_KMOD_ALT) + else if (num == static_cast(VIRTUAL_KMOD(ALT))) text = "Alt"; - else if (num == VIRTUAL_KMOD_GUI) + else if (num == static_cast(VIRTUAL_KMOD(GUI))) text = "Win"; // TODO: Better description of this key? - else if (num > VIRTUAL_JOY(0)) + else if (num > static_cast(VIRTUAL_JOY(0))) { text = gettext("Button %1"); text = StrUtils::Replace(text, "%1", StrUtils::ToString(1 + num - VIRTUAL_JOY(0))); @@ -885,8 +884,6 @@ bool GetResource(ResType type, unsigned int num, std::string& text) else { text = SDL_GetKeyName(static_cast(num)); - text = boost::regex_replace(text, boost::regex("\\[(.*)\\]"), "\\1"); - text[0] = toupper(text[0]); } return true; } diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp index 5cf724c6..d1003443 100644 --- a/src/graphics/engine/text.cpp +++ b/src/graphics/engine/text.cpp @@ -1017,7 +1017,6 @@ CharTexture CText::CreateCharTexture(UTF8Char ch, CachedFont* font) int w = Math::NextPowerOfTwo(textSurface->w); int h = Math::NextPowerOfTwo(textSurface->h); - //textSurface->flags = textSurface->flags & (~SDL_SRCALPHA); //TODO: ? SDL_Surface* textureSurface = SDL_CreateRGBSurface(0, w, h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); SDL_BlitSurface(textSurface, nullptr, textureSurface, nullptr); diff --git a/src/graphics/opengl/gl21device.cpp b/src/graphics/opengl/gl21device.cpp index d07c6be8..1c63deb6 100644 --- a/src/graphics/opengl/gl21device.cpp +++ b/src/graphics/opengl/gl21device.cpp @@ -727,8 +727,6 @@ Texture CGL21Device::CreateTexture(ImageData *data, const TextureCreateParams &p SDL_PixelFormat format; format.BytesPerPixel = 4; format.BitsPerPixel = 32; - //format.alpha = 0; //TODO: ? - //format.colorkey = 0; //TODO: ? format.Aloss = format.Bloss = format.Gloss = format.Rloss = 0; format.Amask = 0xFF000000; format.Ashift = 24; diff --git a/src/graphics/opengl/gl33device.cpp b/src/graphics/opengl/gl33device.cpp index 65a85845..a3cc6254 100644 --- a/src/graphics/opengl/gl33device.cpp +++ b/src/graphics/opengl/gl33device.cpp @@ -754,8 +754,6 @@ Texture CGL33Device::CreateTexture(ImageData *data, const TextureCreateParams &p SDL_PixelFormat format; format.BytesPerPixel = 4; format.BitsPerPixel = 32; - //format.alpha = 0; //TODO: ? - //format.colorkey = 0; //TODO: ? format.Aloss = format.Bloss = format.Gloss = format.Rloss = 0; format.Amask = 0xFF000000; format.Ashift = 24; diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp index 102fa9bf..f283a0aa 100644 --- a/src/graphics/opengl/gldevice.cpp +++ b/src/graphics/opengl/gldevice.cpp @@ -696,8 +696,6 @@ Texture CGLDevice::CreateTexture(ImageData *data, const TextureCreateParams &par SDL_PixelFormat format; format.BytesPerPixel = 4; format.BitsPerPixel = 32; - //format.alpha = 0; //TODO: ? - //format.colorkey = 0; //TODO: ? format.Aloss = format.Bloss = format.Gloss = format.Rloss = 0; format.Amask = 0xFF000000; format.Ashift = 24;