From b7a6b52fe55479244e0a198e671f957a54194a10 Mon Sep 17 00:00:00 2001 From: fgenesis Date: Mon, 18 Mar 2019 00:50:36 +0100 Subject: [PATCH] SDL window backend cleanup --- BBGE/Window_SDL1.cpp | 72 ++++++++++++++++++++++++++++++-------------- BBGE/Window_SDL2.cpp | 21 ++----------- 2 files changed, 52 insertions(+), 41 deletions(-) diff --git a/BBGE/Window_SDL1.cpp b/BBGE/Window_SDL1.cpp index 60f91d0..88ce0dd 100644 --- a/BBGE/Window_SDL1.cpp +++ b/BBGE/Window_SDL1.cpp @@ -2,6 +2,7 @@ #include #include #include "OSFunctions.h" +#include "Base.h" #define SDL2_BACKEND SDL_VERSION_ATLEAST(2,0,0) @@ -9,21 +10,11 @@ static Window *s_theWindow; // since SDL1 can only create a single window, keep it around to make sure only one exists. -struct Backend -{ - Backend() - : win(NULL) - {} - - SDL_Surface *win; -}; - -#define BACKEND (static_cast(_backend)) -#define WIN (BACKEND->win) +#define WIN ((SDL_Surface*&)(_backend)) void *Window::_initBackend() { - return new Backend; + return NULL; } void Window::_ctor() @@ -34,17 +25,22 @@ void Window::_ctor() Window::~Window() { - delete BACKEND; s_theWindow = NULL; } -bool Window::_open(unsigned w, unsigned h, bool full, unsigned bpp, bool vsync, unsigned display, unsigned hz) +bool Window::isOpen() const +{ + return !!WIN; +} + +void Window::_open(unsigned w, unsigned h, bool full, unsigned bpp, bool vsync, unsigned display, unsigned hz) { // ignored for SDL1 (void)display; (void)hz; - assert(w && h); + if(!w) w = 800; + if(!h) h = 600; // have to cast away constness, since SDL_putenv() might be #defined to // putenv(), which takes a (char *), and freaks out newer GCC releases @@ -55,15 +51,21 @@ bool Window::_open(unsigned w, unsigned h, bool full, unsigned bpp, bool vsync, SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, vsync); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - Uint32 flags = SDL_OPENGL; + Uint32 flags = SDL_OPENGL | SDL_ANYFORMAT; if(full) flags |= SDL_FULLSCREEN; SDL_Surface *surf = SDL_SetVideoMode(w, h, bpp, flags); if(!surf) - return false; + exit_error("SDL_SetVideoMode failed"); WIN = surf; - return true; + + ::initIcon(WIN); +} + +void Window::_adjust(unsigned w, unsigned h, bool full, unsigned bpp, bool vsync, unsigned display, unsigned hz) +{ + _open(w, h, full, bpp, vsync, display, hz); } void Window::warpMouse(int x, int y) @@ -86,11 +88,6 @@ void Window::setTitle(const char *s) SDL_WM_SetCaption(s, s); } -void Window::initIcon() -{ - ::initIcon(WIN); -} - int Window::getDisplayIndex() const { return -1; @@ -106,5 +103,34 @@ bool Window::hasInputFocus() const return ((SDL_GetAppState() & SDL_APPINPUTFOCUS) != 0); } +void Window::_onEventImpl(const SDL_Event& ev) +{ + switch(ev.type) + { + case SDL_KEYDOWN: + { +#if __APPLE__ + if ((ev.key.keysym.sym == SDLK_q) && (ev.key.keysym.mod & KMOD_META)) +#else + if ((ev.key.keysym.sym == SDLK_F4) && (ev.key.keysym.mod & KMOD_ALT)) +#endif + { + onQuit(); + } + } + break; + + case SDL_VIDEORESIZE: + onResize(ev.resize.w, ev.resize.h); + break; + } +} + +void Window::initSize() +{ + onResize(WIN->w, WIN->h); +} + + #endif // !SDL2_BACKEND diff --git a/BBGE/Window_SDL2.cpp b/BBGE/Window_SDL2.cpp index 50e5fda..360651e 100644 --- a/BBGE/Window_SDL2.cpp +++ b/BBGE/Window_SDL2.cpp @@ -66,6 +66,7 @@ void Window::_open(unsigned w, unsigned h, bool full, unsigned bpp, bool vsync, # endif SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_SetHint("SDL_VIDEO_HIGHDPI_DISABLED", "1"); Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE; if(full) @@ -178,32 +179,16 @@ void Window::warpMouse(int x, int y) SDL_WarpMouseInWindow(WIN, x, y); } -#ifndef KMOD_GUI -#define KMOD_GUI KMOD_META -#endif - void Window::_onEventImpl(const SDL_Event& ev) { switch(ev.type) { -#if 0 case SDL_KEYDOWN: - { #if __APPLE__ -#if SDL_VERSION_ATLEAST(2, 0, 0) - if ((ev.key.keysym.sym == SDLK_q) && (ev.key.keysym.mod & KMOD_GUI)) -#else - if ((ev.key.keysym.sym == SDLK_q) && (ev.key.keysym.mod & KMOD_META)) + if ((ev.key.keysym.sym == SDLK_q) && (ev.key.keysym.mod & KMOD_GUI)) + onQuit(); #endif -#else - if ((ev.key.keysym.sym == SDLK_F4) && (ev.key.keysym.mod & KMOD_ALT)) -#endif - { - onQuit() - } - } break; -#endif case SDL_WINDOWEVENT: {