Browse Source

cycles per seconds management

Bastien Sevajol 8 years ago
parent
commit
cea00c5141
2 changed files with 21 additions and 3 deletions
  1. 1 0
      sandbox/engulf/run.py
  2. 20 3
      synergine2/core.py

+ 1 - 0
sandbox/engulf/run.py View File

140
         terminal_manager=TerminalManager([GameTerminal(
140
         terminal_manager=TerminalManager([GameTerminal(
141
             asynchronous=False,
141
             asynchronous=False,
142
         )]),
142
         )]),
143
+        cycles_per_seconds=1,
143
     )
144
     )
144
     core.run()
145
     core.run()
145
 
146
 

+ 20 - 3
synergine2/core.py View File

1
+import time
1
 from synergine2.cycle import CycleManager
2
 from synergine2.cycle import CycleManager
2
 from synergine2.simulation import Simulation
3
 from synergine2.simulation import Simulation
3
 from synergine2.terminals import TerminalManager
4
 from synergine2.terminals import TerminalManager
10
         simulation: Simulation,
11
         simulation: Simulation,
11
         cycle_manager: CycleManager,
12
         cycle_manager: CycleManager,
12
         terminal_manager: TerminalManager=None,
13
         terminal_manager: TerminalManager=None,
13
-
14
+        cycles_per_seconds: int=1,
14
     ):
15
     ):
15
         self.simulation = simulation
16
         self.simulation = simulation
16
         self.cycle_manager = cycle_manager
17
         self.cycle_manager = cycle_manager
17
         self.terminal_manager = terminal_manager or TerminalManager([])
18
         self.terminal_manager = terminal_manager or TerminalManager([])
19
+        self._loop_delta = 1./cycles_per_seconds
20
+        self._current_cycle_start_time = None
18
 
21
 
19
     def run(self):
22
     def run(self):
20
         try:
23
         try:
26
             self.terminal_manager.send(start_package)
29
             self.terminal_manager.send(start_package)
27
 
30
 
28
             while True:
31
             while True:
32
+                self._start_cycle()
33
+
29
                 events = []
34
                 events = []
30
                 packages = self.terminal_manager.receive()
35
                 packages = self.terminal_manager.receive()
31
                 for package in packages:
36
                 for package in packages:
47
                 self.simulation.subjects.adds = []
52
                 self.simulation.subjects.adds = []
48
                 self.simulation.subjects.removes = []
53
                 self.simulation.subjects.removes = []
49
 
54
 
50
-                import time
51
-                time.sleep(1)  # TODO: tick control
55
+                self._end_cycle()
52
         except KeyboardInterrupt:
56
         except KeyboardInterrupt:
53
             pass  # Just stop while
57
             pass  # Just stop while
54
         self.terminal_manager.stop()
58
         self.terminal_manager.stop()
59
+
60
+    def _start_cycle(self):
61
+        self._current_cycle_start_time = time.time()
62
+
63
+    def _end_cycle(self) -> None:
64
+        """
65
+        Make a sleep if cycle duration take less time of wanted (see
66
+        cycles_per_seconds constructor parameter)
67
+        """
68
+        cycle_duration = time.time() - self._current_cycle_start_time
69
+        sleep_time = self._loop_delta - cycle_duration
70
+        if sleep_time > 0:
71
+            time.sleep(sleep_time)