Source

pygamekit / engine.pxi

Full commit

import sys, imp

cdef extern from "gkLogger.h":
    #cdef cppclass gkLogger:
    void gkLogger_enable "gkLogger::enable"(const_std_string&, bool verbose)
    void gkLogger_disable "gkLogger::disable"()
    void gkLogger_write "gkLogger::write"(const_std_string& msg, bool force = 0)
    
    void gkPrintf(char *fmt, ...)
    int GK_CPP_VERBOSE_LOG
    
cdef extern from "gkUserDefs.h":
    cdef cppclass gkUserDefs:
        void load(const_std_string& fname)
        void parseString(const_std_string& key, const_std_string& val)


        OgreRenderSystem        rendersystem       # Ogre render system to use
        std_string              viewportOrientation# portrait, landscaperight, landscapeleft
        int                     sceneManager       # TODO scene manager to use
        std_string              log                # Main log file name
        bool                    verbose            # Enable/Disable Ogre print-out
        gkVector2               winsize            # Window width & height
        std_string              wintitle           # Window title
        std_string              extWinhandle       # External Window Handle
        bool                    fullscreen         # Fullscreen mode
        int                     framingType        # crop/letterbox/extend. Used when window is larger then requested
        std_string              resources          # resources to load
        bool                    blendermat         # convert meshes using blender materials
        bool                    grabInput          # hide & grab the mouse
        bool                    debugFps           # show fps and profiling information
        bool                    debugPhysics       # enable / disable physics debugging
        bool                    debugPhysicsAabb   # show / hide bounding box
        bool                    buildStaticGeometry# Use Static geometry
        bool                    useBulletDbvt      # Use Bullet Dynamic AABB Tree
        bool                    showDebugProps     # Show variable debugging information.
        bool                    debugSounds        # Show 3D sound debug info
        bool                    disableSound       # Disable OpenAL sound.
        bool                    fsaa               # Enable Full scene anti aliasing.
        int                     fsaaSamples        # Anti aliasing samples.
        bool                    enableshadows
        int                     defaultMipMap      # Number of mipmaps to generate per texture (default 5)

        std_string                shadowtechnique
        #gkColor                 colourshadow
        #gkScalar                fardistanceshadow

        #static OgreRenderSystem getOgreRenderSystem(const_std_string& val)
        int getViewportFramingType(const_std_string& val)
        
cdef extern from "gkEngine.h":
    
    cdef cppclass Listener:
        pass
    cdef cppclass gkEngine:
                    
        gkEngine(gkUserDefs *defs)
        
        void initialize()
        void finalize()
        void run()

        bool initializeStepLoop()
        bool stepOneFrame()
        void finalizeStepLoop()

        bool isInitialized()
        bool isRunning()

        void initializeWindow()

        bool hasActiveScene()

        gkUserDefs& getUserDefs()
        void requestExit()

        void loadResources(const_std_string& name)

        #void addDebugProperty(gkVariable* prop)
        #void removeDebugProperty(gkVariable* prop)

        Real getStepRate()
        Real getTickRate()

        gkScene* getActiveScene()

        void registerActiveScene(gkScene* scene)
        void unregisterActiveScene(gkScene* scene)

        void addListener(Listener* listener)
        void removeListener(Listener* listener)

    gkEngine* gkEngine_getSingleton "&(gkEngine::getSingleton)"()


#cdef extern from "gkApplication.h":
#    int mein(gkEngine*)

cdef extern from "Graphics/gkHUDManager.h":
    cdef cppclass gkHUDManager:
        pass

cdef extern from "Graphics/gkHUD.h":
    cdef cppclass gkHUD:
        pass

cdef extern from "Graphics/gkHUDElement.h":
    cdef cppclass gkHUDElement:
        pass

cdef extern from "gkTextFile.h":
    cdef cppclass gkTextFile:
        std_string& getText()

cdef extern from "gkTextManager.h":
    void* gkTextManager_getByName "gkTextManager::getSingleton().getByName"(const_std_string& name)
    bool gkTextManager_exists "gkTextManager::getSingleton().exists"(const_std_string& name)
    
cdef Sensor tick_callback = Sensor()
cdef bool initialized = False

cdef class Engine:
    cdef gkEngine *eng
    #cdef gkHUDManager* mgr
    
    tickSensor = TickSensor()

    def __init__(self, bfile=None, cfgfile=None, initialize=True, seek=0):
        
        global initialized
        
        if initialize:
            if cfgfile:
                prefs.load(to_stdstring(cfgfile))
                
            self.eng = new gkEngine(prefs)
            self.eng.initialize()
            if not self.eng.isInitialized():
                print("Failed to initialize enigne.")
                sys.exit(1)
            #self.mgr = new gkHUDManager()

            if bfile:
                self.loadBlend(bfile, seek)
                
            self.eng.initializeStepLoop()
            initialized = True
            
    def __del__(self):
        self.eng.requestExit()

    def getActiveScene(self):
        return Scene().P(self.eng.getActiveScene())
        
    def initializeStepLoop(self): self.eng.initializeStepLoop()

    def stepOneFrame(self):
        try:
            evaluate_input_callbacks()
            evaluate_collision_callbacks()
            evaluate_tick_callbacks()
        except SystemExit:
            return False
        return self.eng.stepOneFrame()
    
    def loadBlend(self, bfile, seek=0):
        cdef gkScene* scn
        
        b = BlendFile()
        b.load(bfile,IGNORE_CACHE_FILE,seek=seek)
        
        scn = self.eng.getActiveScene()
        
        if scn:
            copyObjects(b.bfile.getMainScene(), scn, OBJ_CAMERA | OBJ_LIGHT)
        else:
            scn = b.bfile.getMainScene()
            
            self.eng.registerActiveScene(scn)
            
            scn.createInstance()
            
    def run(self):
        while self.stepOneFrame():
            pass

cdef class TickSensor(Sensor):
    
    def __init__(self):
        self.callback_list = tick_callback
        
    def __nonzero__(self):
        return True
            
cdef void evaluate_tick_callbacks() except *:
    cdef TickSensor t = tick_callback.next_non_empty()
    while t!=None:
        t.execute()
        t = t.next_non_empty()
        
def _evaluate_all_callbacks():
    evaluate_input_callbacks()
    evaluate_collision_callbacks()
    evaluate_tick_callbacks()
        
def getEngine():
    e = Engine(None, None, False)
    e.eng = gkEngine_getSingleton()
    return e
    
def getActiveScene():
    cdef gkEngine* e = gkEngine_getSingleton()
    return Scene().P(e.getActiveScene())

class TextBlockImporter(object):
    def find_module(self, module_name, package_path):
        if package_path: return
        module_name += '.py'
        if initialized and gkTextManager_exists(to_stdstring(module_name)):
            return self
            
    def load_module(self, module_name):
        mod = imp.new_module(module_name)
        sys.modules[module_name] = mod
        module_name += '.py'
        code = str((<gkTextFile*>gkTextManager_getByName(to_stdstring(module_name))).getText().c_str())
        exec code in mod.__dict__
        return mod

sys.meta_path.append(TextBlockImporter())
del TextBlockImporter

def hasTextBlock(name):
    if initialized: return gkTextManager_exists(to_stdstring(name))
    
def getTextBlock(name):
    if initialized:
        return str((<gkTextFile*>gkTextManager_getByName(to_stdstring(name))).getText().c_str())