lib/gamnit: intro GNU/Linux support
[nit.git] / lib / gamnit / display_linux.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Gamnit display implementation for GNU/Linux using `egl`, `sdl` and `x11`
16 module display_linux
17
18 import sdl
19 import x11
20
21 import egl # local to gamnit
22 import display
23
24 redef class GamnitDisplay
25
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
29
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
33
34 # Setup SDL, X11, EGL in order
35 redef fun setup
36 do
37 if debug_gamnit then print "Setting up SDL"
38 self.sdl_display = setup_sdl(requested_width, requested_height)
39
40 if debug_gamnit then print "Setting up X11"
41 var x11_display = setup_x11
42 var window_handle = window_handle
43 setup_egl_display x11_display
44
45 if debug_gamnit then print "Setting up EGL context"
46 select_egl_config(8, 8, 8, 8, 8, 0, 0)
47 setup_egl_context window_handle
48 end
49
50 # Close EGL and SDL in reverse order of `setup` (nothing to do for X11)
51 redef fun close
52 do
53 close_egl
54 close_sdl
55 end
56
57 # ---
58 # SDL
59
60 # The SDL display managing the window and events
61 var sdl_display: SDLDisplay is noautoinit
62
63 # Setup the SDL display and lib
64 fun setup_sdl(window_width, window_height: Int): SDLDisplay
65 do
66 var sdl_display = new SDLDisplay(window_width, window_height)
67 assert not sdl_display.address_is_null else print "Opening SDL display failed"
68 return sdl_display
69 end
70
71 # Close the SDL display
72 fun close_sdl do sdl_display.destroy
73
74 # Get a native handle to the current SDL window
75 fun window_handle: Pointer
76 do
77 var sdl_wm_info = new SDLSystemWindowManagerInfo
78 return sdl_wm_info.x11_window_handle
79 end
80
81 # ---
82 # X11
83
84 # Get a native handle to the current X11 display
85 fun setup_x11: Pointer
86 do
87 var x11_display = x_open_default_display
88 assert not x11_display.address_is_null else print "Opening X11 display failed"
89 return x11_display
90 end
91 end