gamnit: intro cache for `Camera::mvp_matrix`
authorAlexis Laferrière <alexis.laf@xymus.net>
Sat, 22 Apr 2017 17:52:40 +0000 (13:52 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 9 May 2017 13:37:42 +0000 (09:37 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/gamnit/cameras.nit
lib/gamnit/cameras_cache.nit [new file with mode: 0644]
lib/gamnit/flat.nit

index 401e1ce..c9754f7 100644 (file)
@@ -264,7 +264,7 @@ class UICamera
        # Bottom right corner of the screen, at z = 0
        fun bottom_right: Point3d[Float] do return new Point3d[Float](position.x + width, position.y - height, 0.0)
 
-       # TODO cache the anchors and the matrix
+       # TODO cache the anchors
 
        redef fun mvp_matrix
        do
diff --git a/lib/gamnit/cameras_cache.nit b/lib/gamnit/cameras_cache.nit
new file mode 100644 (file)
index 0000000..08adc8f
--- /dev/null
@@ -0,0 +1,124 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Cache the `Matrix` produced by `Camera::mvp_matrix`
+module cameras_cache
+
+import cameras
+
+redef class Camera
+       private var mvp_matrix_cache: nullable Matrix = null
+
+       private var position_cache = new Point3d[Float](0.0, 0.0, 0.0)
+
+       # Has `position` changed from `position_cache`? Update the cache at the same time
+       private fun check_position_changed: Bool
+       do
+               if position.x != position_cache.x or
+                  position.y != position_cache.y or
+                  position.z != position_cache.z then
+                       position_cache.x = position.x
+                       position_cache.y = position.y
+                       position_cache.z = position.z
+                       return true
+               end
+               return false
+       end
+end
+
+redef class EulerCamera
+       # The returned matrix must not be modified as it is cached.
+       redef fun mvp_matrix
+       do
+               var m = mvp_matrix_cache
+               if m == null or check_position_changed then
+                       m = super
+                       mvp_matrix_cache = m
+               end
+               return m
+       end
+
+       redef fun pitch=(value)
+       do
+               super
+               mvp_matrix_cache = null
+       end
+
+       redef fun yaw=(value)
+       do
+               super
+               mvp_matrix_cache = null
+       end
+
+       redef fun roll=(value)
+       do
+               super
+               mvp_matrix_cache = null
+       end
+
+       redef fun field_of_view_y=(value)
+       do
+               super
+               mvp_matrix_cache = null
+       end
+
+       redef fun near=(value)
+       do
+               super
+               mvp_matrix_cache = null
+       end
+
+       redef fun far=(value)
+       do
+               super
+               mvp_matrix_cache = null
+       end
+end
+
+redef class UICamera
+       # The returned matrix must not be modified as it is cached.
+       redef fun mvp_matrix
+       do
+               var m = mvp_matrix_cache
+               if m == null or check_position_changed then
+                       m = super
+                       mvp_matrix_cache = m
+               end
+               return m
+       end
+
+       redef fun near=(value)
+       do
+               super
+               mvp_matrix_cache = null
+       end
+
+       redef fun far=(value)
+       do
+               super
+               mvp_matrix_cache = null
+       end
+
+       redef fun width=(value)
+       do
+               super
+               mvp_matrix_cache = null
+       end
+
+       redef fun height=(value)
+       do
+               super
+               mvp_matrix_cache = null
+       end
+end
index cfd8150..d5d2ef1 100644 (file)
@@ -40,7 +40,7 @@ import more_collections
 import performance_analysis
 
 import gamnit
-import gamnit::cameras
+import gamnit::cameras_cache
 import gamnit::dynamic_resolution
 import gamnit::limit_fps
 import gamnit::camera_control