Difference between revisions of "User:Crond/sandbox/openglExample"

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Jump to: navigation, search
m (2.5)
m (Requirements: superfluous)
 
(5 intermediate revisions by the same user not shown)
Line 6: Line 6:
  
 
== Requirements ==
 
== Requirements ==
* Python 2.6 (untested on Python 3.+)
+
* Python 2.6
 
* PyCEGUI
 
* PyCEGUI
* PyOpenGL (GLU and GLUT, as well)
+
* PyOpenGL
 +
 
 +
== Notes ==
 +
This example assumes you have the CEGUI resources located in the same directory as the script; change `PATH_RESOURCES` if they are elsewhere.
  
 
== Source ==
 
== Source ==
Line 19: Line 22:
  
 
# Import: std
 
# Import: std
import sys
+
import sys, os
  
 
# Import: PyOpenGL
 
# Import: PyOpenGL
Line 37: Line 40:
 
class Application(object):
 
class Application(object):
  
  # Constructor
+
    # Constructor
  def __init__(self):
+
    def __init__(self):
    super(Application, self).__init__()
+
        super(Application, self).__init__()
    self.lastFrameTime = 0
+
        self.lastFrameTime = 0
    self.updateFPS = 0
+
        self.updateFPS = 0
     return
+
        return
 +
 
 +
     # Initialize: OpenGL
 +
    # - A full list of values for `glutInitDisplayMode` can be found in the GLUT
 +
    #  documentation.
 +
    def initializeOpenGL(self):
 +
        glutInit()
 +
        glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA)
 +
        glutInitWindowSize(1024, 768)
 +
        glutInitWindowPosition(-1, -1) # Let the windowing system figure it out
 +
        glutCreateWindow("Crazy Eddie's GUI Mk-2 - glut Base Application")
 +
        glutSetCursor(GLUT_CURSOR_NONE)
 +
        return
  
  # Initialize: OpenGL
+
    # Initialize: Handlers
  # - A full list of values for `glutInitDisplayMode` can be found in the GLUT
+
    # - Setup the methods which will be called when events happen.
  #  documentation.
+
    def initializeHandlers(self):
  def initializeOpenGL(self):
+
        glutDisplayFunc(self.handlerDisplay)
    glutInit()
+
        glutReshapeFunc(self.handlerResize)
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA)
+
        glutMouseFunc(self.handlerMouse)
    glutInitWindowSize(1024, 768)
+
        glutMotionFunc(self.handlerMouseMotion)
    glutInitWindowPosition(-1, -1) # Let the windowing system figure it out
+
        glutPassiveMotionFunc(self.handlerMouseMotion)
    glutCreateWindow("Crazy Eddie's GUI Mk-2 - glut Base Application")
+
        return
    glutSetCursor(GLUT_CURSOR_NONE)
+
    return
+
  
  # Initialize: Handlers
+
    # Initialize: PyCEGUI
  # - Setup the methods which will be called when events happen.
+
    # - Store some components; saves a lot of typing.
  def initializeHandlers(self):
+
    def initializePyCEGUI(self):
    glutDisplayFunc(self.handlerDisplay)
+
        Renderer.bootstrapSystem()
    glutReshapeFunc(self.handlerResize)
+
        self.sys = PyCEGUI.System.getSingleton()
    glutMouseFunc(self.handlerMouse)
+
        self.rp = self.sys.getResourceProvider()
    glutMotionFunc(self.handlerMouseMotion)
+
        self.scheme = PyCEGUI.SchemeManager.getSingleton()
    glutPassiveMotionFunc(self.handlerMouseMotion)
+
        self.wm = PyCEGUI.WindowManager.getSingleton()
    return
+
        return
  
  # Initialize: PyCEGUI
+
    # Initialize: Defaults
  # - Store some components; saves a lot of typing.
+
    # - Resource locations.
  def initializePyCEGUI(self):
+
    def initializeDefaults(self):
    Renderer.bootstrapSystem()
+
        self.rp.setResourceGroupDirectory('schemes', os.path.join(PATH_RESOURCES, 'datafiles/schemes'))
    self.sys = PyCEGUI.System.getSingleton()
+
        self.rp.setResourceGroupDirectory('imagesets', os.path.join(PATH_RESOURCES, 'datafiles/imagesets'))
    self.rp = self.sys.getResourceProvider()
+
        self.rp.setResourceGroupDirectory('fonts', os.path.join(PATH_RESOURCES, 'datafiles/fonts'))
    self.scheme = PyCEGUI.SchemeManager.getSingleton()
+
        self.rp.setResourceGroupDirectory('layouts', os.path.join(PATH_RESOURCES, 'datafiles/layouts'))
    self.wm = PyCEGUI.WindowManager.getSingleton()
+
        self.rp.setResourceGroupDirectory('looknfeels', os.path.join(PATH_RESOURCES, 'datafiles/looknfeel'))
    return
+
        self.rp.setResourceGroupDirectory('schemas', os.path.join(PATH_RESOURCES, 'datafiles/xml_schemas'))
 +
        PyCEGUI.Imageset.setDefaultResourceGroup('imagesets')
 +
        PyCEGUI.Font.setDefaultResourceGroup('fonts')
 +
        PyCEGUI.Scheme.setDefaultResourceGroup('schemes')
 +
        PyCEGUI.WidgetLookManager.setDefaultResourceGroup('looknfeels')
 +
        PyCEGUI.WindowManager.setDefaultResourceGroup('layouts')
 +
        parser = self.sys.getXMLParser()
 +
        if parser.isPropertyPresent('SchemaDefaultResourceGroup'):
 +
            parser.setProperty('SchemaDefaultResourceGroup', 'schemas')
 +
        return
  
  # Initialize: Defaults
+
    # Initialize: GUI
  # - Resource locations.
+
    # - This is where we are actually setting up the windows we will see.
  def initializeDefaults(self):
+
    def initializeGUI(self):
    self.rp.setResourceGroupDirectory('schemes', './datafiles/schemes')
+
        self.scheme.create('VanillaSkin.scheme')
    self.rp.setResourceGroupDirectory('imagesets', './datafiles/imagesets')
+
        self.scheme.create('TaharezLook.scheme')
    self.rp.setResourceGroupDirectory('fonts', './datafiles/fonts')
+
    self.rp.setResourceGroupDirectory('layouts', './datafiles/layouts')
+
    self.rp.setResourceGroupDirectory('looknfeels', './datafiles/looknfeel')
+
    self.rp.setResourceGroupDirectory('schemas', './datafiles/xml_schemas')
+
    PyCEGUI.Imageset.setDefaultResourceGroup('imagesets')
+
    PyCEGUI.Font.setDefaultResourceGroup('fonts')
+
    PyCEGUI.Scheme.setDefaultResourceGroup('schemes')
+
    PyCEGUI.WidgetLookManager.setDefaultResourceGroup('looknfeels')
+
    PyCEGUI.WindowManager.setDefaultResourceGroup('layouts')
+
    parser = self.sys.getXMLParser()
+
    if parser.isPropertyPresent('SchemaDefaultResourceGroup'):
+
      parser.setProperty('SchemaDefaultResourceGroup', 'schemas')
+
    return
+
  
  # Initialize: GUI
+
        # GUISheet
  # - This is where we are actually setting up the windows we will see.
+
        self.rootWindow = self.wm.loadWindowLayout('VanillaWindows.layout')
  def initializeGUI(self):
+
        self.sys.setGUISheet(self.rootWindow)
    self.scheme.create('VanillaSkin.scheme')
+
    self.scheme.create('TaharezLook.scheme')
+
  
    # GUISheet
+
        # Cursor
    self.rootWindow = self.wm.loadWindowLayout('VanillaWindows.layout')
+
        self.sys.setDefaultMouseCursor('Vanilla-Images', 'MouseArrow')
    self.sys.setGUISheet(self.rootWindow)
+
  
    # Cursor
+
        # An extra window
    self.sys.setDefaultMouseCursor('Vanilla-Images', 'MouseArrow')
+
        w = self.wm.createWindow('TaharezLook/FrameWindow', 'Demo window')
 +
        self.rootWindow.addChildWindow(w)
 +
        return
  
     # An extra window
+
     # Initialize
     w = self.wm.createWindow('TaharezLook/FrameWindow', 'Demo window')
+
     def Initialize(self):
    self.rootWindow.addChildWindow(w)
+
        self.initializeOpenGL()
    return
+
        self.initializeHandlers()
 +
        self.initializePyCEGUI()
 +
        self.initializeDefaults()
 +
        self.initializeGUI()
 +
        return
  
  # Initialize
+
    # Handler: Display
  def Initialize(self):
+
    def handlerDisplay(self):
    self.initializeOpenGL()
+
    self.initializeHandlers()
+
    self.initializePyCEGUI()
+
    self.initializeDefaults()
+
    self.initializeGUI()
+
    return
+
  
  # Handler: Display
+
        # Injecting the time allows CEGUI to know how much time has passed, and
  def handlerDisplay(self):
+
        # use that to coordinate certain activities - fading, animation, tooltips,
 +
        # etc.
 +
        now = glutGet(GLUT_ELAPSED_TIME)
 +
        elapsed = (now - self.lastFrameTime) / 1000.0
 +
        self.lastFrameTime = now
 +
        self.updateFPS = self.updateFPS - elapsed
 +
        self.sys.injectTimePulse(elapsed)
  
    # Injecting the time allows CEGUI to know how much time has passed, and
+
        # Actual rendering
    # use that to coordinate certain activities - fading, animation, tooltips,
+
        # - `renderGUI` updates CEGUI's picture of the GUI.
    # etc.
+
        # - `glutPostRedisplay` is what actually marks the window as needing to
    now = glutGet(GLUT_ELAPSED_TIME)
+
        #   be redrawn by OpenGL.
    elapsed = (now - self.lastFrameTime) / 1000.0
+
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    self.lastFrameTime = now
+
        self.sys.renderGUI()
    self.updateFPS = self.updateFPS - elapsed
+
        glutPostRedisplay()
    self.sys.injectTimePulse(elapsed)
+
        glutSwapBuffers()
 +
        return
  
     # Actual rendering
+
     # Handler: Resize
     # - `renderGUI` updates CEGUI's picture of the GUI.
+
     # - `glViewport` modifies the OpenGL viewport to whatever the window size is.
    # - `glutPostRedisplay` is what actually marks the window as needing to
+
     def handlerResize(self, width, height):
    #  be redrawn by OpenGL.
+
        glViewport(0, 0, width, height)
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
+
        self.sys.notifyDisplaySizeChanged(PyCEGUI.Size(width, height))
    self.sys.renderGUI()
+
        return
    glutPostRedisplay()
+
    glutSwapBuffers()
+
    return
+
  
  # Handler: Resize
+
    # Handler: Mouse buttons
  # - `glViewport` modifies the OpenGL viewport to whatever the window size is.
+
    def handlerMouse(self, button, state, x, y):
  def handlerResize(self, width, height):
+
        if button == GLUT_LEFT_BUTTON:
    glViewport(0, 0, width, height)
+
            if state == GLUT_UP:
    self.sys.notifyDisplaySizeChanged(PyCEGUI.Size(width, height))
+
                self.sys.injectMouseButtonUp(PyCEGUI.LeftButton)
    return
+
            else:
 +
                self.sys.injectMouseButtonDown(PyCEGUI.LeftButton)
 +
        elif button == GLUT_RIGHT_BUTTON:
 +
            if state == GLUT_UP:
 +
                self.sys.injectMouseButtonUp(PyCEGUI.RightButton)
 +
            else:
 +
                self.sys.injectMouseButtonDown(PyCEGUI.RightButton)
 +
        return
  
  # Handler: Mouse buttons
+
    # Handler: Mouse motion
  def handlerMouse(self, button, state, x, y):
+
    def handlerMouseMotion(self, x, y):
    if button == GLUT_LEFT_BUTTON:
+
         self.sys.injectMousePosition(x, y)
      if state == GLUT_UP:
+
         return
         self.sys.injectMouseButtonUp(PyCEGUI.LeftButton)
+
      else:
+
         self.sys.injectMouseButtonDown(PyCEGUI.LeftButton)
+
    elif button == GLUT_RIGHT_BUTTON:
+
      if state == GLUT_UP:
+
        self.sys.injectMouseButtonUp(PyCEGUI.RightButton)
+
      else:
+
        self.sys.injectMouseButtonDown(PyCEGUI.RightButton)
+
    return
+
  
  # Handler: Mouse motion
+
    # Run
  def handlerMouseMotion(self, x, y):
+
    def Run(self):
    self.sys.injectMousePosition(x, y)
+
        self.lastFrameTime = glutGet(GLUT_ELAPSED_TIME)
    return
+
        glutMainLoop()
 +
        return
  
  # Run
 
  def Run(self):
 
    self.lastFrameTime = glutGet(GLUT_ELAPSED_TIME)
 
    glutMainLoop()
 
    return
 
  
 
# Main
 
# Main
 
def main():
 
def main():
  app = Application()
+
    app = Application()
  app.Initialize()
+
    app.Initialize()
  app.Run()
+
    app.Run()
  return 0
+
    return 0
  
  
 
# Guard
 
# Guard
 
if __name__ == '__main__':
 
if __name__ == '__main__':
  sys.exit(main())
+
    sys.exit(main())
 
</source>
 
</source>

Latest revision as of 15:50, 20 June 2011

Written for CEGUI 0.7


Works with versions 0.7.x (obsolete)

Requires at least version
0.7.5





Introduction

This presents a minimal PyCEGUI application, using OpenGL as the renderer. The goal is to familiarize the reader with the basics of how to get PyCEGUI up and running, and to provide a few pointers along the way.

Requirements

  • Python 2.6
  • PyCEGUI
  • PyOpenGL

Notes

This example assumes you have the CEGUI resources located in the same directory as the script; change `PATH_RESOURCES` if they are elsewhere.

Source

#!/usr/bin/env python
#
#
# example.py
 
 
# Import: std
import sys, os
 
# Import: PyOpenGL
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
 
# Import: PyCEGUI
import PyCEGUI
from PyCEGUIOpenGLRenderer import OpenGLRenderer as Renderer
 
# Constants
PATH_RESOURCES = './'
 
 
# Application
class Application(object):
 
    # Constructor
    def __init__(self):
        super(Application, self).__init__()
        self.lastFrameTime = 0
        self.updateFPS = 0
        return
 
    # Initialize: OpenGL
    # - A full list of values for `glutInitDisplayMode` can be found in the GLUT
    #   documentation.
    def initializeOpenGL(self):
        glutInit()
        glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA)
        glutInitWindowSize(1024, 768)
        glutInitWindowPosition(-1, -1) # Let the windowing system figure it out
        glutCreateWindow("Crazy Eddie's GUI Mk-2 - glut Base Application")
        glutSetCursor(GLUT_CURSOR_NONE)
        return
 
    # Initialize: Handlers
    # - Setup the methods which will be called when events happen.
    def initializeHandlers(self):
        glutDisplayFunc(self.handlerDisplay)
        glutReshapeFunc(self.handlerResize)
        glutMouseFunc(self.handlerMouse)
        glutMotionFunc(self.handlerMouseMotion)
        glutPassiveMotionFunc(self.handlerMouseMotion)
        return
 
    # Initialize: PyCEGUI
    # - Store some components; saves a lot of typing.
    def initializePyCEGUI(self):
        Renderer.bootstrapSystem()
        self.sys = PyCEGUI.System.getSingleton()
        self.rp = self.sys.getResourceProvider()
        self.scheme = PyCEGUI.SchemeManager.getSingleton()
        self.wm = PyCEGUI.WindowManager.getSingleton()
        return
 
    # Initialize: Defaults
    # - Resource locations.
    def initializeDefaults(self):
        self.rp.setResourceGroupDirectory('schemes', os.path.join(PATH_RESOURCES, 'datafiles/schemes'))
        self.rp.setResourceGroupDirectory('imagesets', os.path.join(PATH_RESOURCES, 'datafiles/imagesets'))
        self.rp.setResourceGroupDirectory('fonts', os.path.join(PATH_RESOURCES, 'datafiles/fonts'))
        self.rp.setResourceGroupDirectory('layouts', os.path.join(PATH_RESOURCES, 'datafiles/layouts'))
        self.rp.setResourceGroupDirectory('looknfeels', os.path.join(PATH_RESOURCES, 'datafiles/looknfeel'))
        self.rp.setResourceGroupDirectory('schemas', os.path.join(PATH_RESOURCES, 'datafiles/xml_schemas'))
        PyCEGUI.Imageset.setDefaultResourceGroup('imagesets')
        PyCEGUI.Font.setDefaultResourceGroup('fonts')
        PyCEGUI.Scheme.setDefaultResourceGroup('schemes')
        PyCEGUI.WidgetLookManager.setDefaultResourceGroup('looknfeels')
        PyCEGUI.WindowManager.setDefaultResourceGroup('layouts')
        parser = self.sys.getXMLParser()
        if parser.isPropertyPresent('SchemaDefaultResourceGroup'):
            parser.setProperty('SchemaDefaultResourceGroup', 'schemas')
        return
 
    # Initialize: GUI
    # - This is where we are actually setting up the windows we will see.
    def initializeGUI(self):
        self.scheme.create('VanillaSkin.scheme')
        self.scheme.create('TaharezLook.scheme')
 
        # GUISheet
        self.rootWindow = self.wm.loadWindowLayout('VanillaWindows.layout')
        self.sys.setGUISheet(self.rootWindow)
 
        # Cursor
        self.sys.setDefaultMouseCursor('Vanilla-Images', 'MouseArrow')
 
        # An extra window
        w = self.wm.createWindow('TaharezLook/FrameWindow', 'Demo window')
        self.rootWindow.addChildWindow(w)
        return
 
    # Initialize
    def Initialize(self):
        self.initializeOpenGL()
        self.initializeHandlers()
        self.initializePyCEGUI()
        self.initializeDefaults()
        self.initializeGUI()
        return
 
    # Handler: Display
    def handlerDisplay(self):
 
        # Injecting the time allows CEGUI to know how much time has passed, and
        # use that to coordinate certain activities - fading, animation, tooltips,
        # etc.
        now = glutGet(GLUT_ELAPSED_TIME)
        elapsed = (now - self.lastFrameTime) / 1000.0
        self.lastFrameTime = now
        self.updateFPS = self.updateFPS - elapsed
        self.sys.injectTimePulse(elapsed)
 
        # Actual rendering
        # - `renderGUI` updates CEGUI's picture of the GUI.
        # - `glutPostRedisplay` is what actually marks the window as needing to
        #   be redrawn by OpenGL.
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        self.sys.renderGUI()
        glutPostRedisplay()
        glutSwapBuffers()
        return
 
    # Handler: Resize
    # - `glViewport` modifies the OpenGL viewport to whatever the window size is.
    def handlerResize(self, width, height):
        glViewport(0, 0, width, height)
        self.sys.notifyDisplaySizeChanged(PyCEGUI.Size(width, height))
        return
 
    # Handler: Mouse buttons
    def handlerMouse(self, button, state, x, y):
        if button == GLUT_LEFT_BUTTON:
            if state == GLUT_UP:
                self.sys.injectMouseButtonUp(PyCEGUI.LeftButton)
            else:
                self.sys.injectMouseButtonDown(PyCEGUI.LeftButton)
        elif button == GLUT_RIGHT_BUTTON:
            if state == GLUT_UP:
                self.sys.injectMouseButtonUp(PyCEGUI.RightButton)
            else:
                self.sys.injectMouseButtonDown(PyCEGUI.RightButton)
        return
 
    # Handler: Mouse motion
    def handlerMouseMotion(self, x, y):
        self.sys.injectMousePosition(x, y)
        return
 
    # Run
    def Run(self):
        self.lastFrameTime = glutGet(GLUT_ELAPSED_TIME)
        glutMainLoop()
        return
 
 
# Main
def main():
    app = Application()
    app.Initialize()
    app.Run()
    return 0
 
 
# Guard
if __name__ == '__main__':
    sys.exit(main())