Skip to content

Commit

Permalink
UPBGE: Simplificate PHY_GetActiveEnvironment().
Browse files Browse the repository at this point in the history
This function is placed in KX_Globals.h under the name KX_GetPhysicsEnvironment
and directly call for KX_Scene::GetPhysicsEnvironment().
  • Loading branch information
panzergame committed Jun 2, 2018
1 parent c192d20 commit 5163905
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 78 deletions.
6 changes: 5 additions & 1 deletion source/gameengine/Ketsji/KX_Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
*/

#include "KX_Globals.h"
#include "KX_KetsjiEngine.h"
#include "KX_Scene.h"
#include "RAS_Rasterizer.h"

Expand Down Expand Up @@ -74,6 +73,11 @@ KX_Scene *KX_GetActiveScene()
return g_scene;
}

PHY_IPhysicsEnvironment *KX_GetPhysicsEnvironment()
{
return g_scene->GetPhysicsEnvironment();
}

const std::string& KX_GetMainPath()
{
return g_mainPath;
Expand Down
2 changes: 2 additions & 0 deletions source/gameengine/Ketsji/KX_Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

class KX_KetsjiEngine;
class KX_Scene;
class PHY_IPhysicsEnvironment;

void KX_SetActiveEngine(KX_KetsjiEngine *engine);
void KX_SetActiveScene(KX_Scene *scene);
Expand All @@ -40,6 +41,7 @@ void KX_SetOrigPath(const std::string& path);

KX_KetsjiEngine *KX_GetActiveEngine();
KX_Scene *KX_GetActiveScene();
PHY_IPhysicsEnvironment *KX_GetPhysicsEnvironment();
const std::string& KX_GetMainPath();
const std::string& KX_GetOrigPath();

Expand Down
5 changes: 0 additions & 5 deletions source/gameengine/Ketsji/KX_KetsjiEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,6 @@ bool KX_KetsjiEngine::NextFrame()
if (!scene->IsSuspended()) {
m_logger.StartLog(tc_physics, m_kxsystem->GetTimeInSeconds());
// set Python hooks for each scene
#ifdef WITH_PYTHON
PHY_SetActiveEnvironment(scene->GetPhysicsEnvironment());
#endif
KX_SetActiveScene(scene);

// Process sensors, and controllers
Expand Down Expand Up @@ -1035,7 +1032,6 @@ void KX_KetsjiEngine::RenderCamera(KX_Scene *scene, const CameraRenderData& came
DrawDebugShadowFrustum(scene, debugDraw);

#ifdef WITH_PYTHON
PHY_SetActiveEnvironment(scene->GetPhysicsEnvironment());
// Run any pre-drawing python callbacks
scene->RunDrawingCallbacks(KX_Scene::PRE_DRAW, rendercam);
#endif
Expand Down Expand Up @@ -1065,7 +1061,6 @@ RAS_OffScreen *KX_KetsjiEngine::PostRenderScene(KX_Scene *scene, RAS_OffScreen *
RAS_OffScreen *offScreen = scene->Render2DFilters(m_rasterizer, m_canvas, inputofs, targetofs);

#ifdef WITH_PYTHON
PHY_SetActiveEnvironment(scene->GetPhysicsEnvironment());
/* We can't deduce what camera should be passed to the python callbacks
* because the post draw callbacks are per scenes and not per cameras.
*/
Expand Down
110 changes: 45 additions & 65 deletions source/gameengine/Ketsji/KX_PyConstraintBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@
# include "LinearMath/btIDebugDraw.h"
#endif

// nasty glob variable to connect scripting language
// if there is a better way (without global), please do so!
static PHY_IPhysicsEnvironment *g_CurrentActivePhysicsEnvironment = nullptr;

#ifdef WITH_PYTHON

// macro copied from KX_PythonInit.cpp
Expand Down Expand Up @@ -159,8 +155,8 @@ static PyObject *gPySetGravity(PyObject *self,
{
float x, y, z;
if (PyArg_ParseTuple(args, "fff", &x, &y, &z)) {
if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->SetGravity(x, y, z);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->SetGravity(x, y, z);
}
}
else {
Expand All @@ -176,8 +172,8 @@ static PyObject *gPySetDebugMode(PyObject *self,
{
int mode;
if (PyArg_ParseTuple(args, "i", &mode)) {
if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->SetDebugMode(mode);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->SetDebugMode(mode);

}

Expand All @@ -197,8 +193,8 @@ static PyObject *gPySetNumTimeSubSteps(PyObject *self,
{
int substep;
if (PyArg_ParseTuple(args, "i", &substep)) {
if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->SetNumTimeSubSteps(substep);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->SetNumTimeSubSteps(substep);
}
}
else {
Expand All @@ -214,8 +210,8 @@ static PyObject *gPySetNumIterations(PyObject *self,
{
int iter;
if (PyArg_ParseTuple(args, "i", &iter)) {
if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->SetNumIterations(iter);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->SetNumIterations(iter);
}
}
else {
Expand All @@ -231,8 +227,8 @@ static PyObject *gPySetDeactivationTime(PyObject *self,
{
float deactive_time;
if (PyArg_ParseTuple(args, "f", &deactive_time)) {
if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->SetDeactivationTime(deactive_time);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->SetDeactivationTime(deactive_time);
}
}
else {
Expand All @@ -248,8 +244,8 @@ static PyObject *gPySetDeactivationLinearTreshold(PyObject *self,
{
float linearDeactivationTreshold;
if (PyArg_ParseTuple(args, "f", &linearDeactivationTreshold)) {
if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->SetDeactivationLinearTreshold(linearDeactivationTreshold);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->SetDeactivationLinearTreshold(linearDeactivationTreshold);
}
}
else {
Expand All @@ -265,8 +261,8 @@ static PyObject *gPySetDeactivationAngularTreshold(PyObject *self,
{
float angularDeactivationTreshold;
if (PyArg_ParseTuple(args, "f", &angularDeactivationTreshold)) {
if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->SetDeactivationAngularTreshold(angularDeactivationTreshold);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->SetDeactivationAngularTreshold(angularDeactivationTreshold);
}
}
else {
Expand All @@ -281,8 +277,8 @@ static PyObject *gPySetContactBreakingTreshold(PyObject *self,
{
float contactBreakingTreshold;
if (PyArg_ParseTuple(args, "f", &contactBreakingTreshold)) {
if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->SetContactBreakingTreshold(contactBreakingTreshold);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->SetContactBreakingTreshold(contactBreakingTreshold);
}
}
else {
Expand All @@ -298,8 +294,8 @@ static PyObject *gPySetCcdMode(PyObject *self,
{
float ccdMode;
if (PyArg_ParseTuple(args, "f", &ccdMode)) {
if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->SetCcdMode(ccdMode);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->SetCcdMode(ccdMode);
}
}
else {
Expand All @@ -314,8 +310,8 @@ static PyObject *gPySetSorConstant(PyObject *self,
{
float sor;
if (PyArg_ParseTuple(args, "f", &sor)) {
if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->SetSolverSorConstant(sor);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->SetSolverSorConstant(sor);
}
}
else {
Expand All @@ -330,8 +326,8 @@ static PyObject *gPySetSolverTau(PyObject *self,
{
float tau;
if (PyArg_ParseTuple(args, "f", &tau)) {
if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->SetSolverTau(tau);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->SetSolverTau(tau);
}
}
else {
Expand All @@ -347,8 +343,8 @@ static PyObject *gPySetSolverDamping(PyObject *self,
{
float damping;
if (PyArg_ParseTuple(args, "f", &damping)) {
if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->SetSolverDamping(damping);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->SetSolverDamping(damping);
}
}
else {
Expand All @@ -363,8 +359,8 @@ static PyObject *gPySetLinearAirDamping(PyObject *self,
{
float damping;
if (PyArg_ParseTuple(args, "f", &damping)) {
if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->SetLinearAirDamping(damping);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->SetLinearAirDamping(damping);
}
}
else {
Expand All @@ -380,8 +376,8 @@ static PyObject *gPySetUseEpa(PyObject *self,
{
int epa;
if (PyArg_ParseTuple(args, "i", &epa)) {
if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->SetUseEpa(epa);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->SetUseEpa(epa);
}
}
else {
Expand All @@ -395,8 +391,8 @@ static PyObject *gPySetSolverType(PyObject *self,
{
int solverType;
if (PyArg_ParseTuple(args, "i", &solverType)) {
if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->SetSolverType((PHY_SolverType)solverType);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->SetSolverType((PHY_SolverType)solverType);
}
}
else {
Expand All @@ -419,9 +415,9 @@ static PyObject *gPyGetVehicleConstraint(PyObject *self,
if (PyArg_ParseTuple(args, "l", &constraintid))
#endif
{
if (PHY_GetActiveEnvironment()) {
if (KX_GetPhysicsEnvironment()) {

PHY_IVehicle *vehicle = PHY_GetActiveEnvironment()->GetVehicleConstraint(constraintid);
PHY_IVehicle *vehicle = KX_GetPhysicsEnvironment()->GetVehicleConstraint(constraintid);
if (vehicle) {
KX_VehicleWrapper *pyWrapper = new KX_VehicleWrapper(vehicle);
return pyWrapper->NewProxy(true);
Expand Down Expand Up @@ -451,9 +447,9 @@ static PyObject *gPyGetCharacter(PyObject *self,
return nullptr;
}

if (PHY_GetActiveEnvironment()) {
if (KX_GetPhysicsEnvironment()) {

PHY_ICharacter *character = PHY_GetActiveEnvironment()->GetCharacterController(ob);
PHY_ICharacter *character = KX_GetPhysicsEnvironment()->GetCharacterController(ob);
if (character) {
KX_CharacterWrapper *pyWrapper = new KX_CharacterWrapper(character);
return pyWrapper->NewProxy(true);
Expand All @@ -480,13 +476,13 @@ static PyObject *gPyCreateConstraint(PyObject *self,
return nullptr;
}

if (PHY_GetActiveEnvironment()) {
if (KX_GetPhysicsEnvironment()) {
PHY_IPhysicsController *physctrl = (PHY_IPhysicsController *)physicsid;
PHY_IPhysicsController *physctrl2 = (PHY_IPhysicsController *)physicsid2;
if (physctrl) { //TODO:check for existence of this pointer!
if (constrainttype == PHY_VEHICLE_CONSTRAINT) {
EXP_ShowDeprecationWarning("bge.constraints.createConstraint(...)", "bge.constraints.createVehicle(chassis)");
PHY_IVehicle *vehicle = PHY_GetActiveEnvironment()->CreateVehicle(physctrl);
PHY_IVehicle *vehicle = KX_GetPhysicsEnvironment()->CreateVehicle(physctrl);

KX_VehicleWrapper *wrap = new KX_VehicleWrapper(vehicle);

Expand All @@ -502,7 +498,7 @@ static PyObject *gPyCreateConstraint(PyObject *self,
mt::vec3 axis1 = localCFrame.GetColumn(1);
mt::vec3 axis2 = localCFrame.GetColumn(2);

PHY_IConstraint *constraint = PHY_GetActiveEnvironment()->CreateConstraint(
PHY_IConstraint *constraint = KX_GetPhysicsEnvironment()->CreateConstraint(
physctrl, physctrl2, (enum PHY_ConstraintType)constrainttype, pivotX, pivotY, pivotZ,
(float)axis0.x, (float)axis0.y, (float)axis0.z,
(float)axis1.x, (float)axis1.y, (float)axis1.z,
Expand All @@ -529,7 +525,7 @@ static PyObject *gPyCreateVehicle(PyObject *self, PyObject *args)
return nullptr;
}

if (!PHY_GetActiveEnvironment()) {
if (!KX_GetPhysicsEnvironment()) {
Py_RETURN_NONE;
}

Expand All @@ -538,7 +534,7 @@ static PyObject *gPyCreateVehicle(PyObject *self, PyObject *args)
return nullptr;
}

PHY_IVehicle *vehicle = PHY_GetActiveEnvironment()->CreateVehicle(physctrl);
PHY_IVehicle *vehicle = KX_GetPhysicsEnvironment()->CreateVehicle(physctrl);
if (!vehicle) {
return nullptr;
}
Expand All @@ -562,8 +558,8 @@ static PyObject *gPyGetAppliedImpulse(PyObject *self,
if (PyArg_ParseTuple(args, "l", &constraintid))
#endif
{
if (PHY_GetActiveEnvironment()) {
appliedImpulse = PHY_GetActiveEnvironment()->GetAppliedImpulse(constraintid);
if (KX_GetPhysicsEnvironment()) {
appliedImpulse = KX_GetPhysicsEnvironment()->GetAppliedImpulse(constraintid);
}
}
else {
Expand All @@ -586,8 +582,8 @@ static PyObject *gPyRemoveConstraint(PyObject *self,
if (PyArg_ParseTuple(args, "l", &constraintid))
#endif
{
if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->RemoveConstraintById(constraintid, true);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->RemoveConstraintById(constraintid, true);
}
}
else {
Expand All @@ -604,8 +600,8 @@ static PyObject *gPyExportBulletFile(PyObject *, PyObject *args)
return nullptr;
}

if (PHY_GetActiveEnvironment()) {
PHY_GetActiveEnvironment()->ExportFile(filename);
if (KX_GetPhysicsEnvironment()) {
KX_GetPhysicsEnvironment()->ExportFile(filename);
}
Py_RETURN_NONE;
}
Expand Down Expand Up @@ -736,21 +732,5 @@ PyMODINIT_FUNC initConstraintPythonBinding()
return m;
}

#if 0
static void KX_RemovePythonConstraintBinding()
{
}
#endif

#endif // WITH_PYTHON

void PHY_SetActiveEnvironment(class PHY_IPhysicsEnvironment *env)
{
g_CurrentActivePhysicsEnvironment = env;
}

PHY_IPhysicsEnvironment *PHY_GetActiveEnvironment()
{
return g_CurrentActivePhysicsEnvironment;
}

3 changes: 0 additions & 3 deletions source/gameengine/Ketsji/KX_PyConstraintBinding.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,4 @@ PyMODINIT_FUNC initConstraintPythonBinding();

#endif /* WITH_PYTHON */

void PHY_SetActiveEnvironment(class PHY_IPhysicsEnvironment* env);
PHY_IPhysicsEnvironment* PHY_GetActiveEnvironment();

#endif /* __KX_PYCONSTRAINTBINDING_H__ */
6 changes: 3 additions & 3 deletions source/gameengine/Ketsji/KX_PythonInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ static PyObject *gPySetPhysicsTicRate(PyObject *, PyObject *args)
return nullptr;
}

PHY_GetActiveEnvironment()->SetFixedTimeStep(true, ticrate);
KX_GetPhysicsEnvironment()->SetFixedTimeStep(true, ticrate);
Py_RETURN_NONE;
}
#if 0 // unused
Expand All @@ -443,15 +443,15 @@ static PyObject *gPySetPhysicsDebug(PyObject *, PyObject *args)
return nullptr;
}

PHY_GetActiveEnvironment()->setDebugMode(debugMode);
KX_GetPhysicsEnvironment()->setDebugMode(debugMode);
Py_RETURN_NONE;
}
#endif


static PyObject *gPyGetPhysicsTicRate(PyObject *)
{
return PyFloat_FromDouble(PHY_GetActiveEnvironment()->GetFixedTimeStep());
return PyFloat_FromDouble(KX_GetPhysicsEnvironment()->GetFixedTimeStep());
}

static PyObject *gPyGetAverageFrameRate(PyObject *)
Expand Down
Loading

0 comments on commit 5163905

Please sign in to comment.