commit:6d1cd20b7a27a5b99b7617445a6bf49b5bb93d98
author:chip
committer:chip
date:Thu Nov 15 08:00:49 2007 +0000
parents:70517cc59a422708f2af2638e794af27a7d99c2a
Fixed texture alignment and added sprite scaling
diff --git a/Engine.py b/Engine.py
line changes: +10/-1
index 9209d84..9490031
--- a/Engine.py
+++ b/Engine.py
@@ -34,7 +34,16 @@ def init(resolution=(640,480)):
 	# Set up the camera
 	glLoadIdentity()
 	glMatrixMode(GL_PROJECTION)
-	gluOrtho2D(0,resolution[0],0,resolution[1])
+	#gluOrtho2D(0,resolution[0],0,resolution[1])
+	# calculate Z-depth from average resolution
+	zdepth = sum(resolution) / 2
+	glFrustum(0,resolution[0],0,resolution[1],zdepth,zdepth * 10)
+	glTranslate(0,0,-zdepth)
+
+	#glTranslate(resolution[0]/2, resolution[1]/2, 0)
+	#glTranslate(0,0,-zdepth)
+	#glRotate(30, 0, 1, 0)
+	#glTranslate(-resolution[0]/2, -resolution[1]/2, 0)
 
 	# And initialize things how we like in GL
 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

diff --git a/Sprite.py b/Sprite.py
line changes: +46/-10
index ec14112..d02a99e
--- a/Sprite.py
+++ b/Sprite.py
@@ -7,25 +7,27 @@ from Numeric import array
 from util import *
 import TexMan
 
-LEFT = 0
-CENTER = 1
-RIGHT = 2
-TOP = 2
-BOTTOM = 0
+LEFT = -1
+BOTTOM = -1
+CENTER = 0
+RIGHT = 1
+TOP = 1
 
 class Sprite:
 	frames = None
 	framerate = 0.0
 	frameno = 0
+	animating = False
+	anim_t = 0
 	color = (1.0,1.0,1.0,1.0)
 	position = (0,0)
 	size = (0,0)
+	scale = 1.0
 	rotation = 0
+	gravity = (LEFT, BOTTOM)
 	uparrow = False
 	displaylists = None
 
-	gravity = (LEFT, LEFT)
-
 	def __init__(self, file, genlist=True):
 		self.frames = []
 		d = None
@@ -39,6 +41,8 @@ class Sprite:
 			self.frames.append(d['texture'])
 		self.size = d['size']
 		self.tsize = d['tsize']
+		print "size:",self.size
+		print "tsize:",self.tsize
 		if genlist:
 			self.gen_displaylist()
 
@@ -77,14 +81,44 @@ class Sprite:
 			glDrawArrays(GL_QUADS, 0, 4)
 			glEndList()
 
+	def setColor(self, r, g, b, a = 1.0):
+		self.color = (r,g,b,a)
+		self.gen_displaylist()
+
+	def setScale(self, scale):
+		self.scale = scale
+	
+
+	def setGravity(self, xg, yg):
+		if self.gravity != (xg,yg):
+			self.gravity = (xg,yg)
+			self.gen_displaylist()
+		
+
+	def setFramerate(self, rate):
+		self.framerate = rate
+		if self.framerate != 0.0:
+			self.anim_period = int(1000/rate)
+			if self.anim_t == 0:
+				self.reset()
+		else:
+			self.stop()
 
-	def anim_start(self):
+
+	def reset(self):
 		self.anim_t = pygame.time.get_ticks()
-		self.anim_period = int(1000/self.framerate)
+
+
+	def play(self):
+		self.animating = True
+
+
+	def stop(self):
+		self.animating = False
 
 
 	def draw(self):
-		if self.framerate > 0.0:
+		if self.animating:
 			now = pygame.time.get_ticks()
 			dt = now - self.anim_t
 			if dt > self.anim_period:
@@ -94,6 +128,8 @@ class Sprite:
 		glTranslatef(self.position[0], self.position[1], 0)
 		if self.rotation != 0:
 			glRotatef(self.rotation, 0, 0, -1)
+		if self.scale != 1.0:
+			glScalef(self.scale, self.scale, 1.0)
 		glCallList(self.displaylists[self.frameno])
 		if self.uparrow:
 			glDisable(GL_TEXTURE_2D)

diff --git a/TexMan.py b/TexMan.py
line changes: +2/-2
index f328f51..c8488c1
--- a/TexMan.py
+++ b/TexMan.py
@@ -21,9 +21,9 @@ def load(file):
         th = n2ceil(h)
 	if config.square_textures:
 		tw = th = max(tw,th)
-        newImage = pygame.Surface((tw,th), SRCALPHA, 32);
+        newImage = pygame.Surface((tw,th), SRCALPHA, 32)
         newImage.fill((255,255,255,0))
-        newImage.blit(image, (0,0));
+        newImage.blit(image, (0, th - h))	# We want this in the lower corner
 
         # Freaking pygame making us do this by hand...
         colors = pygame.surfarray.pixels3d(newImage)

diff --git a/data/example1 b/data/example1
line changes: +1/-1
index 19533f0..5a5e4c6
--- a/data/example1
+++ b/data/example1
@@ -21,4 +21,4 @@ tile 6 2
 
 tilesize 1 1
 texture img/ByteIco.png
-tile 263 249
+tile 263 263

diff --git a/scrolltest.py b/scrolltest.py
line changes: +6/-6
index f9e050a..595a48f
--- a/scrolltest.py
+++ b/scrolltest.py
@@ -17,12 +17,12 @@ ground = Collage('data/example1')
 ground.position = [0,0]
 stars = Collage('data/stars')
 stars.position = [0,0]
-candelabra = Sprite(['Sprites/candelabra_short/cand_s_1.png','Sprites/candelabra_short/cand_s_2.png','Sprites/candelabra_short/cand_s_3.png','Sprites/candelabra_short/cand_s_4.png'],False)
-candelabra.position = [64, 50]
-candelabra.gravity = (CENTER, BOTTOM)
-candelabra.framerate = 10.0
-candelabra.anim_start()
-candelabra.gen_displaylist()
+candelabra = Sprite(['Sprites/candelabra_short/cand_s_1.png','Sprites/candelabra_short/cand_s_2.png','Sprites/candelabra_short/cand_s_3.png','Sprites/candelabra_short/cand_s_4.png'])
+candelabra.position = [64, 64]
+candelabra.setScale(4.0)
+candelabra.setGravity(CENTER, BOTTOM)
+candelabra.setFramerate(10.0)
+candelabra.play()
 
 def update():
 	ground.position[0] -= 5