1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Gamnit display implementation for GNU/Linux using `egl`, `sdl` and `x11`
21 import egl
# local to gamnit
22 intrude import display
23 intrude import textures
25 redef class GamnitDisplay
27 # Actual width or desired width of the window, can be set before calling `setup`
28 fun width
=(value
: Int) do requested_width
= value
29 private var requested_width
= 1920
31 # Actual height or desired height of the window, can be set before calling `setup`
32 fun height
=(value
: Int) do requested_height
= value
33 private var requested_height
= 1080
35 redef fun show_cursor
do return sdl
.show_cursor
37 redef fun show_cursor
=(val
) do sdl
.show_cursor
= val
39 redef fun lock_cursor
=(val
) do sdl
.relative_mouse_mode
= val
41 redef fun lock_cursor
do return sdl
.relative_mouse_mode
43 # Setup SDL, wm, EGL in order
46 if debug_gamnit
then print
"Setting up SDL"
47 self.sdl_window
= setup_sdl
(requested_width
, requested_height
)
49 if debug_gamnit
then print
"Setting up window manager"
50 setup_egl_display sdl_window
.wm_info
.display_handle
52 if debug_gamnit
then print
"Setting up EGL context"
53 select_egl_config
(red_bits
, green_bits
, blue_bits
, 8, 8, 0, 0)
54 setup_egl_context sdl_window
.wm_info
.window_handle
57 # Close EGL and SDL in reverse order of `setup` (nothing to do for X11)
67 # The SDL display managing the window and events
68 var sdl_window
: SDLWindow is noautoinit
70 # Title of the window, must be set before creating the window (or redef)
71 var window_title
= "gamnit game" is lazy
, writable
73 # Setup the SDL display and lib
74 fun setup_sdl
(window_width
, window_height
: Int): SDLWindow
76 assert sdl
.initialize
((new SDLInitFlags).video
.audio
) else
77 print_error
"Failed to initialize SDL2: {sdl.error}"
80 var img_flags
= (new SDLImgInitFlags).png
.jpg
81 assert sdl
.img
.initialize
(img_flags
) == img_flags
else
82 print_error
"Failed to initialize SDL2 IMG: {sdl.error}"
85 var sdl_window
= new SDLWindow(window_title
.to_cstring
, window_width
, window_height
, sdl_window_flags
)
86 assert not sdl_window
.address_is_null
else
87 print_error
"Failed to create SDL2 window: {sdl.error}"
91 var inited
= mix
.initialize
(mix_init_flags
)
92 if inited
!= mix_init_flags
then
93 print_error
"Failed to load SDL2 mixer format supports: {mix.error}"
96 var open
= mix
.open_audio
(44100, mix
.default_format
, 2, 1024)
98 print_error
"Failed to initialize SDL2 mixer: {mix.error}"
104 # SDL2 window initialization flags
106 # The default value supports OpenGL and window resizing.
107 var sdl_window_flags
: SDLWindowFlags = (new SDLWindowFlags).opengl
.resizable
is lazy
, writable
109 # SDL2 mixer initialization flags
111 # Defaults to FLAC, MP3 and OGG.
112 var mix_init_flags
: MixInitFlags = mix
.flac
| mix
.mp3
| mix
.ogg
is lazy
, writable
114 # Close the SDL display
124 redef class TextureAsset
126 redef fun load_from_platform
128 var path
= "assets" / path
130 var surface
= new SDLSurface.load
(path
.to_cstring
)
131 if surface
.address_is_null
then
132 error
= new Error("Failed to load texture at '{path}'")
136 self.width
= surface
.w
.to_f
137 self.height
= surface
.h
.to_f
138 var format
= if surface
.format
.amask
> 0u32
then gl_RGBA
else gl_RGB
139 var pixels
= surface
.pixels
141 load_from_pixels
(pixels
, surface
.w
, surface
.h
, format
)