ya2 · news · projects · code · about

do not do asserts at runtime
[pmachines.git] / pmachines / app.py
index bbb82c7194f8ec386c1cb94f4a32b4cba8c263a1..994b0fab35546bcad438ee5b0a84db7434ff6b0f 100755 (executable)
@@ -8,6 +8,7 @@ from sys import platform, argv, exit
 from logging import info, debug
 from os.path import exists
 from os import makedirs
+from multiprocessing import cpu_count
 from panda3d.core import Filename, load_prc_file_data, AntialiasAttrib, \
     Texture, WindowProperties, LVector2i, TextNode
 from panda3d.bullet import BulletWorld, BulletDebugNode
@@ -31,6 +32,8 @@ from ya2.p3d.p3d import LibP3d
 from ya2.utils.lang import LangMgr
 from ya2.utils.log import LogMgr
 from ya2.utils.functional import FunctionalTest
+from ya2.p3d.asserts import assert_threads, assert_tasks, assert_render3d, \
+    assert_render2d, assert_aspect2d, assert_events, assert_buffers
 
 
 class MainFsm(FSM):
@@ -44,12 +47,25 @@ class MainFsm(FSM):
 
     def exitMenu(self):
         self._pmachines.on_menu_exit()
+        self.__do_asserts()
 
     def enterScene(self, cls):
         self._pmachines.on_scene_enter(cls)
 
     def exitScene(self):
         self._pmachines.on_scene_exit()
+        self.__do_asserts()
+
+    def __do_asserts(self):
+        args = self._pmachines._args
+        if not LibP3d.runtime() or args.functional_test or args.functional_ref:
+            assert_threads()
+            assert_tasks()
+            assert_render3d()
+            assert_render2d()
+            assert_aspect2d()
+            assert_events()
+            assert_buffers()
 
 
 class PmachinesApp:
@@ -101,6 +117,9 @@ class PmachinesApp:
             self._fsm.demand('Menu')
         if args.functional_test or args.functional_ref:
             FunctionalTest(args.functional_ref, self._pos_mgr)
+        if not LibP3d.runtime() or args.functional_test or args.functional_ref:
+            self.__fps_lst = []
+            taskMgr.do_method_later(1.0, self.__assert_fps, 'assert_fps')
 
     def on_menu_enter(self):
         self._menu_bg = Background()
@@ -125,7 +144,8 @@ class PmachinesApp:
             self.reload,
             self.scenes,
             self._pos_mgr,
-            self._args.functional_test or self._args.functional_ref)
+            self._args.functional_test or self._args.functional_ref,
+            self._options['development']['mouse_coords'])
 
     def on_scene_exit(self):
         self._unset_physics()
@@ -280,3 +300,12 @@ class PmachinesApp:
     def _on_aspect_ratio_changed(self):
         if self._fsm.state == 'Scene':
             self._scene.on_aspect_ratio_changed()
+
+    def __assert_fps(self, task):
+        if len(self.__fps_lst) > 3:
+            self.__fps_lst.pop(0)
+        self.__fps_lst += [globalClock.average_frame_rate]
+        if len(self.__fps_lst) == 4:
+            fps_threshold = 55 if cpu_count() >= 4 else 25
+            assert(any(fps > fps_threshold for fps in self.__fps_lst), 'low fps %s' % self.__fps_lst)
+        return task.again