[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [20474] trunk/blender/source: [#18840] Joystick sensor lag

Campbell Barton ideasman42 at gmail.com
Thu May 28 15:44:33 CEST 2009


Revision: 20474
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=20474
Author:   campbellbarton
Date:     2009-05-28 15:44:32 +0200 (Thu, 28 May 2009)

Log Message:
-----------
[#18840] Joystick sensor lag
if(SDL_PollEvent(&sdl_event)) // if -> while fixed it
removed 'm_buttonnum' was misleading, wasn't used as you expect.

Added gravity to variable to world to be used by collada.

Modified Paths:
--------------
    trunk/blender/source/blender/python/api2_2x/World.c
    trunk/blender/source/blender/python/api2_2x/doc/World.py
    trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp
    trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.h
    trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h
    trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp

Modified: trunk/blender/source/blender/python/api2_2x/World.c
===================================================================
--- trunk/blender/source/blender/python/api2_2x/World.c	2009-05-28 11:04:45 UTC (rev 20473)
+++ trunk/blender/source/blender/python/api2_2x/World.c	2009-05-28 13:44:32 UTC (rev 20474)
@@ -106,6 +106,8 @@
 static PyObject *World_setCurrent( BPy_World * self );
 static PyObject *World_getTextures( BPy_World * self );
 static int 		 World_setTextures( BPy_World * self, PyObject * value );
+static PyObject *World_getGravity( BPy_World * self );
+static int 	  World_setGravity( BPy_World * self, PyObject * value );
 static PyObject *World_copy( BPy_World * self );
 
 
@@ -260,6 +262,9 @@
     {"textures", (getter)World_getTextures, (setter)World_setTextures,
      "The World's texture list as a tuple",
      NULL},
+    {"gravity", (getter)World_getGravity, (setter)World_setGravity,
+     "Physics gravity setting",
+     NULL},
 	{NULL,NULL,NULL,NULL,NULL}  /* Sentinel */
 };
 
@@ -1136,3 +1141,21 @@
 	Py_DECREF(value);
 	return 0;
 }
+
+static PyObject *World_getGravity( BPy_World * self )
+{
+	return PyFloat_FromDouble(self->world->gravity);
+}
+
+static int World_setGravity( BPy_World * self, PyObject * value )
+{
+	float f = PyFloat_AsDouble(value);
+	if (f==-1 && PyErr_Occurred())
+		return EXPP_ReturnIntError( PyExc_TypeError, "expected a float or int" );
+	
+	if (f<0.0f)f= 0.0f;
+	else if (f>25.0f)f= 25.0f;
+	
+	self->world->gravity= f;
+	return 0;
+}

Modified: trunk/blender/source/blender/python/api2_2x/doc/World.py
===================================================================
--- trunk/blender/source/blender/python/api2_2x/doc/World.py	2009-05-28 11:04:45 UTC (rev 20473)
+++ trunk/blender/source/blender/python/api2_2x/doc/World.py	2009-05-28 13:44:32 UTC (rev 20474)
@@ -84,6 +84,8 @@
 	@ivar ipo: The world type ipo linked to this world object.
 	@type textures: a tuple of Blender MTex objects.
 	@ivar textures: The World's texture list.  Empty texture channels contains None.
+	@type gravity: float
+	@ivar gravity: World physics gravity setting between 0.0 and 25.0
 	"""
 
 	def getRange():

Modified: trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp	2009-05-28 11:04:45 UTC (rev 20473)
+++ trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp	2009-05-28 13:44:32 UTC (rev 20474)
@@ -38,7 +38,6 @@
 	:
 	m_joyindex(index),
 	m_prec(3200),
-	m_buttonnum(-2),
 	m_axismax(-1),
 	m_buttonmax(-1),
 	m_hatmax(-1),
@@ -68,6 +67,7 @@
 }
 
 SCA_Joystick *SCA_Joystick::m_instance[JOYINDEX_MAX];
+int SCA_Joystick::m_joynum = 0;
 int SCA_Joystick::m_refCount = 0;
 
 SCA_Joystick *SCA_Joystick::GetInstance( short int joyindex )
@@ -88,6 +88,9 @@
 			echo("Error-Initializing-SDL: " << SDL_GetError());
 			return NULL;
 		}
+		
+		m_joynum = SDL_NumJoysticks();
+		
 		for (i=0; i<JOYINDEX_MAX; i++) {
 			m_instance[i] = new SCA_Joystick(i);
 			m_instance[i]->CreateJoystickDevice();
@@ -155,14 +158,15 @@
 
 bool SCA_Joystick::aAnyButtonPressIsPositive(void)
 {
-	return (m_buttonnum==-2) ? false : true;
+	/* this is needed for the "all events" option
+	 * so we know if there are no buttons pressed */
+	for (int i=0; i<m_buttonmax; i++)
+		if (SDL_JoystickGetButton(m_private->m_joystick, i))
+			return true;
+		
+	return false;
 }
 
-bool SCA_Joystick::aAnyButtonReleaseIsPositive(void)
-{
-	return (m_buttonnum==-2) ? true : false;
-}
-
 bool SCA_Joystick::aButtonPressIsPositive(int button)
 {
 #ifdef DISABLE_SDL
@@ -217,7 +221,7 @@
 	return false;
 #else
 	if(m_isinit == false){
-		if (m_joyindex>=SDL_NumJoysticks()) {
+		if (m_joyindex>=m_joynum) {
 			// don't print a message, because this is done anyway
 			//echo("Joystick-Error: " << SDL_NumJoysticks() << " avaiable joystick(s)");
 			

Modified: trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.h
===================================================================
--- trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.h	2009-05-28 11:04:45 UTC (rev 20473)
+++ trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.h	2009-05-28 13:44:32 UTC (rev 20474)
@@ -44,6 +44,7 @@
 
 {
 	static SCA_Joystick *m_instance[JOYINDEX_MAX];
+	static int m_joynum;
 	static int m_refCount;
 
 	class PrivateData;
@@ -67,11 +68,6 @@
 	 */
 	int 			m_prec;
 
-	/* 
-	 *	button values stored here 
-	 */
-	int 			m_buttonnum;
-
 	/*
 	 * max # of buttons avail
 	*/
@@ -146,7 +142,6 @@
 	bool aAxisIsPositive(int axis_single); /* check a single axis only */
 
 	bool aAnyButtonPressIsPositive(void);
-	bool aAnyButtonReleaseIsPositive(void);
 	bool aButtonPressIsPositive(int button);
 	bool aButtonReleaseIsPositive(int button);
 	bool aHatIsPositive(int hatnum, int dir);
@@ -160,10 +155,6 @@
 	int GetAxisPosition(int index){
 		return m_axis_array[index];
 	}
-	
-	int GetButton(void){
-		return m_buttonnum;
-	}
 
 	int GetHat(int index){
 		return m_hat_array[index];

Modified: trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h
===================================================================
--- trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h	2009-05-28 11:04:45 UTC (rev 20473)
+++ trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h	2009-05-28 13:44:32 UTC (rev 20474)
@@ -40,6 +40,7 @@
 
 #define JOYINDEX_MAX			8
 #define JOYAXIS_MAX				16
+#define JOYBUT_MAX				18
 #define JOYHAT_MAX				4
 
 #define JOYAXIS_RIGHT		0

Modified: trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp	2009-05-28 11:04:45 UTC (rev 20473)
+++ trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp	2009-05-28 13:44:32 UTC (rev 20474)
@@ -42,7 +42,7 @@
 	m_istrig_axis = 1;
 }
 
-
+/* See notes below in the event loop */
 void SCA_Joystick::OnHatMotion(SDL_Event* sdl_event)
 {
 	if(sdl_event->jhat.hat >= JOYAXIS_MAX)
@@ -52,30 +52,20 @@
 	m_istrig_hat = 1;
 }
 
+/* See notes below in the event loop */
 void SCA_Joystick::OnButtonUp(SDL_Event* sdl_event)
 {
 	m_istrig_button = 1;
-	
-	/* this is needed for the "all events" option
-	 * so we know if there are no buttons pressed */
-	int i;
-	for (i=0; i<m_buttonmax; i++) {
-		if (SDL_JoystickGetButton(m_private->m_joystick, i)) {
-			m_buttonnum = i;
-			return;
-		}
-	}
-	m_buttonnum = -2;
 }
 
 
 void SCA_Joystick::OnButtonDown(SDL_Event* sdl_event)
 {
-	if(sdl_event->jbutton.button <= m_buttonmax) /* unsigned int so always above 0 */
-	{
-		m_istrig_button = 1;
-		m_buttonnum = sdl_event->jbutton.button;
-	}
+	//if(sdl_event->jbutton.button > m_buttonmax) /* unsigned int so always above 0 */
+	//	return;
+	// sdl_event->jbutton.button;
+	
+	m_istrig_button = 1;
 }
 
 
@@ -84,22 +74,27 @@
 	m_istrig_axis = m_istrig_button = m_istrig_hat = 0;
 }
 
-/* only handle events for 1 joystick */
-
 void SCA_Joystick::HandleEvents(void)
 {
 	SDL_Event		sdl_event;
 	
 	int i;
-	for (i=0; i<JOYINDEX_MAX; i++) {
+	for (i=0; i<m_joynum; i++) { /* could use JOYINDEX_MAX but no reason to */
 		if(SCA_Joystick::m_instance[i])
 			SCA_Joystick::m_instance[i]->OnNothing(&sdl_event);
 	}
 	
-	if(SDL_PollEvent(&sdl_event))
+	while(SDL_PollEvent(&sdl_event))
 	{
 		/* Note! m_instance[sdl_event.jaxis.which]
 		 * will segfault if over JOYINDEX_MAX, not too nice but what are the chances? */
+		
+		/* Note!, with buttons, this wont care which button is pressed,
+		 * only to set 'm_istrig_button', actual pressed buttons are detected by SDL_JoystickGetButton */
+		
+		/* Note!, if you manage to press and release a button within 1 logic tick
+		 * it wont work as it should */
+		
 		switch(sdl_event.type)
 		{
 		case SDL_JOYAXISMOTION:





More information about the Bf-blender-cvs mailing list