Understanding BufferedInputHandler

McFarlane

10-07-2012 10:03:41

Hi everyone,

I'm trying to understand how to use buffered input in the basics tutorial 6.
there is an example class called BufferedInputHandler. but where do I create this class?
The buffered input in the other examples work but they use the sample framework.

How do I setup buffered input using the setup in tutorial 6? I'm really stuck at this:(

Thanks in advance,
McFarlane

dermont

10-07-2012 14:14:42

Not sure what the BufferedInputHandler is supposed to show in relevance to the demo, but it should be a case of sub classing ,registering the listeners/callbacks and creating the keyboard/mouse as buffered.

You could probably combine the handler with the ExitListener or alternatively do this from your main application, e,g:


import sys
sys.path.insert(0,'..')
sys.path.insert(0,'.')
import PythonOgreConfig

import ogre.renderer.OGRE as ogre
import math
import ogre.io.OIS as OIS

##from sf_OIS import *
import os
import ctypes
import random

import ogre.renderer.OGRE as ogre
import ogre.io.OIS as OIS

### ------------------------------------------------------
class MouseCursor:
### ------------------------------------------------------
def __init__(self):
self.mMaterial = ogre.MaterialManager.getSingleton().create("MouseCursor/default", "General")
self.mCursorContainer = ogre.OverlayManager.getSingletonPtr().createOverlayElement("Panel", "MouseCursor")
self.mCursorContainer.setMaterialName(self.mMaterial.getName())
self.mCursorContainer.setPosition(0, 0)

self.mGuiOverlay = ogre.OverlayManager.getSingletonPtr().create("MouseCursor")
self.mGuiOverlay.setZOrder(649)
self.mGuiOverlay.add2D(self.mCursorContainer)
self.mGuiOverlay.show()

self.mWindowWidth = 1
self.mWindowHeight = 1
self.pos_X = 0
self.pos_Y = 0

self.mTexture = None


def __del__(self):
self._removeImage()
if self.mGuiOverlay:
self.mGuiOverlay.remove2D(self.mCursorContainer)
ogre.MaterialManager.getSingleton().remove(self.mMaterial.getName())
ogre.OverlayManager.getSingletonPtr().destroyOverlayElement("MouseCursor")
del self.mCursorContainer
del self.mMaterial
ogre.OverlayManager.getSingletonPtr().destroy(self.mGuiOverlay.getName())
del self.mGuiOverlay
print "Deleted Mouse Cursor"

def _removeImage(self):
if self.mTexture:
ogre.TextureManager.getSingletonPtr().remove(self.mTexture.getName())
del self.mTexture
self.mTexture = None

def setImage(self,filename):
self._removeImage()
self.mTexture = ogre.TextureManager.getSingleton().load(filename, "General")
matPass = self.mMaterial.getTechnique(0).getPass(0)
if matPass.getNumTextureUnitStates():
pTexState = matPass.getTextureUnitState(0)
else:
pTexState = matPass.createTextureUnitState( self.mTexture.getName() )
pTexState.setTextureAddressingMode(ogre.TextureUnitState.TAM_CLAMP)
matPass.setSceneBlending(ogre.SBT_TRANSPARENT_ALPHA)


def setWindowDimensions(self, width, height):
self.mWindowWidth = width
self.mWindowHeight = height
if self.mWindowWidth==0:
self.mWindowWidth=1
if self.mWindowHeight==0:
self.mWindowHeight=1

dx = self.mTexture.getWidth() / float(self.mWindowWidth )
dy = self.mTexture.getHeight() / float(self. mWindowHeight)
self.mCursorContainer.setWidth(dx)
self.mCursorContainer.setHeight(dy)

def setVisible(self,visible):
if(visible):
self.mCursorContainer.show()
else:
self.mCursorContainer.hide()

def updatePosition(self, x, y):
rx = float(x) / float(self.mWindowWidth)
ry = float(y) / float(self.mWindowHeight)
self.mCursorContainer.setPosition(ogre.Math.Clamp(rx, 0.0, 1.0), ogre.Math.Clamp(ry, 0.0, 1.0))
self.pos_X = rx
self.pos_Y = ry

class Application(ogre.FrameListener, OIS.KeyListener, OIS.MouseListener):

def __init__(self):
ogre.FrameListener.__init__(self)
OIS.KeyListener.__init__(self)
OIS.MouseListener.__init__(self)

self.sceneManager = None
self.camera = None
self.mouse = None
self.keyboard = None
self.inputManager=None
self.mCrosshair = None


def go(self):
self.createRoot()
self.defineResources()
self.setupRenderSystem()
self.createRenderWindow()
self.initializeResourceGroups()
self.setupScene()
self.setupInputSystem()
#self.setupCEGUI()
self.createScene()
self.createFrameListener()
self.startRenderLoop()
self.cleanUp()

# The Root constructor for the ogre
def createRoot(self):
if os.sys.platform.startswith("linux") or os.sys.platform == 'darwin':
self.root = ogre.Root('../plugins.cfg.posix')
else:
self.root = ogre.Root('../plugins.cfg.nt')

# Here the resources are read from the resources.cfg
def defineResources(self):
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)

# Create and configure the rendering system (either DirectX or OpenGL) here
def setupRenderSystem(self):
if not self.root.restoreConfig() and not self.root.showConfigDialog():
raise Exception("User canceled the config dialog -> Application.setupRenderSystem()")


# Create the render window
def createRenderWindow(self):
self.root.initialise(True, "Tutorial Render Window")

# Initialize the resources here (which were read from resources.cfg in defineResources()
def initializeResourceGroups(self):
ogre.TextureManager.getSingleton().setDefaultNumMipmaps(5)
ogre.ResourceGroupManager.getSingleton().initialiseAllResourceGroups()

# Now, create a scene here. Three things that MUST BE done are sceneManager, camera and
# viewport initializations
def setupScene(self):
sceneManager = self.root.createSceneManager(ogre.ST_GENERIC, "Default SceneManager")
camera = sceneManager.createCamera("Camera")
viewPort = self.root.getAutoCreatedWindow().addViewport(camera)
self.sceneManager = sceneManager
self.camera = camera
self.camera.setPosition(ogre.Vector3(0, 0, 500))
self.camera.lookAt(ogre.Vector3(0, 0, -300))
self.camera.NearClipDistance = 5


# here setup the input system (OIS is the one preferred with Ogre3D)
def setupInputSystem(self):
windowHandle = 0
renderWindow = self.root.getAutoCreatedWindow()
windowHandle = renderWindow.getCustomAttributeInt("WINDOW")
paramList = [("WINDOW", str(windowHandle))]
self.inputManager = OIS.createPythonInputSystem(paramList)

# Now InputManager is initialized for use. Keyboard and Mouse objects
# must still be initialized separately
try:
self.keyboard = self.inputManager.createInputObjectKeyboard(OIS.OISKeyboard, True)
self.mouse = self.inputManager.createInputObjectMouse(OIS.OISMouse, True)
except Exception, e:
raise e

renderWindow = self.root.getAutoCreatedWindow()
dummyint = 0
width, height, depth, left, top= renderWindow.getMetrics(dummyint,dummyint,dummyint, dummyint, dummyint) # Note the wrapped function as default needs unsigned int's
ms = self.mouse.getMouseState()
ms.width = width
ms.height = height

def createScene(self):
# create head entity
headNode = self.sceneManager.getRootSceneNode().createChildSceneNode()
entity = self.sceneManager.createEntity('head', 'ogrehead.mesh')
headNode.attachObject(entity)

renderWindow = self.root.getAutoCreatedWindow()

self.mCrosshair = MouseCursor()
self.mCrosshair.setImage("dark_grid.png")
self.mCrosshair.setVisible(True)
self.mCrosshair.setWindowDimensions(renderWindow.getWidth(), renderWindow.getHeight())


# Create the frame listeners
def createFrameListener(self):
self.mouse.setEventCallback(self)
self.keyboard.setEventCallback(self)
self.root.addFrameListener(self)

# This is the rendering loop
def startRenderLoop(self):
self.root.startRendering()

# In the end, clean everything up (= delete)
def cleanUp(self):
del self.mCrosshair
self.inputManager.destroyInputObjectKeyboard(self.keyboard)
self.inputManager.destroyInputObjectMouse(self.mouse)
OIS.InputManager.destroyInputSystem(self.inputManager)
self.inputManager = None

def frameEnded(self, evt):
return ogre.FrameListener.frameEnded(self,evt)

def frameStarted(self, evt):
return ogre.FrameListener.frameStarted(self,evt)


def frameRenderingQueued(self, evt):
self.keyboard.capture()
self.mouse.capture()
return not self.keyboard.isKeyDown(OIS.KC_ESCAPE)

# KeyListener
def keyPressed(self, evt):
print "Key Pressed {0}".format(evt.text)
return True
def keyReleased(self, evt):
print "Key Released {0}".format(evt.text)
return True

# MouseListener
def mouseMoved(self, evt):
if not self.mCrosshair:
return
ms = self.mouse.getMouseState()
## update mouse cursor position to sync with OS mouse
## since we are running in non exclusive mode
self.mCrosshair.updatePosition(ms.X.abs, ms.Y.abs)

return True
def mousePressed(self, evt, id):
return True
def mouseReleased(self, evt, id):
return True

# JoystickListener
def buttonPressed(self, evt, button):
return True
def buttonReleased(self, evt, button):
return True
def axisMoved(self, evt, axis):
return True
if __name__ == '__main__':
try:
ta = Application()
ta.go()
except ogre.OgreException, e:
print e