Rabbyt and GTK GLExt example

To clarify my previous post a little I have made a simple example on how to use Rabbyt library with GTK GL Extension. Hope this helps. Cheers all…

# /usr/bin/env python
#
# Rabbyt GTK GLext test
#

__author__ = 'Karol "Wolverine" Tomala'
__email__ = 'karol (at) karoltomala (dot) com'

# Imports
import sys

import pygtk
pygtk.require('2.0')
import gtk
import gtk.gtkgl
import gobject

import rabbyt

# Globals
angle = 0.0
sprite = None
zfactor = 0.005
zoom = 1.0

# Drawing function
def draw(glarea, event):
    # Get surface and context
    glcontext = glarea.get_gl_context()
    gldrawable = glarea.get_gl_drawable()

    # Start opengl context
    if not gldrawable.gl_begin(glcontext): return

    # Actual drawing
    global sprite, angle, zoom

    # Clear screen
    rabbyt.clear((0.0, 0.0, 0.0))

    # Render sprite
    if sprite is not None:
	sprite.rot = angle
	sprite.scale = zoom
	sprite.render()

    # Flush screen
    gldrawable.swap_buffers()

    # End opengl context
    gldrawable.gl_end()

    return True

# Resizing function
def reshape(glarea, event):
    # Get surface and context
    glcontext = glarea.get_gl_context()
    gldrawable = glarea.get_gl_drawable()

    # Start opengl context
    if not gldrawable.gl_begin(glcontext): return

    # Get widget dimensions
    x, y, width, height = glarea.get_allocation()

    # Reset rabbyt viewport
    rabbyt.set_viewport((width, height))
    rabbyt.set_default_attribs()

    # End opengl context
    gldrawable.gl_end()

    return True

# Initialization function
def init(glarea):
    # Get surface and context
    glcontext = glarea.get_gl_context()
    gldrawable = glarea.get_gl_drawable()

    # Start opengl context
    if not gldrawable.gl_begin(glcontext): return

    # Get widget dimensions
    x, y, width, height = glarea.get_allocation()

    # Reset rabbyt viewport
    rabbyt.set_viewport((width, height))
    rabbyt.set_default_attribs()    

    # Get sprite variable
    global sprite

    # Load sprite
    sprite = rabbyt.Sprite('sprite.png')

    # End opengl context
    gldrawable.gl_end()

    return True

# Idle function
def idle(glarea):
    # Get vars
    global angle, zoom, zfactor

    # Update angle
    angle += 1.0
    if angle > 359:
	angle = 0.0

    # Update zoom
    if zoom > 10 or zoom < 1:
	zfactor = -zfactor
    zoom += zfactor

    # Needed for synchronous updates
    glarea.window.invalidate_rect(glarea.allocation, False)
    glarea.window.process_updates(False)

    return True

# Map events function
def map(glarea, event):
    # Add idle event
    gobject.idle_add(idle, glarea)
    return True

# Create opengl configuration
try:
    # Try creating rgb, double buffering and depth test modes for opengl
    glconfig = gtk.gdkgl.Config(mode = (gtk.gdkgl.MODE_RGB |
					gtk.gdkgl.MODE_DOUBLE |
					gtk.gdkgl.MODE_DEPTH))
except:
    # Failed, so quit
    sys.exit(0)

# Create window
win = gtk.Window()
win.set_title('Rabbyt GTK test')

if sys.platform != 'win32':
    win.set_resize_mode(gtk.RESIZE_IMMEDIATE)
win.set_reallocate_redraws(True)

win.connect('destroy', gtk.main_quit)

# Create vertical packing box
vbox = gtk.VBox()
win.add(vbox)
vbox.show()

# Create our glarea widget
glarea = gtk.gtkgl.DrawingArea(glconfig)
glarea.set_size_request(300, 300)

glarea.connect_after('realize', init)
glarea.connect('configure_event', reshape)
glarea.connect('expose_event', draw)
glarea.connect('map_event', map)

vbox.pack_start(glarea)
glarea.show()

# Create button quit
btn = gtk.Button('Quit')
btn.connect('clicked', gtk.main_quit)

vbox.pack_start(btn, expand = False)
btn.show()

# Show window
win.show()

# We're finished, enter main gtk loop
gtk.main()

# Go get some pizza :)

About Wolverine

If you are looking for IT consultant, let me know! karol at karoltomala dot REMOVE com Just remove the REMOVE word from the e-mail above!
This entry was posted in Coding, Game Programming, Python and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *