[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [16136] trunk/blender: BGE patch: New Delay sensor (derived from patch #17472)

Benoit Bolsee benoit.bolsee at online.be
Sat Aug 16 22:45:40 CEST 2008


Revision: 16136
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=16136
Author:   ben2610
Date:     2008-08-16 22:45:37 +0200 (Sat, 16 Aug 2008)

Log Message:
-----------
BGE patch: New Delay sensor (derived from patch #17472) 

Introduction of a new Delay sensor that can be used to 
generate positive and negative triggers at precise time,
expressed in number of frames. 
The delay parameter defines the length of the initial 
OFF period. A positive trigger is generated at the end
of this period. The duration parameter defines the 
length of the ON period following the OFF period.
A negative trigger is generated at the end of the ON period. 
If duration is 0, the sensor stays ON and there is no 
negative trigger. 
The sensor runs the OFF-ON cycle once unless the repeat 
option is set: the OFF-ON cycle repeats indefinately 
(or the OFF cycle if duration is 0).

The new generic SCA_ISensor::reset() Python function
can be used at any time to restart the sensor: the
current cycle is interrupted and no trigger is generated.

Modified Paths:
--------------
    trunk/blender/projectfiles_vc7/gameengine/gamelogic/SCA_GameLogic.vcproj
    trunk/blender/source/blender/blenkernel/intern/sca.c
    trunk/blender/source/blender/blenloader/intern/writefile.c
    trunk/blender/source/blender/makesdna/DNA_sensor_types.h
    trunk/blender/source/blender/src/buttons_logic.c
    trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_ISensor.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_ISensor.h
    trunk/blender/source/gameengine/PyDoc/GameLogic.py
    trunk/blender/source/gameengine/PyDoc/SCA_ISensor.py

Added Paths:
-----------
    trunk/blender/source/gameengine/GameLogic/SCA_DelaySensor.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_DelaySensor.h
    trunk/blender/source/gameengine/PyDoc/SCA_DelaySensor.py

Modified: trunk/blender/projectfiles_vc7/gameengine/gamelogic/SCA_GameLogic.vcproj
===================================================================
--- trunk/blender/projectfiles_vc7/gameengine/gamelogic/SCA_GameLogic.vcproj	2008-08-16 20:31:38 UTC (rev 16135)
+++ trunk/blender/projectfiles_vc7/gameengine/gamelogic/SCA_GameLogic.vcproj	2008-08-16 20:45:37 UTC (rev 16136)
@@ -348,6 +348,9 @@
 				RelativePath="..\..\..\source\gameengine\GameLogic\SCA_ANDController.cpp">
 			</File>
 			<File
+				RelativePath="..\..\..\source\gameengine\GameLogic\SCA_DelaySensor.cpp">
+			</File>
+			<File
 				RelativePath="..\..\..\source\gameengine\GameLogic\SCA_EventManager.cpp">
 			</File>
 			<File
@@ -466,6 +469,9 @@
 				RelativePath="..\..\..\source\gameengine\GameLogic\SCA_ANDController.h">
 			</File>
 			<File
+				RelativePath="..\..\..\source\gameengine\GameLogic\SCA_DelaySensor.h">
+			</File>
+			<File
 				RelativePath="..\..\..\source\gameengine\GameLogic\SCA_EventManager.h">
 			</File>
 			<File

Modified: trunk/blender/source/blender/blenkernel/intern/sca.c
===================================================================
--- trunk/blender/source/blender/blenkernel/intern/sca.c	2008-08-16 20:31:38 UTC (rev 16135)
+++ trunk/blender/source/blender/blenkernel/intern/sca.c	2008-08-16 20:45:37 UTC (rev 16136)
@@ -153,6 +153,9 @@
 	case SENS_ACTUATOR:
 		sens->data= MEM_callocN(sizeof(bActuatorSensor), "actsens");
 		break;
+	case SENS_DELAY:
+		sens->data= MEM_callocN(sizeof(bDelaySensor), "delaysens");
+		break;
 	case SENS_MOUSE:
 		ms=sens->data= MEM_callocN(sizeof(bMouseSensor), "mousesens");
 		ms->type= LEFTMOUSE;

Modified: trunk/blender/source/blender/blenloader/intern/writefile.c
===================================================================
--- trunk/blender/source/blender/blenloader/intern/writefile.c	2008-08-16 20:31:38 UTC (rev 16135)
+++ trunk/blender/source/blender/blenloader/intern/writefile.c	2008-08-16 20:45:37 UTC (rev 16136)
@@ -608,6 +608,9 @@
 		case SENS_ACTUATOR:
 			writestruct(wd, DATA, "bActuatorSensor", 1, sens->data);
 			break;
+		case SENS_DELAY:
+			writestruct(wd, DATA, "bDelaySensor", 1, sens->data);
+			break;
 		case SENS_COLLISION:
 			writestruct(wd, DATA, "bCollisionSensor", 1, sens->data);
 			break;

Modified: trunk/blender/source/blender/makesdna/DNA_sensor_types.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_sensor_types.h	2008-08-16 20:31:38 UTC (rev 16135)
+++ trunk/blender/source/blender/makesdna/DNA_sensor_types.h	2008-08-16 20:45:37 UTC (rev 16136)
@@ -88,6 +88,13 @@
 	char name[32];
 } bActuatorSensor;
 
+typedef struct bDelaySensor {
+    short delay;
+	short duration;
+	short flag;
+	short pad;
+} bDelaySensor;
+
 typedef struct bCollisionSensor {
 	char name[32];          /* property name */
 	char materialName[32];  /* material      */
@@ -204,6 +211,7 @@
 #define SENS_MESSAGE   10
 #define SENS_JOYSTICK  11
 #define SENS_ACTUATOR  12
+#define SENS_DELAY     13
 /* sensor->flag */
 #define SENS_SHOW		1
 #define SENS_DEL		2
@@ -254,5 +262,7 @@
 #define SENS_JOY_HAT			2
 #define SENS_JOY_HAT_DIR		0
 
+#define SENS_DELAY_REPEAT		1
+
 #endif
 

Modified: trunk/blender/source/blender/src/buttons_logic.c
===================================================================
--- trunk/blender/source/blender/src/buttons_logic.c	2008-08-16 20:31:38 UTC (rev 16135)
+++ trunk/blender/source/blender/src/buttons_logic.c	2008-08-16 20:45:37 UTC (rev 16136)
@@ -683,6 +683,8 @@
 		return "Property";
 	case SENS_ACTUATOR:
 		return "Actuator";
+	case SENS_DELAY:
+		return "Delay";
 	case SENS_MOUSE:
 		return "Mouse";
 	case SENS_COLLISION:
@@ -704,7 +706,7 @@
 static char *sensor_pup(void)
 {
 	/* the number needs to match defines in game.h */
-	return "Sensors %t|Always %x0|Keyboard %x3|Mouse %x5|"
+	return "Sensors %t|Always %x0|Delay %x13|Keyboard %x3|Mouse %x5|"
 		"Touch %x1|Collision %x6|Near %x2|Radar %x7|"
 		"Property %x4|Random %x8|Ray %x9|Message %x10|Joystick %x11|Actuator %x12";
 }
@@ -1000,6 +1002,7 @@
 {
 	switch(type) {
 	case SENS_ALWAYS:		return TH_BUT_ACTION;
+	case SENS_DELAY:		return TH_BUT_ACTION;
 	case SENS_TOUCH:		return TH_BUT_NEUTRAL;
 	case SENS_COLLISION:	return TH_BUT_SETTING;
 	case SENS_NEAR:			return TH_BUT_SETTING1; 
@@ -1070,8 +1073,8 @@
 	bRaySensor       *raySens      = NULL;
 	bMessageSensor   *mes          = NULL;
 	bJoystickSensor	 *joy		   = NULL;
-	bActuatorSensor  *as          = NULL;
-
+	bActuatorSensor  *as           = NULL;
+	bDelaySensor     *ds		   = NULL;
 	short ysize;
 	char *str;
 	
@@ -1297,6 +1300,27 @@
 			yco-= ysize;
 			break;
 		}
+	case SENS_DELAY:
+		{
+			ysize= 48;
+			
+			glRects(xco, yco-ysize, xco+width, yco);
+			uiEmboss((float)xco, (float)yco-ysize,
+				(float)xco+width, (float)yco, 1);
+			
+			draw_default_sensor_header(sens, block, xco, yco, width);
+			ds = sens->data;
+			
+			uiDefButS(block, NUM, 0, "Delay",(short)(10+xco),(short)(yco-44),(short)((width-22)*0.4+10), 19,
+				&ds->delay, 0.0, 5000.0, 0, 0, "Delay in number of frames before the positive trigger");
+			uiDefButS(block, NUM, 0, "Dur",(short)(10+xco+(width-22)*0.4+10),(short)(yco-44),(short)((width-22)*0.4-10), 19,
+				&ds->duration, 0.0, 5000.0, 0, 0, "If >0, delay in number of frames before the negative trigger following the positive trigger");
+			uiDefButBitS(block, TOG, SENS_DELAY_REPEAT, 0, "REP",(short)(xco + 10 + (width-22)*0.8),(short)(yco - 44),
+				(short)(0.20 * (width-22)), 19, &ds->flag, 0.0, 0.0, 0, 0,
+				"Toggle repeat option. If selected, the sensor restarts after Delay+Dur frames");
+			yco-= ysize;
+			break;
+		}
 	case SENS_MOUSE:
 		{
 			ms= sens->data;

Modified: trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp
===================================================================
--- trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp	2008-08-16 20:31:38 UTC (rev 16135)
+++ trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp	2008-08-16 20:45:37 UTC (rev 16136)
@@ -65,6 +65,7 @@
 #include "SCA_JoystickSensor.h"
 #include "KX_NetworkMessageSensor.h"
 #include "SCA_ActuatorSensor.h"
+#include "SCA_DelaySensor.h"
 
 
 #include "SCA_PropertySensor.h"
@@ -281,6 +282,22 @@
 				break;
 			}
 			
+		case  SENS_DELAY:
+			{
+				// we can reuse the Always event manager for the delay sensor
+				SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::ALWAYS_EVENTMGR);
+				if (eventmgr)
+				{
+					bDelaySensor* delaysensor = (bDelaySensor*)sens->data;
+					gamesensor = new SCA_DelaySensor(eventmgr, 
+						gameobj,
+						delaysensor->delay,
+						delaysensor->duration,
+						(delaysensor->flag & SENS_DELAY_REPEAT) != 0);
+				}
+				break;
+			}
+
 		case SENS_COLLISION:
 			{
 				SCA_EventManager* eventmgr = logicmgr->FindEventManager(SCA_EventManager::TOUCH_EVENTMGR);

Added: trunk/blender/source/gameengine/GameLogic/SCA_DelaySensor.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_DelaySensor.cpp	                        (rev 0)
+++ trunk/blender/source/gameengine/GameLogic/SCA_DelaySensor.cpp	2008-08-16 20:45:37 UTC (rev 16136)
@@ -0,0 +1,257 @@
+/**
+ * Delay trigger
+ *
+ * $Id$
+ *
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): none yet.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#ifdef WIN32
+// This warning tells us about truncation of __long__ stl-generated names.
+// It can occasionally cause DevStudio to have internal compiler warnings.
+#pragma warning( disable : 4786 )     
+#endif
+
+#include "SCA_DelaySensor.h"
+#include "SCA_LogicManager.h"
+#include "SCA_EventManager.h"
+
+/* ------------------------------------------------------------------------- */
+/* Native functions                                                          */
+/* ------------------------------------------------------------------------- */
+
+SCA_DelaySensor::SCA_DelaySensor(class SCA_EventManager* eventmgr,
+								 SCA_IObject* gameobj,
+								 int delay,
+								 int duration,
+								 bool repeat,
+								 PyTypeObject* T)
+	: SCA_ISensor(gameobj,eventmgr, T), 
+	m_delay(delay),
+	m_duration(duration),
+	m_repeat(repeat)
+{
+	Init();
+}
+
+void SCA_DelaySensor::Init()
+{
+	m_lastResult = false;
+	m_frameCount = -1;
+	m_reset = true;
+}
+
+SCA_DelaySensor::~SCA_DelaySensor()
+{
+	/* intentionally empty */
+}
+
+CValue* SCA_DelaySensor::GetReplica()
+{
+	CValue* replica = new SCA_DelaySensor(*this);
+	// this will copy properties and so on...
+	CValue::AddDataToReplica(replica);
+
+	return replica;
+}
+
+
+
+bool SCA_DelaySensor::IsPositiveTrigger()
+{ 
+	return (m_invert ? !m_lastResult : m_lastResult);
+}
+
+bool SCA_DelaySensor::Evaluate(CValue* event)
+{
+	bool trigger = false;
+	bool result;
+
+	if (m_frameCount==-1) {
+		// this is needed to ensure ON trigger in case delay==0
+		// and avoid spurious OFF trigger when duration==0
+		m_lastResult = false;
+		m_frameCount = 0;
+	}
+
+	if (m_frameCount<m_delay) {
+		m_frameCount++;
+		result = false;
+	} else if (m_duration > 0) {
+		if (m_frameCount < m_delay+m_duration) {
+			m_frameCount++;
+			result = true;
+		} else {
+			result = false;
+			if (m_repeat)
+				m_frameCount = -1;
+		}
+	} else {
+		result = true;
+		if (m_repeat)
+			m_frameCount = -1;
+	}
+	if ((m_reset && m_level) || result != m_lastResult)
+		trigger = true;
+	m_reset = false;
+	m_lastResult = result;
+	return trigger;
+}
+
+/* ------------------------------------------------------------------------- */

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list