commit:51dd80f39a729d19e7cb97e5dabf101b57e0537e
author:Chip Black
committer:Chip Black
date:Mon Jul 7 16:05:33 2008 -0500
parents:ca9d57f5ac3b1750f2424ac5d8e1623f5e91f1cc
Factored camera functionality out to Camera.py
diff --git a/Camera.py b/Camera.py
line changes: +53/-0
index 0000000..6df61f9
--- /dev/null
+++ b/Camera.py
@@ -0,0 +1,53 @@
+class Camera:
+	stage = None
+	resolution = None
+	focus = None
+	camerapos = None
+	bbox = None
+
+	def __init__(self, stage, resolution):
+		self.stage = stage
+		self.resolution = resolution
+		self.camerapos = [0,0]
+
+
+	def setFocus(self, obj):
+		self.focus = obj
+
+
+	def clearFocus(self):
+		self.focus = None
+
+
+	def setPosition(self, x, y):
+		self.camerapos = [x, y]
+
+
+	def setBbox(self, bbox):
+		self.bbox = bbox
+
+
+	def clearBbox(self):
+		self.bbox = None
+
+
+	def update(self):
+		if self.focus:
+			self.camerapos = [self.focus.position[0] - self.resolution[0]/2,
+					  self.focus.position[1] - self.resolution[1]/4]
+
+		if self.bbox:
+			# This is a wee bit confusing since Rects assume (0,0) in the
+			# upper left, but we want it in the lower left.  Tops and
+			# bottoms are reversed.
+			if self.bbox.height < self.resolution[1] or self.camerapos[1] < self.bbox.top:
+				self.camerapos[1] = self.bbox.top
+			elif self.camerapos[1] + self.resolution[1] > self.bbox.bottom:
+				self.camerapos[1] = self.bbox.bottom - self.resolution[1]
+
+			if self.bbox.width < self.resolution[0] or self.camerapos[0] < self.bbox.left:
+				self.camerapos[0] = self.bbox.left
+			elif self.camerapos[0] + self.resolution[0] > self.bbox.right:
+				self.camerapos[0] = self.bbox.right - self.resolution[0]
+
+		self.stage.moveTo(self.camerapos[0], self.camerapos[1])

diff --git a/platformtest.py b/platformtest.py
line changes: +6/-18
index 057739f..2604b68
--- a/platformtest.py
+++ b/platformtest.py
@@ -6,6 +6,7 @@ from Collage import *
 from Surface import *
 from Actor import *
 from Stage import *
+from Camera import *
 import Engine
 
 #resolution = (1280,1024)
@@ -31,6 +32,9 @@ candelabra.play()
 
 main.add(candelabra)
 
+camera = Camera(stage, resolution)
+camera.setBbox(main.bbox)
+
 class Richter(Actor):
 	def __init__(self, surfaces=None):
 		Actor.__init__(self, surfaces)
@@ -107,6 +111,7 @@ class Richter(Actor):
 richter = Richter(stage.surfaces)
 richter.setPosition(32, 64)
 main.add(richter)
+camera.setFocus(richter)
 
 
 def input(e):
@@ -130,23 +135,6 @@ def update():
 	richter.update()
 	if richter.position[1] < 0:
 		richter.position = [richter.position[0],480]
-	stagepos = [richter.position[0] - resolution[0]/2, richter.position[1] - resolution[1]/4]
-
-	### Stage bounding box stuff should be moved upstream
-	# This is a wee bit confusing since Rects assume (0,0) in the upper
-	# left, but we want it in the lower left.  Flip tops and bottoms.
-	if main.bbox.height < resolution[1] or stagepos[1] < main.bbox.top:
-		stagepos[1] = main.bbox.top
-	elif stagepos[1] + resolution[1] > main.bbox.bottom:
-		stagepos[1] = main.bbox.bottom - resolution[1]
-
-	if main.bbox.width < resolution[0] or stagepos[0] < main.bbox.left:
-		stagepos[0] = main.bbox.left
-	elif stagepos[0] + resolution[0] > main.bbox.right:
-		stagepos[0] = main.bbox.right- resolution[0]
-
-	stage.moveTo(stagepos[0], stagepos[1])
-
 
 n = 0
 def fpsthing():
@@ -160,6 +148,6 @@ def fpsthing():
 
 Engine.engine(
 	[input],
-	[update, fpsthing],
+	[update, camera.update, fpsthing],
 	[stage.draw]
 )