Commits

Alberto Torres committed a87cffc Draft

Added Entity.add_polygon, Py2D, removed direct calls to ogre and fixed xray.

  • Participants
  • Parent commits 171229c

Comments (0)

Files changed (13)

     sound.pxi
     variable.pxd
     standalone.py
+    py2d/Operations.pxi
+    py2d/Polygon.pxi
+    py2d/_py2d.pyx
+    py2d/Vector.pxi
 )
 
 #include(FindPythonInterp)
 
 add_custom_command(
   DEPENDS ${PYGAMEKIT_SRC}
-  OUTPUT gamekit.cpp mathutils.cpp standalone.py.h
+  OUTPUT gamekit.cpp mathutils.cpp py2d.cpp standalone.py.h
   COMMAND ${CYTHON_EXECUTABLE} ${CYTHON_DEBUG} ${CYTHON_OPTIONS} -o gamekit.cpp
          "${CMAKE_CURRENT_SOURCE_DIR}/gamekit.pyx"
   COMMAND ${CYTHON_EXECUTABLE} ${CYTHON_DEBUG} ${CYTHON_OPTIONS} -o mathutils.cpp
          "${CMAKE_CURRENT_SOURCE_DIR}/mathutils.pyx"
+  COMMAND ${CYTHON_EXECUTABLE} ${CYTHON_DEBUG} ${CYTHON_OPTIONS} -o py2d.cpp
+         "${CMAKE_CURRENT_SOURCE_DIR}/py2d/_py2d.pyx"
   COMMAND cp "${CMAKE_CURRENT_SOURCE_DIR}/standalone.py" .
   COMMAND xxd -i standalone.py standalone.py.h
 )
-list(APPEND ADDITIONAL_MAKE_CLEAN_FILES gamekit.cpp mathutils.cpp standalone.py.h)
+list(APPEND ADDITIONAL_MAKE_CLEAN_FILES gamekit.cpp mathutils.cpp py2d.cpp standalone.py.h)
 
-set(PYGAMEKIT_BUILD_DIR ${CMAKE_CURRENT_BINARY_DIR})
+add_library(pygamekit STATIC gamekit.cpp ${PYGAMEKIT_SRC})
+add_library(mathutils STATIC mathutils.cpp ${PYGAMEKIT_SRC})
+add_library(py2d STATIC py2d.cpp ${PYGAMEKIT_SRC})
 
-#add_custom_target(gamekit.cpp DEPENDS gamekit.pyx)
-#add_dependencies(gamekit.cpp gamekit.pyx)
+set_target_properties(pygamekit mathutils PROPERTIES
+  COMPILE_FLAGS "-fno-permissive" )
+  
 
-# Compile the extension
-if (PYGAMEKIT_AS_MODULES OR OGREKIT_BUILD_ANDROID)
-    add_library(cython_gamekit MODULE gamekit.cpp ${PYGAMEKIT_SRC})
-    # Build mathutils as a separate module instead
-    add_library(cython_mathutils MODULE mathutils.cpp ${PYGAMEKIT_SRC})
-    set_target_properties(cython_mathutils PROPERTIES
-    PREFIX ""
-    OUTPUT_NAME "mathutils${PY_SUFFIX}"
-    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
-    COMPILE_FLAGS "-fno-permissive" )
-    target_link_libraries(cython_mathutils ${OGREKIT_LIB} ${PYTHON_LIBRARIES})
-else()
-    add_executable(cython_gamekit gamekit.cpp mathutils.cpp ${PYGAMEKIT_SRC} standalone.cpp)
-endif()
+add_executable(pygamekit_runtime standalone.cpp)
+target_link_libraries(pygamekit_runtime pygamekit mathutils py2d ${OGREKIT_LIB} ${PYTHON_LIBRARIES})
+set_target_properties(pygamekit_runtime PROPERTIES
+  OUTPUT_NAME "gamekit"
+  COMPILE_FLAGS "-fno-permissive" )
 
-set_target_properties(cython_gamekit PROPERTIES
-  PREFIX ""
-  OUTPUT_NAME "gamekit${PY_SUFFIX}"
-  LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
-  COMPILE_FLAGS "-fno-permissive" )
-target_link_libraries(cython_gamekit ${OGREKIT_LIB} ${PYTHON_LIBRARIES})
-if (OGREKIT_BUILD_ANDROID)
-  target_link_libraries(cython_gamekit log)
-endif()
 include_directories(
     ${PYTHON_INCLUDE_PATH}
     ${OGREKIT_INCLUDE}
 
 cdef class BlendFile:
 
+    # TODO: property list with scenes and other data
 
     cdef gkBlendFile* bfile
     
         if filename:
             self.load(filename, options, scene, group)
 
-    def load(self, filename, options = ONLY_ACTIVE_SCENE, scene = "", group = "", seek = 0):
+    def load(self, filename, options = ONLY_ACTIVE_SCENE, scene = "", group = ""):
         
         self.bfile = gkBlendLoader().loadFile(gkUtils_getFile(gkString(filename)), options, gkString(scene), gkString(group))
 
         cdef gkVector3 ray_from, ray_to
         cdef gkGameObject *obj, *hit
         cdef bool result
+        cdef gkRayTestFilter *tfilter
         
         if self.owner is not None:
             obj = <gkGameObject*>self.owner._p
                 ray_to.iadd(ray_from)
             
             if self.xray:
-                result = self.test.collides(ray_from, ray_to,
-                                            xrayFilter(obj, self.prop[0], gkString("")))
+                tfilter = new xrayFilter(obj, self.prop[0], gkString(""))
+                result = self.test.collides(ray_from, ray_to, tfilter[0])
             else:
-                result = self.test.collides(ray_from, ray_to, notMeFilter(obj))
+                tfilter = new notMeFilter(obj)
+                result = self.test.collides(ray_from, ray_to, tfilter[0])
                 if result:
                     hit = gkPhysicsController_castObject(self.test.getCollisionObject())
                     if hit:
                         result = result and gkPhysicsController_sensorTest(
                             hit, self.prop[0], gkString(""), self.only_actors, False)
                             
+            del tfilter
+            
             return result                            
         return False
         
 
         void loadResources(gkString& name)
 
-        #void addDebugProperty(gkVariable* prop)
-        #void removeDebugProperty(gkVariable* prop)
+        void addDebugProperty(gkVariable* prop)
+        void removeDebugProperty(gkVariable* prop)
 
         gkScalar getStepRate()
         gkScalar getTickRate()
     gkEngine* gkEngine_getSingleton "&gkEngine::getSingleton"()
 
 
-#cdef extern from "gkApplication.h":
-#    int mein(gkEngine*)
-
 cdef extern from "Graphics/gkHUDManager.h":
     cdef cppclass gkHUDManager:
         pass
     
     cdef Scene _act_scene
 
-    def __init__(self, bfile=None, cfgfile=None, initialize=True, seek=0):
+    def __init__(self, bfile=None, cfgfile=None, initialize=True):
         
         global initialized, current_engine
         
                 #self.mgr = new gkHUDManager()
 
                 if bfile:
-                    self.load_blend(bfile, seek)
+                    self.load_blend(bfile)
                     
                 self.eng.initializeStepLoop()
             else:
             #traceback.print_exc()
         return self.eng.stepOneFrame()
     
-    def load_blend(self, bfile, seek=0):
+    def load_blend(self, bfile):
         cdef gkScene* scn
         
         b = BlendFile()
-        b.load(bfile,IGNORE_CACHE_FILE,seek=seek)
+        b.load(bfile, IGNORE_CACHE_FILE)
+        scn = self.eng.getActiveScene()
         
-        scn = self.eng.getActiveScene()
         
         if scn:
             gkSceneManager().copyObjects(b.bfile.getMainScene(), scn, OBJ_CAMERA | OBJ_LIGHT)
     return current_engine
     
     
-def get_act_scene():
+def get_active_scene():
     if current_engine!=None:
         return current_engine.active_scene
 
 from gameobject cimport *
 from gkmath cimport *
 
-cdef extern from "OgreMovableObject.h":
-
-    cdef cppclass Ogre_MovableObject "Ogre::MovableObject":
-        gkString& getMovableType()
-        
-    Ogre_Entity *cast_entity "static_cast<Ogre::Entity *>" (void *)
-    
-cdef extern from "OgreEntity.h":
-    enum:
-        VES_TANGENT "Ogre::VES_TANGENT"
-        
-    cdef cppclass Ogre_Entity "Ogre::Entity":
-        void buildTangentVectors "getMesh()->buildTangentVectors"(
-            int, unsigned short sourceTexCoordSet, unsigned short index, bool splitMirrored, bool splitRotated, bool storeParityInW)
-        
-        void reload "getMesh()->reload"()
-
-
 cdef extern from "gkEntity.h":
     cppclass gkEntity(gkGameObject):
         gkEntityProperties&  getEntityProperties()
         gkVertex&             getMeshVertex(int)
         int                   getMeshTriFaceCount()
         gkTriFace             getMeshTriFace(int)
+        void                  reload()
     
 cdef extern from "gkMeshManager.h":
     cppclass gkMeshManager "gkMeshManager::getSingleton":
 
 from entity cimport *
 from gkmath cimport *
+from _py2d import Polygon
 
+cdef inline gkVector3 point2vector3(p):
+    return gkVector3(p[0], p[1], 0)
+    
 cdef class Entity(GameObject):
 
     def __init__(self, name='Unnamed'):
         ob.getEntityProperties().m_mesh = gkMeshManager().create(ob.getName())
         self._p = ob
 
-        
-    def buildTangentVectors(self, sourceTexCoordSet):
-        # (<gkEntity*>self._p).getNode().getAttachedObject(0).getMovableType().c_str() == "Entity"
-        cast_entity((<gkEntity*>self._p).getNode().getAttachedObject(0)).buildTangentVectors(VES_TANGENT, sourceTexCoordSet, 0, True, False, True)
-
-    def test(self):
+    def add_polygon(self, polygon, material):
+        if not isinstance(polygon, Polygon):
+            polygon = Polygon.from_tuples(polygon)
+            
+        # TODO: UV transform?
+            
         cdef gkMesh *me = (<gkEntity*>self._p).getMesh()
-        #cdef SubMeshIterator smi = me.getSubMeshIterator()
-        #cdef gkSubMesh *sm = <gkSubMesh*>smi.getNext()
         cdef gkSubMesh *sm = new gkSubMesh()
         me.addSubMesh(sm)
-        sm.setMaterialName(gkString("Material"))
+        sm.setMaterialName(gkString(material))
         cdef gkVertex v0, v1, v2
-        i = me.getMeshVertexCount()
-        v0.co = gkVector3(2, 2, 0)
-        v2.co = gkVector3(2, -2, 0)
-        v1.co = gkVector3(-2, -2, 0)
-        v0.no = v1.no = v2.no = gkVector3(0,0,1)
-        v0.vba = i
-        v1.vba = i+1
-        v2.vba = i+2
-        sm.addTriangle(v0, i, v1, i+1, v2, i+2, TRI_COLLIDER)
+        cdef gkVector3 p0, p1, p2
+
+        cdef int i = me.getMeshVertexCount()
         
-        cast_entity((<gkEntity*>self._p).getNode().getAttachedObject(0)).reload()
+        for pol in Polygon.convex_decompose(polygon):
+            
+            i0 = i
+            p0 = point2vector3(pol[0])
+            p1 = point2vector3(pol[1])
+            i += 2
+            
+            for p in pol[2:]:
+                p2 = point2vector3(p)
+            
+                v0.co = p0
+                v1.co = p1
+                v2.co = p2
+                v0.no = v1.no = v2.no = gkVector3(0,0,1)
+                v0.vba = i0
+                v1.vba = i-1
+                v2.vba = i
+                v0.uv[0] = gkVector2(p0.x, p0.y)
+                v1.uv[0] = gkVector2(p1.x, p1.y)
+                v2.uv[0] = gkVector2(p2.x, p2.y)
+                sm.addTriangle(v0, i0,
+                                v1, i-1,
+                                v2, i,
+                                TRI_COLLIDER)
+                p1 = p2
+                
+                i += 1
+            
+        
+        me.reload()
 
 cdef class Mesh:
 
 #cdef class GameObject(pywrap):
     #pass
 
-cdef extern from "OgreSceneNode.h":
-    cppclass Ogre_MovableObject
-    
-    cdef cppclass Ogre_SceneNode "Ogre::SceneNode":
-        gkString& getName()
-        void setVisible(bool,bool)
-        
-        Ogre_MovableObject *getAttachedObject (int)
-
 cdef extern from "gkScene.h":
     cppclass gkScene
     
         gkString&   getName()
         gkString&   getGroupName()
         
-        Ogre_SceneNode*          getNode()
         int getType()
         gkGameObjectProperties&   getProperties()
         
         #bool                     hasParent()
         gkGameObject*             getParent()
         gkGameObjectArray&        getChildren()
+        
+        void setVisible(bool)
+        void setVisibleRecursive(bool)
+        bool getVisible()
 
         void setParent(gkGameObject* par)
         void setParentInPlace(gkGameObject* par)
         If `True` it will be visible; if `False` it will be invisible. Physics will be unaffected.'''
         
         def __get__(self):
-            return not (<gkGameObject*>self._p).getProperties().isInvisible()
+            return (<gkGameObject*>self._p).getVisible()
         
         def __set__(self, bool v):
-            cdef gkGameObjectProperties *prop = &((<gkGameObject*>self._p).getProperties())
-            prop.m_mode |= GK_INVISIBLE
-            if v:
-                prop.m_mode -= GK_INVISIBLE
-            (<gkGameObject*>self._p).getNode().setVisible(v, False)
+            (<gkGameObject*>self._p).setVisible(v)
         
 
 
 
 cdef extern from "gkMathUtils.h":
     cppclass gkVector2:
+        gkVector2()
+        gkVector2(gkScalar,gkScalar)
         gkScalar x
         gkScalar y
         bool isZeroLength()
         gkVector3& imul "operator*="(gkVector3)
         gkVector3& idiv "operator/="(gkScalar)
         gkVector3& idiv "operator/="(gkVector3)
+        gkVector3 operator- (gkVector3)
         gkScalar *ptr()
         gkRadian angleBetween(gkVector3)
         gkQuaternion getRotationTo(gkVector3)
     
     cppclass gkRadian:
         gkRadian(gkScalar)
-        gkScalar mRad
         gkScalar valueRadians()
         gkScalar valueDegrees()
         

prerequisites.pxd

 cdef inline int isnan "_gk_isnan"(gkScalar f):
     return f!=f
         
-cdef extern from "gkString.h": 
-    cppclass gkString: 
-        gkString()
-        gkString(char*)
-        char* c_str()
-        #gkString& assign ( gkString& str )
-        #gkString& assign ( gkString& str, size_t pos, size_t n )
-        #gkString& assign ( char* s, size_t n )
-        gkString& assign ( char* s )
-        #gkString& assign ( size_t n, char c )
-        
 cdef extern from "gkUtils.h" namespace "gkUtils":
     cdef gkString getUniqueName(gkString&)
         
         reserve(UTsize nr)
         sort(bool (*cmp)(T &a, T &b))
         T &getitem "&operator[]"(UTsize idx)
+        T operator[](UTsize idx)
         UTsize capacity()
         UTsize size()
         
         T      getNext()
         void   next()
         T      peekNext()
+        
+    cppclass utStack[T]:
+        utStack()
+        utStack(utStack &o)
+        void clear()
+        void push(T &v)
+        T &pop()
+        T &top()
+        T &peek(UTsize offset)
+        UTsize size()
+        UTsize itop()
+        bool empty()
+        
+        
+cdef extern from "gkString.h": 
+    cppclass gkString: 
+        gkString()
+        gkString(char*)
+        char* c_str()
+        gkString& assign ( gkString& str )
+        gkString& assign ( gkString& str, size_t pos, size_t n )
+        gkString& assign ( char* s, size_t n )
+        gkString& assign ( char* s )
+        gkString& assign ( size_t n, char c )
+        
+    ctypedef utArray[gkString] gkStringVector
+    #ctypedef utArrayIterator[gkString] gkStringVectorIterator
+    cppclass gkStringVectorIterator:
+        gkStringVectorIterator()
+        gkStringVectorIterator(gkStringVector)
+        bool     hasMoreElements()
+        gkString getNext()
+        void     next()
+        gkString peekNext()
 
-        
-        
+    
+    
 from libcpp.vector cimport vector
 
 cdef extern from *:
     
 cdef extern from "Sound/gkSoundManager.h":
     cppclass gkSoundManager "gkSoundManager::getSingleton":
+        gkSoundManager()
         gkSound* getByName "getByName<gkSound>"(gkString&)
         gkSound* create "create<gkSound>"(gkString&)
         void stopAllSounds()
 #if PY_MAJOR_VERSION < 3
 PyMODINIT_FUNC initgamekit();
 PyMODINIT_FUNC initmathutils();
+PyMODINIT_FUNC init_py2d();
 #else
 PyMODINIT_FUNC PyInit_gamekit(void);
 PyMODINIT_FUNC PyInit_mathutils(void);
+PyMODINIT_FUNC PyInit__py2d();
 #endif
 
 #include "standalone.py.h"
 
   Py_Initialize();
   #if PY_MAJOR_VERSION < 3
+    init_py2d();
     initmathutils();
     initgamekit();
   #else
+    PyInit__py2d();
     PyInit_mathutils(void);
     PyInit_gamekit(void);
   #endif
             p = a
         else:
             bname = a
-e = gamekit.Engine(bname,cfgfile)
+e = gamekit.Engine(bname, cfgfile)
 
 try:
     print('Loading init...')