diff options
author | Johannes Schickel | 2015-01-25 01:34:57 +0100 |
---|---|---|
committer | Johannes Schickel | 2015-01-25 20:23:25 +0100 |
commit | 8530997fff7b5b9d558f7dd6a0d07c236e4de16f (patch) | |
tree | 3ded3cc11bae7b50138f35be7626cdae9c400af2 /backends/graphics/sdl | |
parent | defe71792dfc0ab4bcb14a64a9fc8eab9a638e69 (diff) | |
download | scummvm-rg350-8530997fff7b5b9d558f7dd6a0d07c236e4de16f.tar.gz scummvm-rg350-8530997fff7b5b9d558f7dd6a0d07c236e4de16f.tar.bz2 scummvm-rg350-8530997fff7b5b9d558f7dd6a0d07c236e4de16f.zip |
SDL: Add experimental support for SDL2.
This is based upon skristiansson's change set to make ScummVM work with SDL2.
Diffstat (limited to 'backends/graphics/sdl')
-rw-r--r-- | backends/graphics/sdl/sdl-graphics.cpp | 131 | ||||
-rw-r--r-- | backends/graphics/sdl/sdl-graphics.h | 20 |
2 files changed, 150 insertions, 1 deletions
diff --git a/backends/graphics/sdl/sdl-graphics.cpp b/backends/graphics/sdl/sdl-graphics.cpp index 58253c9509..f6d56ece01 100644 --- a/backends/graphics/sdl/sdl-graphics.cpp +++ b/backends/graphics/sdl/sdl-graphics.cpp @@ -27,10 +27,18 @@ #include "common/textconsole.h" SdlGraphicsManager::SdlGraphicsManager(SdlEventSource *source) - : _eventSource(source) { + : _eventSource(source) +#if SDL_VERSION_ATLEAST(2, 0, 0) + , _window(nullptr), _inputGrabState(false), _windowCaption("ScummVM"), _windowIcon(nullptr) +#endif + { } SdlGraphicsManager::~SdlGraphicsManager() { +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_FreeSurface(_windowIcon); + destroyWindow(); +#endif } void SdlGraphicsManager::activateManager() { @@ -42,33 +50,111 @@ void SdlGraphicsManager::deactivateManager() { } void SdlGraphicsManager::setWindowCaption(const Common::String &caption) { +#if SDL_VERSION_ATLEAST(2, 0, 0) + _windowCaption = caption; + if (_window) { + SDL_SetWindowTitle(_window, caption.c_str()); + } +#else SDL_WM_SetCaption(caption.c_str(), caption.c_str()); +#endif } void SdlGraphicsManager::setWindowIcon(SDL_Surface *icon) { +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_FreeSurface(_windowIcon); + _windowIcon = icon; + if (_window) { + SDL_SetWindowIcon(_window, icon); + } +#else SDL_WM_SetIcon(icon, NULL); SDL_FreeSurface(icon); +#endif } void SdlGraphicsManager::toggleMouseGrab() { +#if SDL_VERSION_ATLEAST(2, 0, 0) + if (_window) { + _inputGrabState = !(SDL_GetWindowGrab(_window) == SDL_TRUE); + SDL_SetWindowGrab(_window, _inputGrabState ? SDL_TRUE : SDL_FALSE); + } +#else if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_OFF) { SDL_WM_GrabInput(SDL_GRAB_ON); } else { SDL_WM_GrabInput(SDL_GRAB_OFF); } +#endif } bool SdlGraphicsManager::hasMouseFocus() const { +#if SDL_VERSION_ATLEAST(2, 0, 0) + if (_window) { + return (SDL_GetWindowFlags(_window) & SDL_WINDOW_MOUSE_FOCUS); + } else { + return false; + } +#else return (SDL_GetAppState() & SDL_APPMOUSEFOCUS); +#endif } void SdlGraphicsManager::warpMouseInWindow(uint x, uint y) { +#if SDL_VERSION_ATLEAST(2, 0, 0) + if (_window) { + SDL_WarpMouseInWindow(_window, x, y); + } +#else SDL_WarpMouse(x, y); +#endif } void SdlGraphicsManager::iconifyWindow() { +#if SDL_VERSION_ATLEAST(2, 0, 0) + if (_window) { + SDL_MinimizeWindow(_window); + } +#else SDL_WM_IconifyWindow(); +#endif +} + +SdlGraphicsManager::State::State() +#if SDL_VERSION_ATLEAST(2, 0, 0) + : windowIcon(nullptr) +#endif + { +} + +SdlGraphicsManager::State::~State() { +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_FreeSurface(windowIcon); +#endif +} + +#if SDL_VERSION_ATLEAST(2, 0, 0) +SDL_Surface *copySDLSurface(SDL_Surface *src) { + const bool locked = SDL_MUSTLOCK(src) == SDL_TRUE; + + if (locked) { + if (SDL_LockSurface(src) != 0) { + return nullptr; + } + } + + SDL_Surface *res = SDL_CreateRGBSurfaceFrom(src->pixels, + src->w, src->h, src->format->BitsPerPixel, + src->pitch, src->format->Rmask, src->format->Gmask, + src->format->Bmask, src->format->Amask); + + if (locked) { + SDL_UnlockSurface(src); + } + + return res; } +#endif SdlGraphicsManager::State SdlGraphicsManager::getState() { State state; @@ -82,10 +168,29 @@ SdlGraphicsManager::State SdlGraphicsManager::getState() { state.pixelFormat = getScreenFormat(); #endif +#if SDL_VERSION_ATLEAST(2, 0, 0) + state.inputGrabState = _inputGrabState; + state.windowCaption = _windowCaption; + state.windowIcon = copySDLSurface(_windowIcon); +#endif + return state; } bool SdlGraphicsManager::setState(const State &state) { +#if SDL_VERSION_ATLEAST(2, 0, 0) + _inputGrabState = state.inputGrabState; + if (!_window) { + _windowCaption = state.windowCaption; + SDL_FreeSurface(_windowIcon); + _windowIcon = copySDLSurface(state.windowIcon); + } else { + SDL_SetWindowGrab(_window, _inputGrabState ? SDL_TRUE : SDL_FALSE); + setWindowCaption(state.windowCaption); + setWindowIcon(copySDLSurface(state.windowIcon)); + } +#endif + beginGFXTransaction(); #ifdef USE_RGB_COLOR initSize(state.screenWidth, state.screenHeight, &state.pixelFormat); @@ -102,3 +207,27 @@ bool SdlGraphicsManager::setState(const State &state) { return true; } } + +#if SDL_VERSION_ATLEAST(2, 0, 0) +bool SdlGraphicsManager::createWindow(int width, int height, uint32 flags) { + destroyWindow(); + + if (_inputGrabState) { + flags |= SDL_WINDOW_INPUT_GRABBED; + } + + _window = SDL_CreateWindow(_windowCaption.c_str(), SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, width, height, flags); + if (!_window) { + return false; + } + SDL_SetWindowIcon(_window, _windowIcon); + + return true; +} + +void SdlGraphicsManager::destroyWindow() { + SDL_DestroyWindow(_window); + _window = nullptr; +} +#endif diff --git a/backends/graphics/sdl/sdl-graphics.h b/backends/graphics/sdl/sdl-graphics.h index 3a53a4e40e..af7242b99e 100644 --- a/backends/graphics/sdl/sdl-graphics.h +++ b/backends/graphics/sdl/sdl-graphics.h @@ -134,6 +134,9 @@ public: * between different SDL graphic managers on runtime. */ struct State { + State(); + ~State(); + int screenWidth, screenHeight; bool aspectRatio; bool fullscreen; @@ -142,6 +145,12 @@ public: #ifdef USE_RGB_COLOR Graphics::PixelFormat pixelFormat; #endif + +#if SDL_VERSION_ATLEAST(2, 0, 0) + bool inputGrabState; + Common::String windowCaption; + SDL_Surface *windowIcon; +#endif }; /** @@ -156,6 +165,17 @@ public: protected: SdlEventSource *_eventSource; + +#if SDL_VERSION_ATLEAST(2, 0, 0) + SDL_Window *_window; + + bool createWindow(int width, int height, uint32 flags); + void destroyWindow(); +private: + bool _inputGrabState; + Common::String _windowCaption; + SDL_Surface *_windowIcon; +#endif }; #endif |