Esempio n. 1
0
    def testFrameLimiter(self):
        t = Timer(fps = 60)

        fps = []
        while t.frame < 100:
            ticks = list(t.advanceFrame())
            fps.append(t.fpsEstimate)

        fps = fps[30:]
        avgFps = sum(fps) / len(fps)
        assert 0.8 * t.fps < avgFps < 1.2 * t.fps
Esempio n. 2
0
  def testFrameLimiter(self):
    t = Timer(fps = 60)

    fps = []
    while t.frame < 100:
      ticks = list(t.advanceFrame())
      fps.append(t.fpsEstimate)

    fps = fps[30:]
    avgFps = sum(fps) / len(fps)
    assert 0.8 * t.fps < avgFps < 1.2 * t.fps
Esempio n. 3
0
class Engine:
  """Main task scheduler."""
  def __init__(self, fps = 60, tickrate = 1.0):
    self.tasks = []
    self.frameTasks = []
    self.timer = Timer(fps = fps, tickrate = tickrate)
    self.currentTask = None
    self.paused = []
    self.running = True






#Paragraph responsible for tasks:

  def addTask(self, task, synchronized = True):
    """
    Add a task to the engine.

    @param task:          L{Task} to add
    @type  synchronized:  bool
    @param synchronized:  If True, the task will be run with small
                          timesteps tied to the engine clock.
                          Otherwise the task will be run once per frame.
    """
    if synchronized:
      queue = self.tasks
    else:
      queue = self.frameTasks

    if not task in queue:
      queue.append(task)
      task.started()

  def removeTask(self, task):
    """
    Remove a task from the engine.

    @param task:    L{Task} to remove
    """
    found = False
    queues = self._getTaskQueues(task)
    for q in queues:
      q.remove(task)
    if queues:
      task.stopped()

  def _getTaskQueues(self, task):
    queues = []
    for queue in [self.tasks, self.frameTasks]:
      if task in queue:
        queues.append(queue)
    return queues

  def pauseTask(self, task):
    """
    Pause a task.

    @param task:  L{Task} to pause
    """
    self.paused.append(task)

  def resumeTask(self, task):
    """
    Resume a paused task.

    @param task:  L{Task} to resume
    """
    self.paused.remove(task)

  def _runTask(self, task, ticks = 0):
    if not task in self.paused:
      self.currentTask = task
      task.run(ticks)
      self.currentTask = None







#Paragraph responsible for garbage collector:

  def enableGarbageCollection(self, enabled):
    """
    Enable or disable garbage collection whenever a random garbage
    collection run would be undesirable. Disabling the garbage collector
    has the unfortunate side-effect that your memory usage will skyrocket.
    """
    if enabled:
      gc.enable()
    else:
      gc.disable()

  def collectGarbage(self):
    """
    Run a garbage collection run.
    """
    gc.collect()

  def boostBackgroundThreads(self, boost):
    """
    Increase priority of background threads.

    @param boost True of the scheduling of the main UI thread should  be
                 made fairer to background threads, False otherwise.
    """
    self.timer.highPriority = not bool(boost)






  """
  Paragraph responsible to start and quit the game
  """
  def quit(self):
    for t in list(self.tasks + self.frameTasks):
      self.removeTask(t)
    self.running = False



  def run(self):
    """Run one cycle of the task scheduler engine."""
    if not self.frameTasks and not self.tasks:
      return False

    for task in self.frameTasks:
      self._runTask(task)
    for ticks in self.timer.advanceFrame():
      for task in self.tasks:
        self._runTask(task, ticks)
    return True
Esempio n. 4
0
class Engine:
    """Main task scheduler."""
    def __init__(self, fps = 60, tickrate = 1.0):
        self.tasks = []
        self.frameTasks = []
        self.timer = Timer(fps = fps, tickrate = tickrate)
        self.currentTask = None
        self.paused = []
        self.running = True

    def quit(self):
        for t in list(self.tasks + self.frameTasks):
            self.removeTask(t)
        self.running = False

    def addTask(self, task, synchronized = True):
        """
        Add a task to the engine.

        @param task:          L{Task} to add
        @type  synchronized:  bool
        @param synchronized:  If True, the task will be run with small
                              timesteps tied to the engine clock.
                              Otherwise the task will be run once per frame.
        """
        if synchronized:
            queue = self.tasks
        else:
            queue = self.frameTasks

        if not task in queue:
            queue.append(task)
            task.started()

    def removeTask(self, task):
        """
        Remove a task from the engine.

        @param task:    L{Task} to remove
        """
        found = False
        queues = self._getTaskQueues(task)
        for q in queues:
            q.remove(task)
        if queues:
            task.stopped()

    def _getTaskQueues(self, task):
        queues = []
        for queue in [self.tasks, self.frameTasks]:
            if task in queue:
                queues.append(queue)
        return queues

    def pauseTask(self, task):
        """
        Pause a task.

        @param task:  L{Task} to pause
        """
        self.paused.append(task)

    def resumeTask(self, task):
        """
        Resume a paused task.

        @param task:  L{Task} to resume
        """
        self.paused.remove(task)

    def enableGarbageCollection(self, enabled):
        """
        Enable or disable garbage collection whenever a random garbage
        collection run would be undesirable. Disabling the garbage collector
        has the unfortunate side-effect that your memory usage will skyrocket.
        """
        if enabled:
            gc.enable()
        else:
            gc.disable()

    def collectGarbage(self):
        """
        Run a garbage collection run.
        """
        gc.collect()

    def boostBackgroundThreads(self, boost):
        """
        Increase priority of background threads.

        @param boost True of the scheduling of the main UI thread should  be
                     made fairer to background threads, False otherwise.
        """
        self.timer.highPriority = not bool(boost)

    def _runTask(self, task, ticks = 0):
        if not task in self.paused:
            self.currentTask = task
            task.run(ticks)
            self.currentTask = None

    def run(self):
        """Run one cycle of the task scheduler engine."""
        if not self.frameTasks and not self.tasks:
            return False

        for task in self.frameTasks:
            self._runTask(task)
        for ticks in self.timer.advanceFrame():
            for task in self.tasks:
                self._runTask(task, ticks)
        return True