rendered paste body#Circle Thing
import os
from OpenGL.GL import *
#from OpenGL.GLU import *
import pygame, pygame.image
from pygame.locals import *
rotating = 0
rotatingLeft = 1
rotation = 0.0
textures = [0,0]
def initOGL():
print "Initializing OpenGL Settings ..."
## Here is where we will set up various OpenGL things
glViewport(0, 0, 640, 480) ## set the view port to take the whole screen
glMatrixMode(GL_PROJECTION) ## switch to the Projection Matrix
glLoadIdentity() ## reset Projection Matrix
glOrtho(0, 640, 480, 0, -1, 1) ## Everyone loves the orthoscopic view matrix! YES!
glMatrixMode(GL_MODELVIEW) ## switch to the Model View Matrix
glLoadIdentity() ## reset Model View Matrix
glClearColor(0.0,0.0,0.0,0.0) ## Set our Clear Color to Black
glEnable (GL_BLEND) ## used for transparent textures
## Disabling the depth test (z will not be used to tell what object
## will be shown above another, only the order in which they are drawn.)
glDisable(GL_DEPTH_TEST)
print "OpenGL Settings OK!"
def loadTextures():
global textures
glEnable(GL_TEXTURE_2D)
texturefile = os.path.join('data','bg1.png')
textureSurface = pygame.image.load(texturefile)
textureData = pygame.image.tostring(textureSurface, "RGBX", 1)
glBindTexture(GL_TEXTURE_2D, textures[0])
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, textureSurface.get_width(), textureSurface.get_height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, textureData )
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
texturefile = os.path.join('data','circle.png')
textureSurface = pygame.image.load(texturefile)#
textureData = pygame.image.tostring(textureSurface, "RGBA", 1)
glBindTexture(GL_TEXTURE_2D, textures[1])
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, textureSurface.get_width(), textureSurface.get_height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, textureData )
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
def DrawImageRot(srcImg, spriteW, spriteH, sourceX, sourceY, sourceW, sourceH, destX, destY, destW, destH, Rotation, Flipped):
glLoadIdentity() # Reset the matrix! "Woah... no way."
## Enable texturing
glEnable(GL_TEXTURE_2D)
## Load the texture
glBindTexture(GL_TEXTURE_2D, srcImg)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) ## Blending, alpha-style.
## move to where we are going to draw ..
glTranslatef(destX, destY, 0.0)
## move it back (un-doing the center of rotation move below)
glTranslatef((destW / 2), (destH / 2), 0.0)
## rotation!
if (Rotation <> 0):
glRotatef(Rotation,0.0,0.0,1.0)
if (Flipped == 1):
glRotatef(180, 0.0, 1.0, 0.0)
## move to the center for rotation (remember that rotates and translates are teh backwards executing!)
glTranslatef(-(destW / 2), -(destH / 2), 0.0)
## color the quad white (no strange tinting)
glColor4ub(255, 255, 255, 255)
## Create some texturing coordinates
## Because they need to be between 0 and 1
topx = (sourceX / spriteW)
topy = (sourceY / spriteH)
lowx = topx + (sourceW / spriteW) ## this is a little tricky, meow.
lowy = topy + (sourceH / spriteH)
glBegin(GL_QUADS)
##Top-left vertex (corner)
glTexCoord2f(topx, topy)
glVertex3f(0, 0, 0.0) ##need to convert to floats, because they are OpenGL's favorite.
##Bottom-left vertex (corner)
glTexCoord2f(lowx, topy)
glVertex3f((destW), 0.0, 0.0)
##Bottom-right vertex (corner)
glTexCoord2f(lowx, lowy)
glVertex3f((destW), (destH), 0.0)
##Top-right vertex (corner)
glTexCoord2f(topx, lowy)
glVertex3f(0.0, (destH), 0.0)
glEnd()
##Disable texturing
glDisable(GL_TEXTURE_2D)
def handleEvent(theEvent):
global rotatingLeft, rotating, rotation
if (theEvent.type == KEYDOWN):
if (theEvent.type == K_LEFT):
rotatingLeft = 1
rotating = 1
elif (theEvent.type == K_RIGHT):
rotatingLeft = 0
rotating = 1
else:
if (theEvent.type == K_LEFT):
rotating = 0
elif (theEvent.type == K_RIGHT):
rotating = 0
if rotating == 1:
if rotatingLeft == 1:
rotation = rotation - 0.5
else:
rotation = rotation + 0.5
def BlankScreen():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ## clears the screen and depth buffer bits
glMatrixMode( GL_MODELVIEW )
glLoadIdentity( )
def draw():
global rotation, textures
BlankScreen()
DrawImageRot(textures[0], 640, 480, 0, 0, 640, 480, 0, 0, 640, 480, 0, 0)
##def DrawImageRot(srcImg, spriteW, spriteH, sourceX, sourceY, sourceW, sourceH, destX, destY, destW, destH, Rotation, Flipped):
DrawImageRot(textures[1], 166, 166, 0, 0, 166, 166, 237, 157, 166, 166, rotation, 0)
pygame.display.flip()
def main():
video_flags = OPENGL|DOUBLEBUF
pygame.init()
surface = pygame.display.set_mode((640, 480), video_flags)
pygame.display.set_caption('Circle Thingy')
pygame.mouse.set_visible(0)
initOGL()
loadTextures()
frames = 0
ticks = pygame.time.get_ticks()
while 1:
event = pygame.event.poll()
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
break
handleEvent(event)
draw()
frames = frames + 1
print "fps: %d" % ((frames*1000)/(pygame.time.get_ticks()-ticks))
if __name__ == '__main__':
import profile
profile.run('main()')
#main()