cau/render.py
changeset 7 95ea605276a3
child 11 a131769728f1
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/cau/render.py	Thu Dec 30 18:31:19 2010 +0100
     1.3 @@ -0,0 +1,49 @@
     1.4 +import Tkinter as tk
     1.5 +from grid import Grid
     1.6 +
     1.7 +class Render(object):
     1.8 +	def __init__(self, title, width, height):
     1.9 +		self.title = title		
    1.10 +		self.width = width
    1.11 +		self.height = height
    1.12 +		self.root = tk.Tk(className=title, sync=1)
    1.13 +		self.canvas = tk.Canvas(width=self.width, height=self.height, bg="black")
    1.14 +		self.canvas.pack(expand=tk.YES, fill=tk.BOTH)
    1.15 +	def render(self, grid):
    1.16 +		offset = (self.width / 2, self.height / 2)
    1.17 +		size = 1
    1.18 +		cell_ids = []
    1.19 +		for pos in grid.cells.iterkeys():
    1.20 +			x = pos[0] + offset[0]
    1.21 +			y = pos[1] + offset[1]
    1.22 +			cell_id = self.canvas.create_rectangle(x, y, x+size, y+size, 
    1.23 +					fill="yellow", width=0)
    1.24 +			cell_ids.append(cell_id)
    1.25 +		self.root.update()
    1.26 +		for cell_id in cell_ids:
    1.27 +			self.canvas.delete(cell_id)
    1.28 +
    1.29 +import sys
    1.30 +import time
    1.31 +import gridmarshal as marshal
    1.32 +import os, stat
    1.33 +
    1.34 +def main():
    1.35 +	filename = sys.argv[1]
    1.36 +	print "rendering ", filename	
    1.37 +	render = Render("hope", 800, 800)
    1.38 +	mod_time = 0
    1.39 +	while True:
    1.40 +		current = os.stat(filename)[stat.ST_MTIME]
    1.41 +		if current > mod_time:
    1.42 +			mod_time = current
    1.43 +			print "updating render"
    1.44 +			try:
    1.45 +				grid = marshal.loadGrid(filename)
    1.46 +				render.render(grid)
    1.47 +			except IndexError, ValueError:
    1.48 +				print "loading error"
    1.49 +		time.sleep(1)
    1.50 +
    1.51 +if __name__ == "__main__":
    1.52 +	main()