Merge branch 'explain-assert' into master
[nit.git] / lib / gamnit / dynamic_resolution.nit
index 6e9ed85..2c6a4c8 100644 (file)
@@ -42,7 +42,7 @@ redef class App
        #
        # This value is applied to both X and Y, so it has an exponential effect on
        # the number of pixels.
-       var dynamic_resolution_ratio = 1.0
+       var dynamic_resolution_ratio = 1.0 is writable
 
        # Minimum dynamic screen resolution
        var min_dynamic_resolution_ratio = 0.0125 is writable
@@ -54,7 +54,21 @@ redef class App
 
        private var perf_clock_dynamic_resolution = new Clock is lazy
 
-       redef fun on_create
+       # Real screen framebuffer
+       private var screen_framebuffer_cache: Int = -1
+
+       # Real screen framebuffer name
+       fun screen_framebuffer: Int
+       do
+               var cache = screen_framebuffer_cache
+               if cache != -1 then return cache
+
+               cache = glGetIntegerv(gl_FRAMEBUFFER_BINDING, 0)
+               self.screen_framebuffer_cache = cache
+               return cache
+       end
+
+       redef fun create_gamnit
        do
                super
 
@@ -62,11 +76,13 @@ redef class App
                program.compile_and_link
                var error = program.error
                assert error == null else print_error error
+
+               dynamic_context_cache = null
        end
 
        redef fun on_resize(display)
        do
-               dynamic_context.resize(display, max_dynamic_resolution_ratio)
+               if dynamic_context_cache != null then dynamic_context.resize(display, max_dynamic_resolution_ratio)
                super
        end
 
@@ -77,7 +93,7 @@ redef class App
 
                if dynamic_resolution_ratio == 1.0 then
                        # Draw directly to the screen framebuffer
-                       glBindFramebuffer(gl_FRAMEBUFFER, dynamic_context.screen_framebuffer)
+                       bind_screen_framebuffer screen_framebuffer
                        glViewport(0, 0, display.width, display.height)
                        glClear gl_COLOR_BUFFER_BIT | gl_DEPTH_BUFFER_BIT
 
@@ -107,7 +123,7 @@ redef class App
                var ratio = dynamic_resolution_ratio
                ratio = ratio.clamp(min_dynamic_resolution_ratio, max_dynamic_resolution_ratio)
 
-               glBindFramebuffer(gl_FRAMEBUFFER, dynamic_context.screen_framebuffer)
+               bind_screen_framebuffer screen_framebuffer
                glBindBuffer(gl_ARRAY_BUFFER, dynamic_context.buffer_array)
                glViewport(0, 0, display.width, display.height)
                glClear gl_COLOR_BUFFER_BIT | gl_DEPTH_BUFFER_BIT
@@ -143,7 +159,17 @@ redef class App
        end
 
        # Framebuffer and texture for dynamic resolution intermediate drawing
-       private var dynamic_context: DynamicContext = create_dynamic_context is lazy
+       private fun dynamic_context: DynamicContext
+       do
+               var cache = dynamic_context_cache
+               if cache != null then return cache
+
+               cache = create_dynamic_context
+               dynamic_context_cache = cache
+               return cache
+       end
+
+       private var dynamic_context_cache: nullable DynamicContext = null
 
        private fun create_dynamic_context: DynamicContext
        do
@@ -161,9 +187,6 @@ end
 # Handles to reused GL buffers and texture
 private class DynamicContext
 
-       # Real screen framebuffer
-       var screen_framebuffer: Int = -1
-
        # Dynamic screen framebuffer
        var dynamic_framebuffer: Int = -1
 
@@ -181,10 +204,6 @@ private class DynamicContext
        do
                # TODO enable antialiasing.
 
-               # Set aside the real screen framebuffer name
-               var screen_framebuffer = glGetIntegerv(gl_FRAMEBUFFER_BINDING, 0)
-               self.screen_framebuffer = screen_framebuffer
-
                # Framebuffer
                var framebuffer = glGenFramebuffers(1).first
                glBindFramebuffer(gl_FRAMEBUFFER, framebuffer)