Module Engine
[hide private]
[frames] | no frames]

Source Code for Module Engine

  1  ##################################################################### 
  2  # -*- coding: iso-8859-1 -*-                                        # 
  3  #                                                                   # 
  4  # Frets on Fire                                                     # 
  5  # Copyright (C) 2006 Sami Kyöstilä                                  # 
  6  #                                                                   # 
  7  # This program is free software; you can redistribute it and/or     # 
  8  # modify it under the terms of the GNU General Public License       # 
  9  # as published by the Free Software Foundation; either version 2    # 
 10  # of the License, or (at your option) any later version.            # 
 11  #                                                                   # 
 12  # This program is distributed in the hope that it will be useful,   # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of    # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     # 
 15  # GNU General Public License for more details.                      # 
 16  #                                                                   # 
 17  # You should have received a copy of the GNU General Public License # 
 18  # along with this program; if not, write to the Free Software       # 
 19  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,        # 
 20  # MA  02110-1301, USA.                                              # 
 21  ##################################################################### 
 22   
 23  import gc 
 24   
 25  import Network 
 26  import Object 
 27  from World import World 
 28  from Timer import Timer 
 29  from Task import Task 
 30   
31 -class Engine:
32 """Main task scheduler."""
33 - def __init__(self, fps = 60, tickrate = 1.0):
34 self.tasks = [] 35 self.frameTasks = [] 36 self.timer = Timer(fps = fps, tickrate = tickrate) 37 self.currentTask = None 38 self.paused = [] 39 self.running = True
40
41 - def quit(self):
42 for t in list(self.tasks + self.frameTasks): 43 self.removeTask(t) 44 self.running = False
45
46 - def addTask(self, task, synchronized = True):
47 """ 48 Add a task to the engine. 49 50 @param task: L{Task} to add 51 @type synchronized: bool 52 @param synchronized: If True, the task will be run with small 53 timesteps tied to the engine clock. 54 Otherwise the task will be run once per frame. 55 """ 56 if synchronized: 57 queue = self.tasks 58 else: 59 queue = self.frameTasks 60 61 if not task in queue: 62 queue.append(task) 63 task.started()
64
65 - def removeTask(self, task):
66 """ 67 Remove a task from the engine. 68 69 @param task: L{Task} to remove 70 """ 71 found = False 72 queues = self._getTaskQueues(task) 73 for q in queues: 74 q.remove(task) 75 if queues: 76 task.stopped()
77
78 - def _getTaskQueues(self, task):
79 queues = [] 80 for queue in [self.tasks, self.frameTasks]: 81 if task in queue: 82 queues.append(queue) 83 return queues
84
85 - def pauseTask(self, task):
86 """ 87 Pause a task. 88 89 @param task: L{Task} to pause 90 """ 91 self.paused.append(task)
92
93 - def resumeTask(self, task):
94 """ 95 Resume a paused task. 96 97 @param task: L{Task} to resume 98 """ 99 self.paused.remove(task)
100
101 - def enableGarbageCollection(self, enabled):
102 """ 103 Enable or disable garbage collection whenever a random garbage 104 collection run would be undesirable. Disabling the garbage collector 105 has the unfortunate side-effect that your memory usage will skyrocket. 106 """ 107 if enabled: 108 gc.enable() 109 else: 110 gc.disable()
111
112 - def collectGarbage(self):
113 """ 114 Run a garbage collection run. 115 """ 116 gc.collect()
117
118 - def boostBackgroundThreads(self, boost):
119 """ 120 Increase priority of background threads. 121 122 @param boost True of the scheduling of the main UI thread should be 123 made fairer to background threads, False otherwise. 124 """ 125 self.timer.highPriority = not bool(boost)
126
127 - def _runTask(self, task, ticks = 0):
128 if not task in self.paused: 129 self.currentTask = task 130 task.run(ticks) 131 self.currentTask = None
132
133 - def run(self):
134 """Run one cycle of the task scheduler engine.""" 135 if not self.frameTasks and not self.tasks: 136 return False 137 138 for task in self.frameTasks: 139 self._runTask(task) 140 for ticks in self.timer.advanceFrame(): 141 for task in self.tasks: 142 self._runTask(task, ticks) 143 return True
144