Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 33c60aa4c4
Fetching contributors…

Cannot retrieve contributors at this time

84 lines (60 sloc) 2.431 kB
import gtk
import threading
import time
import sys # for stdout.flush()
from world import TheWorld
class SpaceTime (threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.ticking = False
self.timer_id = 0
# rate limiting
ticks_per_second = 20.0 # may not reach in practice :-)
redraws_per_second = 30.0 # may not reach in practice :-)
self.ticks_increment = 1.0 / ticks_per_second
self.redraws_increment = 1.0 / redraws_per_second
self.next_redraw_time = time.time()
self.next_tick_time = time.time()
# only used when we use the gtk idle loop for updates
def tick(self):
TheWorld.tick()
return True
# used when running updates in a thread
# TODO Apparently this approach does not work on WIN32 -> which is
# problematic, because the gdk idle loop is dog slow
def run(self):
print "Simulation started."
self.ticking = True
while self.ticking:
# rate limit simulation
now = time.time()
if now >= self.next_tick_time:
TheWorld.tick()
self.next_tick_time = now + self.ticks_increment
#self.next_tick_time = self.next_tick_time + self.ticks_increment
#else:
# print ".",
# sys.stdout.flush()
# rate limit redraw
if now >= self.next_redraw_time:
gtk.gdk.threads_enter()
try:
TheWorld.queue_draw()
finally:
gtk.gdk.flush()
gtk.gdk.threads_leave()
self.next_redraw_time = now + self.redraws_increment
#self.next_redraw_time = self.next_redraw_time + self.redraws_increment
else:
time.sleep(0.00001) # don't suck 100% of cpu!
#print now, " -> ", (self.next_tick_time, self.next_redraw_time), "loop: %.8f" % (time.time() - now)
#print "Main loop took: %.8f" % (time.time() - now)
# TODO How do we handle things cleanly when one or both elements of main loop is taking longer than tick times ?
# We can optimize for constant frame rate - i.e. sim slows down
# or we can optimize for constant sim speed - i.e. frame rate slows down
# or something inbetween ?
# What are the questions we should be asking ?
def stop(self):
print "Simulation stopped."
self.ticking = False
TheSpaceTime = SpaceTime()
Jump to Line
Something went wrong with that request. Please try again.