~ubuntu-branches/ubuntu/precise/fofix-dfsg/precise

« back to all changes in this revision

Viewing changes to src/Engine.py

  • Committer: Bazaar Package Importer
  • Author(s): Christian Hammers
  • Date: 2010-02-21 12:09:32 UTC
  • Revision ID: james.westby@ubuntu.com-20100221120932-6bh992d2u8dtj9gr
Tags: upstream-3.121
Import upstream version 3.121

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#####################################################################
 
2
# -*- coding: iso-8859-1 -*-                                        #
 
3
#                                                                   #
 
4
# Frets on Fire                                                     #
 
5
# Copyright (C) 2006 Sami Ky�stil�                                  #
 
6
#               2008 evilynux <evilynux@gmail.com>                  #
 
7
#                                                                   #
 
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.            #
 
12
#                                                                   #
 
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.                      #
 
17
#                                                                   #
 
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
#####################################################################
 
23
 
 
24
import gc
 
25
 
 
26
import Network
 
27
import Object
 
28
from World import World
 
29
from Task import Task
 
30
import pygame
 
31
import Player
 
32
 
 
33
class Engine:
 
34
  """Main task scheduler."""
 
35
  def __init__(self, fps = 60):
 
36
    self.tasks = []
 
37
    self.frameTasks = []
 
38
    self.fps = fps
 
39
    self.currentTask = None
 
40
    self.paused = []
 
41
    self.running = True
 
42
    self.clock = pygame.time.Clock()
 
43
 
 
44
  def quit(self):
 
45
    for t in list(self.tasks + self.frameTasks):
 
46
      self.removeTask(t)
 
47
    self.running = False
 
48
 
 
49
  def addTask(self, task, synchronized = True):
 
50
    """
 
51
    Add a task to the engine.
 
52
    
 
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.
 
58
    """
 
59
    if synchronized:
 
60
      queue = self.tasks
 
61
    else:
 
62
      queue = self.frameTasks
 
63
      
 
64
    if not task in queue:
 
65
      queue.append(task)
 
66
      task.started()
 
67
 
 
68
  def removeTask(self, task):
 
69
    """
 
70
    Remove a task from the engine.
 
71
    
 
72
    @param task:    L{Task} to remove
 
73
    """
 
74
    found = False
 
75
    queues = self._getTaskQueues(task)
 
76
    for q in queues:
 
77
      q.remove(task)
 
78
    if queues:
 
79
      task.stopped()
 
80
 
 
81
  def _getTaskQueues(self, task):
 
82
    queues = []
 
83
    for queue in [self.tasks, self.frameTasks]:
 
84
      if task in queue:
 
85
        queues.append(queue)
 
86
    return queues
 
87
 
 
88
  def pauseTask(self, task):
 
89
    """
 
90
    Pause a task.
 
91
    
 
92
    @param task:  L{Task} to pause
 
93
    """
 
94
    self.paused.append(task)
 
95
 
 
96
  def resumeTask(self, task):
 
97
    """
 
98
    Resume a paused task.
 
99
    
 
100
    @param task:  L{Task} to resume
 
101
    """
 
102
    self.paused.remove(task)
 
103
    
 
104
  def enableGarbageCollection(self, enabled):
 
105
    """
 
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.
 
109
    """
 
110
    if enabled:
 
111
      gc.enable()
 
112
    else:
 
113
      gc.disable()
 
114
      
 
115
  def collectGarbage(self):
 
116
    """
 
117
    Run a garbage collection run.
 
118
    """
 
119
    gc.collect()
 
120
 
 
121
  def _runTask(self, task, ticks = 0):
 
122
    if not task in self.paused:
 
123
      self.currentTask = task
 
124
      task.run(ticks)
 
125
      self.currentTask = None
 
126
 
 
127
  def run(self):
 
128
    """Run one cycle of the task scheduler engine."""
 
129
    if not self.frameTasks and not self.tasks:
 
130
      return False
 
131
    
 
132
    for task in self.frameTasks:
 
133
      self._runTask(task)
 
134
    tick = self.clock.get_time()
 
135
    for task in self.tasks:
 
136
      self._runTask(task, tick)
 
137
    self.clock.tick(self.fps)
 
138
    return True