gamnit: cache `GamnitDisplay::aspect_ratio`
[nit.git] / lib / gamnit / gamnit_linux.nit
index fe6cdac..b86f8e5 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,13 @@ 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
+                               display.aspect_ratio = sdl_event.data1.to_f / sdl_event.data2.to_f
+                               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
@@ -40,6 +48,10 @@ redef class App
        end
 end
 
+redef class GamnitDisplay
+       redef var aspect_ratio = super is lazy
+end
+
 # ---
 # Redef services from `sdl2::events`
 
@@ -56,6 +68,10 @@ redef class SDLMouseEvent
        redef fun to_gamnit_event(buffer) do return new GamnitPointerEvent(buffer, self)
 end
 
+redef class SDLMouseWheelEvent
+       redef fun to_gamnit_event(buffer) do return new GamnitMouseWheelEvent(buffer, self)
+end
+
 redef class SDLKeyboardEvent
        redef fun to_gamnit_event(buffer) do return new GamnitKeyEvent(buffer, self)
 end
@@ -126,6 +142,19 @@ class GamnitPointerEvent
        end
 end
 
+# Event on mouse wheel input
+class GamnitMouseWheelEvent
+       super GamnitInputEvent
+
+       redef type NATIVE: SDLMouseWheelEvent
+
+       # Horizontal scroll amount
+       fun x: Float do return native.x.to_f
+
+       # Vertical scroll amount
+       fun y: Float do return native.y.to_f
+end
+
 # SDL2 event not handled by gamnit
 class GamnitOtherEvent
        super GamnitInputEvent