blender/source/gameengine/GameLogic/SCA_MouseSensor.cpp
Benoit Bolsee 386122ada6 BGE performance, 4th round: logic
This commit extends the technique of dynamic linked list to the logic
system to eliminate as much as possible temporaries, map lookup or 
full scan. The logic engine is now free of memory allocation, which is
an important stability factor. 

The overhead of the logic system is reduced by a factor between 3 and 6
depending on the logic setup. This is the speed-up you can expect on 
a logic setup using simple bricks. Heavy bricks like python controllers
and ray sensors will still take about the same time to execute so the
speed up will be less important.

The core of the logic engine has been much reworked but the functionality
is still the same except for one thing: the priority system on the 
execution of controllers. The exact same remark applies to actuators but
I'll explain for controllers only:

Previously, it was possible, with the "executePriority" attribute to set
a controller to run before any other controllers in the game. Other than
that, the sequential execution of controllers, as defined in Blender was
guaranteed by default.

With the new system, the sequential execution of controllers is still 
guaranteed but only within the controllers of one object. the user can
no longer set a controller to run before any other controllers in the
game. The "executePriority" attribute controls the execution of controllers
within one object. The priority is a small number starting from 0 for the
first controller and incrementing for each controller.

If this missing feature is a must, a special method can be implemented
to set a controller to run before all other controllers.

Other improvements:
- Systematic use of reference in parameter passing to avoid unnecessary data copy
- Use pre increment in iterator instead of post increment to avoid temporary allocation
- Use const char* instead of STR_String whenever possible to avoid temporary allocation
- Fix reference counting bugs (memory leak)
- Fix a crash in certain cases of state switching and object deletion
- Minor speed up in property sensor
- Removal of objects during the game is a lot faster
2009-05-10 20:53:58 +00:00

360 lines
9.3 KiB
C++

/**
* Sensor for mouse input
*
*
* $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): José I. Romero (cleanup and fixes)
*
* ***** END GPL LICENSE BLOCK *****
*/
#include "SCA_MouseSensor.h"
#include "SCA_EventManager.h"
#include "SCA_MouseManager.h"
#include "SCA_LogicManager.h"
#include "SCA_IInputDevice.h"
#include "ConstExpr.h"
#include <iostream>
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/* ------------------------------------------------------------------------- */
/* Native functions */
/* ------------------------------------------------------------------------- */
SCA_MouseSensor::SCA_MouseSensor(SCA_MouseManager* eventmgr,
int startx,int starty,
short int mousemode,
SCA_IObject* gameobj,
PyTypeObject* T)
: SCA_ISensor(gameobj,eventmgr, T),
m_pMouseMgr(eventmgr),
m_x(startx),
m_y(starty)
{
m_mousemode = mousemode;
m_triggermode = true;
UpdateHotkey(this, NULL);
Init();
}
void SCA_MouseSensor::Init()
{
m_val = (m_invert)?1:0; /* stores the latest attribute */
m_reset = true;
}
SCA_MouseSensor::~SCA_MouseSensor()
{
/* Nothing to be done here. */
}
int SCA_MouseSensor::UpdateHotkey(void *self, const PyAttributeDef*)
{
// gosh, this function is so damn stupid
// its here because of a design mistake in the mouse sensor, it should only
// have 3 trigger modes (button, wheel, move), and let the user set the
// hotkey separately, like the other sensors. but instead it has a mode for
// each friggin key and i have to update the hotkey based on it... genius!
SCA_MouseSensor* sensor = reinterpret_cast<SCA_MouseSensor*>(self);
switch (sensor->m_mousemode) {
case KX_MOUSESENSORMODE_LEFTBUTTON:
sensor->m_hotkey = SCA_IInputDevice::KX_LEFTMOUSE;
break;
case KX_MOUSESENSORMODE_MIDDLEBUTTON:
sensor->m_hotkey = SCA_IInputDevice::KX_MIDDLEMOUSE;
break;
case KX_MOUSESENSORMODE_RIGHTBUTTON:
sensor->m_hotkey = SCA_IInputDevice::KX_RIGHTMOUSE;
break;
case KX_MOUSESENSORMODE_WHEELUP:
sensor->m_hotkey = SCA_IInputDevice::KX_WHEELUPMOUSE;
break;
case KX_MOUSESENSORMODE_WHEELDOWN:
sensor->m_hotkey = SCA_IInputDevice::KX_WHEELDOWNMOUSE;
break;
default:
; /* ignore, no hotkey */
}
// return value is used in py_setattro(),
// 0=attribute checked ok (see Attributes array definition)
return 0;
}
CValue* SCA_MouseSensor::GetReplica()
{
SCA_MouseSensor* replica = new SCA_MouseSensor(*this);
// this will copy properties and so on...
replica->ProcessReplica();
replica->Init();
return replica;
}
bool SCA_MouseSensor::IsPositiveTrigger()
{
bool result = (m_val != 0);
if (m_invert)
result = !result;
return result;
}
short int SCA_MouseSensor::GetModeKey()
{
return m_mousemode;
}
SCA_IInputDevice::KX_EnumInputs SCA_MouseSensor::GetHotKey()
{
return m_hotkey;
}
bool SCA_MouseSensor::Evaluate()
{
bool result = false;
bool reset = m_reset && m_level;
SCA_IInputDevice* mousedev = m_pMouseMgr->GetInputDevice();
m_reset = false;
switch (m_mousemode) {
case KX_MOUSESENSORMODE_LEFTBUTTON:
case KX_MOUSESENSORMODE_MIDDLEBUTTON:
case KX_MOUSESENSORMODE_RIGHTBUTTON:
case KX_MOUSESENSORMODE_WHEELUP:
case KX_MOUSESENSORMODE_WHEELDOWN:
{
const SCA_InputEvent& mevent = mousedev->GetEventValue(m_hotkey);
switch (mevent.m_status){
case SCA_InputEvent::KX_JUSTACTIVATED:
m_val = 1;
result = true;
break;
case SCA_InputEvent::KX_JUSTRELEASED:
m_val = 0;
result = true;
break;
case SCA_InputEvent::KX_ACTIVE:
if (m_val == 0)
{
m_val = 1;
if (m_level)
result = true;
}
break;
default:
if (m_val == 1)
{
m_val = 0;
result = true;
}
break;
}
break;
}
case KX_MOUSESENSORMODE_MOVEMENT:
{
const SCA_InputEvent& eventX = mousedev->GetEventValue(SCA_IInputDevice::KX_MOUSEX);
const SCA_InputEvent& eventY = mousedev->GetEventValue(SCA_IInputDevice::KX_MOUSEY);
if (eventX.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
eventY.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
eventX.m_status == SCA_InputEvent::KX_ACTIVE ||
eventY.m_status == SCA_InputEvent::KX_ACTIVE)
{
m_val = 1;
result = true;
}
else if (eventX.m_status == SCA_InputEvent::KX_JUSTRELEASED ||
eventY.m_status == SCA_InputEvent::KX_JUSTRELEASED )
{
m_val = 0;
result = true;
}
else //KX_NO_IMPUTSTATUS
{
if (m_val == 1)
{
m_val = 0;
result = true;
}
}
break;
}
default:
; /* error */
}
if (reset)
// force an event
result = true;
return result;
}
void SCA_MouseSensor::setX(short x)
{
m_x = x;
}
void SCA_MouseSensor::setY(short y)
{
m_y = y;
}
bool SCA_MouseSensor::isValid(SCA_MouseSensor::KX_MOUSESENSORMODE m)
{
return ((m > KX_MOUSESENSORMODE_NODEF) && (m < KX_MOUSESENSORMODE_MAX));
}
/* ------------------------------------------------------------------------- */
/* Python functions */
/* ------------------------------------------------------------------------- */
//Deprecated functions ------>
/* get x position ---------------------------------------------------------- */
const char SCA_MouseSensor::GetXPosition_doc[] =
"getXPosition\n"
"\tReturns the x-coordinate of the mouse sensor, in frame coordinates.\n"
"\tThe lower-left corner is the origin. The coordinate is given in\n"
"\tpixels\n";
PyObject* SCA_MouseSensor::PyGetXPosition() {
ShowDeprecationWarning("getXPosition()", "the position property");
return PyInt_FromLong(m_x);
}
/* get y position ---------------------------------------------------------- */
const char SCA_MouseSensor::GetYPosition_doc[] =
"getYPosition\n"
"\tReturns the y-coordinate of the mouse sensor, in frame coordinates.\n"
"\tThe lower-left corner is the origin. The coordinate is given in\n"
"\tpixels\n";
PyObject* SCA_MouseSensor::PyGetYPosition() {
ShowDeprecationWarning("getYPosition()", "the position property");
return PyInt_FromLong(m_y);
}
//<----- Deprecated
KX_PYMETHODDEF_DOC_O(SCA_MouseSensor, getButtonStatus,
"getButtonStatus(button)\n"
"\tGet the given button's status (KX_INPUT_NONE, KX_INPUT_NONE, KX_INPUT_JUST_ACTIVATED, KX_INPUT_ACTIVE, KX_INPUT_JUST_RELEASED).\n")
{
if (PyInt_Check(value))
{
int button = PyInt_AsLong(value);
if ((button < SCA_IInputDevice::KX_LEFTMOUSE)
|| (button > SCA_IInputDevice::KX_RIGHTMOUSE)){
PyErr_SetString(PyExc_ValueError, "sensor.getButtonStatus(int): Mouse Sensor, invalid button specified!");
return NULL;
}
SCA_IInputDevice* mousedev = m_pMouseMgr->GetInputDevice();
const SCA_InputEvent& event = mousedev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) button);
return PyInt_FromLong(event.m_status);
}
Py_RETURN_NONE;
}
/* ------------------------------------------------------------------------- */
/* Python Integration Hooks */
/* ------------------------------------------------------------------------- */
PyTypeObject SCA_MouseSensor::Type = {
#if (PY_VERSION_HEX >= 0x02060000)
PyVarObject_HEAD_INIT(NULL, 0)
#else
/* python 2.5 and below */
PyObject_HEAD_INIT( NULL ) /* required py macro */
0, /* ob_size */
#endif
"SCA_MouseSensor",
sizeof(PyObjectPlus_Proxy),
0,
py_base_dealloc,
0,
0,
0,
0,
py_base_repr,
0,0,0,0,0,0,
py_base_getattro,
py_base_setattro,
0,0,0,0,0,0,0,0,0,
Methods
};
PyParentObject SCA_MouseSensor::Parents[] = {
&SCA_MouseSensor::Type,
&SCA_ISensor::Type,
&SCA_ILogicBrick::Type,
&CValue::Type,
NULL
};
PyMethodDef SCA_MouseSensor::Methods[] = {
//Deprecated functions ------>
{"getXPosition", (PyCFunction) SCA_MouseSensor::sPyGetXPosition, METH_VARARGS, (PY_METHODCHAR)GetXPosition_doc},
{"getYPosition", (PyCFunction) SCA_MouseSensor::sPyGetYPosition, METH_VARARGS, (PY_METHODCHAR)GetYPosition_doc},
//<----- Deprecated
KX_PYMETHODTABLE_O(SCA_MouseSensor, getButtonStatus),
{NULL,NULL} //Sentinel
};
PyAttributeDef SCA_MouseSensor::Attributes[] = {
KX_PYATTRIBUTE_SHORT_RW_CHECK("mode",KX_MOUSESENSORMODE_NODEF,KX_MOUSESENSORMODE_MAX-1,true,SCA_MouseSensor,m_mousemode,UpdateHotkey),
KX_PYATTRIBUTE_SHORT_LIST_RO("position",SCA_MouseSensor,m_x,2),
{ NULL } //Sentinel
};
PyObject* SCA_MouseSensor::py_getattro(PyObject *attr)
{
py_getattro_up(SCA_ISensor);
}
PyObject* SCA_MouseSensor::py_getattro_dict() {
py_getattro_dict_up(SCA_ISensor);
}
int SCA_MouseSensor::py_setattro(PyObject *attr, PyObject *value)
{
py_setattro_up(SCA_ISensor);
}
/* eof */