Package toon :: Module draw
[hide private]
[frames] | no frames]

Source Code for Module toon.draw

  1  #!/usr/bin/env python 
  2  # 
  3  # Toonloop for Python 
  4  # 
  5  # Copyright 2008 Alexandre Quessy & Tristan Matthews 
  6  # <alexandre@quessy.net> & <le.businessman@gmail.com> 
  7  # http://www.toonloop.com 
  8  # 
  9  # Original idea by Alexandre Quessy 
 10  # http://alexandre.quessy.net 
 11  # 
 12  # Toonloop is free software: you can redistribute it and/or modify 
 13  # it under the terms of the GNU General Public License as published by 
 14  # the Free Software Foundation, either version 3 of the License, or 
 15  # (at your option) any later version. 
 16  # 
 17  # Toonloop is distributed in the hope that it will be useful, 
 18  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20  # GNU General Public License for more details. 
 21  # 
 22  # You should have received a copy of the gnu general public license 
 23  # along with Toonloop.  If not, see <http://www.gnu.org/licenses/>. 
 24  # 
 25  import pygame 
 26  from OpenGL.GL import * 
 27  """ 
 28  Draws things in OpenGL 
 29  """ 
 30  # no need for glut for now 
 31  # from OpenGL.GLUT import * 
 32  #  
 33  # def draw_text(text, font=GLUT_STROKE_ROMAN): 
 34  #     """ 
 35  #     Draws text in OpenGL 
 36  #     """ 
 37  #     #font=GLUT_BITMAP_TIMES_ROMAN_24 
 38  #     for c in text: 
 39  #         glutStrokeCharacter(font, ord(c)) 
 40  #         # glutBitmapCharacter(font, ord(c)) 
 41   
42 -def texture_from_image(texture, image, square_texture=False):
43 """ 44 Copies the pixels from a pygame surface to an OpenGL texture object. 45 """ 46 textureData = pygame.image.tostring(image, "RGBX", True) # vertically flipped 47 if square_texture: 48 glBindTexture(GL_TEXTURE_2D, texture) 49 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.get_width(), image.get_height(), 0, \ 50 GL_RGBA, GL_UNSIGNED_BYTE, textureData) 51 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) 52 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) 53 else: 54 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture) 55 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, image.get_width(), \ 56 image.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData) 57 glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST) 58 glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
59
60 -def draw_textured_square(w=None, h=None):
61 """ 62 Draws a texture square of 2 x 2 size centered at 0, 0 63 64 Make sure to call glEnable(GL_TEXTURE_RECTANGLE_ARB) first. 65 66 :param w: width of the image in pixels 67 :param h: height of the image in pixels 68 """ 69 if w is None or h is None: 70 glBegin(GL_QUADS) 71 glTexCoord2f(0.0, 0.0) 72 glVertex2f(-1.0, -1.0) # Bottom Left Of The Texture and Quad 73 glTexCoord2f(1.0, 0.0) 74 glVertex2f(1.0, -1.0) # Bottom Right Of The Texture and Quad 75 glTexCoord2f(1.0, 1.0) 76 glVertex2f(1.0, 1.0) # Top Right Of The Texture and Quad 77 glTexCoord2f(0.0, 1.0) 78 glVertex2f(-1.0, 1.0) # Top Left Of The Texture and Quad 79 glEnd() 80 else: 81 glBegin(GL_QUADS) 82 glTexCoord2f(0.0, 0.0) 83 glVertex2f(-1.0, -1.0) # Bottom Left 84 glTexCoord2f(w, 0.0) 85 glVertex2f(1.0, -1.0) # Bottom Right 86 glTexCoord2f(w, h) 87 glVertex2f(1.0, 1.0) # Top Right 88 glTexCoord2f(0.0, h) 89 glVertex2f(-1.0, 1.0) # Top Left 90 glEnd()
91
92 -def draw_square():
93 """ 94 Draws a square of 2 x 2 size centered at 0, 0 95 96 Make sure to call glDisable(GL_TEXTURE_RECTANGLE_ARB) first. 97 """ 98 glBegin(GL_QUADS) 99 glVertex2f(-1.0, -1.0) # Bottom Left of Quad 100 glVertex2f(1.0, -1.0) # Bottom Right of Quad 101 glVertex2f(1.0, 1.0) # Top Right Of Quad 102 glVertex2f(-1.0, 1.0) # Top Left Of Quad 103 glEnd()
104
105 -def draw_horizontal_progress_bar(background_color=(0.0, 0.0, 0.0, 1.0), foreground_color=(1.0, 1.0, 1.0, 1.0), progress=0.0, line_color=(0.0, 0.0, 0.0, 1.0)):
106 """ 107 Draws an horizontal progress bar. 108 109 The programmer should scale this shape, since its position is between 110 -1 and 1 on both axis. 111 112 Make sure to call glDisable(GL_TEXTURE_RECTANGLE_ARB) first. 113 114 :param background_color: tuple of 4 floats from 0 to 1 115 :param foreground_color: tuple of 4 floats from 0 to 1 116 :param progress: float from 0 to 1 117 """ 118 glColor4f(*foreground_color) 119 glBegin(GL_QUADS) 120 glVertex2f(-1.0, -1.0) # Bottom Left of Quad 121 glVertex2f(progress * 2.0 - 1.0, -1.0) # Bottom Right of Quad 122 glVertex2f(progress * 2.0 - 1.0, 1.0) # Top Right Of Quad 123 glVertex2f(-1.0, 1.0) # Top Left Of Quad 124 glEnd() 125 glColor4f(*background_color) 126 glBegin(GL_QUADS) 127 glVertex2f(-1.0, -1.0) # Bottom Left of Quad 128 glVertex2f(1.0, -1.0) # Bottom Right of Quad 129 glVertex2f(1.0, 1.0) # Top Right Of Quad 130 glVertex2f(-1.0, 1.0) # Top Left Of Quad 131 glEnd() 132 #glLineWidth(1.0) 133 glColor4f(*line_color) 134 glBegin(GL_LINE_LOOP) 135 glVertex2f(-1.0, -1.0) # Bottom Left of Quad 136 glVertex2f(1.0, -1.0) # Bottom Right of Quad 137 glVertex2f(1.0, 1.0) # Top Right Of Quad 138 glVertex2f(-1.0, 1.0) # Top Left Of Quad 139 glEnd()
140