inneractive
11-06-2007 06:51:12
I'm continuing my Python-Ogre education by building a minimal application that is not dependent on the SampleFramework. I'm using the "MinimalApplication" Ogre Wiki entry and sf_OIS.py as inspiration. Right now I have code to get an Ogre render window up with a frame listener. Any comments, corrections, or improvement suggestions would be welcome. When it is finished I can add it to the Python-Ogre wiki.
Right now I guess I need to figure out clean up. Will python garbage collection not be enough? Should I manually release resources? Also, I noticed that the Ogre Config Dialog automatically loads up all the plugins and resources in my .cfg files. So I guess I need to create my own config dialog if I want to create an app that does not rely on those .cfg files.
Here is the code so far:
Right now I guess I need to figure out clean up. Will python garbage collection not be enough? Should I manually release resources? Also, I noticed that the Ogre Config Dialog automatically loads up all the plugins and resources in my .cfg files. So I guess I need to create my own config dialog if I want to create an app that does not rely on those .cfg files.
Here is the code so far:
#!/usr/bin/env python
# This code is Public Domain and was written for Python-Ogre 1.0.
"""minimal_application.py sets up a basic framework for interacting with Ogre."""
import ogre.renderer.OGRE as ogre
import ogre.io.OIS as OIS
class Application(object):
"""Application class is the base for an Ogre application."""
def __init__(self):
self.root = None
self.renderWindow = None
self.sceneManager = None
self.camera = None
self.viewport = None
self.frameListener = None
def start(self):
"""Starts the rendering loop."""
if not self.__setup():
return False
# Render until a frame listner returns false.
self.root.startRendering()
def __setup(self):
"""Sets up the ogre application."""
self.root = ogre.Root()
self.root.setFrameSmoothingPeriod (5.0)
if not self.__configure():
return False
self.__chooseSceneManager()
self.__createCamera()
self.__createViewport()
self.__createFrameListener()
return True
def __configure(self):
"""This shows the config dialog and creates the Ogre Render Window."""
if not self.root.showConfigDialog():
return False
self.renderWindow = self.root.initialise(True, "OGRE Render Window")
return True
def __chooseSceneManager(self):
"""Chooses a default SceneManager."""
self.sceneManager = self.root.createSceneManager(ogre.ST_GENERIC,"GenericSM")
def __createCamera(self):
"""Creates the camera."""
self.camera = self.sceneManager.createCamera('UserCam')
def __createViewport(self):
"""Creates the Viewport."""
self.viewport = self.renderWindow.addViewport(self.camera)
def __createFrameListener(self):
"""Creates the FrameListener."""
self.frameListener = FrameListener(self.renderWindow, self.camera)
self.root.addFrameListener(self.frameListener)
class FrameListener(ogre.FrameListener, ogre.WindowEventListener):
"""FrameListener class, which takes care of basic mouse and keyboard input."""
def __init__(self, renderWindow, camera):
ogre.FrameListener.__init__(self)
ogre.WindowEventListener.__init__(self)
self.camera = camera
self.renderWindow = renderWindow
self.inputManager = None
self.keyboard = None
self.mouse = None
self.__setupInput()
def frameStarted(self, frameEvent):
# If the render window is closed, quit the application.
if(self.renderWindow.isClosed()):
return False
# If the escape key is pressed quit the application.
self.keyboard.capture()
return not self.keyboard.isKeyDown(OIS.KC_ESCAPE)
def __setupInput(self):
"""Sets up the OIS input manager and user input devices."""
# Setup OIS with the Ogre Render Window.
window = self.renderWindow.getCustomAttributeInt("WINDOW")
self.inputManager = OIS.createPythonInputSystem([("WINDOW",str(window))])
# Setup the user input devices.
self.keyboard = self.inputManager.createInputObjectKeyboard(OIS.OISKeyboard, False)
# Add the render window as an event listener.
ogre.WindowEventUtilities.addWindowEventListener(self.renderWindow, self);
if __name__ == '__main__':
# Import Psyco if it is available.
try:
import psyco
psyco.full()
except ImportError:
pass
# Start the application.
try:
app = Application()
app.start()
except ogre.OgreException, e:
print e