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 # Setup SDL, X11, EGL in order
38 if debug_gamnit
then print
"Setting up SDL"
39 self.sdl_display
= setup_sdl
(requested_width
, requested_height
)
41 if debug_gamnit
then print
"Setting up X11"
42 var x11_display
= setup_x11
43 var window_handle
= window_handle
44 setup_egl_display x11_display
46 if debug_gamnit
then print
"Setting up EGL context"
47 select_egl_config
(8, 8, 8, 8, 8, 0, 0)
48 setup_egl_context window_handle
51 # Close EGL and SDL in reverse order of `setup` (nothing to do for X11)
61 # The SDL display managing the window and events
62 var sdl_display
: SDLDisplay is noautoinit
64 # Setup the SDL display and lib
65 fun setup_sdl
(window_width
, window_height
: Int): SDLDisplay
67 var sdl_display
= new SDLDisplay(window_width
, window_height
)
68 assert not sdl_display
.address_is_null
else print
"Opening SDL display failed"
72 # Close the SDL display
73 fun close_sdl
do sdl_display
.destroy
75 # Get a native handle to the current SDL window
76 fun window_handle
: Pointer
78 var sdl_wm_info
= new SDLSystemWindowManagerInfo
79 return sdl_wm_info
.x11_window_handle
85 # Get a native handle to the current X11 display
86 fun setup_x11
: Pointer
88 var x11_display
= x_open_default_display
89 assert not x11_display
.address_is_null
else print
"Opening X11 display failed"
94 redef class GamnitAssetTexture
96 redef fun load_from_platform
98 var path
= "assets" / path
# TODO use app.assets_dir
99 var sdl_tex
= new SDLImage.from_file
(path
)
101 if sdl_tex
.address_is_null
then
102 error
= new Error("Failed to load texture at '{path}'")
106 self.width
= sdl_tex
.width
.to_f
107 self.height
= sdl_tex
.height
.to_f
108 var format
= if sdl_tex
.amask
> 0 then gl_RGBA
else gl_RGB
109 var pixels
= sdl_tex
.pixels
111 load_from_pixels
(pixels
, sdl_tex
.width
, sdl_tex
.height
, format
)