Commits

TWO  committed d906fdd

Added python bindings
Fixed CMake build

  • Participants
  • Parent commits 3ff4866

Comments (0)

Files changed (16)

File CMakeLists.txt

 )
 
 add_subdirectory(${Poly_SOURCE_DIR}/PolyMain)
+add_subdirectory(${Poly_SOURCE_DIR}/PolyPython)
 add_subdirectory(${Poly_SOURCE_DIR}/Extensions/PolyAudioIrrKlang)
 add_subdirectory(${Poly_SOURCE_DIR}/Extensions/PolyInputOIS)
 add_subdirectory(${Poly_SOURCE_DIR}/Extensions/PolyNetworkRaknet)

File PolyMain/CMakeLists.txt

 
 set(LIBRARIES
 	debug OgreMain_d.lib
+	debug PhysX3CHECKED_x86.lib
+	debug PhysX3CommonCHECKED_x86.lib
+	debug PhysX3CookingCHECKED_x86.lib
+	debug PhysX3CharacterKinematicCHECKED_x86.lib
+	debug PhysX3ExtensionsCHECKED.lib
+	debug PhysXVisualDebuggerSDKCHECKED.lib
 	debug MyGUIEngine_d.lib
 	debug MyGUI.OgrePlatform_d.lib
 	optimized OgreMain.lib
+	optimized PhysX3CHECKED_x86.lib
+	optimized PhysX3CommonCHECKED_x86.lib
+	optimized PhysX3CookingCHECKED_x86.lib
+	optimized PhysX3CharacterKinematicCHECKED_x86.lib
+	optimized PhysX3ExtensionsCHECKED.lib
+	optimized PhysXVisualDebuggerSDKCHECKED.lib
 	optimized MyGUIEngine.lib
 	optimized MyGUI.OgrePlatform.lib
 )

File PolyMain/include/game/PolyEntity.cpp

 			return Entity();
 	}
 
+	Component* Entity::getComponent(const String& familyName, bool _throw) const {
+		POLY_ASSERT(mEID != UNASSIGNED_ENTITY_ID);
+
+		TypeNameComponentMap& components = msComponentsByFamily[mEID];
+		TypeNameComponentMap::const_iterator itr;
+
+		itr = components.find(InternString(familyName));
+
+		if(itr != components.end()) {
+			return itr->second;
+		}
+		else if(_throw) {
+			POLY_EXCEPT(Exception::ERR_ITEM_NOT_FOUND,
+				"Component '" + familyName + "' not found in Entity!",
+				"Entity::getComponent");
+		}
+
+		return POLY_NULLPTR;
+	}
+
 	const EntitySet& Entity::getWithComponent(const String& _typeName) {
 		InternString typeName(_typeName);
 

File PolyMain/include/game/PolyEntity.h

 		*/
 		template<typename ComponentType>
 		ComponentType*				getComponent(bool _throw=false) const;
+		Component*					getComponent(const String& typeName, bool _throw=false) const;
 
 		const TypeNameComponentMap&	getComponents() const;
 

File PolyPython/CMakeLists.txt

+# Copyright (c) Oliver 'SirPolly' Weitzel
+                                                                                  
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+                                                                                  
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+                                                                                  
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE. 
+
+include(Precompiled)
+
+project(PolyPython)
+
+add_definitions(-D_PolyPythonExportInterface -DBOOST_PYTHON_SOURCE)
+include_directories(${Poly_SOURCE_DIR}/dependencies/include)
+include_directories(${PolyMain_SOURCE_DIR}/include)
+link_directories(${Poly_SOURCE_DIR}/dependencies/lib/debug)
+link_directories(${Poly_SOURCE_DIR}/dependencies/lib/release)
+
+file(GLOB_RECURSE HEADER_FILES "include/*.h")
+
+file(GLOB_RECURSE SOURCE_FILES "include/*.cpp")
+
+set(LIBRARIES
+	debug OgreMain_d.lib
+	debug PhysX3CHECKED_x86.lib
+	debug PhysX3CommonCHECKED_x86.lib
+	debug PhysX3CookingCHECKED_x86.lib
+	debug PhysX3CharacterKinematicCHECKED_x86.lib
+	debug PhysX3ExtensionsCHECKED.lib
+	debug PhysXVisualDebuggerSDKCHECKED.lib
+	debug MyGUIEngine_d.lib
+	debug MyGUI.OgrePlatform_d.lib
+	debug PolyMain_d.lib
+	optimized OgreMain.lib
+	optimized PhysX3CHECKED_x86.lib
+	optimized PhysX3CommonCHECKED_x86.lib
+	optimized PhysX3CookingCHECKED_x86.lib
+	optimized PhysX3CharacterKinematicCHECKED_x86.lib
+	optimized PhysX3ExtensionsCHECKED.lib
+	optimized PhysXVisualDebuggerSDKCHECKED.lib
+	optimized MyGUIEngine.lib
+	optimized MyGUI.OgrePlatform.lib
+	optimized PolyMain.lib
+)
+
+ADD_PRECOMPILED_HEADER("PolyPythonPrerequisites.h" "PolyPythonPrerequisites.cpp" ${SOURCE_FILES})
+
+add_library(PolyPython SHARED ${HEADER_FILES} ${SOURCE_FILES})
+set_target_properties(PolyPython PROPERTIES DEBUG_POSTFIX _d)
+target_link_libraries(PolyPython ${LIBRARIES})
+
+install(TARGETS PolyPython 
+	RUNTIME DESTINATION ${Poly_SOURCE_DIR}/bin
+	LIBRARY DESTINATION ${Poly_SOURCE_DIR}/PolyPython/lib
+	ARCHIVE DESTINATION ${Poly_SOURCE_DIR}/PolyPython/lib)

File PolyPython/PolyPython.vcxproj

   <ItemGroup>
     <ClInclude Include="include\PolyPython.h" />
     <ClInclude Include="include\PolyPythonBindings.h" />
+    <ClInclude Include="include\PolyPythonCoreBindings.h" />
+    <ClInclude Include="include\PolyPythonGameBindings.h" />
     <ClInclude Include="include\PolyPythonPrerequisites.h" />
+    <ClInclude Include="include\PolyPythonRenderBindings.h" />
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="include\converter\arg_to_python_base.cpp">

File PolyPython/PolyPython.vcxproj.filters

     <ClInclude Include="include\PolyPython.h" />
     <ClInclude Include="include\PolyPythonBindings.h" />
     <ClInclude Include="include\PolyPythonPrerequisites.h" />
+    <ClInclude Include="include\PolyPythonRenderBindings.h" />
+    <ClInclude Include="include\PolyPythonGameBindings.h" />
+    <ClInclude Include="include\PolyPythonCoreBindings.h" />
   </ItemGroup>
   <ItemGroup>
     <Filter Include="python">

File PolyPython/include/PolyPythonBindings.cpp

 */
 
 #include "PolyPythonPrerequisites.h"
+#include "PolyPythonCoreBindings.h"
+#include "PolyPythonRenderBindings.h"
+#include "PolyPythonGameBindings.h"
 
 #include "PolyPythonBindings.h"
 
 
-class PythonEntityLogic : public EntityLogic {
-public:
-	PythonEntityLogic(PyObject* p)
-	:	mSelf(p) {}
-	PythonEntityLogic(PyObject* p, const EntityLogic& self)
-	:	mSelf(p), EntityLogic(self) {}
-
-	virtual void updateEntities(const EntityList& entities) {
-		bp::call_method<void>(mSelf, "updateEntities", entities);
-	}
-	static void updateEntitiesDefault(EntityLogic& self, const EntityList& entities) {
-		self.EntityLogic::updateEntities(entities);
-	}
-private:
-	PyObject* mSelf;
-};
-
-void logDefault(const char* str) {
-	POLY_LOG(str);
-}
-void logError(const char* str) {
-	POLY_LOG_CRITICAL(str);
-}
-
 BOOST_PYTHON_MODULE(poly) {
-	// Core
-	bp::def("_log_hook", &logDefault);
-	bp::def("_log_error_hook", &logError);
-
-	// Game
-	bp::class_<EntityList>("EntityList")
-		.def("__len__", &EntityList::size);
-
-	bp::class_<EntityLogic, PythonEntityLogic, EntityLogicPtr>("EntityLogic")
-		.def("updateEntities", &PythonEntityLogic::updateEntitiesDefault);
-
-	bp::class_<EntityManager, boost::noncopyable>("EntityManager")
-		.def("addEntityLogic", &EntityManager::addEntityLogic);
+	bindCore();
+	bindGame();
+	bindRender();
 
 	// Runtime environment
 	EntityManager* entityMgr =  gEnv.entityMgr.get();

File PolyPython/include/PolyPythonCoreBindings.h

+/*
+    Copyright (c) Oliver 'SirPolly' Weitzel
+                                                                                  
+    Permission is hereby granted, free of charge, to any person obtaining a copy
+    of this software and associated documentation files (the "Software"), to deal
+    in the Software without restriction, including without limitation the rights
+    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+    copies of the Software, and to permit persons to whom the Software is
+    furnished to do so, subject to the following conditions:
+                                                                                  
+    The above copyright notice and this permission notice shall be included in
+    all copies or substantial portions of the Software.
+                                                                                  
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+    THE SOFTWARE. 
+    
+*/
+
+#ifndef PolyPythonCoreBindings_h
+#define PolyPythonCoreBindings_h
+
+void logDefault(const char* str) {
+	POLY_LOG(str);
+}
+void logError(const char* str) {
+	POLY_LOG_CRITICAL(str);
+}
+
+String Vector3_str(const Vector3& self) {
+	return StringConverter::toString(self);
+}
+
+void bindCore() {
+	bp::def("_log_hook", &logDefault);
+	bp::def("_log_error_hook", &logError);
+
+	bp::object v3 = bp::class_<Vector3>("Vector3", bp::init<>())
+		.def_readwrite("x", &Vector3::x)
+		.def_readwrite("y", &Vector3::y)
+		.def_readwrite("z", &Vector3::z)
+
+		.def(bp::init<float>())
+		.def(bp::init<float, float, float>())
+
+		.def(bp::self + Vector3())
+		.def(bp::self - Vector3())
+		.def(bp::self * Vector3())
+		.def(bp::self * float())
+		.def(bp::self / Vector3())
+		.def(bp::self / float())
+
+		.def(bp::self += Vector3())
+		.def(bp::self -= Vector3())
+		.def(bp::self *= Vector3())
+		.def(bp::self *= float())
+		.def(bp::self /= Vector3())
+		.def(bp::self /= float())
+
+		.def("__str__", &Vector3_str)
+
+		.def("length", &Vector3::length)
+		;
+
+	v3.attr("ZERO") = boost::ref(Vector3::ZERO);
+}
+
+#endif

File PolyPython/include/PolyPythonGameBindings.h

+/*
+    Copyright (c) Oliver 'SirPolly' Weitzel
+                                                                                  
+    Permission is hereby granted, free of charge, to any person obtaining a copy
+    of this software and associated documentation files (the "Software"), to deal
+    in the Software without restriction, including without limitation the rights
+    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+    copies of the Software, and to permit persons to whom the Software is
+    furnished to do so, subject to the following conditions:
+                                                                                  
+    The above copyright notice and this permission notice shall be included in
+    all copies or substantial portions of the Software.
+                                                                                  
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+    THE SOFTWARE. 
+    
+*/
+
+#ifndef PolyPythonGameBindings_h
+#define PolyPythonGameBindings_h
+
+class PythonEntityLogic : public EntityLogic {
+public:
+	PythonEntityLogic(PyObject* p)
+		:	mSelf(p) {}
+	PythonEntityLogic(PyObject* p, const EntityLogic& self)
+		:	mSelf(p), EntityLogic(self) {}
+
+	virtual void updateEntities(const EntityList& entities) {
+		try {
+			bp::call_method<void>(mSelf, "updateEntities", entities);
+		}
+		catch(const bp::error_already_set&) {
+			Poly::Python::_fetchError();
+		}
+	}
+	static void updateEntitiesDefault(EntityLogic& self, const EntityList& entities) {
+		self.EntityLogic::updateEntities(entities);
+	}
+
+	virtual void updateEntitiesOfInterest(const String& typeName, const EntityList& entities) {
+		try {
+			bp::call_method<void>(mSelf, "updateEntitiesOfInterest", typeName, entities);
+		}
+		catch(const bp::error_already_set&) {
+			Poly::Python::_fetchError();
+		}
+	}
+	static void updateEntitiesOfInterestDefault(EntityLogic& self, const String& typeName, const EntityList& entities) {
+		self.EntityLogic::updateEntitiesOfInterest(typeName, entities);
+	}
+
+	ComponentNameSet& getComponentTypesOfInterest() {
+		return mComponentTypesOfInterest;
+	}
+	void setComponentTypesOfInterest(ComponentNameSet& set) {
+		mComponentTypesOfInterest = set;
+	}
+
+private:
+	PyObject* mSelf;
+};
+
+class PythonComponent : public Component {
+public:
+	POLY_COMPONENT_BASE;
+};
+
+POLY_COMPONENT_BASE_IMPL(PythonComponent, "Python", "Python");
+
+bp::object Entity_GetComponent(Entity& self, bp::object componentType) {
+	String typeName = bp::extract<String>(componentType.attr("__name__"));
+	
+	bp::object ret;
+
+	if(typeName == TransformComponent::GetFamilyName()) {
+		ret = bp::object(self.getComponent<TransformComponent>());
+	}
+	else {
+		// Try to convert to PythonComponent
+		PythonComponent* component = dynamic_cast<PythonComponent*>(self.getComponent(typeName));
+
+		if(component)
+			ret = bp::object(component);
+	}
+
+	return ret;
+}
+
+void ComponentNameSet_add(ComponentNameSet& self, const String& str) {
+	self.insert(InternString(str));
+}
+
+void bindGame() {
+	bp::class_<Component, boost::noncopyable>("Component", bp::no_init);
+
+	bp::class_<TransformComponent, bp::bases<Component>>("Transform")
+		.add_property("sceneNode", bp::make_getter(&TransformComponent::sceneNode, bp::return_internal_reference<>()));
+
+	bp::class_<Entity>("Entity")
+		.def("getComponent", &Entity_GetComponent);
+
+	bp::class_<EntityList>("EntityList")
+		.def("__iter__", bp::iterator<EntityList>())
+		.def("__len__", &EntityList::size);
+
+	bp::class_<ComponentNameSet>("ComponentNameSet")
+		.def("add", &ComponentNameSet_add);
+
+	bp::class_<EntityLogic, PythonEntityLogic, EntityLogicPtr>("EntityLogic")
+		.def("updateEntities", &PythonEntityLogic::updateEntitiesDefault)
+		.def("updateEntitiesOfInterest", &PythonEntityLogic::updateEntitiesOfInterestDefault)
+
+		.add_property("componentTypesOfInterest",
+			bp::make_function(&PythonEntityLogic::getComponentTypesOfInterest, bp::return_value_policy<bp::reference_existing_object>()),
+			&PythonEntityLogic::setComponentTypesOfInterest)
+		;
+
+	bp::class_<EntityManager, boost::noncopyable>("EntityManager")
+		.def("addEntityLogic", &EntityManager::addEntityLogic);
+}
+
+#endif

File PolyPython/include/PolyPythonPrerequisites.cpp

 
 			String msg = "Error " + filename + "(" + StringConverter::toString( lineno ) + ") : " + strErrorMessage;
 
-
 			POLY_LOG_CRITICAL(msg);
 		}
 	}

File PolyPython/include/PolyPythonRenderBindings.h

+/*
+    Copyright (c) Oliver 'SirPolly' Weitzel
+                                                                                  
+    Permission is hereby granted, free of charge, to any person obtaining a copy
+    of this software and associated documentation files (the "Software"), to deal
+    in the Software without restriction, including without limitation the rights
+    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+    copies of the Software, and to permit persons to whom the Software is
+    furnished to do so, subject to the following conditions:
+                                                                                  
+    The above copyright notice and this permission notice shall be included in
+    all copies or substantial portions of the Software.
+                                                                                  
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+    THE SOFTWARE. 
+    
+*/
+
+#ifndef PolyPythonRenderBindings_h
+#define PolyPythonRenderBindings_h
+
+void bindRender() {
+	bp::class_<Ogre::Node, boost::noncopyable>("Node", bp::no_init)
+		.add_property("position",
+			bp::make_function(&Ogre::Node::getPosition, bp::return_value_policy<bp::copy_const_reference>()),
+			(void(Ogre::Node::*)(const Vector3&))&Ogre::Node::setPosition);
+
+	bp::class_<Ogre::SceneNode, bp::bases<Ogre::Node>, boost::noncopyable>("SceneNode", bp::no_init);
+}
+
+#endif

File extensions/PolyAudioIrrKlang/CMakeLists.txt

 	optimized PolyMain.lib
 )
 
-ADD_PRECOMPILED_HEADER("PolyAudioIrrKlangPrerequisites.h" "src/PolyAudioIrrKlangPrerequisites.cpp" ${SOURCE_FILES})
+ADD_PRECOMPILED_HEADER("PolyAudioIrrKlangPrerequisites.h" "PolyAudioIrrKlangPrerequisites.cpp" ${SOURCE_FILES})
 
 add_library(PolyAudioIrrKlang SHARED ${HEADER_FILES} ${SOURCE_FILES})
 set_target_properties(PolyAudioIrrKlang PROPERTIES DEBUG_POSTFIX _d)

File extensions/PolyInputOIS/CMakeLists.txt

 	optimized PolyMain.lib
 )
 
-ADD_PRECOMPILED_HEADER("PolyInputOISPrerequisites.h" "src/PolyInputOISPrerequisites.cpp" ${SOURCE_FILES})
+ADD_PRECOMPILED_HEADER("PolyInputOISPrerequisites.h" "PolyInputOISPrerequisites.cpp" ${SOURCE_FILES})
 
 add_library(PolyInputOIS SHARED ${HEADER_FILES} ${SOURCE_FILES})
 set_target_properties(PolyInputOIS PROPERTIES DEBUG_POSTFIX _d)

File extensions/PolyInputOIS/PolyInputOIS.vcxproj.filters

+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup>
+    <ClInclude Include="include\PolyInputOISExtension.h" />
+    <ClInclude Include="include\PolyInputOISKeyboard.h" />
+    <ClInclude Include="include\PolyInputOISMouse.h" />
+    <ClInclude Include="include\PolyInputOISPlugin.h" />
+    <ClInclude Include="include\PolyInputOISPrerequisites.h" />
+    <ClInclude Include="include\PolyInputOISSystem.h" />
+    <ClInclude Include="include\PolyInputOISSystemFactory.h" />
+  </ItemGroup>
+  <ItemGroup>
+    <ClCompile Include="include\PolyInputOISKeyboard.cpp" />
+    <ClCompile Include="include\PolyInputOISMouse.cpp" />
+    <ClCompile Include="include\PolyInputOISPlugin.cpp" />
+    <ClCompile Include="include\PolyInputOISPrerequisites.cpp" />
+    <ClCompile Include="include\PolyInputOISSystem.cpp" />
+    <ClCompile Include="include\PolyInputOISSystemFactory.cpp" />
+  </ItemGroup>
+</Project>

File extensions/PolyNetworkRaknet/CMakeLists.txt

 	optimized PolyMain.lib
 )
 
-ADD_PRECOMPILED_HEADER("PolyNetworkRaknetPrerequisites.h" "src/PolyNetworkRaknetPrerequisites.cpp" ${SOURCE_FILES})
+ADD_PRECOMPILED_HEADER("PolyNetworkRaknetPrerequisites.h" "PolyNetworkRaknetPrerequisites.cpp" ${SOURCE_FILES})
 
 add_library(PolyNetworkRaknet SHARED ${HEADER_FILES} ${SOURCE_FILES})
 set_target_properties(PolyNetworkRaknet PROPERTIES DEBUG_POSTFIX _d)