[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] OpenGL: Fonts



I did a condensed version of one of the font tutorials, making a version of it that worked for me. Unfortunately it doesn't seem to work now, because it complains: "ImportError: No module named win32ui." But this class might be useful as part of a font tutorial:

<code>

"""
RenderFont.py
Draws text on the screen in OpenGL.
Closely based on (ie. nearly ripped off from) a tutorial with the
following header:

Ported to PyOpenGL 2.0 by Brian Leair 18 Jan 2004
This code was created by Jeff Molofee 2000
The port was based on the PyOpenGL tutorial and from the
PyOpenGLContext (tests/glprint.py).
If you've found this code useful, please let me know (email Brian Leair
at telcom_sage@xxxxxxxxx).
See original source and C based tutorial at <http://nehe.gamedev.net>.
"""

##### INCLUDED MODULES #####

from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.WGL import *
import win32ui, sys

##### CLASSES #####

class FontKeeper:
    def __init__(self,screensize=[800,600],color=[0.4,0.4,0.8]):
        """Handles drawing text on the screen."""
        self.BuildFont()
        self.screensize = screensize ## Used for reversing draw coords.
        self.color = color

def BuildFont(self,fontname="Courier New",width=0,height=-24,weight=800):
"""Load a font as a set of OpenGL drawing lists, for quick
drawing of each letter."""
wgldc = wglGetCurrentDC()
hDC = win32ui.CreateDCFromHandle(wgldc)
properties = {"name":fontname,"width":width,"height":height,"weight":weight}
self.fontlists = glGenLists(96)
font = win32ui.CreateFont(properties)
wglUseFontBitmaps(wgldc,32,96,self.fontlists)


def Write(self,text="",coords=[0,50]):
"""Draw text on the screen. Coords = actual screen coords.
Y gets reversed because OpenGL does too, treating LL corner as Y=0."""
if not text:
return
glColor(self.color)
glRasterPos2i(coords[0],self.screensize[1]-coords[1])
glPushAttrib(GL_LIST_BIT)
glListBase(self.fontlists - 32)
glCallLists(text)
glPopAttrib()


</code>