lib: Add a tiny GNU readline wrapper
[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 intrude import display
23 intrude import textures
24
25 redef class GamnitDisplay
26
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
30
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
34
35 redef fun show_cursor do return sdl_display.show_cursor
36
37 redef fun show_cursor=(val) do sdl_display.show_cursor = val
38
39 # Setup SDL, X11, EGL in order
40 redef fun setup
41 do
42 if debug_gamnit then print "Setting up SDL"
43 self.sdl_display = setup_sdl(requested_width, requested_height)
44
45 if debug_gamnit then print "Setting up X11"
46 var x11_display = setup_x11
47 var window_handle = window_handle
48 setup_egl_display x11_display
49
50 if debug_gamnit then print "Setting up EGL context"
51 select_egl_config(8, 8, 8, 8, 8, 0, 0)
52 setup_egl_context window_handle
53 end
54
55 # Close EGL and SDL in reverse order of `setup` (nothing to do for X11)
56 redef fun close
57 do
58 close_egl
59 close_sdl
60 end
61
62 # ---
63 # SDL
64
65 # The SDL display managing the window and events
66 var sdl_display: SDLDisplay is noautoinit
67
68 # Setup the SDL display and lib
69 fun setup_sdl(window_width, window_height: Int): SDLDisplay
70 do
71 var sdl_display = new SDLDisplay(window_width, window_height)
72 assert not sdl_display.address_is_null else print "Opening SDL display failed"
73 return sdl_display
74 end
75
76 # Close the SDL display
77 fun close_sdl do sdl_display.destroy
78
79 # Get a native handle to the current SDL window
80 fun window_handle: Pointer
81 do
82 var sdl_wm_info = new SDLSystemWindowManagerInfo
83 return sdl_wm_info.x11_window_handle
84 end
85
86 # ---
87 # X11
88
89 # Get a native handle to the current X11 display
90 fun setup_x11: Pointer
91 do
92 var x11_display = x_open_default_display
93 assert not x11_display.address_is_null else print "Opening X11 display failed"
94 return x11_display
95 end
96 end
97
98 redef class GamnitAssetTexture
99
100 redef fun load_from_platform
101 do
102 var path = "assets" / path # TODO use app.assets_dir
103 var sdl_tex = new SDLImage.from_file(path)
104
105 if sdl_tex.address_is_null then
106 error = new Error("Failed to load texture at '{path}'")
107 return
108 end
109
110 self.width = sdl_tex.width.to_f
111 self.height = sdl_tex.height.to_f
112 var format = if sdl_tex.amask > 0 then gl_RGBA else gl_RGB
113 var pixels = sdl_tex.pixels
114
115 load_from_pixels(pixels, sdl_tex.width, sdl_tex.height, format)
116 end
117 end