From 3dda97d32a45d2ace15bc7caf1ee29d5b5c729d5 Mon Sep 17 00:00:00 2001 From: fgenesis Date: Wed, 15 Feb 2017 04:34:32 +0100 Subject: [PATCH] Hopefully fix build against SDL 1.2. Does not yet run! Also: Recently introduced key names in usersettings.xml will no longer work. But this should automatically detect key names as they used to be, and convert automatically. Needs testing. --- Aquaria/DSQ.cpp | 3 + BBGE/ActionInput.cpp | 64 ++++++++++++++------- BBGE/ActionStatus.h | 1 + BBGE/Core.cpp | 72 +++++++++++++++--------- BBGE/Core.h | 2 +- BBGE/GameKeys.h | 2 + BBGE/LegacyKeycodes.cpp | 111 +++++++++++++++++++++++++++++++++++++ BBGE/LegacyKeycodes.h | 6 ++ BBGE/OSFunctions.cpp | 5 +- ExternalLibs/minipstdint.h | 11 +++- win/Aquaria_vc90.sln | 7 +++ win/vc90/Aquaria.vcproj | 81 +++++++++++++++++++++++++++ win/vc90/BBGE.vcproj | 77 +++++++++++++++++++++++++ win/vc90/external.vcproj | 70 ++++++++++++++++++++++- 14 files changed, 461 insertions(+), 51 deletions(-) create mode 100644 BBGE/LegacyKeycodes.cpp create mode 100644 BBGE/LegacyKeycodes.h diff --git a/Aquaria/DSQ.cpp b/Aquaria/DSQ.cpp index 9e1a58b..439c3dd 100644 --- a/Aquaria/DSQ.cpp +++ b/Aquaria/DSQ.cpp @@ -910,6 +910,7 @@ This build is not yet final, and as such there are a couple things lacking. They SDL_Init(SDL_INIT_VIDEO); if (fullscreen && !sdlVideoModeOK(user.video.displayindex, user.video.resx, user.video.resy, user.video.bits)) { +#ifdef BBGE_BUILD_SDL2 SDL_DisplayMode mode, closest; mode.format = 0; mode.driverdata = 0; @@ -923,11 +924,13 @@ This build is not yet final, and as such there are a couple things lacking. They user.video.hz = closest.refresh_rate; } else +#endif { // maybe we can force a sane resolution if SetVideoMode is going to fail... user.video.resx = 800; user.video.resy = 600; user.video.hz = 60; + user.video.bits = 32; user.video.displayindex = 0; if (!sdlVideoModeOK(0, user.video.resx, user.video.resy, user.video.bits)) fullscreen = false; // last chance. diff --git a/BBGE/ActionInput.cpp b/BBGE/ActionInput.cpp index 1aa7a08..bf12a9b 100644 --- a/BBGE/ActionInput.cpp +++ b/BBGE/ActionInput.cpp @@ -23,6 +23,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "ActionMapper.h" #include "Core.h" #include "SDL.h" +#include "LegacyKeycodes.h" static std::string inputcode2string(int k) @@ -31,17 +32,15 @@ static std::string inputcode2string(int k) return std::string(); if(k < KEY_MAXARRAY) { - const char *name; -#ifdef BBGE_BUILD_SDL2 - name = SDL_GetScancodeName((SDL_Scancode)k); -#else - name = SDL_GetKeyName((SDLKey)k); -#endif - if(name) - return name; - + // See parseKey() below std::stringstream os; - os << "K:" << k; + os << "K:"; +#ifdef BBGE_BUILD_SDL2 + int keycode = SDL_GetKeyFromScancode((SDL_Scancode)k); + os << keycode << "," << k; +#else + os << k; +#endif return os.str(); } @@ -149,11 +148,37 @@ std::string getInputCodeToUserString(unsigned int k, size_t joystickID) return inputcode2string(k); } -#ifndef BBGE_BUILD_SDL2 -static bool s_needKeyTabInit = false; -std::map s_keyNameMap; +// two comma-separated ints +// first is the keycode, second the scancode +// (Keymap-independent) Scancodes are used when built with SDL2 support and specified +// (Keymap-dependent) Keycode is used otherwise +static int parseKey(const char *ks) +{ + int k = 0; + +#ifdef BBGE_BUILD_SDL2 + if(const char *comma = strchr(ks, ',')) + { + k = atoi(comma + 1); + if(k && k < KEY_MAXARRAY) + return k; + } #endif + // Use the keycode + k = atoi(ks); + if(k < KEY_MAXARRAY) + { +#ifdef BBGE_BUILD_SDL2 + // But when we're on SDL2, don't forget to turn they keycode back into a scancode, since we work with scancodes internally + k = SDL_GetScancodeFromKey(k); +#endif + return k; + } + + return 0; +} + int getStringToInputCode(const std::string& s) { if(s == "LMB") @@ -163,7 +188,7 @@ int getStringToInputCode(const std::string& s) if(s == "MMB") return MOUSE_BUTTON_MIDDLE; if(!strncmp(s.c_str(), "K:", 2)) - return atoi(s.c_str() + 2); + return parseKey(s.c_str() + 2); if(!strncmp(s.c_str(), "JB:", 3)) return JOY_BUTTON_0 + atoi(s.c_str() + 3); if(!strncmp(s.c_str(), "MB:", 3)) @@ -181,11 +206,12 @@ int getStringToInputCode(const std::string& s) if(s == "NONE") return 0; -#ifdef BBGE_BUILD_SDL2 - return SDL_GetScancodeFromName(underscoresToSpaces(s).c_str()); -#else - return SDL_GetKeyFromName(underscoresToSpaces(s).c_str()); -#endif + // Maybe we're upgrading from an old config? + // Note that this returns 0 for "0", which was considered "no key" + if(int k = getInputCodeFromLegacyName(s.c_str())) + return k; + + return 0; } diff --git a/BBGE/ActionStatus.h b/BBGE/ActionStatus.h index 6491bef..863fa1b 100644 --- a/BBGE/ActionStatus.h +++ b/BBGE/ActionStatus.h @@ -8,6 +8,7 @@ class ActionSet; const unsigned mouseExtraButtons = 8; +// *_END is non-inclusive! enum ActionButtonType { MOUSE_BUTTON_LEFT = KEY_MAXARRAY + 1, diff --git a/BBGE/Core.cpp b/BBGE/Core.cpp index b434e5c..f8f8ec9 100644 --- a/BBGE/Core.cpp +++ b/BBGE/Core.cpp @@ -126,7 +126,12 @@ void Core::reloadDevice() void Core::setup_opengl() { +#ifdef BBGE_BUILD_SDL2 assert(gGLctx); +#else + assert(gScreen); +#endif + glViewport(0, 0, width, height); @@ -150,6 +155,11 @@ void Core::initGraphics(int w, int h, int fullscreen, int vsync, int bpp, int di { assert(lib_graphics); + 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) @@ -174,11 +184,12 @@ void Core::initGraphics(int w, int h, int fullscreen, int vsync, int bpp, int di SDL_SetWindowPosition(gScreen, center, center); } - const int oldw = width; - const int oldh = height; - - 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) @@ -190,11 +201,13 @@ void Core::initGraphics(int w, int h, int fullscreen, int vsync, int bpp, int di if (vsync == -1) vsync = _vsync; +#ifdef BBGE_BUILD_SDL2 if(useDesktop) { w = displaymode.w; h = displaymode.h; } +#endif if (w == -1) w = width; @@ -209,9 +222,10 @@ void Core::initGraphics(int w, int h, int fullscreen, int vsync, int bpp, int di _fullscreen = fullscreen; _bpp = bpp; _refreshRate = hz; - displaymode.refresh_rate = hz; #ifdef BBGE_BUILD_SDL2 + displaymode.refresh_rate = hz; + if(vsync) { if(SDL_GL_SetSwapInterval(-1) != 0) @@ -227,7 +241,6 @@ void Core::initGraphics(int w, int h, int fullscreen, int vsync, int bpp, int di SDL_SetWindowFullscreen(gScreen, 0); bool resize = true; - bool reloadRes = false; bool maximize = true; if(useDesktop != _useDesktopResolution) { @@ -245,7 +258,7 @@ void Core::initGraphics(int w, int h, int fullscreen, int vsync, int bpp, int di useh = oldh; maximize = false; } - createWindow(usew, useh, useDesktop, false); + createWindow(usew, useh, useDesktop, false, bpp); reloadRes = true; } @@ -276,7 +289,7 @@ void Core::initGraphics(int w, int h, int fullscreen, int vsync, int bpp, int di if(resize && !fullscreen && !useDesktop) { - if(w != oldw|| h != oldh) + if(w != oldw || h != oldh) { SDL_SetWindowSize(gScreen, w, h); int c = SDL_WINDOWPOS_CENTERED_DISPLAY(display); @@ -284,17 +297,20 @@ void Core::initGraphics(int w, int h, int fullscreen, int vsync, int bpp, int di } } -#else + if(!resize) + SDL_GetWindowSize(gScreen, &w, &h); -# error FIXME: backport to support SDL 1.2 +#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); + } #endif _useDesktopResolution = useDesktop; - if(!resize) - SDL_GetWindowSize(gScreen, &w, &h); - updateWindowDrawSize(w, h); if(reloadRes) @@ -525,8 +541,10 @@ 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++) @@ -866,20 +884,24 @@ bool Core::initGraphicsLibrary(int width, int height, bool fullscreen, bool vsyn initIcon(gScreen); - std::ostringstream os; - os << "setting vsync: " << vsync; - debugLog(os.str()); -#ifndef BBGE_BUILD_SDL2 - SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, vsync); - SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); - #ifdef _DEBUG - SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS SDL_GL_CONTEXT_DEBUG_FLAG); - #endif + +#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(width, height, !width && !height, fullscreen); + createWindow(width, height, !width && !height, fullscreen, bpp); if (SDL_GL_LoadLibrary(NULL) == -1) { @@ -913,7 +935,7 @@ bool Core::initGraphicsLibrary(int width, int height, bool fullscreen, bool vsyn return true; } -void Core::createWindow(int w, int h, bool resizable, bool fullscreen) +void Core::createWindow(int w, int h, bool resizable, bool fullscreen, int bpp) { #ifdef BBGE_BUILD_SDL2 if(gScreen) @@ -1039,7 +1061,7 @@ void Core::enumerateScreenModes() { if (modes[i]->w > modes[i]->h) { - screenModes.push_back(ScreenMode(i, modes[i]->w, modes[i]->h, 0)); + screenModes.push_back(ScreenMode(i, modes[i]->w, modes[i]->h)); } } } diff --git a/BBGE/Core.h b/BBGE/Core.h index 743940d..c6557da 100644 --- a/BBGE/Core.h +++ b/BBGE/Core.h @@ -486,7 +486,7 @@ protected: 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); + void createWindow(int w, int h, bool resizable, bool fullscreen, int bpp); void shutdownInputLibrary(); void shutdownJoystickLibrary(); void shutdownGraphicsLibrary(); diff --git a/BBGE/GameKeys.h b/BBGE/GameKeys.h index 916b619..45b032f 100644 --- a/BBGE/GameKeys.h +++ b/BBGE/GameKeys.h @@ -122,6 +122,8 @@ #else // BBGE_BUILD_SDL2 +// ------------- SDL 1.2 code path ----------------- + #include #define KEY_LSUPER SDLK_LSUPER diff --git a/BBGE/LegacyKeycodes.cpp b/BBGE/LegacyKeycodes.cpp new file mode 100644 index 0000000..cfef9b9 --- /dev/null +++ b/BBGE/LegacyKeycodes.cpp @@ -0,0 +1,111 @@ +#include "LegacyKeycodes.h" +#include "ActionStatus.h" +#include +#include + +typedef std::map InputCodeMap; + +InputCodeMap inputCodeMap; + +static void initInputCodeMap() +{ +#define K(k)inputCodeMap[#k] = k; + K(KEY_A) + K(KEY_B) + K(KEY_C) + K(KEY_D) + K(KEY_E) + K(KEY_F) + K(KEY_G) + K(KEY_H) + K(KEY_I) + K(KEY_J) + K(KEY_K) + K(KEY_L) + K(KEY_M) + K(KEY_N) + K(KEY_O) + K(KEY_P) + K(KEY_Q) + K(KEY_R) + K(KEY_S) + K(KEY_T) + K(KEY_U) + K(KEY_V) + K(KEY_W) + K(KEY_X) + K(KEY_Y) + K(KEY_Z) + K(KEY_1) + K(KEY_2) + K(KEY_3) + K(KEY_4) + K(KEY_5) + K(KEY_6) + K(KEY_7) + K(KEY_8) + K(KEY_9) + K(KEY_0) + K(KEY_NUMPAD1) + K(KEY_NUMPAD2) + K(KEY_NUMPAD3) + K(KEY_NUMPAD4) + K(KEY_NUMPAD5) + K(KEY_NUMPAD6) + K(KEY_NUMPAD7) + K(KEY_NUMPAD8) + K(KEY_NUMPAD9) + K(KEY_NUMPAD0) + K(KEY_F1) + K(KEY_F2) + K(KEY_F3) + K(KEY_F4) + K(KEY_F5) + K(KEY_F6) + K(KEY_F7) + K(KEY_F8) + K(KEY_F9) + K(KEY_F10) + K(KEY_F11) + K(KEY_F12) + K(KEY_LEFT) + K(KEY_RIGHT) + K(KEY_UP) + K(KEY_DOWN) + K(KEY_SPACE) + K(KEY_LCONTROL) + K(KEY_RCONTROL) + K(KEY_LSHIFT) + K(KEY_RSHIFT) + K(KEY_LMETA) + K(KEY_RMETA) + K(KEY_LALT) + K(KEY_RALT) + K(KEY_RETURN) + K(KEY_TAB) + K(KEY_ESCAPE) + + K(MOUSE_BUTTON_LEFT) + K(MOUSE_BUTTON_RIGHT) + K(MOUSE_BUTTON_MIDDLE) +#undef K + + for (int jb = JOY_BUTTON_0; jb < JOY_BUTTON_END; jb++) + { + std::ostringstream os; + os << "JOY_BUTTON_" << jb - JOY_BUTTON_0; + inputCodeMap[os.str()] = jb; + } +} + +int getInputCodeFromLegacyName(const char *name) +{ + return inputCodeMap[name]; +} + + +struct LegacyKeymapInitializer +{ + LegacyKeymapInitializer() { initInputCodeMap(); } +}; +static LegacyKeymapInitializer s_kinit; diff --git a/BBGE/LegacyKeycodes.h b/BBGE/LegacyKeycodes.h new file mode 100644 index 0000000..9506570 --- /dev/null +++ b/BBGE/LegacyKeycodes.h @@ -0,0 +1,6 @@ +#ifndef BBGE_LEGACYKEYCODES_H +#define BBGE_LEGACYKEYCODES_H + +int getInputCodeFromLegacyName(const char *name); + +#endif diff --git a/BBGE/OSFunctions.cpp b/BBGE/OSFunctions.cpp index ab1de98..edac9fc 100644 --- a/BBGE/OSFunctions.cpp +++ b/BBGE/OSFunctions.cpp @@ -66,11 +66,12 @@ void initIcon(void *screen) #ifdef BBGE_BUILD_SDL2 SDL_GetWindowWMInfo((SDL_Window*)screen, &wminfo); + HWND hwnd = wminfo.info.win.window; #else - SDL_GetWindowWMInfo((SDL_Surface*)screen, &wminfo); + SDL_GetWMInfo(&wminfo); + HWND hwnd = wminfo.window; #endif - HWND hwnd = wminfo.info.win.window; ::SetClassLongPtr(hwnd, -14, (LONG) icon_windows); // -14 is GCL_HICON (32bit) or GCLP_HICON (64bit) #endif } diff --git a/ExternalLibs/minipstdint.h b/ExternalLibs/minipstdint.h index 63d5f02..da44697 100644 --- a/ExternalLibs/minipstdint.h +++ b/ExternalLibs/minipstdint.h @@ -9,13 +9,18 @@ #include #if defined(_MSC_VER) && _MSC_VER < 1600 +#include "SDL_version.h" +#if SDL_VERSION_ATLEAST(2, 0, 0) // AQUARIA HACK: Included SDL 1.2 includes define some of these, SDL does not. Avoid conflicts. typedef signed __int8 int8_t; - typedef signed __int16 int16_t; - typedef signed __int32 int32_t; - typedef signed __int64 int64_t; typedef unsigned __int8 uint8_t; + typedef signed __int16 int16_t; typedef unsigned __int16 uint16_t; + typedef signed __int32 int32_t; typedef unsigned __int32 uint32_t; +#else +#include "SDL_config.h" +#endif + typedef signed __int64 int64_t; typedef unsigned __int64 uint64_t; # ifdef __cplusplus namespace std diff --git a/win/Aquaria_vc90.sln b/win/Aquaria_vc90.sln index eb69167..e85be73 100644 --- a/win/Aquaria_vc90.sln +++ b/win/Aquaria_vc90.sln @@ -16,19 +16,26 @@ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Win32 = Debug|Win32 + Debug-SDL1.2|Win32 = Debug-SDL1.2|Win32 Release|Win32 = Release|Win32 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {4DB6D5AA-4EAD-4195-9B54-389B558036D8}.Debug|Win32.ActiveCfg = Debug|Win32 {4DB6D5AA-4EAD-4195-9B54-389B558036D8}.Debug|Win32.Build.0 = Debug|Win32 + {4DB6D5AA-4EAD-4195-9B54-389B558036D8}.Debug-SDL1.2|Win32.ActiveCfg = Debug-SDL1.2|Win32 + {4DB6D5AA-4EAD-4195-9B54-389B558036D8}.Debug-SDL1.2|Win32.Build.0 = Debug-SDL1.2|Win32 {4DB6D5AA-4EAD-4195-9B54-389B558036D8}.Release|Win32.ActiveCfg = Release|Win32 {4DB6D5AA-4EAD-4195-9B54-389B558036D8}.Release|Win32.Build.0 = Release|Win32 {4C2AD812-6776-4728-A4B0-ABA397224152}.Debug|Win32.ActiveCfg = Debug|Win32 {4C2AD812-6776-4728-A4B0-ABA397224152}.Debug|Win32.Build.0 = Debug|Win32 + {4C2AD812-6776-4728-A4B0-ABA397224152}.Debug-SDL1.2|Win32.ActiveCfg = Debug-SDL1.2|Win32 + {4C2AD812-6776-4728-A4B0-ABA397224152}.Debug-SDL1.2|Win32.Build.0 = Debug-SDL1.2|Win32 {4C2AD812-6776-4728-A4B0-ABA397224152}.Release|Win32.ActiveCfg = Release|Win32 {4C2AD812-6776-4728-A4B0-ABA397224152}.Release|Win32.Build.0 = Release|Win32 {6A2DACD7-DA30-49A1-9214-CCDEB48E6050}.Debug|Win32.ActiveCfg = Debug|Win32 {6A2DACD7-DA30-49A1-9214-CCDEB48E6050}.Debug|Win32.Build.0 = Debug|Win32 + {6A2DACD7-DA30-49A1-9214-CCDEB48E6050}.Debug-SDL1.2|Win32.ActiveCfg = Debug-SDL1.2|Win32 + {6A2DACD7-DA30-49A1-9214-CCDEB48E6050}.Debug-SDL1.2|Win32.Build.0 = Debug-SDL1.2|Win32 {6A2DACD7-DA30-49A1-9214-CCDEB48E6050}.Release|Win32.ActiveCfg = Release|Win32 {6A2DACD7-DA30-49A1-9214-CCDEB48E6050}.Release|Win32.Build.0 = Release|Win32 EndGlobalSection diff --git a/win/vc90/Aquaria.vcproj b/win/vc90/Aquaria.vcproj index a11ec97..ee208d7 100644 --- a/win/vc90/Aquaria.vcproj +++ b/win/vc90/Aquaria.vcproj @@ -179,6 +179,83 @@ Name="VCPostBuildEventTool" /> + + + + + + + + + + + + + + + + + + + @@ -468,6 +545,10 @@ RelativePath="..\..\Aquaria\SceneEditor.cpp" > + + diff --git a/win/vc90/BBGE.vcproj b/win/vc90/BBGE.vcproj index 08237af..1d1ebb1 100644 --- a/win/vc90/BBGE.vcproj +++ b/win/vc90/BBGE.vcproj @@ -160,6 +160,75 @@ Name="VCPostBuildEventTool" /> + + + + + + + + + + + + + + + + + @@ -321,6 +390,14 @@ RelativePath="..\..\BBGE\Joystick.h" > + + + + diff --git a/win/vc90/external.vcproj b/win/vc90/external.vcproj index dfb00c2..1ce7b68 100644 --- a/win/vc90/external.vcproj +++ b/win/vc90/external.vcproj @@ -117,7 +117,6 @@ OmitFramePointers="true" EnableFiberSafeOptimizations="true" AdditionalIncludeDirectories=""$(SolutionDir)\..\ExternalLibs\freetype2\include\freetype\config";"$(SolutionDir)\..\BBGE";"$(SolutionDir)\..\ExternalLibs\zlib";"$(SolutionDir)\..\ExternalLibs\png";"$(SolutionDir)\..\ExternalLibs\libogg\include";"$(SolutionDir)\..\ExternalLibs\libvorbis\include";"$(SolutionDir)\..\ExternalLibs\freetype2\include";"$(SolutionDir)\..\ExternalLibs\FTGL\include";"$(SolutionDir)\..\ExternalLibs\SDL2\include";"$(SolutionDir)\..\ExternalLibs\ttvfs";"$(SolutionDir)\..\ExternalLibs\ttvfs_cfileapi";"$(SolutionDir)\..\ExternalLibs\"" - PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE;HAVE_FCNTL_H;FT_CONFIG_OPTION_SYSTEM_ZLIB;FTGL_LIBRARY_STATIC;FT2_BUILD_LIBRARY;GL_GLEXT_LEGACY=1;TIXML_USE_STL=1;HAVE_PUTENV=1;_HAS_EXCEPTIONS=0;VFS_ENABLE_C_API=1" StringPooling="true" ExceptionHandling="0" @@ -162,6 +161,75 @@ Name="VCPostBuildEventTool" /> + + + + + + + + + + + + + + + + +