def create_data_folder(): """ Creates the data folder if it doesn't exist. """ path = "./data" # default folder try: path = CONFIG["data_folder"] except KeyError: pass if not os.path.exists(path): os.mkdir(path) Logger.log_system("Created data directory.")
def set_tick_speed(controller, symbol): old_tps = controller.ticks_per_secs max_tps = 400 try: max_tps = CONFIG["max_TPS"] except KeyError: pass if symbol == key.UP: new_tps = min(old_tps * 2.0, max_tps) else: new_tps = max(1, old_tps / 2.0) pyglet.clock.unschedule(controller.tick) pyglet.clock.schedule_interval(controller.tick, 1.0 / new_tps) controller.ticks_per_secs = new_tps Logger.log_system("Set TPS to {}.".format(controller.ticks_per_secs))
def run_app(config=None): # Setup model & controller sim = simulation.setup() keyboard = pyglet.window.key.KeyStateHandler() controller = sim_control.SimController(sim, keyboard) # Add handlers app.win.push_handlers(controller) app.win.push_handlers(keyboard) # Profiling Profiler.make_profiler_category("draw_times") Profiler.make_profiler_category("tick_times") # Logger verbose level default_verbose = 3 if "verbose_level" in app.CONFIG: default_verbose = app.CONFIG["verbose_level"] Logger.set_verbose_level(default_verbose) fps_counter = None if "show_FPS" in app.CONFIG and app.CONFIG["show_FPS"]: fps_counter = pyglet.window.FPSDisplay(app.win) # Schedule initial simulation speed ticks_per_sec = 25.0 pyglet.clock.schedule_interval(controller.tick, 1 / ticks_per_sec) # Cap FPS max_fps = 120 if "max_FPS" in app.CONFIG: max_fps = app.CONFIG["max_FPS"] pyglet.clock.set_fps_limit(max_fps) # Draw event @app.win.event def on_draw(): starttime = time.time() app.win.clear() sim.draw() controller.draw() Profiler.add_profiler_data("draw_times", time.time() - starttime) if fps_counter: fps_counter.draw() # Generate history (run the simulation without opening the window) # This is useful to save time on drawing history_length = 0 if history_length > 0: paused_state = sim.paused Logger.log_system( "Generating {} ticks of history.".format(history_length)) starttime = time.time() sim.paused = False for _ in range(history_length): controller.tick() sim.paused = paused_state Logger.log_system("Finished generating history. ({} s)".format( (time.time() - starttime))) # Main app loop pyglet.app.run() # Collect & log some stats post_run_stats(controller, sim) return