glFont.py :  » Development » PyObjC » trunk » pyobjc » PyOpenGL-2.0.2.01 » OpenGL » Demo » NeHe » lesson44 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » PyObjC 
PyObjC » trunk » pyobjc » PyOpenGL 2.0.2.01 » OpenGL » Demo » NeHe » lesson44 » glFont.py
# glFont.py -- SImple font class that uses a single texture represent 16x16 tiles
# for a font.
# // Code writen by: Vic Hollis 09/07/2003
# // I don't mind if you use this class in your own code. All I ask is 
# // that you give me and Giuseppe D'Agata credit for it if you do.  
# // And plug NeHe while your at it! :P  Thanks go to Giuseppe D'Agata
# // for the code that this class is based off of. Thanks Enjoy.
# //////////////////////////////////////////////////////////////////////
# // glFont.cpp: implementation of the glFont class.
# //////////////////////////////////////////////////////////////////////

from OpenGL.GL import *

class glFont:
  def __init__ (self):
    self.m_FontTexture = 0
    self.m_ListBase = 0

  def __del__ (self):
    self.release ()
    return

  def release (self):
    """ We've made a separate resoruce-deallocation method
      so that the client can return system resources when the
      want to explcitly. Python will eventually garbage collect,
      once all refs to the object go away. This method
      allows the client to retain the object, and yet free up the
      gl resources. 
    """
    if (self.m_FontTexture != 0):
      glDeleteTextures (self.m_FontTexture)
    if (self.m_ListBase != 0):
      glDeleteLists (self.m_ListBase, 256)
    return

  def SetFontTexture (self, tex):
    if (tex != 0):                        # // If the texture is valid
      self.m_FontTexture = tex;                # // Set the font texture
    else:
      # Client should not pass an invalid texture.
      raise RuntimeError, "SetFontTexture passed invalid texture (ID == 0)"
    return

  def BuildFont (self, Scale):
    self.m_ListBase=glGenLists(256);              # // Creating 256 Display Lists
    if (self.m_FontTexture != 0):
      glBindTexture(GL_TEXTURE_2D, self.m_FontTexture);    # // Select Our Font Texture
      for loop in xrange (256):                 # // Loop Through All 256 Lists
        cx=float(loop%16)/16.0;                # // X Position Of Current Character
        cy=float(loop/16)/16.0;                # // Y Position Of Current Character

        glNewList(self.m_ListBase+loop,GL_COMPILE);      # // Start Building A List
        # List start
        glBegin(GL_QUADS);                  # // Use A Quad For Each Character
        glTexCoord2f(cx, 1 - cy - 0.0625);          # // Texture Coord (Bottom Left)
        glVertex2f(0,0);                  # // Vertex Coord (Bottom Left)
        glTexCoord2f(cx + 0.0625, 1 - cy - 0.0625);      # // Texture Coord (Bottom Right)
        glVertex2f(16 * Scale,0);              # // Vertex Coord (Bottom Right)
        glTexCoord2f(cx + 0.0625, 1 - cy);          # // Texture Coord (Top Right)
        glVertex2f(16 * Scale, 16 * Scale);          # // Vertex Coord (Top Right)
        glTexCoord2f(cx, 1 - cy);              # // Texture Coord (Top Left)
        glVertex2f(0, 16 * Scale);              # // Vertex Coord (Top Left)
        glEnd();                      # // Done Building Our Quad (Character)
        glTranslated(10*Scale,0,0);              # // Move To The Right Of The Character
        glEndList();                    # // Done Building The Display List
        # List end

  def glPrintf (self, x,y, set, text):
    glEnable(GL_TEXTURE_2D);                  # // Enable 2d Textures
    glEnable(GL_BLEND);                      # // Enable Blending
    glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
    glBindTexture(GL_TEXTURE_2D, self.m_FontTexture);      # // Select Our Font Texture
    glDisable(GL_DEPTH_TEST);                  # // Disables Depth Testing
    glMatrixMode(GL_PROJECTION);                # // Select The Projection Matrix
    glPushMatrix();                        # // Store The Projection Matrix
    glLoadIdentity();                      # // Reset The Projection Matrix
    glOrtho(0,self.m_WindowWidth,0,self.m_WindowHeight,-1,1);  # // Set Up An Ortho Screen
    glMatrixMode(GL_MODELVIEW);                  # // Select The Modelview Matrix
    glPushMatrix();                        # // Store The Modelview Matrix
    glLoadIdentity();                      # // Reset The Modelview Matrix
    glTranslated(x,y,0);                    # // Position The Text (0,0 - Bottom Left)
    glListBase(self.m_ListBase-32+(128*set));          # // Choose The Font Set (0 or 1)
    # glCallLists(len(text),GL_BYTE,text);            # // Write The Text To The Screen
    # function can figure out the count and TYP
    glCallLists(text);                      # // Write The Text To The Screen
    glMatrixMode(GL_PROJECTION);                # // Select The Projection Matrix
    glPopMatrix();                        # // Restore The Old Projection Matrix
    glMatrixMode(GL_MODELVIEW);                  # // Select The Modelview Matrix
    glPopMatrix();                        # // Restore The Old Projection Matrix
    glEnable(GL_DEPTH_TEST);
    glDisable(GL_BLEND);
    glDisable(GL_TEXTURE_2D);
    return

  def SetWindowSize (self, width, height):
    self.m_WindowWidth = width
    self.m_WindowHeight = height
    return

  def GetTexture (self):
    return self.m_FontTexture

  def GetListBase (self):
    return self.m_ListBase


w__w_w_.j___a_va___2___s._c___o___m__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.