1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-10-19 12:59:36 +00:00

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.
This commit is contained in:
fgenesis 2017-02-15 04:34:32 +01:00
commit 3dda97d32a
14 changed files with 461 additions and 51 deletions

View file

@ -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<std::string, SDLKey> 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;
}

View file

@ -8,6 +8,7 @@ class ActionSet;
const unsigned mouseExtraButtons = 8;
// *_END is non-inclusive!
enum ActionButtonType
{
MOUSE_BUTTON_LEFT = KEY_MAXARRAY + 1,

View file

@ -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));
}
}
}

View file

@ -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();

View file

@ -122,6 +122,8 @@
#else // BBGE_BUILD_SDL2
// ------------- SDL 1.2 code path -----------------
#include <SDL_keysym.h>
#define KEY_LSUPER SDLK_LSUPER

111
BBGE/LegacyKeycodes.cpp Normal file
View file

@ -0,0 +1,111 @@
#include "LegacyKeycodes.h"
#include "ActionStatus.h"
#include <map>
#include <sstream>
typedef std::map<std::string, int> 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;

6
BBGE/LegacyKeycodes.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef BBGE_LEGACYKEYCODES_H
#define BBGE_LEGACYKEYCODES_H
int getInputCodeFromLegacyName(const char *name);
#endif

View file

@ -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
}