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`
20 import egl
# local to gamnit
21 intrude import display
22 intrude import textures
24 redef class GamnitDisplay
26 # Actual width or desired width of the window, can be set before calling `setup`
27 fun width
=(value
: Int) do requested_width
= value
28 private var requested_width
= 1920
30 # Actual height or desired height of the window, can be set before calling `setup`
31 fun height
=(value
: Int) do requested_height
= value
32 private var requested_height
= 1080
34 redef fun show_cursor
do return sdl
.show_cursor
36 redef fun show_cursor
=(val
) do sdl
.show_cursor
= val
38 # Setup SDL, wm, EGL in order
41 if debug_gamnit
then print
"Setting up SDL"
42 self.sdl_window
= setup_sdl
(requested_width
, requested_height
)
44 if debug_gamnit
then print
"Setting up window manager"
45 setup_egl_display sdl_window
.wm_info
.display_handle
47 if debug_gamnit
then print
"Setting up EGL context"
48 select_egl_config
(red_bits
, green_bits
, blue_bits
, 8, 8, 0, 0)
49 setup_egl_context sdl_window
.wm_info
.window_handle
52 # Close EGL and SDL in reverse order of `setup` (nothing to do for X11)
62 # The SDL display managing the window and events
63 var sdl_window
: SDLWindow is noautoinit
65 # Title of the window, must be set before creating the window (or redef)
66 var window_title
= "gamnit game" is lazy
, writable
68 # Setup the SDL display and lib
69 fun setup_sdl
(window_width
, window_height
: Int): SDLWindow
71 assert sdl
.initialize
((new SDLInitFlags).video
) else
72 print_error
"Failed to initialize SDL2: {sdl.error}"
75 var img_flags
= (new SDLImgInitFlags).png
.jpg
76 assert sdl
.img
.initialize
(img_flags
) == img_flags
else
77 print_error
"Failed to initialize SDL2 IMG: {sdl.error}"
80 var sdl_window
= new SDLWindow(window_title
.to_cstring
, window_width
, window_height
, (new SDLWindowFlags).opengl
)
81 assert not sdl_window
.address_is_null
else
82 print_error
"Failed to create SDL2 window: {sdl.error}"
88 # Close the SDL display
89 fun close_sdl
do sdl_window
.destroy
92 redef class TextureAsset
94 redef fun load_from_platform
96 var path
= "assets" / path
98 var surface
= new SDLSurface.load
(path
.to_cstring
)
99 if surface
.address_is_null
then
100 error
= new Error("Failed to load texture at '{path}'")
104 self.width
= surface
.w
.to_f
105 self.height
= surface
.h
.to_f
106 var format
= if surface
.format
.amask
> 0u32
then gl_RGBA
else gl_RGB
107 var pixels
= surface
.pixels
109 load_from_pixels
(pixels
, surface
.w
, surface
.h
, format
)