#Written by: Josh Taylor (deficite) joshtaylor dot mail at gmail dot com # and Gordon Allott import pygame from pygame.locals import * import ogre.renderer.OGRE as ogre from ctypes import * class PyGameOGREApp: "Provides a base for an application using PyGame and PyOgre" def __init__(self, width=640, height=480, fullscreen=False): self._initPyGame() self._initPyOgre() self._createWindow(width, height, fullscreen) self._createViewport() self._loadResources("resources.cfg") self._createEntities() def _initPyGame(self): "Starts up PyGame" pygame.init() def _initPyOgre(self): "Instantiates the PyOgre root and sceneManager objects and initialises" self.root = ogre.Root("plugins.cfg") self.root.showConfigDialog() self.root.initialise(False) self.sceneManager = self.root.createSceneManager(ogre.ST_GENERIC, "Default SceneManager") #self.root.getSceneManager(ogre.ST_GENERIC) def _createWindow(self, width, height, fullscreen): "Creates a PyGame window and sets PyOgre to render onto it" self.screen = pygame.display.set_mode((width,height)) renderParameters = ogre.NameValuePairList() window = pygame.display.get_wm_info()['window'] display = pygame.display.get_wm_info()['display'] #prototype for PyCObject_AsVoidPtr pythonapi.PyCObject_AsVoidPtr.restype = c_void_p pythonapi.PyCObject_AsVoidPtr.argtypes = [py_object] #we use the python api to get the display structure that ogre wants display_address = pythonapi.PyCObject_AsVoidPtr(display) renderParameters['parentWindowHandle'] = str(display_address) + ':0:' + str(window) self.renderWindow = self.root.createRenderWindow('PyOgre through PyGame', width, height, \ fullscreen, renderParameters) self.renderWindow.active = True def _createViewport(self): "Creates the user's viewport and camera" self.camera = self.sceneManager.createCamera("camera") self.camera.position = (0, 0, 500) self.camera.lookAt((0, 0, -300)) self.camera.nearClipDistance = 5 self.viewport = self.renderWindow.addViewport(self.camera) self.viewport.backgroundColour = (0, 0, 0) def _loadResources(self, rcfile): "Loads the resource paths from specified resource config file" cf = ogre.ConfigFile() cf.load("resources.cfg") seci = cf.getSectionIterator() while seci.hasMoreElements(): secName = seci.peekNextKey() settings = seci.getNext() for item in settings: typeName = item.key archName = item.value ogre.ResourceGroupManager.getSingleton().addResourceLocation(archName, typeName, secName) def _createEntities(self): "For simple demo purposes this can be used to create entities and attach them to sceneNode's" self.entities = [self.sceneManager.createEntity("robot", "robot.mesh")] self.sceneNodes = [self.sceneManager.rootSceneNode.createChildSceneNode("robotNode")] for i in range(len(self.sceneNodes)): self.sceneNodes[i].attachObject(self.entities[i]) def _createScene(self): "Prepare the scene. All logic should go here" pass def _presentScene(self): "Render the scene and anything else needed done at the time of presentation" self.root.renderOneFrame() def run(self): "Brings the application to life" while self._processEvents(): self._createScene() self._presentScene() pygame.quit() def _processEvents(self): "Process events and take appropriate action" for event in pygame.event.get(): if event.type is QUIT: return False elif event.type is KEYDOWN and event.key is K_ESCAPE: return False return True # Instantiate and run! app = PyGameOGREApp() app.run()