1
#####################################################################
2
# -*- coding: iso-8859-1 -*- #
5
# Copyright (C) 2006 Sami Ky�stil� #
6
# 2008 evilynux <evilynux@gmail.com> #
8
# This program is free software; you can redistribute it and/or #
9
# modify it under the terms of the GNU General Public License #
10
# as published by the Free Software Foundation; either version 2 #
11
# of the License, or (at your option) any later version. #
13
# This program is distributed in the hope that it will be useful, #
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16
# GNU General Public License for more details. #
18
# You should have received a copy of the GNU General Public License #
19
# along with this program; if not, write to the Free Software #
20
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, #
21
# MA 02110-1301, USA. #
22
#####################################################################
28
from World import World
34
"""Main task scheduler."""
35
def __init__(self, fps = 60):
39
self.currentTask = None
42
self.clock = pygame.time.Clock()
45
for t in list(self.tasks + self.frameTasks):
49
def addTask(self, task, synchronized = True):
51
Add a task to the engine.
53
@param task: L{Task} to add
54
@type synchronized: bool
55
@param synchronized: If True, the task will be run with small
56
timesteps tied to the engine clock.
57
Otherwise the task will be run once per frame.
62
queue = self.frameTasks
68
def removeTask(self, task):
70
Remove a task from the engine.
72
@param task: L{Task} to remove
75
queues = self._getTaskQueues(task)
81
def _getTaskQueues(self, task):
83
for queue in [self.tasks, self.frameTasks]:
88
def pauseTask(self, task):
92
@param task: L{Task} to pause
94
self.paused.append(task)
96
def resumeTask(self, task):
100
@param task: L{Task} to resume
102
self.paused.remove(task)
104
def enableGarbageCollection(self, enabled):
106
Enable or disable garbage collection whenever a random garbage
107
collection run would be undesirable. Disabling the garbage collector
108
has the unfortunate side-effect that your memory usage will skyrocket.
115
def collectGarbage(self):
117
Run a garbage collection run.
121
def _runTask(self, task, ticks = 0):
122
if not task in self.paused:
123
self.currentTask = task
125
self.currentTask = None
128
"""Run one cycle of the task scheduler engine."""
129
if not self.frameTasks and not self.tasks:
132
for task in self.frameTasks:
134
tick = self.clock.get_time()
135
for task in self.tasks:
136
self._runTask(task, tick)
137
self.clock.tick(self.fps)