#
# cadisplay.py: Display engine for 2D CA model of bike traffic
#               Called by bike.py.
#               Exercise: Rewrite as a class.
#
"""Display engine for 2D CA model of bike traffic"""

try:
	import pygame
except ImportError:
	# catch any import errors in case packages not installed
	raise ImportError, "PyGame required."

# Cell colors, format is (R, G, B)
empty  = (0,   0,   0)   # Black
hColor = (0, 190,  60)   # Greenish = east-west
vColor = (0,  80, 250)   # Blueish  = north-south

# Dummy assignments to declare variables set in Init, making them global
ConfigRect = 0
screen     = 0
CellPixels = 5

def InitCADisplay(ca,CellSize):
	# Want the assignments to be to global variables
	global ConfigRect,screen,CellPixels
	# Create a square as big as the entire surface, for clearing the surface
	nSites = ca.size
	CellPixels = CellSize
	ConfigRect = pygame.Rect(0,0,nSites*CellPixels,nSites*CellPixels)
	# Initialize PyGame
	pygame.init()
	pygame.display.set_caption('2D Cellular Automaton Bike Traffic Simulator')
	screen = pygame.display.set_mode((nSites*CellPixels,nSites*CellPixels))
	screen.fill(empty)

def SpDisplayState(ca):
	state = ca.curr
	nSites = ca.size
	screen.fill(empty, ConfigRect)	# clear surface before drawing new cells
	# Draw cells on the pygame window
	for i in xrange(nSites):
		for j in xrange(nSites):
			if state[i][j] == 1:
				screen.fill(hColor,[j*CellPixels,i*CellPixels,CellPixels,CellPixels])
			elif state[i][j] == 2:
				screen.fill(vColor,[j*CellPixels,i*CellPixels,CellPixels,CellPixels])
	pygame.display.flip()
