Merge: gamnit: customize writing with BMFont
[nit.git] / lib / gamnit / display_linux.nit
index 231ee17..e3ab243 100644 (file)
@@ -16,6 +16,7 @@
 module display_linux
 
 import sdl2::image
+import sdl2::mixer
 
 import egl # local to gamnit
 intrude import display
@@ -35,6 +36,10 @@ redef class GamnitDisplay
 
        redef fun show_cursor=(val) do sdl.show_cursor = val
 
+       redef fun lock_cursor=(val) do sdl.relative_mouse_mode = val
+
+       redef fun lock_cursor do return sdl.relative_mouse_mode
+
        # Setup SDL, wm, EGL in order
        redef fun setup
        do
@@ -68,7 +73,7 @@ redef class GamnitDisplay
        # Setup the SDL display and lib
        fun setup_sdl(window_width, window_height: Int): SDLWindow
        do
-               assert sdl.initialize((new SDLInitFlags).video) else
+               assert sdl.initialize((new SDLInitFlags).video.audio) else
                        print_error "Failed to initialize SDL2: {sdl.error}"
                end
 
@@ -77,16 +82,43 @@ 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
 
+               # Audio support
+               var inited = mix.initialize(mix_init_flags)
+               assert inited != mix_init_flags else
+                       print_error "Failed to load SDL2 mixer format supports: {mix.error}"
+               end
+
+               var opened = mix.open_audio(44100, mix.default_format, 2, 1024)
+               assert opened else
+                       print_error "Failed to initialize SDL2 mixer: {mix.error}"
+               end
+
                return sdl_window
        end
 
+       # 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
+
        # Close the SDL display
-       fun close_sdl do sdl_window.destroy
+       fun close_sdl
+       do
+               sdl_window.destroy
+               mix.close_audio
+               mix.quit
+               sdl.finalize
+       end
 end
 
 redef class TextureAsset