Просмотр исходного кода

Added joystick support in Linux SDL core, added joystick event functions to polycode player

Ivan Safrin 12 лет назад
Родитель
Сommit
00ca758c8c

+ 9 - 0
Bindings/Contents/LUA/API/defaults.lua

@@ -117,6 +117,15 @@ end
 function onMouseMove(x,y)
 function onMouseMove(x,y)
 end
 end
 
 
+function onJoystickButtonDown(id, button)
+end
+
+function onJoystickButtonUp(id, button)
+end
+
+function onJoystickAxisMoved(id, axis, value)
+end
+
 function Update(e)
 function Update(e)
 end
 end
 
 

+ 27 - 4
Core/Contents/Source/PolySDLCore.cpp

@@ -75,7 +75,7 @@ SDLCore::SDLCore(PolycodeView *view, int _xRes, int _yRes, bool fullScreen, bool
 		setenv("SDL_VIDEO_CENTERED", "1", 1);
 		setenv("SDL_VIDEO_CENTERED", "1", 1);
 	}
 	}
 
 
-	if(SDL_Init(SDL_INIT_VIDEO) < 0) {
+	if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0) {
 	}
 	}
 	
 	
 	eventMutex = createMutex();
 	eventMutex = createMutex();
@@ -88,6 +88,15 @@ SDLCore::SDLCore(PolycodeView *view, int _xRes, int _yRes, bool fullScreen, bool
 	SDL_EnableUNICODE(1);
 	SDL_EnableUNICODE(1);
 	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
 	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
 	
 	
+	SDL_JoystickEventState(SDL_ENABLE);
+	
+	int numJoysticks = SDL_NumJoysticks();
+	
+	for(int i=0; i < numJoysticks; i++) {
+		SDL_JoystickOpen(i);
+		input->addJoystick(i);
+	}
+
 	((OpenGLRenderer*)renderer)->initOSSpecific();
 	((OpenGLRenderer*)renderer)->initOSSpecific();
 	CoreServices::getInstance()->installModule(new GLSLShaderModule());	
 	CoreServices::getInstance()->installModule(new GLSLShaderModule());	
 }
 }
@@ -113,8 +122,6 @@ void SDLCore::setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int
 		SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 0);
 		SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 0);
 	}
 	}
 	
 	
-	SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
-	
 	flags = SDL_OPENGL;
 	flags = SDL_OPENGL;
 
 
 	if(fullScreen) {
 	if(fullScreen) {
@@ -124,6 +131,16 @@ void SDLCore::setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int
 	if(resizableWindow) {
 	if(resizableWindow) {
 		flags |= SDL_RESIZABLE;
 		flags |= SDL_RESIZABLE;
 	}
 	}
+
+	if(vSync) {
+		flags |= SDL_DOUBLEBUF;
+		SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
+		SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
+	} else {
+		SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
+		SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
+	}
+
 	SDL_SetVideoMode(xRes, yRes, 0, flags);
 	SDL_SetVideoMode(xRes, yRes, 0, flags);
 	
 	
 	renderer->Resize(xRes, yRes);
 	renderer->Resize(xRes, yRes);
@@ -295,8 +312,14 @@ bool SDLCore::Update() {
 						}
 						}
 					}
 					}
 				break;
 				break;
+				case SDL_JOYAXISMOTION:
+					input->joystickAxisMoved(event.jaxis.axis, ((Number)event.jaxis.value)/32767.0, event.jaxis.which);
+				break;
 				case SDL_JOYBUTTONDOWN:
 				case SDL_JOYBUTTONDOWN:
-//					input->setKeyState((PolyKEY)(event.key.keysym.sym), true);
+					input->joystickButtonDown(event.jbutton.button, event.jbutton.which);
+				break;
+				case SDL_JOYBUTTONUP:
+					input->joystickButtonUp(event.jbutton.button, event.jbutton.which);
 				break;
 				break;
 				case SDL_KEYDOWN:
 				case SDL_KEYDOWN:
 					if(!checkSpecialKeyEvents((PolyKEY)(event.key.keysym.sym))) {
 					if(!checkSpecialKeyEvents((PolyKEY)(event.key.keysym.sym))) {

+ 43 - 0
Player/Contents/Source/PolycodePlayer.cpp

@@ -677,6 +677,9 @@ void PolycodePlayer::loadFile(const char *fileName) {
 	core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
 	core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
 	core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
 	core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
 	core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEUP);
 	core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEUP);
+	core->getInput()->addEventListener(this, InputEvent::EVENT_JOYBUTTON_DOWN);
+	core->getInput()->addEventListener(this, InputEvent::EVENT_JOYBUTTON_UP);
+	core->getInput()->addEventListener(this, InputEvent::EVENT_JOYAXIS_MOVED);
 					
 					
 	if(nameString == "") {
 	if(nameString == "") {
 		return;
 		return;
@@ -890,6 +893,46 @@ void PolycodePlayer::handleEvent(Event *event) {
 				}
 				}
 			}
 			}
 			break;																			
 			break;																			
+			case InputEvent::EVENT_JOYBUTTON_DOWN:
+			{
+				if(L && !crashed) {
+					lua_getfield (L, LUA_GLOBALSINDEX, "__customError");
+					errH = lua_gettop(L);									
+					lua_getfield(L, LUA_GLOBALSINDEX, "onJoystickButtonDown");
+					lua_pushnumber(L, inputEvent->joystickIndex);
+					lua_pushnumber(L, inputEvent->joystickButton);
+					lua_pcall(L, 2,0,errH);	
+					lua_settop(L, 0);					
+				}
+			}
+			break;																			
+			case InputEvent::EVENT_JOYBUTTON_UP:
+			{
+				if(L && !crashed) {
+					lua_getfield (L, LUA_GLOBALSINDEX, "__customError");
+					errH = lua_gettop(L);									
+					lua_getfield(L, LUA_GLOBALSINDEX, "onJoystickButtonUp");
+					lua_pushnumber(L, inputEvent->joystickIndex);
+					lua_pushnumber(L, inputEvent->joystickButton);
+					lua_pcall(L, 2,0,errH);	
+					lua_settop(L, 0);
+				}
+			}
+			break;																			
+			case InputEvent::EVENT_JOYAXIS_MOVED:
+			{
+				if(L && !crashed) {
+					lua_getfield (L, LUA_GLOBALSINDEX, "__customError");
+					errH = lua_gettop(L);									
+					lua_getfield(L, LUA_GLOBALSINDEX, "onJoystickAxisMoved");
+					lua_pushnumber(L, inputEvent->joystickIndex);
+					lua_pushnumber(L, inputEvent->joystickAxis);
+					lua_pushnumber(L, inputEvent->joystickAxisValue);
+					lua_pcall(L, 3,0,errH);	
+					lua_settop(L, 0);
+				}
+			}
+			break;																			
 		}
 		}
 	}
 	}
 }
 }