Using PyGame with PyOgre
From Ogre Wiki
Using PyGame with PyOgre
Original version by Josh Taylor (deficite)
PyGame with PyOgre.....why?
PyOgre, like C++ OGRE, was not intended as a complete game SDK. Therefore one must find other API's to fill in the gaps (a game is more than just graphics). PyGame is a good choice for this because it provides close to everything PyOgre doesn't. Anything one can do with SDL can be accomplished using PyGame. Check it out at http://www.pygame.org
Getting PyOgre and PyGame to Meld
The process of getting PyOgre to render to a PyGame window is quite simple. The trick is simply to tell PyOgre not to create a window but instead use the window PyGame creates:
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._StringStringMap()
renderParameters['externalWindowHandle'] = str(pygame.display.get_wm_info()['window'])
self.renderWindow = self.root.createRenderWindow('PyOgre through PyGame', width, height, \
fullscreen, renderParameters)
self.renderWindow.active = True
Complete code example and application class
I have provided an application class (which the above code was taken from) to base programs off of or to study:
# Written by: Josh Taylor (deficite) <joshtaylor.mail@gmail.com>
import pygame
from pygame.locals import *
from pyogre import ogre
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.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._StringStringMap()
renderParameters['externalWindowHandle'] = str(pygame.display.get_wm_info()['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"
rc = ogre.ConfigFile()
rc.loadFromFile(rcfile)
for section, key, path in rc.values:
ogre.ResourceGroupManager.getSingleton().addResourceLocation(path, key, section)
ogre.ResourceGroupManager.getSingleton().initialiseAllResourceGroups()
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()
Press escape or the window manager's close button to exit the application.

