Creating a Custom Orthographic Projection With Pyglet
When you create a pyglet window, pyglet sets up an OpenGL orthographic projection for the window. The size of the projection matches the size of the window. If the window is 640 pixels wide and 480 pixels wide, the orthographic projection is 640 units wide and 480 units high. Pyglet’s default behavior works well in many cases, but there are situations where you may want to create an orthographic projection with different dimensions. If you’re writing a tile-based game, it is convenient to size the projection in tiles instead of pixels. How do you create a custom orthographic projection?
Override pyglet’s on_resize() method for the window and make a call to glOrtho(). The following code provides an example of creating a custom-sized orthographic projection:
@window.event
def on_resize(width, height):
glViewport(0, 0, width, height)
glMatrixMode(gl.GL_PROJECTION)
glLoadIdentity()
projectionWidth = 24
projectionHeight = 16
glOrtho(0, projectionWidth, 0, projectionHeight, -1, 1)
glMatrixMode(gl.GL_MODELVIEW)
glLoadIdentity()
return pyglet.event.EVENT_HANDLED
My example copies what the default on_resize() method does, but I supply a custom width and height to glOrtho().
Installing Pyglet on 64-bit Macs
Pyglet is a technology to write cross-platform OpenGL games and applications in Python. If you go to the pyglet site, you will find installers for Mac OS X and Windows. The current Mac version of the installer, 1.1.4, uses Carbon, which means it doesn’t work with 64-bit applications. Not working with 64-bit applications is a problem if you’re running Mac OS X 10.6 and later. Version 1.2 uses Cocoa and works with 64-bit applications, but an installer is not currently available because 1.2 is still in development. Until an installer for 1.2 becomes available, you will have to install pyglet from source code to use version 1.2.
Install Mercurial
The source code for pyglet is in a Mercurial repository. If you do not have Mercurial installed on your computer, you must install it to be able to install pyglet 1.2. Installers for Mac OS X and Windows are available at the Mercurial site.
Clone the Repository
Cloning the pyglet repository gives you a copy of the source code so you can install pyglet. Launch the Terminal application, navigate to where you want the source code to reside, and run the following command:
hg clone https://pyglet.googlecode.com/hg/ pyglet
There is a space before pyglet in the last part of the URL. You should see a pyglet folder after cloning the repository.
Install Pyglet
After cloning the repository, it’s time to install pyglet. In the Terminal application go to the pyglet folder and run the following command:
python setup.py install
Now pyglet should be installed and you can start coding.
Install AVbin
Installing pyglet alone has one limitation. It can play only uncompressed audio files. To play compressed audio files, such as MP3 and Ogg Vorbis files, with pyglet, you must install the AVbin library, which is what pyglet uses to play compressed audio. There are multiple versions available for Mac OS X, Windows, and Linux on the AVbin download page. Those of you running Mac OS X 10.6 and later should download a version that supports 64-bit Intel.
After downloading AVbin, you must install it from the Terminal application. Navigate to the avbin folder (the latest name of the folder on Mac OS X is avbin-darwin-x86-64-v8) and enter the following command:
sudo bash install.sh
Running the installation script installs AVbin and lets you play compressed audio files with pyglet.
OpenGL Driver Instrument
Researching Instruments for Xcode Tools Sensei allowed me to become familiar with most of the built-in instruments. Due to the high number of instruments, there was no way for me to cover them all in the book. I focused on the most commonly used instruments in the book. I can use this blog to explain some of the other instruments. I start with the OpenGL Driver instrument.
What the OpenGL Driver Instrument Does
The OpenGL Driver instrument records OpenGL statistics. Some statistics this instrument records include the following:
- The amount of free video memory.
- The number of textures used.
- The number of buffer swaps.
- The amount of time the CPU waited for the GPU.
- The number of surfaces the GPU allocated.
Obviously OpenGL applications are the ones to benefit from the OpenGL Driver instrument. The OpenGL ES Driver instrument records similar information for iOS applications. The OpenGL ES Driver instrument is available in both the OpenGL ES Driver and OpenGL ES Analysis templates. In most cases you should use the OpenGL ES Analysis template for OpenGL ES profiling.
One thing you’ll notice when you create a new trace document is there is no template for the OpenGL Driver instrument. Pick one of the other templates (Time Profiler and Empty would be good choices for profiling purposes) and add the OpenGL Driver instrument to the trace document by dragging the instrument from the Library to the instrument list. Click the Library button in the trace document window toolbar to open the Library.
Before You Trace
Before you start tracing you must tell Instruments the statistics to show in the track pane and detail view. Click the Info button next to the OpenGL Driver instrument to open a pop-up editor. Click the Configure button to see all the available statistics. Select the checkbox next to a statistic to tell Instruments to show it in the track pane and detail view. The extended detail view shows all the statistics so you should show only the most important statistics in the track pane and detail view.

The screenshot also shows where you set the sampling rate.
Trace Results
Use the Target menu in the trace document window toolbar to pick an application to profile. Click the Record button to start profiling. Click the Pause button to pause recording and see the statistics. The track pane graphs each statistic you told the OpenGL Driver instrument to observe.
The detail view at the bottom of the trace document window has one column for each statistic you told Instruments to show. There is one row in the detail view for each sample the OpenGL Driver instrument recorded. Selecting a sample from the list shows all the OpenGL statistics for that sample in the extended detail view. The extended detail view is on the right side of the trace document window. If you don’t see it, choose View > Extended Detail or click the right button in the View group in the toolbar.
SDL OpenGL Typedef Redefinition Error on Mac OS X 10.7
If you build a SDL OpenGL application for Mac OS X using SDL 1.2.14 (using the binary installer at the SDL site) and the Mac OS X 10.7 SDK, you can get the following error:
typedef redefinition with different types ('unsigned int' vs 'void *')
The SDL header file SDL_opengl.h and the OpenGL header file glext.h both define a data type GLhandleARB. SDL_opengl.h defines it as an unsigned integer. The Mac OS X 10.7 version of glext.h defines it as a void pointer. Defining GLhandleARB as different data types in different header files causes problems.
The SDL team fixed the problem in their Mercurial repository. The fix is not too difficult to apply. In your SDL_opengl.h file, change the following line of code:
typedef unsigned int GLhandleARB;
To the following:
#if defined(__APPLE__)
typedef void *GLhandleARB;
#else
typedef unsigned int GLhandleARB;
#endif
The fix defines GLhandleARB as a void pointer on Mac OS X and an unsigned integer on other operating systems.
OpenGL Profiler Workaround
I ran into a problem with OpenGL Profiler 4.2 on Snow Leopard. I would profile an application and pause profiling so I could look at the OpenGL statistics. When I opened the statistics window, it was blank. The trace window was also blank, and I had selected the Collect Trace checkbox in OpenGL Profiler’s main window.
My workaround was to open the statistics and trace windows and start profiling. When I did this, both windows filled with the appropriate profiling data.