gamnit: enable dynamic window resizing on desktop
authorAlexis Laferrière <alexis.laf@xymus.net>
Tue, 6 Jun 2017 23:33:08 +0000 (19:33 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Thu, 8 Jun 2017 15:57:24 +0000 (11:57 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/gamnit/cameras.nit
lib/gamnit/cameras_cache.nit
lib/gamnit/depth/depth.nit
lib/gamnit/display.nit
lib/gamnit/display_linux.nit
lib/gamnit/flat.nit
lib/gamnit/gamnit.nit
lib/gamnit/gamnit_linux.nit

index 7183e6b..688082b 100644 (file)
@@ -207,8 +207,8 @@ class UICamera
        # Clipping wall the farthest of the camera, defaults to -100.0
        var far: Float = -100.0 is writable
 
-       # Width in world units, defaults to the width in pixels of the screen
-       var width: Float = display.width.to_f is lazy
+       # Width in world units, calculated from `height` and the screen aspect ratio
+       fun width: Float do return height * display.aspect_ratio
 
        # Height in world units, defaults to 1080.0
        #
@@ -221,9 +221,7 @@ class UICamera
        fun reset_height(height: nullable Float)
        do
                if height == null then height = display.height.to_f
-
                self.height = height
-               self.width = height * display.aspect_ratio
        end
 
        # Convert the position `x, y` on screen, to UI coordinates
index 08adc8f..852ac9b 100644 (file)
@@ -110,12 +110,6 @@ redef class UICamera
                mvp_matrix_cache = null
        end
 
-       redef fun width=(value)
-       do
-               super
-               mvp_matrix_cache = null
-       end
-
        redef fun height=(value)
        do
                super
index f18f2eb..9b4be47 100644 (file)
@@ -45,6 +45,7 @@ redef class App
        # Draw all elements of `actors` and then call `frame_core_flat`
        protected fun frame_core_depth(display: GamnitDisplay)
        do
+               glViewport(0, 0, display.width, display.height)
                frame_core_dynamic_resolution_before display
 
                # Update cameras on both our programs
index 6e7c58c..2b694c6 100644 (file)
@@ -34,7 +34,7 @@ class GamnitDisplay
        fun height: Int is abstract
 
        # Aspect ratio of the screen, `width / height`
-       var aspect_ratio: Float is lazy do return width.to_f / height.to_f
+       fun aspect_ratio: Float do return width.to_f / height.to_f
 
        # Is the cursor locked et the center of the screen?
        var lock_cursor = false is writable
index 634656b..15f5281 100644 (file)
@@ -78,7 +78,7 @@ redef class GamnitDisplay
                        print_error "Failed to initialize SDL2 IMG: {sdl.error}"
                end
 
-               var sdl_window = new SDLWindow(window_title.to_cstring, window_width, window_height, (new SDLWindowFlags).opengl)
+               var sdl_window = new SDLWindow(window_title.to_cstring, window_width, window_height, sdl_window_flags)
                assert not sdl_window.address_is_null else
                        print_error "Failed to create SDL2 window: {sdl.error}"
                end
@@ -97,7 +97,12 @@ redef class GamnitDisplay
                return sdl_window
        end
 
-       # Initialization flags passed to SDL2 mixer
+       # SDL2 window initialization flags
+       #
+       # The default value supports OpenGL and window resizing.
+       var sdl_window_flags: SDLWindowFlags = (new SDLWindowFlags).opengl.resizable is lazy, writable
+
+       # SDL2 mixer initialization flags
        #
        # Defaults to all available formats.
        var mix_init_flags: MixInitFlags = mix.flac | mix.mod | mix.mp3 | mix.ogg is lazy, writable
index cafac8a..5375929 100644 (file)
@@ -41,8 +41,8 @@ import more_collections
 import performance_analysis
 
 import gamnit
-import gamnit::cameras_cache
 intrude import gamnit::cameras
+intrude import gamnit::cameras_cache
 import gamnit::dynamic_resolution
 import gamnit::limit_fps
 import gamnit::camera_control
@@ -294,7 +294,7 @@ redef class App
        do
                texture.load
 
-               var splash = new Sprite(texture, ui_camera.center)
+               var splash = new Sprite(texture, ui_camera.center.offset(0.0, 0.0, 0.0))
                ui_sprites.add splash
 
                var display = display
@@ -372,6 +372,17 @@ redef class App
                if display != null then display.close
        end
 
+       redef fun on_resize(display)
+       do
+               super
+
+               world_camera.mvp_matrix_cache = null
+               ui_camera.mvp_matrix_cache = null
+
+               # Update all sprites in the UI
+               for sprite in ui_sprites do sprite.needs_update
+       end
+
        redef fun frame_core(display)
        do
                # Prepare to draw, clear buffers
index 0d39f1e..fccf2ae 100644 (file)
@@ -77,4 +77,9 @@ redef class App
        # The instances passed as `event` may be freed (or overwritten),
        # right after this method returns. They should not be preserved.
        fun accept_event(event: InputEvent): Bool do return false
+
+       # The window has been resized by the user or system
+       #
+       # The framework handles resizing the viewport automatically.
+       fun on_resize(display: GamnitDisplay) do end
 end
index e68ee8e..b0c4241 100644 (file)
@@ -18,6 +18,7 @@ module gamnit_linux
 import sdl2::events
 
 intrude import gamnit
+intrude import display_linux
 
 redef class App
        private var sdl_event_buffer = new SDLEventBuffer.malloc
@@ -33,6 +34,12 @@ redef class App
 
                        # Convert to an SDL event with data
                        var sdl_event = sdl_event_buffer.to_event
+                       if sdl_event isa SDLWindowEvent and sdl_event.is_resized then
+                               display.width = sdl_event.data1
+                               display.height = sdl_event.data2
+                               on_resize display
+                       end
+
                        # Convert to `mnit::inputs` conforming objects
                        var gamnit_event = sdl_event.to_gamnit_event(sdl_event_buffer)
                        accept_event gamnit_event