commit:6f8815fe5a2bf5674664b36aa06407f4ac3d3709
author:chip
committer:chip
date:Mon Nov 12 06:11:09 2007 +0000
parents:
Initial commit for sprite_engine
diff --git a/Sprite.py b/Sprite.py
line changes: +66/-0
index 0000000..4e32059
--- /dev/null
+++ b/Sprite.py
@@ -0,0 +1,66 @@
+from OpenGL.GL import *
+from OpenGL.GLU import *
+import pygame
+from pygame.locals import *
+from Numeric import array,reshape,concatenate
+
+from util import *
+import TexMan
+
+class Sprite:
+	frames = None
+	framerate = 0.0
+	frameno = 0
+	color = (1.0,1.0,1.0,1.0)
+	position = (0,0)
+	size = (0,0)
+	rotation = 0
+	uparrow = False
+	displaylist = None
+
+	def __init__(self, file, genlist=True):
+		d = TexMan.load(file)
+		self.texture = d['texture']
+		self.size = d['size']
+		self.tsize = d['tsize']
+		#self.frames = [self.texture]
+		if genlist:
+			self.gen_displaylist()
+
+	def gen_displaylist(self):
+		if self.displaylist == None:
+			self.displaylist = glGenLists(1)
+		glNewList(self.displaylist, GL_COMPILE)
+		glEnable(GL_TEXTURE_2D)
+		glBindTexture(GL_TEXTURE_2D, self.texture)
+		glColor4fv(self.color)
+		glBegin(GL_QUADS)
+		x = -self.size[0]/2
+		y = -self.size[1]/2
+		glTexCoord2d(1.0,0.0)
+		glVertex3f(x, y, 0)
+		glTexCoord2d(1.0,1.0)
+		glVertex3f(x + self.tsize[0], y, 0)
+		glTexCoord2d(0.0,1.0)
+		glVertex3f(x + self.tsize[0], y + self.tsize[1], 0)
+		glTexCoord2d(0.0,0.0)
+		glVertex3f(x, y + self.tsize[1], 0)
+		glEnd()
+		glEndList()
+
+
+	def draw(self):
+		glPushMatrix()
+		glTranslatef(self.position[0], self.position[1], 0)
+		if self.rotation != 0:
+			glRotatef(self.rotation, 0, 0, -1)
+		glCallList(self.displaylist)
+		if self.uparrow:
+			glDisable(GL_TEXTURE_2D)
+			glColor3f(0.0,0.0,1.0)
+			glBegin(GL_TRIANGLES)
+			glVertex(-5, 0, 0)
+			glVertex(0, 20, 0)
+			glVertex(5, 0, 0)
+			glEnd()
+		glPopMatrix()

diff --git a/TexMan.py b/TexMan.py
line changes: +40/-0
index 0000000..bed22c3
--- /dev/null
+++ b/TexMan.py
@@ -0,0 +1,40 @@
+from OpenGL.GL import *
+from OpenGL.GLU import *
+import pygame
+from pygame.locals import *
+from Numeric import array,reshape,concatenate
+
+from util import *
+
+sprites = {}
+
+def load(file):
+	global sprites
+	if sprites.has_key(file):
+		return sprites[file]
+
+        image = pygame.image.load(file)
+        w = image.get_width()
+        h = image.get_height()
+        tw = n2ceil(w)
+        th = n2ceil(h)
+        newImage = pygame.Surface((tw,th), SRCALPHA, 32);
+        newImage.fill((255,255,255,0))
+        newImage.blit(image, (0,0));
+
+        # Freaking pygame making us do this by hand...
+        colors = pygame.surfarray.pixels3d(newImage)
+        alphas = pygame.surfarray.pixels_alpha(newImage)
+        alphas = reshape(alphas, (tw,th,1))
+        pixels = concatenate((colors,alphas),2)
+
+        # Create Texture
+        texture = glGenTextures(1)
+	glBindTexture(GL_TEXTURE_2D, texture)
+	glTexImage2Dub(GL_TEXTURE_2D, 0, GL_RGBA8, 0, GL_RGBA, pixels)
+	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
+	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
+	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)
+
+	sprites[file] = {'texture':texture,'pixels':pixels,'size':(w,h),'tsize':(tw,th)}
+	return sprites[file]

diff --git a/bouncetest.py b/bouncetest.py
line changes: +97/-0
index 0000000..9b36cbc
--- /dev/null
+++ b/bouncetest.py
@@ -0,0 +1,97 @@
+#!/usr/bin/env python
+
+import random
+import sys
+import pygame
+from pygame.locals import *
+from Sprite import Sprite
+
+try:
+    from OpenGL.GL import *
+    from OpenGL.GLU import *
+except:
+    print 'This requires PyOpenGL'
+    raise SystemExit
+
+
+#resolution = (1280,1024)
+resolution = (640,480)
+#resolution = (320,240)
+
+spritefile = 'img/ball.png'
+try:
+	spritefile = sys.argv[1]
+except IndexError:
+	pass
+
+#initialize pygame and setup an opengl display
+pygame.init()
+pygame.display.set_mode(resolution, OPENGL|DOUBLEBUF)
+#pygame.display.set_mode(resolution, DOUBLEBUF)
+pygame.display.set_caption("bouncetest: loading")
+
+
+#setup the camera
+glLoadIdentity()
+glMatrixMode(GL_PROJECTION)
+gluOrtho2D(0,resolution[0],0,resolution[1])
+
+glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
+glEnable(GL_TEXTURE_2D)
+glEnable(GL_BLEND)
+
+class Bouncer:
+	def __init__(self):
+		self.velocity = [random.uniform(-10,10),random.uniform(-10,10)]
+		self.sprite = Sprite(spritefile,False)
+		self.sprite.color = (random.random(),random.random(),random.random(), 0.5)
+		self.sprite.position = [random.randrange(0,resolution[0]),random.randrange(0,resolution[1])]
+		self.sprite.gen_displaylist()
+
+	def update(self):
+		if self.sprite.position[0] < 0:
+			self.sprite.position[0] = 0
+			self.velocity[0] = -self.velocity[0]
+		elif self.sprite.position[0] > resolution[0]:
+			self.sprite.position[0] = resolution[0]
+			self.velocity[0] = -self.velocity[0]
+		if self.sprite.position[1] < 0:
+			self.sprite.position[1] = 0
+			self.velocity[1] = -self.velocity[1]
+		elif self.sprite.position[1] > resolution[1]:
+			self.sprite.position[1] = resolution[1]
+			self.velocity[1] = -self.velocity[1]
+
+		#self.velocity[1] -= 1.0
+
+		self.sprite.position[0] += self.velocity[0]
+		self.sprite.position[1] += self.velocity[1]
+
+bouncers = []
+for i in range(0,9001):
+	h = Bouncer()
+	bouncers.append(h)
+
+pygame.display.set_caption("bouncetest")
+t = pygame.time.get_ticks()
+n = 0
+while 1:
+	event = pygame.event.poll()
+	if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
+	    break
+
+	#clear screen and move camera
+	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
+
+	for b in bouncers:
+		b.update()
+		b.sprite.draw()
+
+	pygame.display.flip()
+	#pygame.time.wait(10)
+	n += 1
+	if n == 100:
+		now = pygame.time.get_ticks()
+		pygame.display.set_caption("bouncetest [%0.1f FPS]" % (100./((now-t)/1000.)))
+		n = 0
+		t = now

diff --git a/img/ByteIco.png b/img/ByteIco.png
line changes: +0/-0
index 0000000..024f3d2
--- /dev/null
+++ b/img/ByteIco.png

diff --git a/img/ball.png b/img/ball.png
line changes: +0/-0
index 0000000..f06ca83
--- /dev/null
+++ b/img/ball.png

diff --git a/img/tex.png b/img/tex.png
line changes: +0/-0
index 0000000..5aa2c84
--- /dev/null
+++ b/img/tex.png

diff --git a/util.py b/util.py
line changes: +10/-0
index 0000000..d3cc058
--- /dev/null
+++ b/util.py
@@ -0,0 +1,10 @@
+def n2ceil(n):
+	"Find the next power of two greater than or equal to n"
+	assert n > 0
+	x = 1
+	while x <= 1024:
+		if x >= n:
+			return x
+		x <<= 1
+	# FAIL
+	return 1024