diff --git a/Aquaria/AquariaProgressBar.cpp b/Aquaria/AquariaProgressBar.cpp deleted file mode 100644 index b9857f8..0000000 --- a/Aquaria/AquariaProgressBar.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* -Copyright (C) 2007, 2010 - Bit-Blot - -This file is part of Aquaria. - -Aquaria is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -*/ -#include "AquariaProgressBar.h" -#include "../BBGE/Core.h" - - -AquariaProgressBar::AquariaProgressBar() : RenderObject() -{ - perc = 0; - spinner.setTexture("Progress"); - spinner.alphaMod = 0.1f; - addChild(&spinner, PM_STATIC); - followCamera = 1; -} - -void AquariaProgressBar::progress(float addPerc) -{ - if (addPerc==0) - addPerc = 0.01f; - this->perc += addPerc; - spinner.rotation = Vector(0,0,this->perc*360); - core->render(); - core->showBuffer(); -} - diff --git a/Aquaria/AquariaProgressBar.h b/Aquaria/AquariaProgressBar.h deleted file mode 100644 index cf55e5c..0000000 --- a/Aquaria/AquariaProgressBar.h +++ /dev/null @@ -1,38 +0,0 @@ -/* -Copyright (C) 2007, 2010 - Bit-Blot - -This file is part of Aquaria. - -Aquaria is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -*/ -#ifndef AQUARIAPROGRESSBAR_H -#define AQUARIAPROGRESSBAR_H - -#include "../BBGE/Quad.h" - -class AquariaProgressBar : public RenderObject -{ -public: - AquariaProgressBar(); - - void progress(float addPerc=0); - -protected: - Quad spinner; - float perc; -}; - -#endif diff --git a/Aquaria/DSQ.cpp b/Aquaria/DSQ.cpp index 4b947d9..27b4910 100644 --- a/Aquaria/DSQ.cpp +++ b/Aquaria/DSQ.cpp @@ -156,6 +156,7 @@ static const Vector savesz(750.0f/1024.0f, 750.0f/1024.0f); DSQ::DSQ(const std::string& fileSystem, const std::string& extraDataDir) : Core(fileSystem, extraDataDir, LR_MAX, APPNAME, PARTICLE_AMOUNT_DEFAULT, "Aquaria") { + assert(!dsq); dsq = this; cutscene_bg = 0; @@ -206,7 +207,6 @@ DSQ::DSQ(const std::string& fileSystem, const std::string& extraDataDir) console = 0; #endif cmDebug = 0; - languagePack = "english"; saveSlotMode = SSM_NONE; afterEffectManagerLayer = LR_AFTER_EFFECTS; // LR_AFTER_EFFECTS renderObjectLayers.resize(LR_MAX); @@ -224,14 +224,16 @@ DSQ::DSQ(const std::string& fileSystem, const std::string& extraDataDir) DSQ::~DSQ() { + assert(dsq == this); dsq = 0; } // actually toggle void DSQ::toggleFullscreen() { - setFullscreen(!_fullscreen); - user.video.full = _fullscreen; + bool newfull = !window->isFullscreen(); + setFullscreen(newfull); + user.video.full = newfull; } // for handling the input, not the actual switch functionality @@ -1689,12 +1691,10 @@ void DSQ::setInputMode(InputDevice mode) { case INPUT_JOYSTICK: core->joystickAsMouse = true; - updateCursorFromMouse = false; break; case INPUT_MOUSE: setMousePosition(core->mouse.position); core->joystickAsMouse = false; - updateCursorFromMouse = true; break; case INPUT_KEYBOARD: break; @@ -3045,7 +3045,7 @@ bool DSQ::confirm(const std::string &text, const std::string &image, bool ok, fl AquariaGuiElement::currentGuiInputLevel = GUILEVEL_CONFIRM; - dsq->run(t); + dsq->run(t, true); float t2 = 0.05f; @@ -3116,11 +3116,11 @@ bool DSQ::confirm(const std::string &text, const std::string &image, bool ok, fl txt->alpha.interpolateTo(1, t2); addRenderObject(txt, LR_CONFIRM); - dsq->run(t2); + dsq->run(t2, true); while (!confirmDone) { - dsq->run(FRAME_TIME); + dsq->run(FRAME_TIME, true); if (countdown > 0) { countdown -= FRAME_TIME; if (countdown < 0) @@ -3133,11 +3133,11 @@ bool DSQ::confirm(const std::string &text, const std::string &image, bool ok, fl txt->alpha.interpolateTo(0, t2); if (yes) yes->alpha.interpolateTo(0, t2); if (no) no->alpha.interpolateTo(0, t2); - dsq->run(t2); + dsq->run(t2, true); bgLabel->alpha.interpolateTo(0, t); bgLabel->scale.interpolateTo(Vector(0.5, 0.5), t); - dsq->run(t); + dsq->run(t, true); bgLabel->safeKill(); txt->safeKill(); @@ -4024,10 +4024,6 @@ void DSQ::onUpdate(float dt) } } - static bool lastfullscreen = false; - - lastfullscreen = _fullscreen; - updatepecue(dt); Network::update(); @@ -4045,11 +4041,6 @@ void DSQ::shakeCamera(float mag, float time) shakeCameraTimer = time; } -bool DSQ::isShakingCamera() -{ - return (shakeCameraTimer > 0); -} - void DSQ::delay(float dt) { core->run(dt); diff --git a/Aquaria/DSQ.h b/Aquaria/DSQ.h index 20ea5e7..fb10fb6 100644 --- a/Aquaria/DSQ.h +++ b/Aquaria/DSQ.h @@ -299,10 +299,6 @@ public: ParticleEffect *spawnParticleEffect(const std::string &name, Vector position, float rot=0, float t=0, int layer=LR_PARTICLES, float follow=0); void spawnAllIngredients(const Vector &position); - bool isShakingCamera(); - - std::string languagePack; - int getEntityTypeIndexByName(std::string s); void screenMessage(const std::string &msg); #ifdef AQUARIA_BUILD_CONSOLE // No need to override it otherwise. diff --git a/Aquaria/ExternalGUI.h b/Aquaria/ExternalGUI.h deleted file mode 100644 index a2d5dd4..0000000 --- a/Aquaria/ExternalGUI.h +++ /dev/null @@ -1,27 +0,0 @@ -/* -Copyright (C) 2007, 2010 - Bit-Blot - -This file is part of Aquaria. - -Aquaria is free software; you can redistribute it and/or -modify it under the terms of the GNU General Public License -as published by the Free Software Foundation; either version 2 -of the License, or (at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program; if not, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -*/ - -class ExternalGUI -{ -public: - ExternalGui(); -}; - diff --git a/Aquaria/Game.h b/Aquaria/Game.h index 5b55354..443befb 100644 --- a/Aquaria/Game.h +++ b/Aquaria/Game.h @@ -27,7 +27,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "AquariaMenuItem.h" #include "ScriptedEntity.h" #include "TileVector.h" -#include "AquariaProgressBar.h" #ifdef AQUARIA_DEMO #undef AQUARIA_BUILD_SCENEEDITOR diff --git a/Aquaria/InGameMenu.cpp b/Aquaria/InGameMenu.cpp index 78f96a8..81f2735 100644 --- a/Aquaria/InGameMenu.cpp +++ b/Aquaria/InGameMenu.cpp @@ -1705,8 +1705,7 @@ static std::string screenModeStr(const ScreenMode& m) static bool isCurrentScreenMode(const ScreenMode& m) { if(!m.x && !m.y && core->isDesktopResolution()) - return true; - + return true; return m.x == dsq->user.video.resx && m.y == dsq->user.video.resy && (!m.hz || dsq->user.video.hz == m.hz); } @@ -3280,7 +3279,7 @@ void InGameMenu::onOptionsSave() || v.hz != bv.hz ) { - dsq->initGraphics(v.resx, v.resy, v.full, v.vsync, v.bits, -1, v.hz); + dsq->resizeWindow(v.resx, v.resy, v.full, v.vsync, v.bits, -1, v.hz); if (dsq->confirm("", "graphics", false, 10)) { } else { v.resx = bv.resx; @@ -3292,7 +3291,7 @@ void InGameMenu::onOptionsSave() dsq->user.apply(); - dsq->initGraphics(v.resx, v.resy, v.full); + dsq->resizeWindow(v.resx, v.resy, v.full, -1, -1, -1, -1); } } diff --git a/Aquaria/ModSelector.cpp b/Aquaria/ModSelector.cpp index 7e45873..a1c8c3d 100644 --- a/Aquaria/ModSelector.cpp +++ b/Aquaria/ModSelector.cpp @@ -21,7 +21,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "../BBGE/DebugFont.h" #include "DSQ.h" -#include "AquariaProgressBar.h" #include "ModSelector.h" #include diff --git a/BBGE/Core.cpp b/BBGE/Core.cpp index 97f6c00..d25dff0 100644 --- a/BBGE/Core.cpp +++ b/BBGE/Core.cpp @@ -24,6 +24,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "Particles.h" #include "GLLoad.h" #include "RenderBase.h" +#include "Window.h" #include #include @@ -46,17 +47,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #endif - #include "SDL_syswm.h" - #ifdef BBGE_BUILD_SDL2 - static SDL_Window *gScreen=0; - static SDL_GLContext gGLctx=0; - #else - static SDL_Surface *gScreen=0; - #endif - - bool ignoreNextMouse=false; - Vector unchange; - #ifdef BBGE_BUILD_VFS #include "ttvfs.h" #endif @@ -65,10 +55,30 @@ Core *core = 0; static std::ofstream _logOut; -#ifndef KMOD_GUI - #define KMOD_GUI KMOD_META -#endif +CoreWindow::~CoreWindow() +{ +} +void CoreWindow::onResize(unsigned w, unsigned h) +{ + core->updateWindowDrawSize(w, h); +} + +void CoreWindow::onQuit() +{ + // smooth + //core->quitNestedMain(); + //core->quit(); + + // instant + SDL_Quit(); + _exit(0); +} + +void CoreWindow::onEvent(const SDL_Event& ev) +{ + core->onEvent(ev); +} void Core::resetCamera() { @@ -126,13 +136,6 @@ void Core::reloadDevice() void Core::setup_opengl() { -#ifdef BBGE_BUILD_SDL2 - assert(gGLctx); -#else - assert(gScreen); -#endif - - glViewport(0, 0, width, height); SDL_ShowCursor(SDL_DISABLE); @@ -150,173 +153,9 @@ void Core::setup_opengl() afterEffectManager->updateDevice(); } - -void Core::initGraphics(int w, int h, int fullscreen, int vsync, int bpp, int display, int hz) +void Core::resizeWindow(int w, int h, int full, int bpp, int vsync, int display, int hz) { - const int oldw = width; - const int oldh = height; - bool reloadRes = false; - -#ifdef BBGE_BUILD_SDL2 - const int oldDisplay = getDisplayIndex(); - assert(oldDisplay >= 0); - if(display == -1) - display = oldDisplay; - - SDL_DisplayMode displaymode; - if(SDL_GetDesktopDisplayMode(display, &displaymode) != 0) - { - // fail-safe - displaymode.w = 800; - displaymode.h = 600; - displaymode.driverdata = 0; - displaymode.refresh_rate = 0; - displaymode.format = 0; - display = oldDisplay; - } - - const bool useDesktop = w == 0 || h == 0 || (oldw && w == -1 && oldh && h == -1 && _useDesktopResolution); -#else - const bool useDesktop = false; -#endif - - - const bool wasFullscreen = _fullscreen; - - if(hz == -1) - hz = _refreshRate; - - if (fullscreen == -1) - fullscreen = _fullscreen; - - if (vsync == -1) - vsync = _vsync; - -#ifdef BBGE_BUILD_SDL2 - if(useDesktop) - { - w = displaymode.w; - h = displaymode.h; - } -#endif - - if (w == -1) - w = width; - - if (h == -1) - h = height; - - if (bpp == -1) - bpp = _bpp; - - _vsync = vsync; - _fullscreen = fullscreen; - _bpp = bpp; - _refreshRate = hz; - -#ifdef BBGE_BUILD_SDL2 - displaymode.refresh_rate = hz; - - if(vsync) - { - if(SDL_GL_SetSwapInterval(-1) != 0) - SDL_GL_SetSwapInterval(1); - } - else - SDL_GL_SetSwapInterval(0); - - // Record window position so we can properly restore it when leaving fullscreen - //if(fullscreen && !wasFullscreen) - // SDL_GetWindowPosition(gScreen, &winPosX, &winPosY); - - SDL_SetWindowFullscreen(gScreen, 0); - - bool resize = true; - bool maximize = useDesktop; - if(useDesktop != _useDesktopResolution) - { - int usew = 0, useh = 0; - bool maximize = !fullscreen; - if(!useDesktop) - { - usew = w; - useh = h; - maximize = false; - } - if(useDesktop && !fullscreen && wasFullscreen) - { - usew = oldw; - useh = oldh; - maximize = false; - } - createWindow(usew, useh, useDesktop, false, bpp, display); - - reloadRes = true; - } - - // Move window to specified display if necessary - if(display != oldDisplay) - { - int center = SDL_WINDOWPOS_CENTERED_DISPLAY(display); - SDL_SetWindowPosition(gScreen, center, center); - } - - if(fullscreen) - { - int screenflags = useDesktop ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_FULLSCREEN; - displaymode.w = w; - displaymode.h = h; - // must not be already in fullscreen here, otherwise new display mode doesn't apply properly - SDL_SetWindowSize(gScreen, w, h); - SDL_SetWindowDisplayMode(gScreen, &displaymode); - SDL_SetWindowFullscreen(gScreen, screenflags); - maximize = false; - } - - if(!fullscreen && wasFullscreen) - { - // Need to do this; else the window ends up at (0, 0) with the title bar outside the screen area - int c = SDL_WINDOWPOS_CENTERED_DISPLAY(display); - SDL_SetWindowPosition(gScreen, c, c); - } - if(maximize) - { - SDL_MaximizeWindow(gScreen); - resize = false; - } - - if(resize && !fullscreen && !useDesktop) - { - if(w != oldw || h != oldh) - { - SDL_SetWindowSize(gScreen, w, h); - int c = SDL_WINDOWPOS_CENTERED_DISPLAY(display); - SDL_SetWindowPosition(gScreen, c, c); - } - } - - if(!resize) - SDL_GetWindowSize(gScreen, &w, &h); - -#else - if(w != oldw || h != oldh) - { - reloadRes = true; // Always reload resources on SDL 1.2, since it loses the GL context! - createWindow(w, h, false, fullscreen, bpp, 0); - } - -#endif - - _useDesktopResolution = useDesktop; - - updateWindowDrawSize(w, h); - - if(reloadRes) - { - unloadResources(); - reloadResources(); - resetTimer(); - } + window->open(w, h, full, bpp, vsync, display, hz); } void Core::updateWindowDrawSize(int w, int h) @@ -332,18 +171,29 @@ void Core::updateWindowDrawSize(int w, int h) void Core::onWindowResize(int w, int h) { updateWindowDrawSize(w, h); + + bool reloadRes = false; +#ifndef BBGE_BUILD_SDL2 + reloadRes = true; // SDL1.2 loses the GL context on resize, so all resources must be reloaded +#endif + + if(reloadRes) + { + unloadResources(); + reloadResources(); + resetTimer(); + } + + updateWindowDrawSize(w, h); } void Core::setFullscreen(bool full) { - if(full == !!_fullscreen) - return; - - sound->pause(); - initGraphics(-1, -1, full); + //sound->pause(); + window->setFullscreen(full); cacheRender(); resetTimer(); - sound->resume(); + //sound->resume(); } RenderObjectLayer *Core::getRenderObjectLayer(int i) @@ -389,8 +239,6 @@ void Core::debugLog(const std::string &s) std::cout << s << std::endl; #endif } - -#ifdef BBGE_BUILD_WINDOWS static bool checkWritable(const std::string& path, bool warn, bool critical) { bool writeable = false; @@ -413,14 +261,13 @@ static bool checkWritable(const std::string& path, bool warn, bool critical) << "or try running it as administrator, that may help as well."; if(critical) os << "\n\nWill now exit."; - MessageBoxA(NULL, os.str().c_str(), "Need to write but can't!", MB_OK | MB_ICONERROR); + errorLog(os.str()); } if(critical) exit(1); } return writeable; } -#endif Core::Core(const std::string &filesystem, const std::string& extraDataDir, int numRenderLayers, const std::string &appName, int particleSize, std::string userDataSubFolder) @@ -507,10 +354,7 @@ Core::Core(const std::string &filesystem, const std::string& extraDataDir, int n particleManager = new ParticleManager(particleSize); nowTicks = thenTicks = 0; - _hasFocus = false; lib_graphics = lib_sound = lib_input = false; - clearColor = Vector(0,0,0); - updateCursorFromMouse = true; mouseConstraint = false; mouseCircle = 0; overrideStartLayer = 0; @@ -525,9 +369,6 @@ Core::Core(const std::string &filesystem, const std::string& extraDataDir, int n doScreenshot = false; baseCullRadius = 1; width = height = 0; - _fullscreen = false; - _refreshRate = 0; - _useDesktopResolution = false; _lastEnumeratedDisplayIndex = -1; afterEffectManagerLayer = 0; renderObjectLayers.resize(1); @@ -540,10 +381,6 @@ Core::Core(const std::string &filesystem, const std::string& extraDataDir, int n nestedMains = 0; afterEffectManager = 0; loopDone = false; -#ifdef BBGE_BUILD_SDL2 - winPosX = SDL_WINDOWPOS_CENTERED; - winPosY = SDL_WINDOWPOS_CENTERED; -#endif core = this; for (int i = 0; i < KEY_MAXARRAY; i++) @@ -658,12 +495,7 @@ void Core::updateInputGrab() { // Can and MUST always ungrab if window is not in focus const bool on = grabInput && isWindowFocus(); - - #ifdef BBGE_BUILD_SDL2 - SDL_SetWindowGrab(gScreen, on ? SDL_TRUE : SDL_FALSE); - #else - SDL_WM_GrabInput(on?SDL_GRAB_ON:SDL_GRAB_OFF); - #endif + window->setGrabInput(on); } void Core::setInputGrab(bool on) @@ -674,27 +506,22 @@ void Core::setInputGrab(bool on) bool Core::isFullscreen() { - return _fullscreen; + return window->isFullscreen(); } bool Core::isDesktopResolution() { - return _useDesktopResolution; + return window->isDesktopResolution(); } int Core::getDisplayIndex() { -#ifdef BBGE_BUILD_SDL2 - int display = SDL_GetWindowDisplayIndex(gScreen); - return display < 0 ? 0 : display; // if there's an error, assume primary display -#else - return 0; -#endif + return window->getDisplayIndex(); } int Core::getRefreshRate() { - return _refreshRate; + return window->getRefreshRate(); } bool Core::isShuttingDown() @@ -838,7 +665,6 @@ void Core::onUpdate(float dt) if (minimized) return; core->mouse.lastPosition = core->mouse.position; - core->mouse.lastScrollWheel = core->mouse.scrollWheel; pollEvents(dt); @@ -866,50 +692,19 @@ void Core::globalScaleChanged() void Core::setClearColor(const Vector &c) { - clearColor = c; - glClearColor(c.x, c.y, c.z, 0.0); - + clearColor = c; } -bool Core::initGraphicsLibrary(int width, int height, bool fullscreen, bool vsync, int bpp, int display, int hz) +void Core::initGraphicsLibrary(int width, int height, bool fullscreen, bool vsync, int bpp, int display, int hz) { - assert(!gScreen); + if(!window) + window = new CoreWindow; - _hasFocus = false; - -#ifndef BBGE_BUILD_SDL2 -#if !defined(BBGE_BUILD_MACOSX) - // have to cast away constness, since SDL_putenv() might be #defined to - // putenv(), which takes a (char *), and freaks out newer GCC releases - // when you try to pass a (const!) string literal here... --ryan. - SDL_putenv((char *) "SDL_VIDEO_CENTERED=1"); -#endif - SDL_WM_SetCaption(appName.c_str(), appName.c_str()); -#endif - - initIcon(gScreen); - - -#ifdef BBGE_BUILD_SDL2 -# ifdef _DEBUG - SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); -# endif - SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); -#else - { - std::ostringstream os; - os << "setting vsync: " << vsync; - SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, vsync); // SDL 1.2 can't set this on an existing context - debugLog(os.str()); - } -#endif - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - - createWindow(320, 240, false, false, bpp, display); - - enumerateScreenModes(display); + window->open(width, height, fullscreen, bpp, vsync, display, hz); + window->setTitle(appName.c_str()); + // get GL symbols AFTER opening the window, otherwise we get a super old GL context on windows and nothing works if (!lookup_all_glsyms()) { std::ostringstream os; @@ -918,103 +713,16 @@ bool Core::initGraphicsLibrary(int width, int height, bool fullscreen, bool vsyn exit_error(os.str()); } - initGraphics(width, height, fullscreen, vsync, bpp, display, hz); - debugLog("GL vendor, renderer & version:"); debugLog((const char*)glGetString(GL_VENDOR)); debugLog((const char*)glGetString(GL_RENDERER)); debugLog((const char*)glGetString(GL_VERSION)); - _hasFocus = true; + enumerateScreenModes(window->getDisplayIndex()); + window->initSize(); + cacheRender(); // Clears the window bg to black early; prevents flickering lib_graphics = true; - - // init success - return true; -} - -void Core::createWindow(int w, int h, bool resizable, bool fullscreen, int bpp, int display) -{ -#ifdef BBGE_BUILD_SDL2 - if(gScreen) - { - SDL_DestroyWindow(gScreen); - gScreen = NULL; - } - if(gGLctx) - { - SDL_GL_MakeCurrent(gScreen, NULL); - SDL_GL_DeleteContext(gGLctx); - gGLctx = NULL; - } - Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN; - if(resizable) - flags |= SDL_WINDOW_RESIZABLE; - if(fullscreen) - { - if(!w && !h) - flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; - else - flags |= SDL_WINDOW_FULLSCREEN; - } - if(w <= 0) - w = 640; - if(h <= 0) - h = 480; - int pos = SDL_WINDOWPOS_CENTERED_DISPLAY(display); - gScreen = SDL_CreateWindow(appName.c_str(), pos, pos, w, h, flags); - if (gScreen == NULL) - { - std::ostringstream os; - os << "Couldn't set resolution [" << w << "x" << h << "]\n" << SDL_GetError(); - errorLog(os.str()); - SDL_Quit(); - exit(0); - } - gGLctx = SDL_GL_CreateContext(gScreen); - if (gGLctx == NULL) - { - std::ostringstream os; - os << "Couldn't create OpenGL context!\n" << SDL_GetError(); - errorLog(os.str()); - SDL_Quit(); - exit(0); - } - SDL_GL_MakeCurrent(gScreen, gGLctx); - - { - const char *name = SDL_GetCurrentVideoDriver(); - std::ostringstream os2; - os2 << "Video Driver Name [" << name << "]"; - debugLog(os2.str()); - } - -#else - Uint32 flags = 0; - flags = SDL_OPENGL; - if(fullscreen) - flags |= SDL_FULLSCREEN; - // No well supported in SDL 1.2 - //if (resizable) - // flags |= SDL_RESIZABLE; - - gScreen = SDL_SetVideoMode(w, h, bpp, flags); - if (gScreen == NULL) - { - std::ostringstream os; - os << "Couldn't set resolution [" << width << "x" << height << "]\n" << SDL_GetError(); - SDL_Quit(); - exit_error(os.str()); - } - - { - char name[256]; - SDL_VideoDriverName((char*)name, 256); - std::ostringstream os2; - os2 << "Video Driver Name [" << name << "]"; - debugLog(os2.str()); - } -#endif } void Core::enumerateScreenModesIfNecessary(int display /* = -1 */) @@ -1022,8 +730,8 @@ void Core::enumerateScreenModesIfNecessary(int display /* = -1 */) if(display == -1) { #ifdef BBGE_BUILD_SDL2 - if(gScreen) - display = SDL_GetWindowDisplayIndex(gScreen); + if(window) + display = window->getDisplayIndex(); else #endif display = 0; @@ -1101,18 +809,10 @@ void Core::shutdownGraphicsLibrary() glFinish(); -#ifdef BBGE_BUILD_SDL2 - SDL_GL_MakeCurrent(gScreen, NULL); - SDL_GL_DeleteContext(gGLctx); - SDL_DestroyWindow(gScreen); - gGLctx = 0; -#endif + delete window; SDL_QuitSubSystem(SDL_INIT_VIDEO); - gScreen = 0; unload_all_glsyms(); - _hasFocus = false; - lib_graphics = false; destroyIcon(); @@ -1121,8 +821,6 @@ void Core::shutdownGraphicsLibrary() void Core::quit() { enqueueJumpState("STATE_QUIT"); - - } void Core::applyState(const std::string &state) @@ -1134,37 +832,6 @@ void Core::applyState(const std::string &state) StateManager::applyState(state); } - - -#ifdef BBGE_BUILD_WINDOWS -void centerWindow(HWND hwnd) -{ - int x, y; - HWND hwndDeskTop; - RECT rcWnd, rcDeskTop; - // Get a handle to the desktop window - hwndDeskTop = ::GetDesktopWindow(); - // Get dimension of desktop in a rect - ::GetWindowRect(hwndDeskTop, &rcDeskTop); - // Get dimension of main window in a rect - ::GetWindowRect(hwnd, &rcWnd); - // Find center of desktop - x = (rcDeskTop.right - rcDeskTop.left)/2; - y = (rcDeskTop.bottom - rcDeskTop.top)/2; - x -= (rcWnd.right - rcWnd.left)/2; - y -= (rcWnd.bottom - rcWnd.top)/2; - // Set top and left to center main window on desktop - ::SetWindowPos(hwnd, HWND_TOP, x, y, 0, 0, SWP_NOSIZE); - -} -#endif - - -// No longer part of C/C++ standard -#ifndef M_PI -#define M_PI 3.14159265358979323846 -#endif - void Core::setPixelScale(int pixelScaleX, int pixelScaleY) { virtualWidth = pixelScaleX; @@ -1273,11 +940,10 @@ void Core::setMousePosition(const Vector &p) float px = p.x + virtualOffX; float py = p.y; - #ifdef BBGE_BUILD_SDL2 - SDL_WarpMouseInWindow(gScreen, px * (float(width)/float(virtualWidth)), py * (float(height)/float(virtualHeight))); - #else - SDL_WarpMouse( px * (float(width)/float(virtualWidth)), py * (float(height)/float(virtualHeight))); - #endif + window->warpMouse( + px * (float(width)/float(virtualWidth)), + py * (float(height)/float(virtualHeight)) + ); } // used to update all render objects either uniformly or as part of a time sliced update process @@ -1325,17 +991,11 @@ std::string getScreenshotFilename(bool png) unsigned Core::getTicks() { return SDL_GetTicks(); - return 0; } bool Core::isWindowFocus() { - #ifdef BBGE_BUILD_SDL2 - return ((SDL_GetWindowFlags(gScreen) & SDL_WINDOW_INPUT_FOCUS) != 0); - #else - return ((SDL_GetAppState() & SDL_APPINPUTFOCUS) != 0); - #endif - return true; + return window->hasInputFocus(); } void Core::onBackgroundUpdate() @@ -1582,8 +1242,6 @@ bool Core::doMouseConstraint() { if (mouseConstraint) { - - Vector h = mouseConstraintCenter; Vector d = mouse.position - h; if (!d.isLength2DIn(mouseCircle)) @@ -1597,6 +1255,122 @@ bool Core::doMouseConstraint() return false; } +void Core::onEvent(const SDL_Event& event) +{ + const bool focus = window->hasFocus(); + switch (event.type) + { + case SDL_KEYDOWN: + { + if ((event.key.keysym.sym == SDLK_g) && (event.key.keysym.mod & KMOD_CTRL)) + { + setInputGrab(!grabInput); + } + else if (focus) + { +#ifdef BBGE_BUILD_SDL2 + unsigned kidx = event.key.keysym.scancode; +#else + unsigned kidx = event.key.keysym.sym; +#endif + if(kidx < KEY_MAXARRAY) + keys[kidx] = 1; + } + } + break; + + case SDL_KEYUP: + { + if (focus) + { +#ifdef BBGE_BUILD_SDL2 + unsigned kidx = event.key.keysym.scancode; +#else + unsigned kidx = event.key.keysym.sym; +#endif + if(kidx < KEY_MAXARRAY) + keys[kidx] = 0; + } + } + break; + + case SDL_MOUSEMOTION: + { + if (focus && updateMouse) + { + mouse.lastPosition = mouse.position; + + mouse.position.x = ((event.motion.x) * (float(virtualWidth)/float(getWindowWidth()))) - getVirtualOffX(); + mouse.position.y = event.motion.y * (float(virtualHeight)/float(getWindowHeight())); + + mouse.change = mouse.position - mouse.lastPosition; + + if (doMouseConstraint()) + setMousePosition(mouse.position); + } + } + break; + +#ifdef BBGE_BUILD_SDL2 + + case SDL_MOUSEWHEEL: + { + if (focus && updateMouse) + { + if (event.wheel.y > 0) + mouse.scrollWheelChange = 1; + else if (event.wheel.y < 0) + mouse.scrollWheelChange = -1; + } + } + break; + + case SDL_JOYDEVICEADDED: + onJoystickAdded(event.jdevice.which); + break; + + case SDL_JOYDEVICEREMOVED: + onJoystickRemoved(event.jdevice.which); + break; + +#else + case SDL_MOUSEBUTTONDOWN: + { + if (focus && updateMouse) + { + switch(event.button.button) + { + case 4: + mouse.scrollWheelChange = 1; + break; + case 5: + mouse.scrollWheelChange = -1; + break; + } + } + } + break; + + case SDL_MOUSEBUTTONUP: + { + if (focus && updateMouse) + { + switch(event.button.button) + { + case 4: + mouse.scrollWheelChange = 1; + break; + case 5: + mouse.scrollWheelChange = -1; + break; + } + } + } + break; +#endif + } +} + void Core::pollEvents(float dt) { bool warpMouse=false; @@ -1634,182 +1408,7 @@ void Core::pollEvents(float dt) mouse.change = Vector(0,0); } - - - SDL_Event event; - - - - while ( SDL_PollEvent (&event) ) { - switch (event.type) { - case SDL_KEYDOWN: - { - #if __APPLE__ - #if SDL_VERSION_ATLEAST(2, 0, 0) - if ((event.key.keysym.sym == SDLK_q) && (event.key.keysym.mod & KMOD_GUI)) - #else - if ((event.key.keysym.sym == SDLK_q) && (event.key.keysym.mod & KMOD_META)) - #endif - #else - if ((event.key.keysym.sym == SDLK_F4) && (event.key.keysym.mod & KMOD_ALT)) - #endif - { - quitNestedMain(); - quit(); - } - - if ((event.key.keysym.sym == SDLK_g) && (event.key.keysym.mod & KMOD_CTRL)) - { - setInputGrab(!grabInput); - } - else if (_hasFocus) - { -#ifdef BBGE_BUILD_SDL2 - unsigned kidx = event.key.keysym.scancode; -#else - unsigned kidx = event.key.keysym.sym; -#endif - if(kidx < KEY_MAXARRAY) - keys[kidx] = 1; - } - } - break; - - case SDL_KEYUP: - { - if (_hasFocus) - { -#ifdef BBGE_BUILD_SDL2 - unsigned kidx = event.key.keysym.scancode; -#else - unsigned kidx = event.key.keysym.sym; -#endif - if(kidx < KEY_MAXARRAY) - keys[kidx] = 0; - } - } - break; - - case SDL_MOUSEMOTION: - { - if (_hasFocus && updateMouse) - { - mouse.lastPosition = mouse.position; - - mouse.position.x = ((event.motion.x) * (float(virtualWidth)/float(getWindowWidth()))) - getVirtualOffX(); - mouse.position.y = event.motion.y * (float(virtualHeight)/float(getWindowHeight())); - - mouse.change = mouse.position - mouse.lastPosition; - - if (doMouseConstraint()) warpMouse = true; - } - } - break; - -#ifdef BBGE_BUILD_SDL2 - case SDL_WINDOWEVENT: - { - switch(event.window.event) - { - case SDL_WINDOWEVENT_CLOSE: - SDL_Quit(); - _exit(0); - break; - case SDL_WINDOWEVENT_RESIZED: - onWindowResize(event.window.data1, event.window.data2); - break; - /*case SDL_WINDOWEVENT_FOCUS_GAINED: - _hasFocus = true; - break; - case SDL_WINDOWEVENT_FOCUS_LOST: - _hasFocus = false;*/ - } - } - break; - - case SDL_MOUSEWHEEL: - { - if (_hasFocus && updateMouse) - { - if (event.wheel.y > 0) - mouse.scrollWheelChange = 1; - else if (event.wheel.y < 0) - mouse.scrollWheelChange = -1; - } - } - break; - - case SDL_JOYDEVICEADDED: - onJoystickAdded(event.jdevice.which); - break; - - case SDL_JOYDEVICEREMOVED: - onJoystickRemoved(event.jdevice.which); - break; - -#else - case SDL_MOUSEBUTTONDOWN: - { - if (_hasFocus && updateMouse) - { - switch(event.button.button) - { - case 4: - mouse.scrollWheelChange = 1; - break; - case 5: - mouse.scrollWheelChange = -1; - break; - } - } - } - break; - - case SDL_MOUSEBUTTONUP: - { - if (_hasFocus && updateMouse) - { - switch(event.button.button) - { - case 4: - mouse.scrollWheelChange = 1; - break; - case 5: - mouse.scrollWheelChange = -1; - break; - } - } - } - break; -#endif - - case SDL_QUIT: - SDL_Quit(); - _exit(0); - - - break; - - case SDL_SYSWMEVENT: - { - - } - break; - - default: - break; - } - } - - if (updateMouse) - { - mouse.scrollWheel += mouse.scrollWheelChange; - - if (warpMouse) - { - setMousePosition(mouse.position); - } - } + window->handleInput(); for(size_t i = 0; i < joysticks.size(); ++i) if(joysticks[i]) @@ -2223,14 +1822,7 @@ void Core::render(int startLayer, int endLayer, bool useFrameBufferIfAvail) void Core::showBuffer() { - BBGE_PROF(Core_showBuffer); -#ifdef BBGE_BUILD_SDL2 - SDL_GL_SwapWindow(gScreen); -#else - SDL_GL_SwapBuffers(); - -#endif - + window->present(); } // WARNING: only for use during shutdown @@ -2555,12 +2147,6 @@ void Core::removeTexture(Texture *res) resources.swap(copy); } -void Core::deleteRenderObjectMemory(RenderObject *r) -{ - - delete r; -} - void Core::removeRenderObject(RenderObject *r, RemoveRenderObjectFlag flag) { if (r) @@ -2572,13 +2158,11 @@ void Core::removeRenderObject(RenderObject *r, RemoveRenderObjectFlag flag) if (flag != DO_NOT_DESTROY_RENDER_OBJECT ) { r->destroy(); - - deleteRenderObjectMemory(r); + delete r; } } } - void Core::enqueueRenderObjectDeletion(RenderObject *object) { if (!object->_dead) @@ -2602,7 +2186,7 @@ void Core::clearGarbage() for (RenderObjects::iterator i = garbage.begin(); i != garbage.end(); i++) { - deleteRenderObjectMemory(*i); + delete *i; } garbage.clear(); diff --git a/BBGE/Core.h b/BBGE/Core.h index 90cbd36..c6173c2 100644 --- a/BBGE/Core.h +++ b/BBGE/Core.h @@ -28,12 +28,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "StateManager.h" #include "Effects.h" #include "Localization.h" +#include "Window.h" #include "DarkLayer.h" #include "GameKeys.h" - class ParticleEffect; class Joystick; @@ -49,10 +49,9 @@ struct ScreenMode struct CoreSettings { - CoreSettings() { renderOn = true; updateOn = true; runInBackground = false; prebufferSounds = false; } + CoreSettings() { renderOn = true; runInBackground = false; prebufferSounds = false; } bool renderOn; bool runInBackground; - bool updateOn; // NOT IMPLEMENTED YET bool prebufferSounds; }; @@ -89,7 +88,7 @@ struct Mouse { Mouse() { - scrollWheel = scrollWheelChange = lastScrollWheel = 0; + scrollWheelChange = 0; buttonsEnabled = true; } Vector position, lastPosition; @@ -99,7 +98,7 @@ struct Mouse Vector change; bool buttonsEnabled; - int scrollWheel, scrollWheelChange, lastScrollWheel; + int scrollWheelChange; }; enum FollowCameraLock @@ -195,8 +194,21 @@ protected: size_t iter; }; +class CoreWindow : public Window +{ +public: + virtual ~CoreWindow(); + +protected: + virtual void onEvent(const SDL_Event& ev); + virtual void onResize(unsigned w, unsigned h); + virtual void onQuit(); +}; + + class Core : public ActionMapper, public StateManager { + friend class CoreWindow; public: // init @@ -278,7 +290,6 @@ public: unsigned getTicks(); - void initGraphics(int w, int h, int fullscreen=-1, int vsync=-1, int bpp=-1, int display=-1, int hz=-1); // pass 0x0 for desktop resolution void updateWindowDrawSize(int w, int h); Vector getGameCursorPosition(); @@ -430,9 +441,12 @@ public: void enumerateScreenModes(int display); void enumerateScreenModesIfNecessary(int display = -1); + void resizeWindow(int w, int h, int full, int bpp, int vsync, int display, int hz); + std::vector screenModes; void pollEvents(float dt); + void onEvent(const SDL_Event& event); CoreSettings settings; @@ -448,6 +462,8 @@ public: protected: + CoreWindow *window; + void updateCullData(); std::string userDataFolder; @@ -465,11 +481,8 @@ protected: CountedPtr doTextureAdd(const std::string &texture, const std::string &name, std::string internalTextureName); - void deleteRenderObjectMemory(RenderObject *r); - bool _hasFocus; bool lib_graphics, lib_sound, lib_input; Vector clearColor; - bool updateCursorFromMouse; virtual void unloadDevice(); virtual void reloadDevice(); @@ -486,8 +499,7 @@ protected: bool initSoundLibrary(const std::string &defaultDevice); bool initInputLibrary(); void initJoystickLibrary(); - bool initGraphicsLibrary(int w, int h, bool fullscreen, bool vsync, int bpp, int display, int hz); - void createWindow(int w, int h, bool resizable, bool fullscreen, int bpp, int display); + void initGraphicsLibrary(int w, int h, bool fullscreen, bool vsync, int bpp, int display, int hz); void shutdownInputLibrary(); void shutdownJoystickLibrary(); void shutdownGraphicsLibrary(); @@ -516,13 +528,8 @@ protected: int nowTicks, thenTicks; - int _vsync, _bpp, _refreshRate; - bool _fullscreen, _useDesktopResolution; - int winPosX, winPosY; // pre-fullscreen int _lastEnumeratedDisplayIndex; - CountedPtr texError; - virtual void onUpdate(float dt); virtual void onRender(){} diff --git a/BBGE/Window.cpp b/BBGE/Window.cpp new file mode 100644 index 0000000..195439a --- /dev/null +++ b/BBGE/Window.cpp @@ -0,0 +1,82 @@ +#include "Window.h" +#include +#include "Base.h" + +Window::Window() +: _backend(_initBackend()) +, _w(800), _h(600) +, _display(0) +, _bpp(32) +, _hz(60) +, _full(false) +, _vsync(false) +, _hasFocus(false) +{ +} + +void Window::handleInput() +{ + SDL_Event event; + while ( SDL_PollEvent (&event) ) // This function is the same for SDL1 & 2 ... + { + switch(event.type) + { + case SDL_QUIT: + onQuit(); + break; + } + _onEventImpl(event); + onEvent(event); + } +} + +void Window::onEvent(const SDL_Event& ev) +{ +} + +void Window::onResize(unsigned w, unsigned h) +{ +} + +void Window::onQuit() +{ +} + +void Window::setFullscreen(bool on) +{ + if(_full != on) + open(-1, -1, on, -1, -1, -1, -1); +} + +void Window::open(int w, int h, int full, int bpp, int vsync, int display, int hz) +{ + _fixOpenParams(w, h, full, bpp, vsync, display, hz); + + if(isOpen()) + _adjust(w, h, !!full, bpp, !!vsync, display, hz); + else + _open(w, h, !!full, bpp, !!vsync, display, hz); + + _w = w; + _h = h; + _display = display; + _bpp = bpp; +} + +void Window::_fixOpenParams(int& w, int& h, int& full, int& bpp, int& vsync, int& display, int& hz) +{ + if(w < 0) + w = _w; + if(h < 0) + h = _h; + if(full < 0) + full = _full; + if(bpp < 0) + bpp = _bpp; + if(vsync < 0) + vsync = _vsync; + if(display < 0) + display = _display; + if(hz < 0) + hz = _hz; +} diff --git a/BBGE/Window.h b/BBGE/Window.h new file mode 100644 index 0000000..bca6879 --- /dev/null +++ b/BBGE/Window.h @@ -0,0 +1,53 @@ +#ifndef BBGE_WINDOW_H +#define BBGE_WINDOW_H + +// SDL2 and 1.2-compatible window class. +// Note: With SDL1.2, only one window can exist. + +union SDL_Event; + +class Window +{ +public: + Window(); + virtual ~Window(); + + void handleInput(); + // pass -1 to any to leave unchanged + void open(int w, int h, int full, int bpp, int vsync, int display, int hz); + void setGrabInput(bool on); + void present(); + void setFullscreen(bool on); + void setTitle(const char *s); + inline bool hasFocus() const { return _hasFocus; } + int getDisplayIndex() const; // -1 on error/unsupported + int getRefreshRate() const { return _hz; } + void warpMouse(int x, int y); + void initSize(); + + inline bool isFullscreen() const { return _full; } + bool isOpen() const; + bool isDesktopResolution() const; + bool hasInputFocus() const; + +protected: + + virtual void onEvent(const SDL_Event& ev); + virtual void onResize(unsigned w, unsigned h); + virtual void onQuit(); + + + static void *_initBackend(); + void _ctor(); + void _fixOpenParams(int& w, int& h, int& full, int& bpp, int& vsync, int& display, int& hz); + void _open(unsigned w, unsigned h, bool full, unsigned bpp, bool vsync, unsigned display, unsigned hz); + void _adjust(unsigned w, unsigned h, bool full, unsigned bpp, bool vsync, unsigned display, unsigned hz); + void _onEventImpl(const SDL_Event& ev); + void * const _backend; // backend-specific struct + unsigned _w, _h, _display, _bpp, _hz; + bool _full, _vsync; + bool _hasFocus; +}; + + +#endif diff --git a/BBGE/Window_SDL1.cpp b/BBGE/Window_SDL1.cpp new file mode 100644 index 0000000..60f91d0 --- /dev/null +++ b/BBGE/Window_SDL1.cpp @@ -0,0 +1,110 @@ +#include "Window.h" +#include +#include +#include "OSFunctions.h" + +#define SDL2_BACKEND SDL_VERSION_ATLEAST(2,0,0) + +#if !SDL2_BACKEND // ... to end of file + +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) + +void *Window::_initBackend() +{ + return new Backend; +} + +void Window::_ctor() +{ + assert(!s_theWindow); + s_theWindow = this; +} + +Window::~Window() +{ + delete BACKEND; + s_theWindow = NULL; +} + +bool 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); + + // have to cast away constness, since SDL_putenv() might be #defined to + // putenv(), which takes a (char *), and freaks out newer GCC releases + // when you try to pass a (const!) string literal here... --ryan. + SDL_putenv((char *) "SDL_VIDEO_CENTERED=1"); + + // SDL 1.2 can't set this on an existing context + SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, vsync); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + Uint32 flags = SDL_OPENGL; + if(full) + flags |= SDL_FULLSCREEN; + SDL_Surface *surf = SDL_SetVideoMode(w, h, bpp, flags); + if(!surf) + return false; + + WIN = surf; + return true; +} + +void Window::warpMouse(int x, int y) +{ + SDL_WarpMouse(x, y); +} + +void Window::setGrabInput(bool on) +{ + SDL_WM_GrabInput(on ? SDL_GRAB_ON : SDL_GRAB_OFF); +} + +void Window::present() +{ + SDL_GL_SwapBuffers(); +} + +void Window::setTitle(const char *s) +{ + SDL_WM_SetCaption(s, s); +} + +void Window::initIcon() +{ + ::initIcon(WIN); +} + +int Window::getDisplayIndex() const +{ + return -1; +} + +bool Window::isDesktopResolution() const +{ + return false; +} + +bool Window::hasInputFocus() const +{ + return ((SDL_GetAppState() & SDL_APPINPUTFOCUS) != 0); +} + + +#endif // !SDL2_BACKEND diff --git a/BBGE/Window_SDL2.cpp b/BBGE/Window_SDL2.cpp new file mode 100644 index 0000000..50e5fda --- /dev/null +++ b/BBGE/Window_SDL2.cpp @@ -0,0 +1,231 @@ +#include "Window.h" +#include +#include +#include "OSFunctions.h" +#include "Base.h" + +#define SDL2_BACKEND SDL_VERSION_ATLEAST(2,0,0) + +#if SDL2_BACKEND + +struct Backend +{ + Backend() + : win(NULL) + , glctx(NULL) + {} + SDL_Window *win; + SDL_GLContext glctx; +}; + +#define BACKEND (static_cast(_backend)) +#define WIN (BACKEND->win) +#define GLCTX (BACKEND->glctx) + +void Window::_ctor() +{ +} + +void *Window::_initBackend() +{ + return new Backend; +} + + +Window::~Window() +{ + SDL_GL_MakeCurrent(WIN, NULL); + SDL_GL_DeleteContext(GLCTX); + SDL_DestroyWindow(WIN); + + delete BACKEND; +} + +bool Window::isOpen() const +{ + return !!WIN; +} + +static void setvsync(bool vsync) +{ + if(vsync) + { + if(SDL_GL_SetSwapInterval(-1) != 0) + SDL_GL_SetSwapInterval(1); + } + else + SDL_GL_SetSwapInterval(0); +} + +void Window::_open(unsigned w, unsigned h, bool full, unsigned bpp, bool vsync, unsigned display, unsigned hz) +{ + assert(!WIN); + assert(!GLCTX); +# ifdef _DEBUG + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); +# endif + SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE; + if(full) + { + if(!w && !h) + flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; + else + flags |= SDL_WINDOW_FULLSCREEN; + } + + int pos = SDL_WINDOWPOS_CENTERED_DISPLAY(display); + WIN = SDL_CreateWindow("", pos, pos, w, h, flags); + if(!WIN) + exit_error("Failed to create window"); + + ::initIcon(WIN); + + GLCTX = SDL_GL_CreateContext(WIN); + if(!GLCTX) + exit_error("Failed to create GL context"); + SDL_GL_MakeCurrent(WIN, GLCTX); + + _adjust(w, h, full, bpp, vsync, display, hz); +} + +void Window::_adjust(unsigned w, unsigned h, bool full, unsigned bpp, bool vsync, unsigned display, unsigned hz) +{ + const bool useDesktop = w == 0 || h == 0; + + SDL_DisplayMode displaymode; + if(SDL_GetDesktopDisplayMode(display, &displaymode) != 0) + { + // fail-safe + displaymode.w = 800; + displaymode.h = 600; + displaymode.driverdata = 0; + displaymode.refresh_rate = 0; + displaymode.format = 0; + display = 0; + } + + setvsync(vsync); + + if(useDesktop) + { + w = 800; + h = 600; + } + + if(full) + { + int screenflags = useDesktop ? SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_WINDOW_FULLSCREEN; + displaymode.w = w; + displaymode.h = h; + // must not be already in fullscreen here, otherwise new display mode doesn't apply properly + SDL_SetWindowDisplayMode(WIN, &displaymode); + SDL_SetWindowFullscreen(WIN, screenflags); + } + else + { + SDL_SetWindowFullscreen(WIN, 0); + SDL_SetWindowSize(WIN, w, h); + int center = SDL_WINDOWPOS_CENTERED_DISPLAY(display); + SDL_SetWindowPosition(WIN, center, center); + + if(useDesktop) + SDL_MaximizeWindow(WIN); + } +} + +void Window::initSize() +{ + int ww, hh; + SDL_GetWindowSize(WIN, &ww, &hh); + onResize(ww, hh); +} + +bool Window::isDesktopResolution() const +{ + return !!(SDL_GetWindowFlags(WIN) & (SDL_WINDOW_MAXIMIZED || SDL_WINDOW_FULLSCREEN_DESKTOP)); +} + +void Window::setGrabInput(bool on) +{ + SDL_SetWindowGrab(WIN, (SDL_bool)on); +} + +void Window::present() +{ + SDL_GL_SwapWindow(WIN); +} + +int Window::getDisplayIndex() const +{ + return SDL_GetWindowDisplayIndex(WIN); +} + +bool Window::hasInputFocus() const +{ + return (SDL_GetWindowFlags(WIN) & SDL_WINDOW_INPUT_FOCUS) != 0; +} + +void Window::setTitle(const char *s) +{ + SDL_SetWindowTitle(WIN, s); +} + +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)) +#endif +#else + if ((ev.key.keysym.sym == SDLK_F4) && (ev.key.keysym.mod & KMOD_ALT)) +#endif + { + onQuit() + } + } + break; +#endif + + case SDL_WINDOWEVENT: + { + switch(ev.window.event) + { + case SDL_WINDOWEVENT_FOCUS_GAINED: + _hasFocus = true; + break; + case SDL_WINDOWEVENT_FOCUS_LOST: + _hasFocus = false; + break; + case SDL_WINDOWEVENT_CLOSE: + onQuit(); + break; + case SDL_WINDOWEVENT_RESIZED: + onResize(ev.window.data1, ev.window.data2); + break; + } + } + break; + } +} + + +#endif // SDL2_BACKEND diff --git a/CMakeLists.txt b/CMakeLists.txt index 2814fbf..ca0d740 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -414,7 +414,6 @@ SET(AQUARIA_SRCS ${SRCDIR}/AnimationEditor.cpp ${SRCDIR}/AquariaComboBox.cpp ${SRCDIR}/AquariaMenuItem.cpp - ${SRCDIR}/AquariaProgressBar.cpp ${SRCDIR}/AquariaSaveSlot.cpp ${SRCDIR}/Avatar.cpp ${SRCDIR}/Beam.cpp @@ -537,6 +536,9 @@ SET(BBGE_SRCS ${BBGEDIR}/Texture.cpp ${BBGEDIR}/TTFFont.cpp ${BBGEDIR}/Vector.cpp + ${BBGEDIR}/Window.cpp + ${BBGEDIR}/Window_SDL1.cpp + ${BBGEDIR}/Window_SDL2.cpp ${BBGEDIR}/FmodOpenALBridge.cpp ${COCOA_SRCS} ${EXTLIBDIR}/DeflateCompressor.cpp diff --git a/win/vc90/Aquaria.vcproj b/win/vc90/Aquaria.vcproj index ec57c59..2afa0ec 100644 --- a/win/vc90/Aquaria.vcproj +++ b/win/vc90/Aquaria.vcproj @@ -367,14 +367,6 @@ RelativePath="..\..\Aquaria\AquariaMenuItem.h" > - - - - diff --git a/win/vc90/BBGE.vcproj b/win/vc90/BBGE.vcproj index f29ae43..13f67cc 100644 --- a/win/vc90/BBGE.vcproj +++ b/win/vc90/BBGE.vcproj @@ -709,6 +709,22 @@ RelativePath="..\..\BBGE\Vector.h" > + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +