caurender.py
changeset 3 89d76549ba6e
child 6 cac4ae0f22f4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/caurender.py	Thu Dec 30 14:22:05 2010 +0100
     1.3 @@ -0,0 +1,46 @@
     1.4 +import Tkinter as tk
     1.5 +from caugrid 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 caumarshal 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 +			grid = marshal.loadGrid(filename)
    1.45 +			render.render(grid)
    1.46 +		time.sleep(1)
    1.47 +
    1.48 +if __name__ == "__main__":
    1.49 +	main()