[aquaria] SDL2 patches...

Ryan C. Gordon icculus at icculus.org
Wed Jul 17 00:07:12 EDT 2013


Long overdue, here are the SDL2 patches I promised.

This is just diff'd against the original GPL release and cleaned up as 
appropriate, so if it doesn't apply correctly, you'll have to poke at 
it. Ask questions if you need to.

Not in this patch: The SDL2 headers (get them from SDL, instead of me 
inflating this patch massively) or prebuilt libraries (let me know if 
you want them).

This is what we're shipping on Steam in the linuxbeta depot, and I'm 
about to make it live to the general public by default.

--ryan.

-------------- next part --------------
diff -ru a/Aquaria/DSQ.cpp b/Aquaria/DSQ.cpp
--- a/Aquaria/DSQ.cpp	2013-07-16 22:08:25.459618702 -0400
+++ b/Aquaria/DSQ.cpp	2013-02-28 23:06:35.008430000 -0500
@@ -887,7 +1014,19 @@
 #ifdef BBGE_BUILD_SDL
 static bool sdlVideoModeOK(const int w, const int h, const int bpp)
 {
+#ifdef BBGE_BUILD_SDL2
+	SDL_DisplayMode mode;
+	const int modecount = SDL_GetNumDisplayModes(0);
+	for (int i = 0; i < modecount; i++) {
+		SDL_GetDisplayMode(0, i, &mode);
+        if (!mode.w || !mode.h || (w >= mode.w && h >= mode.h)) {
+			return true;
+        }
+    }
+    return false;
+#else
 	return SDL_VideoModeOK(w, h, bpp, SDL_OPENGL | SDL_FULLSCREEN);
+#endif
 }
 #endif
 
diff -ru a/Aquaria/Main.cpp b/Aquaria/Main.cpp
--- a/Aquaria/Main.cpp	2013-07-16 22:08:25.483618699 -0400
+++ b/Aquaria/Main.cpp	2013-02-28 20:58:33.800255000 -0500
@@ -100,6 +80,7 @@
 
 	void enumerateTest()
 	{
+#ifndef BBGE_BUILD_SDL2  // I don't think this thing is ever seriously used, so I didn't reimplement it.  --ryan.
 		SDL_Rect **modes;
 		int i;
 
@@ -128,6 +109,7 @@
 			}
 		}
 #endif
+#endif
 	}
 
 
diff -ru a/BBGE/Core.cpp b/BBGE/Core.cpp
--- a/BBGE/Core.cpp	2013-07-16 22:08:25.603618702 -0400
+++ b/BBGE/Core.cpp	2013-03-01 01:14:35.516604000 -0500
@@ -39,7 +20,13 @@
 
 #ifdef BBGE_BUILD_SDL
 	#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;
 #endif
@@ -211,8 +202,10 @@
 void Core::setWindowCaption(const std::string &caption, const std::string &icon)
 {
 #ifdef BBGE_BUILD_SDL
+#ifndef BBGE_BUILD_SDL2
 	SDL_WM_SetCaption(caption.c_str(), icon.c_str());
 #endif
+#endif
 }
 
 RenderObjectLayer *Core::getRenderObjectLayer(int i)
@@ -1149,7 +1193,11 @@
 {
 	if (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
 	}
 }
 
@@ -1459,8 +1518,12 @@
 {
 	//joystickEnabled = false;
 #ifdef BBGE_BUILD_SDL
+#ifdef BBGE_BUILD_SDL2
+	SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER);
+#else
 	SDL_InitSubSystem(SDL_INIT_JOYSTICK);
 #endif
+#endif
 
 	for (int i = 0; i < numSticks; i++)
 		joystick.init(0);
@@ -1752,17 +1815,13 @@
 	os << "setting vsync: " << _vsync;
 	debugLog(os.str());
 
+#ifndef BBGE_BUILD_SDL2
 	SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, _vsync);
+#endif
+
 	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
 }
 
-#ifdef BBGE_BUILD_UNIX
-#ifdef GLAPIENTRY
-#undef GLAPIENTRY
-#endif
-#define GLAPIENTRY
-#endif
-
 #if BBGE_BUILD_OPENGL_DYNAMIC
 #define GL_FUNC(ret,fn,params,call,rt) \
     extern "C" { \
@@ -1823,10 +1882,14 @@
 	//setenv("SDL_VIDEO_CENTERED", "1", 1);
 	//SDL_putenv("SDL_VIDEO_WINDOW_POS=400,300");
 
+#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
+#endif
 
 	if (recreate)
 	{
@@ -1855,6 +1918,30 @@
 
 	//if (!didOnce)
 	{
+#ifdef BBGE_BUILD_SDL2
+		Uint32 flags = 0;
+		flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
+		if (fullscreen)
+			flags |= SDL_WINDOW_FULLSCREEN;
+		gScreen = SDL_CreateWindow(appName.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, flags);
+		if (gScreen == NULL)
+		{
+			std::ostringstream os;
+			os << "Couldn't set resolution [" << width << "x" << height << "]\n" << SDL_GetError();
+			errorLog(os.str());
+			SDL_Quit();
+			exit(0);
+		}
+		gGLctx = SDL_GL_CreateContext(gScreen);
+		if (gScreen == NULL)
+		{
+			std::ostringstream os;
+			os << "Couldn't create OpenGL context!\n" << SDL_GetError();
+			errorLog(os.str());
+			SDL_Quit();
+			exit(0);
+		}
+#else
 		Uint32 flags = 0;
 		flags = SDL_OPENGL;
 		if (fullscreen)
@@ -1869,7 +1956,8 @@
 			SDL_Quit();
 			exit(0);
 		}
-
+#endif
+		
 #if BBGE_BUILD_OPENGL_DYNAMIC
 		if (!lookup_all_glsyms())
 		{
@@ -1884,10 +1972,22 @@
 
 	setWindowCaption(appName, "");
 
+#ifdef BBGE_BUILD_SDL2
+	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+	SDL_GL_SwapWindow(gScreen);
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+	SDL_GL_SwapWindow(gScreen);
+	if ((_vsync != 1) || (SDL_GL_SetSwapInterval(-1) == -1))
+		SDL_GL_SetSwapInterval(_vsync);
+	const char *name = SDL_GetCurrentVideoDriver();
+	SDL_SetWindowGrab(gScreen, SDL_TRUE);
+#else
 	SDL_WM_GrabInput(SDL_GRAB_ON);
 	char name[256];
 	SDL_VideoDriverName((char*)name, 256);
-
+#endif
+	
 	glViewport(0, 0, width, height);
 	glScissor(0, 0, width, height);
 
@@ -1971,6 +2077,25 @@
 void Core::enumerateScreenModes()
 {
 	screenModes.clear();
+
+#ifdef BBGE_BUILD_SDL2
+	SDL_DisplayMode mode;
+	const int modecount = SDL_GetNumDisplayModes(0);
+	if(modecount == 0){
+		debugLog("No modes available!");
+		return;
+	}
+	
+	for (int i = 0; i < modecount; i++) {
+		SDL_GetDisplayMode(0, i, &mode);
+		if (mode.w && mode.h && (mode.w > mode.h))
+		{
+			screenModes.push_back(ScreenMode(i, mode.w, mode.h));
+		}
+    }
+
+#else
+
 	SDL_Rect **modes;
 	int i;
 
@@ -1997,6 +2122,7 @@
 			}
 		}
 	}
+#endif
 }
 
 void Core::shutdownSoundLibrary()
@@ -2008,8 +2134,17 @@
 #ifdef BBGE_BUILD_SDL
 	glFinish();
 	if (killVideo) {
+		#ifdef BBGE_BUILD_SDL2
+		SDL_SetWindowGrab(gScreen, SDL_FALSE);
+		SDL_GL_MakeCurrent(gScreen, NULL);
+		SDL_GL_DeleteContext(gGLctx);
+		SDL_DestroyWindow(gScreen);
+		gGLctx = 0;
+		SDL_QuitSubSystem(SDL_INIT_VIDEO);
+		#else
 		SDL_QuitSubSystem(SDL_INIT_VIDEO);
 		SDL_WM_GrabInput(SDL_GRAB_OFF);
+		#endif
 
 		FrameBuffer::resetOpenGL();
 
@@ -2685,7 +2834,11 @@
 	float px = p.x + virtualOffX;
 	float py = p.y;// + virtualOffY;
 
+	#ifdef BBGE_BUILD_SDL2
+	SDL_WarpMouseInWindow(gScreen, px * (double(width)/double(virtualWidth)), py * (double(height)/double(virtualHeight)));
+	#else
 	SDL_WarpMouse( px * (double(width)/double(virtualWidth)), py * (double(height)/double(virtualHeight)));
+	#endif
 
 	/*
 	ignoreNextMouse = true;
@@ -2773,7 +2926,11 @@
 
 bool Core::isWindowFocus()
 {
+#ifdef BBGE_BUILD_SDL2
+	return ((SDL_GetWindowFlags(gScreen) & SDL_WINDOW_INPUT_FOCUS) != 0);
+#else
 	return ((SDL_GetAppState() & SDL_APPINPUTFOCUS) != 0);
+#endif
 }
 
 void Core::main(float runTime)
@@ -3274,6 +3435,174 @@
 	return false;
 }
 
+#if defined(BBGE_BUILD_SDL)
+
+#if defined(BBGE_BUILD_SDL2)
+typedef std::map<SDL_Keycode,int> sdlKeyMap;
+#else
+typedef std::map<SDLKey,int> sdlKeyMap;
+#endif
+
+static sdlKeyMap *initSDLKeymap(void)
+{
+	sdlKeyMap *_retval = new sdlKeyMap;
+	sdlKeyMap &retval = *_retval;
+
+	#define SETKEYMAP(gamekey,sdlkey) retval[sdlkey] = gamekey
+
+#ifdef BBGE_BUILD_SDL2
+	SETKEYMAP(KEY_LSUPER, SDLK_LGUI);
+	SETKEYMAP(KEY_RSUPER, SDLK_RGUI);
+	SETKEYMAP(KEY_LMETA, SDLK_LGUI);
+	SETKEYMAP(KEY_RMETA, SDLK_RGUI);
+	SETKEYMAP(KEY_PRINTSCREEN, SDLK_PRINTSCREEN);
+	SETKEYMAP(KEY_NUMPAD1, SDLK_KP_1);
+	SETKEYMAP(KEY_NUMPAD2, SDLK_KP_2);
+	SETKEYMAP(KEY_NUMPAD3, SDLK_KP_3);
+	SETKEYMAP(KEY_NUMPAD4, SDLK_KP_4);
+	SETKEYMAP(KEY_NUMPAD5, SDLK_KP_5);
+	SETKEYMAP(KEY_NUMPAD6, SDLK_KP_6);
+	SETKEYMAP(KEY_NUMPAD7, SDLK_KP_7);
+	SETKEYMAP(KEY_NUMPAD8, SDLK_KP_8);
+	SETKEYMAP(KEY_NUMPAD9, SDLK_KP_9);
+	SETKEYMAP(KEY_NUMPAD0, SDLK_KP_0);
+#else
+	SETKEYMAP(KEY_LSUPER, SDLK_LSUPER);
+	SETKEYMAP(KEY_RSUPER, SDLK_RSUPER);
+	SETKEYMAP(KEY_LMETA, SDLK_LMETA);
+	SETKEYMAP(KEY_RMETA, SDLK_RMETA);
+	SETKEYMAP(KEY_PRINTSCREEN, SDLK_PRINT);
+	SETKEYMAP(KEY_NUMPAD1, SDLK_KP1);
+	SETKEYMAP(KEY_NUMPAD2, SDLK_KP2);
+	SETKEYMAP(KEY_NUMPAD3, SDLK_KP3);
+	SETKEYMAP(KEY_NUMPAD4, SDLK_KP4);
+	SETKEYMAP(KEY_NUMPAD5, SDLK_KP5);
+	SETKEYMAP(KEY_NUMPAD6, SDLK_KP6);
+	SETKEYMAP(KEY_NUMPAD7, SDLK_KP7);
+	SETKEYMAP(KEY_NUMPAD8, SDLK_KP8);
+	SETKEYMAP(KEY_NUMPAD9, SDLK_KP9);
+	SETKEYMAP(KEY_NUMPAD0, SDLK_KP0);
+#endif
+	
+	SETKEYMAP(KEY_BACKSPACE, SDLK_BACKSPACE);
+
+	//SETKEYMAP(KEY_CAPSLOCK, DIK_CAPSLOCK);
+	//SETKEYMAP(KEY_CIRCUMFLEX, DIK_CIRCUMFLEX);
+	SETKEYMAP(KEY_LALT, SDLK_LALT);
+	SETKEYMAP(KEY_RALT, SDLK_RALT);
+	SETKEYMAP(KEY_LSHIFT, SDLK_LSHIFT);
+	SETKEYMAP(KEY_RSHIFT, SDLK_RSHIFT);
+	SETKEYMAP(KEY_LCONTROL, SDLK_LCTRL);
+	SETKEYMAP(KEY_RCONTROL, SDLK_RCTRL);
+	SETKEYMAP(KEY_NUMPADMINUS, SDLK_KP_MINUS);
+	SETKEYMAP(KEY_NUMPADPERIOD, SDLK_KP_PERIOD);
+	SETKEYMAP(KEY_NUMPADPLUS, SDLK_KP_PLUS);
+	SETKEYMAP(KEY_NUMPADSLASH, SDLK_KP_DIVIDE);
+	SETKEYMAP(KEY_NUMPADSTAR, SDLK_KP_MULTIPLY);
+	SETKEYMAP(KEY_PGDN, SDLK_PAGEDOWN);
+	SETKEYMAP(KEY_PGUP, SDLK_PAGEUP);
+	SETKEYMAP(KEY_APOSTROPHE, SDLK_QUOTE);
+	SETKEYMAP(KEY_EQUALS, SDLK_EQUALS);
+	SETKEYMAP(KEY_SEMICOLON, SDLK_SEMICOLON);
+	SETKEYMAP(KEY_LBRACKET, SDLK_LEFTBRACKET);
+	SETKEYMAP(KEY_RBRACKET, SDLK_RIGHTBRACKET);
+	//SETKEYMAP(KEY_RALT, GLFW_SETKEYMAP(KEY_RALT);
+	SETKEYMAP(KEY_TILDE, SDLK_BACKQUOTE);
+	SETKEYMAP(KEY_0, SDLK_0);
+	SETKEYMAP(KEY_1, SDLK_1);
+	SETKEYMAP(KEY_2, SDLK_2);
+	SETKEYMAP(KEY_3, SDLK_3);
+	SETKEYMAP(KEY_4, SDLK_4);
+	SETKEYMAP(KEY_5, SDLK_5);
+	SETKEYMAP(KEY_6, SDLK_6);
+	SETKEYMAP(KEY_7, SDLK_7);
+	SETKEYMAP(KEY_8, SDLK_8);
+	SETKEYMAP(KEY_9, SDLK_9);
+	SETKEYMAP(KEY_A, SDLK_a);
+	SETKEYMAP(KEY_B, SDLK_b);
+	SETKEYMAP(KEY_C, SDLK_c);
+	SETKEYMAP(KEY_D, SDLK_d);
+	SETKEYMAP(KEY_E, SDLK_e);
+	SETKEYMAP(KEY_F, SDLK_f);
+	SETKEYMAP(KEY_G, SDLK_g);
+	SETKEYMAP(KEY_H, SDLK_h);
+	SETKEYMAP(KEY_I, SDLK_i);
+	SETKEYMAP(KEY_J, SDLK_j);
+	SETKEYMAP(KEY_K, SDLK_k);
+	SETKEYMAP(KEY_L, SDLK_l);
+	SETKEYMAP(KEY_M, SDLK_m);
+	SETKEYMAP(KEY_N, SDLK_n);
+	SETKEYMAP(KEY_O, SDLK_o);
+	SETKEYMAP(KEY_P, SDLK_p);
+	SETKEYMAP(KEY_Q, SDLK_q);
+	SETKEYMAP(KEY_R, SDLK_r);
+	SETKEYMAP(KEY_S, SDLK_s);
+	SETKEYMAP(KEY_T, SDLK_t);
+	SETKEYMAP(KEY_U, SDLK_u);
+	SETKEYMAP(KEY_V, SDLK_v);
+	SETKEYMAP(KEY_W, SDLK_w);
+	SETKEYMAP(KEY_X, SDLK_x);
+	SETKEYMAP(KEY_Y, SDLK_y);
+	SETKEYMAP(KEY_Z, SDLK_z);
+	
+	SETKEYMAP(KEY_LEFT, SDLK_LEFT);
+	SETKEYMAP(KEY_RIGHT, SDLK_RIGHT);
+	SETKEYMAP(KEY_UP, SDLK_UP);
+	SETKEYMAP(KEY_DOWN, SDLK_DOWN);
+
+	SETKEYMAP(KEY_DELETE, SDLK_DELETE);
+	SETKEYMAP(KEY_SPACE, SDLK_SPACE);
+	SETKEYMAP(KEY_RETURN, SDLK_RETURN);
+	SETKEYMAP(KEY_PERIOD, SDLK_PERIOD);
+	SETKEYMAP(KEY_MINUS, SDLK_MINUS);
+	SETKEYMAP(KEY_CAPSLOCK, SDLK_CAPSLOCK);
+	SETKEYMAP(KEY_SYSRQ, SDLK_SYSREQ);
+	SETKEYMAP(KEY_TAB, SDLK_TAB);
+	SETKEYMAP(KEY_HOME, SDLK_HOME);
+	SETKEYMAP(KEY_END, SDLK_END);
+	SETKEYMAP(KEY_COMMA, SDLK_COMMA);
+	SETKEYMAP(KEY_SLASH, SDLK_SLASH);
+
+	SETKEYMAP(KEY_F1, SDLK_F1);
+	SETKEYMAP(KEY_F2, SDLK_F2);
+	SETKEYMAP(KEY_F3, SDLK_F3);
+	SETKEYMAP(KEY_F4, SDLK_F4);
+	SETKEYMAP(KEY_F5, SDLK_F5);
+	SETKEYMAP(KEY_F6, SDLK_F6);
+	SETKEYMAP(KEY_F7, SDLK_F7);
+	SETKEYMAP(KEY_F8, SDLK_F8);
+	SETKEYMAP(KEY_F9, SDLK_F9);
+	SETKEYMAP(KEY_F10, SDLK_F10);
+	SETKEYMAP(KEY_F11, SDLK_F11);
+	SETKEYMAP(KEY_F12, SDLK_F12);
+	SETKEYMAP(KEY_F13, SDLK_F13);
+	SETKEYMAP(KEY_F14, SDLK_F14);
+	SETKEYMAP(KEY_F15, SDLK_F15);
+
+	SETKEYMAP(KEY_ESCAPE, SDLK_ESCAPE);
+	//SETKEYMAP(KEY_ANYKEY, 4059);
+	//SETKEYMAP(KEY_MAXARRAY, SDLK_LAST+1
+
+	#undef SETKEYMAP
+
+	return _retval;
+}
+
+#if defined(BBGE_BUILD_SDL2)
+static int mapSDLKeyToGameKey(const SDL_Keycode val)
+#else
+static int mapSDLKeyToGameKey(const SDLKey val)
+#endif
+{
+	static sdlKeyMap *keymap = NULL;
+	if (keymap == NULL)
+		keymap = initSDLKeymap();
+
+	return (*keymap)[val];
+}
+#endif
+
+
 void Core::pollEvents()
 {
 #if defined(BBGE_BUILD_SDL)
@@ -3337,12 +3666,15 @@
 				if ((event.key.keysym.sym == SDLK_g) && (event.key.keysym.mod & KMOD_CTRL))
 				{
 					// toggle mouse grab with the magic hotkey.
+					#ifdef BBGE_BUILD_SDL2
+					SDL_SetWindowGrab(gScreen, SDL_GetWindowGrab(gScreen) ? SDL_FALSE : SDL_TRUE);
+					#else
 					SDL_WM_GrabInput((SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON) ? SDL_GRAB_OFF : SDL_GRAB_ON);
+					#endif
 				}
 				else if (_hasFocus)
 				{
-					int k = (int)event.key.keysym.sym;
-					keys[k] = 1;
+					keys[mapSDLKeyToGameKey(event.key.keysym.sym)] = 1;
 				}
 			}
 			break;
@@ -3351,8 +3683,7 @@
 			{
 				if (_hasFocus)
 				{
-					int k = (int)event.key.keysym.sym;
-					keys[k] = 0;
+					keys[mapSDLKeyToGameKey(event.key.keysym.sym)] = 0;
 				}
 			}
 			break;
@@ -3373,6 +3704,31 @@
 			}
 			break;
 
+			#ifdef BBGE_BUILD_SDL2
+			case SDL_WINDOWEVENT:
+			{
+				if (event.window.event == SDL_WINDOWEVENT_CLOSE)
+				{
+					SDL_Quit();
+					_exit(0);
+					//loopDone = true;
+					//quit();
+				}
+			}
+			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;
+			#else
 			case SDL_MOUSEBUTTONDOWN:
 			{
 				if (_hasFocus && updateMouse)
@@ -3406,6 +3762,7 @@
 				}
 			}
 			break;
+			#endif
 
 			case SDL_QUIT:
 				SDL_Quit();
@@ -4088,10 +4469,13 @@
 void Core::showBuffer()
 {
 	BBGE_PROF(Core_showBuffer);
-#ifdef BBGE_BUILD_SDL
+#ifdef BBGE_BUILD_SDL2
+	SDL_GL_SwapWindow(gScreen);
+#elif BBGE_BUILD_SDL
 	SDL_GL_SwapBuffers();
 	//glFlush();
 #endif
+
 #ifdef BBGE_BUILD_GLFW
 	glfwSwapBuffers();
 	//_glfwPlatSwapBuffers();
diff -ru a/BBGE/Core.h b/BBGE/Core.h
--- a/BBGE/Core.h	2013-07-16 22:08:25.603618702 -0400
+++ b/BBGE/Core.h	2013-02-28 22:11:02.888354000 -0500
@@ -281,122 +261,115 @@
 	KEY_LEFTARROW = GLFW_KEY_LEFT,
 	*/
 
-
-	KEY_LSUPER = SDLK_LSUPER,
-	KEY_RSUPER = SDLK_RSUPER,
-	KEY_LMETA = SDLK_LMETA,
-	KEY_RMETA = SDLK_RMETA,
-
-	KEY_BACKSPACE = SDLK_BACKSPACE,
-	KEY_PRINTSCREEN = SDLK_PRINT,
-
-	//KEY_CAPSLOCK = DIK_CAPSLOCK,
-	//KEY_CIRCUMFLEX = DIK_CIRCUMFLEX,
-	KEY_LALT = SDLK_LALT,
-	KEY_RALT = SDLK_RALT,
-	KEY_LSHIFT = SDLK_LSHIFT,
-	KEY_RSHIFT = SDLK_RSHIFT,
-	KEY_LCONTROL = SDLK_LCTRL,
-	KEY_RCONTROL = SDLK_RCTRL,
-	KEY_NUMPADMINUS = SDLK_KP_MINUS,
-	KEY_NUMPADPERIOD = SDLK_KP_PERIOD,
-	KEY_NUMPADPLUS = SDLK_KP_PLUS,
-	KEY_NUMPADSLASH = SDLK_KP_DIVIDE,
-	KEY_NUMPADSTAR = SDLK_KP_MULTIPLY,
-	KEY_PGDN = SDLK_PAGEDOWN,
-	KEY_PGUP = SDLK_PAGEUP,
-	KEY_APOSTROPHE = SDLK_QUOTE,
-	KEY_EQUALS = SDLK_EQUALS,
-	KEY_SEMICOLON = SDLK_SEMICOLON,
-	KEY_LBRACKET = SDLK_LEFTBRACKET,
-	KEY_RBRACKET = SDLK_RIGHTBRACKET,
-	//KEY_RALT = GLFW_KEY_RALT,
-	KEY_TILDE = SDLK_BACKQUOTE,
-    KEY_0 = SDLK_0,
-    KEY_1 = SDLK_1,
-    KEY_2 = SDLK_2,
-    KEY_3 = SDLK_3,
-    KEY_4 = SDLK_4,
-    KEY_5 = SDLK_5,
-    KEY_6 = SDLK_6,
-    KEY_7 = SDLK_7,
-    KEY_8 = SDLK_8,
-    KEY_9 = SDLK_9,
-    KEY_A = SDLK_a,
-	KEY_B = SDLK_b,
-	KEY_C = SDLK_c,
-	KEY_D = SDLK_d,
-	KEY_E = SDLK_e,
-	KEY_F = SDLK_f,
-	KEY_G = SDLK_g,
-	KEY_H = SDLK_h,
-	KEY_I = SDLK_i,
-	KEY_J = SDLK_j,
-	KEY_K = SDLK_k,
-	KEY_L = SDLK_l,
-	KEY_M = SDLK_m,
-	KEY_N = SDLK_n,
-	KEY_O = SDLK_o,
-	KEY_P = SDLK_p,
-	KEY_Q = SDLK_q,
-	KEY_R = SDLK_r,
-	KEY_S = SDLK_s,
-	KEY_T = SDLK_t,
-	KEY_U = SDLK_u,
-	KEY_V = SDLK_v,
-	KEY_W = SDLK_w,
-	KEY_X = SDLK_x,
-	KEY_Y = SDLK_y,
-	KEY_Z = SDLK_z,
-	
-	KEY_LEFT = SDLK_LEFT,
-	KEY_RIGHT = SDLK_RIGHT,
-	KEY_UP = SDLK_UP,
-	KEY_DOWN = SDLK_DOWN,
-
-	KEY_NUMPAD1 = SDLK_KP1,
-	KEY_NUMPAD2 = SDLK_KP2,
-	KEY_NUMPAD3 = SDLK_KP3,
-	KEY_NUMPAD4 = SDLK_KP4,
-	KEY_NUMPAD5 = SDLK_KP5,
-	KEY_NUMPAD6 = SDLK_KP6,
-	KEY_NUMPAD7 = SDLK_KP7,
-	KEY_NUMPAD8 = SDLK_KP8,
-	KEY_NUMPAD9 = SDLK_KP9,
-	KEY_NUMPAD0 = SDLK_KP0,
-
-	KEY_DELETE = SDLK_DELETE,
-	KEY_SPACE = SDLK_SPACE,
-	KEY_RETURN = SDLK_RETURN,
-	KEY_PERIOD = SDLK_PERIOD,
-	KEY_MINUS = SDLK_MINUS,
-	KEY_CAPSLOCK = SDLK_CAPSLOCK,
-	KEY_SYSRQ = SDLK_SYSREQ,
-	KEY_TAB = SDLK_TAB,
-	KEY_HOME = SDLK_HOME,
-	KEY_END = SDLK_END,
-	KEY_COMMA = SDLK_COMMA,
-	KEY_SLASH = SDLK_SLASH,
-
-    KEY_F1 = SDLK_F1,
-    KEY_F2 = SDLK_F2,
-    KEY_F3 = SDLK_F3,
-    KEY_F4 = SDLK_F4,
-    KEY_F5 = SDLK_F5,
-    KEY_F6 = SDLK_F6,
-    KEY_F7 = SDLK_F7,
-    KEY_F8 = SDLK_F8,
-    KEY_F9 = SDLK_F9,
-    KEY_F10 = SDLK_F10,
-    KEY_F11 = SDLK_F11,
-    KEY_F12 = SDLK_F12,
-    KEY_F13 = SDLK_F13,
-    KEY_F14 = SDLK_F14,
-    KEY_F15 = SDLK_F15,
-
-	KEY_ESCAPE = SDLK_ESCAPE,
-	KEY_ANYKEY = 4059,
-	KEY_MAXARRAY = SDLK_LAST+1
+	KEY_LSUPER,
+	KEY_RSUPER,
+	KEY_LMETA,
+	KEY_RMETA,
+	KEY_BACKSPACE,
+	KEY_PRINTSCREEN,
+
+	//KEY_CAPSLOCK,
+	//KEY_CIRCUMFLEX,
+	KEY_LALT,
+	KEY_RALT,
+	KEY_LSHIFT,
+	KEY_RSHIFT,
+	KEY_LCONTROL,
+	KEY_RCONTROL,
+	KEY_NUMPADMINUS,
+	KEY_NUMPADPERIOD,
+	KEY_NUMPADPLUS,
+	KEY_NUMPADSLASH,
+	KEY_NUMPADSTAR,
+	KEY_PGDN,
+	KEY_PGUP,
+	KEY_APOSTROPHE,
+	KEY_EQUALS,
+	KEY_SEMICOLON,
+	KEY_LBRACKET,
+	KEY_RBRACKET,
+	//KEY_RALT,
+	KEY_TILDE,
+    KEY_0,
+    KEY_1,
+    KEY_2,
+    KEY_3,
+    KEY_4,
+    KEY_5,
+    KEY_6,
+    KEY_7,
+    KEY_8,
+    KEY_9,
+    KEY_A,
+	KEY_B,
+	KEY_C,
+	KEY_D,
+	KEY_E,
+	KEY_F,
+	KEY_G,
+	KEY_H,
+	KEY_I,
+	KEY_J,
+	KEY_K,
+	KEY_L,
+	KEY_M,
+	KEY_N,
+	KEY_O,
+	KEY_P,
+	KEY_Q,
+	KEY_R,
+	KEY_S,
+	KEY_T,
+	KEY_U,
+	KEY_V,
+	KEY_W,
+	KEY_X,
+	KEY_Y,
+	KEY_Z,
+	KEY_LEFT,
+	KEY_RIGHT,
+	KEY_UP,
+	KEY_DOWN,
+	KEY_NUMPAD1,
+	KEY_NUMPAD2,
+	KEY_NUMPAD3,
+	KEY_NUMPAD4,
+	KEY_NUMPAD5,
+	KEY_NUMPAD6,
+	KEY_NUMPAD7,
+	KEY_NUMPAD8,
+	KEY_NUMPAD9,
+	KEY_NUMPAD0,
+	KEY_DELETE,
+	KEY_SPACE,
+	KEY_RETURN,
+	KEY_PERIOD,
+	KEY_MINUS,
+	KEY_CAPSLOCK,
+	KEY_SYSRQ,
+	KEY_TAB,
+	KEY_HOME,
+	KEY_END,
+	KEY_COMMA,
+	KEY_SLASH,
+    KEY_F1,
+    KEY_F2,
+    KEY_F3,
+    KEY_F4,
+    KEY_F5,
+    KEY_F6,
+    KEY_F7,
+    KEY_F8,
+    KEY_F9,
+    KEY_F10,
+    KEY_F11,
+    KEY_F12,
+    KEY_F13,
+    KEY_F14,
+    KEY_F15,
+	KEY_ESCAPE,
+	KEY_ANYKEY,
+	KEY_MAXARRAY
 };
 #elif defined(BBGE_BUILD_XINPUT)
 enum GameKeys
@@ -805,6 +778,10 @@
 	bool inited, xinited;
 	bool anyButton();
 #ifdef BBGE_BUILD_SDL
+#ifdef BBGE_BUILD_SDL2
+	SDL_GameController *sdl_controller;
+	SDL_Haptic *sdl_haptic;
+#endif
 	SDL_Joystick *sdl_joy;
 #endif
 	int stickIndex;
diff -ru a/BBGE/Joystick.cpp b/BBGE/Joystick.cpp
--- a/BBGE/Joystick.cpp	2013-07-16 22:08:25.663618708 -0400
+++ b/BBGE/Joystick.cpp	2013-03-01 01:12:52.568603000 -0500
@@ -71,7 +51,11 @@
 	xinited = false;
 	stickIndex = -1;
 #ifdef BBGE_BUILD_SDL
-	sdl_joy = 0;
+#ifdef BBGE_BUILD_SDL2
+	sdl_controller = NULL;
+	sdl_haptic = NULL;
+#endif
+	sdl_joy = NULL;
 #endif
 	inited = false;
 	for (int i = 0; i < maxJoyBtns; i++)
@@ -99,17 +83,49 @@
 {
 #ifdef BBGE_BUILD_SDL
 	stickIndex = stick;
-	int numJoy = SDL_NumJoysticks();
+	const int numJoy = SDL_NumJoysticks();
 	std::ostringstream os;
 	os << "Found [" << numJoy << "] joysticks";
 	debugLog(os.str());
+
 	if (numJoy > stick)
 	{
-		sdl_joy = SDL_JoystickOpen(stick);
+		#ifdef BBGE_BUILD_SDL2
+		if (SDL_IsGameController(stick))
+		{
+			sdl_controller = SDL_GameControllerOpen(stick);
+			if (sdl_controller)
+				sdl_joy = SDL_GameControllerGetJoystick(sdl_controller);
+		}
+		if (!sdl_joy)
+			sdl_joy = SDL_JoystickOpen(stick);
+		if (sdl_joy && SDL_JoystickIsHaptic(sdl_joy))
+		{
+			sdl_haptic = SDL_HapticOpenFromJoystick(sdl_joy);
+			bool rumbleok = false;
+			if (sdl_haptic && SDL_HapticRumbleSupported(sdl_haptic))
+				rumbleok = (SDL_HapticRumbleInit(sdl_haptic) == 0);
+			if (!rumbleok)
+			{
+				SDL_HapticClose(sdl_haptic);
+				sdl_haptic = NULL;
+			}
+		}
+		#endif
+
+		if (!sdl_joy)
+			sdl_joy = SDL_JoystickOpen(stick);
+
 		if (sdl_joy)
 		{
 			inited = true;
+			#ifdef BBGE_BUILD_SDL2
+			debugLog(std::string("Initialized Joystick [") + std::string(SDL_JoystickName(sdl_joy)) + std::string("]"));
+			if (sdl_controller) debugLog(std::string("Joystick is a Game Controller"));
+			if (sdl_haptic) debugLog(std::string("Joystick has force feedback support"));
+			#else
 			debugLog(std::string("Initialized Joystick [") + std::string(SDL_JoystickName(stick)) + std::string("]"));
+			#endif
 		}
 		else
 		{
@@ -144,7 +160,17 @@
 {
 	if (core->joystickEnabled && inited)
 	{
-#if defined(BBGE_BUILD_WINDOWS) && defined(BBGE_BUILD_XINPUT)
+#ifdef BBGE_BUILD_SDL2
+		if (sdl_haptic)
+		{
+			const float power = (leftMotor + rightMotor) / 2.0f;
+			if ((power > 0.0f) && (time > 0.0f))
+				SDL_HapticRumblePlay(sdl_haptic, power, (Uint32) (time * 1000.0f));
+			else
+				SDL_HapticRumbleStop(sdl_haptic);
+		}
+
+#elif defined(BBGE_BUILD_WINDOWS) && defined(BBGE_BUILD_XINPUT)
 		XINPUT_VIBRATION vib;
 		vib.wLeftMotorSpeed = WORD(leftMotor*65535);
 		vib.wRightMotorSpeed = WORD(rightMotor*65535);
@@ -201,11 +227,52 @@
 {	
 #ifdef BBGE_BUILD_SDL
 	if (core->joystickEnabled && inited && sdl_joy && stickIndex != -1)
-	{	
+	{
+#ifdef BBGE_BUILD_SDL2
+		if (!SDL_JoystickGetAttached(sdl_joy))
+		{
+			debugLog("Lost Joystick");
+			if (sdl_haptic) { SDL_HapticClose(sdl_haptic); sdl_haptic = NULL; }
+			if (!sdl_controller)
+				SDL_JoystickClose(sdl_joy);
+			else
+			{
+				SDL_GameControllerClose(sdl_controller);
+				sdl_controller = NULL;
+			}
+			sdl_joy = NULL;
+			return;
+		}
+
+		if (sdl_controller)
+		{
+			Sint16 xaxis = SDL_GameControllerGetAxis(sdl_controller, SDL_CONTROLLER_AXIS_LEFTX);
+			Sint16 yaxis = SDL_GameControllerGetAxis(sdl_controller, SDL_CONTROLLER_AXIS_LEFTY);
+			position.x = double(xaxis)/32768.0;
+			position.y = double(yaxis)/32768.0;
+
+			Sint16 xaxis2 = SDL_GameControllerGetAxis(sdl_controller, SDL_CONTROLLER_AXIS_RIGHTX);
+			Sint16 yaxis2 = SDL_GameControllerGetAxis(sdl_controller, SDL_CONTROLLER_AXIS_RIGHTY);
+			rightStick.x = double(xaxis2)/32768.0;
+			rightStick.y = double(yaxis2)/32768.0;
+		}
+		else
+		{
+			Sint16 xaxis = SDL_JoystickGetAxis(sdl_joy, s1ax);
+			Sint16 yaxis = SDL_JoystickGetAxis(sdl_joy, s1ay);
+			position.x = double(xaxis)/32768.0;
+			position.y = double(yaxis)/32768.0;
+
+			Sint16 xaxis2 = SDL_JoystickGetAxis(sdl_joy, s2ax);
+			Sint16 yaxis2 = SDL_JoystickGetAxis(sdl_joy, s2ay);
+			rightStick.x = double(xaxis2)/32768.0;
+			rightStick.y = double(yaxis2)/32768.0;
+		}
+#else
 		if (!SDL_JoystickOpened(stickIndex))
 		{
 			debugLog("Lost Joystick");
-			sdl_joy = 0;
+			sdl_joy = NULL;
 			return;
 		}
 
@@ -214,11 +281,11 @@
 		position.x = double(xaxis)/32768.0;
 		position.y = double(yaxis)/32768.0;
 
-
 		Sint16 xaxis2 = SDL_JoystickGetAxis(sdl_joy, s2ax);
 		Sint16 yaxis2 = SDL_JoystickGetAxis(sdl_joy, s2ay);
 		rightStick.x = double(xaxis2)/32768.0;
 		rightStick.y = double(yaxis2)/32768.0;
+#endif
 
 		/*
 		std::ostringstream os;
@@ -237,8 +304,23 @@
 		os2 << "joy2(" << position.x << ", " << position.y << ")";
 		debugLog(os2.str());
 		*/
+#ifdef BBGE_BUILD_SDL2
+		if (sdl_controller)
+		{
+			for (int i = 0; i < SDL_CONTROLLER_BUTTON_MAX; i++)
+				buttons[i] = SDL_GameControllerGetButton(sdl_controller, (SDL_GameControllerButton)i)?DOWN:UP;
+			for (int i = SDL_CONTROLLER_BUTTON_MAX; i < maxJoyBtns; i++)
+				buttons[i] = UP;
+		}
+		else
+		{
+			for (int i = 0; i < maxJoyBtns; i++)
+				buttons[i] = SDL_JoystickGetButton(sdl_joy, i)?DOWN:UP;
+		}
+#else
 		for (int i = 0; i < maxJoyBtns; i++)
 			buttons[i] = SDL_JoystickGetButton(sdl_joy, i)?DOWN:UP;
+#endif
 		/*
 		unsigned char btns[maxJoyBtns];
 		glfwGetJoystickButtons(GLFW_JOYSTICK_1, btns, maxJoyBtns);
diff -ru a/CMakeLists.txt b/CMakeLists.txt
--- a/CMakeLists.txt	2013-07-16 23:52:49.551761632 -0400
+++ b/CMakeLists.txt	2013-07-16 22:08:26.743618731 -0400
@@ -18,6 +18,8 @@
     ADD_DEFINITIONS(-DBETAEXPIRE=${BUILD_TIMESTAMP})
 ENDIF(AQUARIA_BETAEXPIRE)
 
+OPTION(AQUARIA_USE_SDL2 "Use SDL2" TRUE)
+
 # No Steamworks SDK for Linux at the moment. Roll our own achievements.
 ADD_DEFINITIONS(-DBBGE_BUILD_ACHIEVEMENTS_INTERNAL=1)
 
@@ -26,6 +28,7 @@
 SET(LUADIR ${CMAKE_CURRENT_SOURCE_DIR}/Aquaria/lua-5.1.4)
 SET(LUASRCDIR ${LUADIR}/src)
 SET(SDL12DIR ${BBGEDIR}/SDL12)
+SET(SDL2DIR ${BBGEDIR}/SDL2)
 SET(FTGLDIR ${BBGEDIR}/FTGL)
 SET(FTGLSRCDIR ${FTGLDIR}/src)
 SET(FREETYPE2DIR ${BBGEDIR}/freetype2)
@@ -37,7 +40,11 @@
 INCLUDE_DIRECTORIES(${BBGEDIR}/GL)
 INCLUDE_DIRECTORIES(${BBGEDIR}/glpng/zlib)
 INCLUDE_DIRECTORIES(${SRCDIR})
+IF(AQUARIA_USE_SDL2)
+INCLUDE_DIRECTORIES(${SDL2DIR}/include)
+ELSE(AQUARIA_USE_SDL2)
 INCLUDE_DIRECTORIES(${SDL12DIR}/include)
+ENDIF(AQUARIA_USE_SDL2)
 INCLUDE_DIRECTORIES(${LUASRCDIR})
 INCLUDE_DIRECTORIES(${FTGLDIR}/include)
 INCLUDE_DIRECTORIES(${FREETYPE2DIR}/include)
@@ -74,6 +81,10 @@
     ADD_DEFINITIONS(-DBBGE_BUILD_WINDOWS=1)
 ENDIF(MSVC)
 
+IF(AQUARIA_USE_SDL2)
+    ADD_DEFINITIONS(-DBBGE_BUILD_SDL2=1)
+ENDIF(AQUARIA_USE_SDL2)
+
 
 # Build Lua with Unix _setjmp/_longjmp support.
 ADD_DEFINITIONS(-DLUA_USE_ULONGJMP=1)
@@ -398,14 +409,24 @@
     SET(OPTIONAL_LIBS ${OPTIONAL_LIBS} "-framework Cocoa")
     SET(OPTIONAL_LIBS ${OPTIONAL_LIBS} "-framework OpenGL")
     SET(OPTIONAL_LIBS ${OPTIONAL_LIBS} "-framework OpenAL")
-    SET(OPTIONAL_LIBS ${OPTIONAL_LIBS} "${SDL12DIR}/lib/macosx/libSDL-1.2.0.dylib")
-    SET(OPTIONAL_LIBS ${OPTIONAL_LIBS} "${SDL12DIR}/lib/macosx/libSDLmain.a")
+    IF(AQUARIA_USE_SDL2)
+        SET(OPTIONAL_LIBS ${OPTIONAL_LIBS} "${SDL2DIR}/lib/macosx/libSDL2-2.0.0.dylib")
+    ELSE(AQUARIA_USE_SDL2)
+        SET(OPTIONAL_LIBS ${OPTIONAL_LIBS} "${SDL12DIR}/lib/macosx/libSDL-1.2.0.dylib")
+        SET(OPTIONAL_LIBS ${OPTIONAL_LIBS} "${SDL12DIR}/lib/macosx/libSDLmain.a")
+    ENDIF(AQUARIA_USE_SDL2)
     TARGET_LINK_LIBRARIES(aquaria ${OPTIONAL_LIBS})
 ENDIF(MACOSX)
 
 IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
     # !!! FIXME: amd64 someday?
-    TARGET_LINK_LIBRARIES(aquaria ${SDL12DIR}/lib/linux-x86/libSDL-1.2.so.0 ${BBGEDIR}/AL/lib/linux-x86/libopenal.so.1)
+    IF(AQUARIA_USE_SDL2)
+        TARGET_LINK_LIBRARIES(aquaria "${SDL2DIR}/lib/linux-x86/libSDL2-2.0.so.0")
+    ELSE(AQUARIA_USE_SDL2)
+        TARGET_LINK_LIBRARIES(aquaria "${SDL12DIR}/lib/linux-x86/libSDL-1.2.so.0")
+    ENDIF(AQUARIA_USE_SDL2)
+
+    TARGET_LINK_LIBRARIES(aquaria "${BBGEDIR}/AL/lib/linux-x86/libopenal.so.1")
     SET_TARGET_PROPERTIES(aquaria PROPERTIES LINK_FLAGS "-Wl,-rpath,$ORIGIN")
 ENDIF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
 


More information about the aquaria mailing list