Gamnit display implementation for GNU/Linux using egl, sdl and x11

Redefined classes

redef class GamnitDisplay

gamnit :: display_linux $ GamnitDisplay

General display class, is sized and drawable
redef class TextureAsset

gamnit :: display_linux $ TextureAsset

Texture loaded from the assets folder

All class definitions

redef class GamnitDisplay

gamnit :: display_linux $ GamnitDisplay

General display class, is sized and drawable
redef class TextureAsset

gamnit :: display_linux $ TextureAsset

Texture loaded from the assets folder
package_diagram gamnit::display_linux display_linux sdl2::image image gamnit::display_linux->sdl2::image sdl2::mixer mixer gamnit::display_linux->sdl2::mixer gamnit::egl egl gamnit::display_linux->gamnit::egl gamnit::textures textures gamnit::display_linux->gamnit::textures sdl2 sdl2 sdl2::image->sdl2 sdl2::sdl2_base sdl2_base sdl2::mixer->sdl2::sdl2_base egl egl gamnit::egl->egl gamnit::display display gamnit::egl->gamnit::display gamnit::textures->gamnit::display ...sdl2 ... ...sdl2->sdl2 ...sdl2::sdl2_base ... ...sdl2::sdl2_base->sdl2::sdl2_base ...egl ... ...egl->egl ...gamnit::display ... ...gamnit::display->gamnit::display gamnit::gamnit_linux gamnit_linux gamnit::gamnit_linux->gamnit::display_linux gamnit::camera_control_linux camera_control_linux gamnit::camera_control_linux->gamnit::gamnit_linux gamnit::camera_control_linux... ... gamnit::camera_control_linux...->gamnit::camera_control_linux

Ancestors

module abstract_collection

core :: abstract_collection

Abstract collection classes and services.
module abstract_text

core :: abstract_text

Abstract class for manipulation of sequences of characters
module array

core :: array

This module introduces the standard array structure.
module aware

android :: aware

Android compatibility module
module bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
module c

c :: c

Structures and services for compatibility with the C language
module circular_array

core :: circular_array

Efficient data structure to access both end of the sequence.
module codec_base

core :: codec_base

Base for codecs to use with streams
module codecs

core :: codecs

Group module for all codec-related manipulations
module collection

core :: collection

This module define several collection classes.
module core

core :: core

Standard classes and methods used by default by Nit programs and libraries.
module display

gamnit :: display

Abstract display services
module egl

egl :: egl

Interface between rendering APIs (OpenGL, OpenGL ES, etc.) and the native windowing system.
module environ

core :: environ

Access to the environment variables of the process
module error

core :: error

Standard error-management infrastructure.
module events

sdl2 :: events

SDL 2 events and related services
module exec

core :: exec

Invocation and management of operating system sub-processes.
module file

core :: file

File manipulations (create, read, write, etc.)
module fixed_ints

core :: fixed_ints

Basic integers of fixed-precision
module fixed_ints_text

core :: fixed_ints_text

Text services to complement fixed_ints
module flat

core :: flat

All the array-based text representations
module gc

core :: gc

Access to the Nit internal garbage collection mechanism
module glesv2

glesv2 :: glesv2

OpenGL graphics rendering library for embedded systems, version 2.0
module hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module input

mnit :: input

Defines abstract classes for user and general inputs to the application.
module iso8859_1

core :: iso8859_1

Codec for ISO8859-1 I/O
module kernel

core :: kernel

Most basic classes and methods.
module list

core :: list

This module handle double linked lists
module math

core :: math

Mathematical operations
module native

core :: native

Native structures for text and bytes
module numeric

core :: numeric

Advanced services for Numeric types
module protocol

core :: protocol

module queue

core :: queue

Queuing data structures and wrappers
module range

core :: range

Module for range of discrete objects.
module re

core :: re

Regular expression support for all services based on Pattern
module ropes

core :: ropes

Tree-based representation of a String.
module sdl2

sdl2 :: sdl2

Simple DirectMedia Layer (SDL) 2.0 services for easy window creation and 2D drawing
module sdl2_base

sdl2 :: sdl2_base

Basic SDL 2 features
module sorter

core :: sorter

This module contains classes used to compare things and sorts arrays.
module stream

core :: stream

Input and output streams of characters
module syswm

sdl2 :: syswm

Window manager related SDL 2 services
module text

core :: text

All the classes and methods related to the manipulation of text entities
module time

core :: time

Management of time and dates
module union_find

core :: union_find

union–find algorithm using an efficient disjoint-set data structure
module utf8

core :: utf8

Codec for UTF-8 I/O

Parents

module egl

gamnit :: egl

Use of EGL to implement Gamnit on GNU/Linux and Android
module image

sdl2 :: image

Services of the SDL_image 2.0 library
module mixer

sdl2 :: mixer

SDL2 mixer with sample/sounds and music
module textures

gamnit :: textures

Load textures, create subtextures and manage their life-cycle

Children

module gamnit_linux

gamnit :: gamnit_linux

Support services for Gamnit on GNU/Linux

Descendants

module a_star-m

a_star-m

module camera_control_linux

gamnit :: camera_control_linux

Mouse wheel and middle mouse button to control camera
# Gamnit display implementation for GNU/Linux using `egl`, `sdl` and `x11`
module display_linux

import sdl2::image
import sdl2::mixer

import egl # local to gamnit
intrude import display
intrude import textures

redef class GamnitDisplay

	# Actual width or desired width of the window, can be set before calling `setup`
	fun width=(value: Int) do requested_width = value
	private var requested_width = 1920

	# Actual height or desired height of the window, can be set before calling `setup`
	fun height=(value: Int) do requested_height = value
	private var requested_height = 1080

	redef fun show_cursor do return sdl.show_cursor

	redef fun show_cursor=(val) do sdl.show_cursor = val

	redef fun lock_cursor=(val) do sdl.relative_mouse_mode = val

	redef fun lock_cursor do return sdl.relative_mouse_mode

	# Setup SDL, wm, EGL in order
	redef fun setup
	do
		if debug_gamnit then print "Setting up SDL"
		self.sdl_window = setup_sdl(requested_width, requested_height)

		if debug_gamnit then print "Setting up window manager"
		setup_egl_display sdl_window.wm_info.display_handle

		if debug_gamnit then print "Setting up EGL context"
		select_egl_config(red_bits, green_bits, blue_bits, 8, 8, 0)
		setup_egl_context sdl_window.wm_info.window_handle
	end

	# Close EGL and SDL in reverse order of `setup` (nothing to do for X11)
	redef fun close
	do
		close_egl
		close_sdl
	end

	# ---
	# SDL

	# The SDL display managing the window and events
	var sdl_window: SDLWindow is noautoinit

	# Title of the window, must be set before creating the window (or redef)
	var window_title = "gamnit game" is lazy, writable

	# Setup the SDL display and lib
	fun setup_sdl(window_width, window_height: Int): SDLWindow
	do
		assert sdl.initialize((new SDLInitFlags).video.audio) else
			print_error "Failed to initialize SDL2: {sdl.error}"
		end

		var img_flags = (new SDLImgInitFlags).png.jpg
		assert sdl.img.initialize(img_flags) == img_flags else
			print_error "Failed to initialize SDL2 IMG: {sdl.error}"
		end

		var sdl_window = new SDLWindow(window_title.to_cstring, window_width, window_height, sdl_window_flags)
		assert not sdl_window.address_is_null else
			print_error "Failed to create SDL2 window: {sdl.error}"
		end

		# Audio support
		var inited = mix.initialize(mix_init_flags)
		if inited != mix_init_flags then
			print_error "Failed to load SDL2 mixer format supports: {mix.error}"
		end

		var open = mix.open_audio(44100, mix.default_format, 2, 1024)
		if not open then
			print_error "Failed to initialize SDL2 mixer: {mix.error}"
		end

		return sdl_window
	end

	# SDL2 window initialization flags
	#
	# The default value supports OpenGL and window resizing.
	var sdl_window_flags: SDLWindowFlags = (new SDLWindowFlags).opengl.resizable is lazy, writable

	# SDL2 mixer initialization flags
	#
	# Defaults to FLAC, MP3 and OGG.
	var mix_init_flags: MixInitFlags = mix.flac | mix.mp3 | mix.ogg is lazy, writable

	# Close the SDL display
	fun close_sdl
	do
		sdl_window.destroy
		mix.close_audio
		mix.quit
		sdl.finalize
	end
end

redef class TextureAsset

	redef fun load_from_platform
	do
		var path = "assets" / path

		var surface = new SDLSurface.load(path.to_cstring)
		if surface.address_is_null then
			error = new Error("Failed to load texture at '{path}'")
			return
		end

		self.width = surface.w.to_f
		self.height = surface.h.to_f
		var format = if surface.format.amask > 0u32 then gl_RGBA else gl_RGB
		var pixels = surface.pixels

		load_from_pixels(pixels, surface.w, surface.h, format)

		surface.free
	end
end
lib/gamnit/display_linux.nit:15,1--145,3