Game Objects library

On September 15, 2007 in development, games, python

Game Objects is a collection of 2D and 3D maths classes, and algorithms for helping in the creation of games with Python.

Will McGugan’s Game Objects library has been invaluable for my dabblings in Python game development. It provides optimized objects for common graphics math (3D vector class, matrix class, etc.). The objects are flexible and framework independent, so you can use them with the platform of your choice, be it pygame, PyOpenGL, or DirectPython.

It’s a somewhat new project, and the documentation a bit sparse, but the library is pretty straightforward and he’s been updating it regularly.

Relevant links:

Vertex buffer objects in PyOpenGL

On August 31, 2007 in development, games, python

Decided to move some of my vertex array code over to ARB_vertex_buffer_object, to see if I could get a bit of a speed boost out of PyOpenGL. Much to my dismay, these functions don’t have the sexy extension wrappers I talked about before, so it was a bit of a chore to get it all working.

Here’s a little wrapper class I wrote to make things easier:

from OpenGL.GL import *
from OpenGL.raw import GL
from OpenGL.arrays import ArrayDatatype as ADT

class VertexBuffer(object):

  def __init__(self, data, usage):
    self.buffer = GL.GLuint(0)
    glGenBuffers(1, self.buffer)
    self.buffer = self.buffer.value
    glBindBuffer(GL_ARRAY_BUFFER_ARB, self.buffer)
    glBufferData(GL_ARRAY_BUFFER_ARB, ADT.arrayByteCount(data), \
                 ADT.voidDataPointer(data), usage)

  def __del__(self):
    glDeleteBuffers(1, GL.GLuint(self.buffer))

  def bind(self):
    glBindBuffer(GL_ARRAY_BUFFER_ARB, self.buffer)

  def bind_vertexes(self, size, type, stride=0):
    self.bind()
    glVertexPointer(size, type, stride, None)

  ... snipped for length ...

Download buffers.py.

So, how to use it? Let’s say you have a Python list with a bunch of vertexes with float x, y, and z components:

verts = [[x, y, z], [x, y, z], [x, y, z], ...]

You’ll have to use NumPy or an equivalent to convert it to a PyOpenGL compatible array:

import numpy
numpy_verts = numpy.array(verts, dtype=numpy.float32)

Create the VertexBuffer object with it:

buffer = VertexBuffer(numpy_verts, GL_STATIC_DRAW)

Use it in your day to day rendering:

glEnableClientState(GL_VERTEX_ARRAY)
buffer.bind_vertexes(3, GL_FLOAT)
glDrawElementsui(GL_TRIANGLES, indexes)

They’re not shown in the snippet, but I’ve also defined bind_colors, bind_edgeflags, bind_indexes, bind_normals, and bind_texcoords, as shortcuts for the rest of the GL array functions.

PyOpenGL's extension wrappers

On August 07, 2007 in development, games, python

Normally if you wanted to get access to an OpenGL extension, you’d have to do something like:

from ctypes import *    
from OpenGL import platform
gl = platform.OpenGL
glGetShaderiv = gl.glGetShaderiv
glGetShaderiv.argtypes = [c_int, c_int, POINTER(c_int)]

Gross. Luckily, PyOpenGL version 3.0.0a5 and later will automatically map ARB standard functions for you when they are available. Now all you have to do is:

from OpenGL.GL import *

And everything will be magically available to you, wrapped so you don’t have to use ctypes to create c_char_p or c_int variables to pass them.

Watch out, though! The functions won’t be mapped correctly if your display context hasn’t been created yet. Always make sure to import OpenGL after the pygame display has been initialized.

# first initialize pygame
import pygame
from pygame.locals import *
pygame.init()
pygame.display.set_mode((800, 600), OPENGL | DOUBLEBUF)

# now you can import it
from OpenGL.GL import *

I would love to know how to manually tell PyOpenGL to re-map everything after initializing pygame, though. I don’t like having to reposition the import like that. Nathan Gray has a deep reload replacement for the reload builtin, which might work, but I’m not sure if that’s the best solution to this problem…

Rotating development

On July 05, 2007 in design, development, games

This post on 2D Boy states:
“As love and effort increase, the probability of self destruction approaches 1.” - Kyle’s Theorem of Destruction #2a
Or, the more you care about what you’re working on, the less likely you’ll actually turn it into something totally awesome.

The more you work on something, the more personally invested you become. The more personally invested you are, the less likely you are to look at it objectively and trash or rethink problem areas. The product suffers. This applies to any type of development, but it’s especially true for game development.

How to fix it? 2D Boy suggests occasional distraction with side projects. I have some more thoughts for large teams…

First: organize your staff into small, independent, Scrum-ish) groups. Each group must be self-sufficient, with an artist, programmer, etc. When assigned a task, it must be able to create a playable product without outside interaction with other departments.

Next: identify the areas of development for the project. For example, in Battlefield 2142, these might be: infantry, vehicles, conquest gameplay, titan gameplay, statistics and awards, and user interface.

Finally: each group works on one specific area for one iteration of your Scrum development cycle. In the next iteration, rotate the group to a new area of development.

This results in:
  • Fresh eyes looking over the important parts of your game each iteration of the development cycle.
  • Less competition between departments.
  • The game won’t suffer because one group doesn’t want to make changes to the precious content they’ve been slaving over for the past 9 months.

Py* + Windows

On June 05, 2007 in development, games, python

To install Python, pygame, and PyOpenGL on Windows:

  1. Download Python here and install it.
  2. Download pygame here and install it. The “Windows” section has an executable installer (e.g. pygame-1.7.1release.win32-py2.5.exe).
  3. You should also download and install Numeric from the pygame page.
  4. Download ez_setup.py and save it to a folder outside your Python installation (e.g. C:\).
  5. Open a command prompt, change to the folder you saved ez_setup.py to, and run it:
    c:\python\python c:\ez_setup.py
    It will download the easy_install executables and put them in your Python\Scripts folder.
  6. Now install PyOpenGL using easy_install:
    c:\python\scripts\easy_install PyOpenGL

All done!

Little Big Planet

On May 18, 2007 in games

This game makes me feel all warm and fuzzy. You could say it’s like Loco Roco + Garry’s Mod. This game is the most convincing reason I can find to buy a PS3. I guess I shouldn’t be surprised by that, as Loco Roco did the same thing for me with the PSP.

Continue reading...