231ee17cf2129693e7413fb4e238c068bf962642
[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 sdl2::image
19
20 import egl # local to gamnit
21 intrude import display
22 intrude import textures
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 redef fun show_cursor do return sdl.show_cursor
35
36 redef fun show_cursor=(val) do sdl.show_cursor = val
37
38 # Setup SDL, wm, EGL in order
39 redef fun setup
40 do
41 if debug_gamnit then print "Setting up SDL"
42 self.sdl_window = setup_sdl(requested_width, requested_height)
43
44 if debug_gamnit then print "Setting up window manager"
45 setup_egl_display sdl_window.wm_info.display_handle
46
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
50 end
51
52 # Close EGL and SDL in reverse order of `setup` (nothing to do for X11)
53 redef fun close
54 do
55 close_egl
56 close_sdl
57 end
58
59 # ---
60 # SDL
61
62 # The SDL display managing the window and events
63 var sdl_window: SDLWindow is noautoinit
64
65 # Title of the window, must be set before creating the window (or redef)
66 var window_title = "gamnit game" is lazy, writable
67
68 # Setup the SDL display and lib
69 fun setup_sdl(window_width, window_height: Int): SDLWindow
70 do
71 assert sdl.initialize((new SDLInitFlags).video) else
72 print_error "Failed to initialize SDL2: {sdl.error}"
73 end
74
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}"
78 end
79
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}"
83 end
84
85 return sdl_window
86 end
87
88 # Close the SDL display
89 fun close_sdl do sdl_window.destroy
90 end
91
92 redef class TextureAsset
93
94 redef fun load_from_platform
95 do
96 var path = "assets" / path
97
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}'")
101 return
102 end
103
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
108
109 load_from_pixels(pixels, surface.w, surface.h, format)
110 end
111 end