An example to test and demonstrate the sdl2 lib with image and events

Redefined classes

redef class Sys

sdl2 :: minimal $ Sys

The main class of the program.

All class definitions

redef class Sys

sdl2 :: minimal $ Sys

The main class of the program.
package_diagram sdl2::minimal minimal sdl2::all all sdl2::minimal->sdl2::all sdl2::image image sdl2::all->sdl2::image ...sdl2::image ... ...sdl2::image->sdl2::image a_star-m a_star-m a_star-m->sdl2::minimal

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 bitset

core :: bitset

Services to handle BitSet
module bytes

core :: bytes

Services for byte streams and arrays
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 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 hash_collection

core :: hash_collection

Introduce HashMap and HashSet.
module image

sdl2 :: image

Services of the SDL_image 2.0 library
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 all

sdl2 :: all

Unites the main sdl2 module and its sister library sdl2::image

Children

module a_star-m

a_star-m

# An example to test and demonstrate the `sdl2` lib with `image` and `events`
module minimal is example

import sdl2::all

# Check for an error, then print and clear it
#
# Some errors are not fatal, so we ignore them at this level.
fun check_error(loc: String)
do
	var error = sys.sdl.error.to_s
	if not error.is_empty then
		print "at {loc}: {error}"
		sys.sdl.clear_error
	end
end

# Init `sdl2`, including video and events
sys.sdl.initialize((new SDLInitFlags).video)
check_error "init"

# Init `sdl2::image`
sdl.img.initialize((new SDLImgInitFlags).png)
check_error "img_init"

# Create a window
var window = new SDLWindow("Window title!".to_cstring, 800, 600, (new SDLWindowFlags).opengl)
check_error "window"

# Create a render, the suffested one
var renderer = new SDLRenderer(window, -1, (new SDLRendererFlags).accelerated)
check_error "renderer"

# Load an image
var surface = new SDLSurface.load("assets/fighter.png".to_cstring)
check_error "surface"
assert not surface.address_is_null

# Alternative code to load a BMP image without `sdl2::image`
#
# var surface = new SDLSurface.load_bmp("assets/fighter.bmp".to_cstring)

# Set the window icon
window.icon = surface
check_error "icon"

# Get a texture out of that surface
var texture = new SDLTexture.from_surface(renderer, surface)
check_error "texture"

# Allocate memory for reusable objects
var event = new SDLEventBuffer.malloc
var src = new SDLRect.nil
var dst = new SDLRect(0, 0, 128, 128)

# Set the colors we will be using
var white = new SDLColor(255, 255, 255, 255)
var green = new SDLColor(  0, 255,   0, 255)
var spacy = new SDLColor( 25,  25,  50, 255)

loop
	# Loop over events until we get a quit event
	while event.poll_event do
		var higher_event = event.to_event
		if higher_event isa SDLQuitEvent then
			break label out
		else if higher_event isa SDLMouseButtonDownEvent then
			# Update `dst` to be centered on the latest click
			dst.x = higher_event.x - dst.w/2
			dst.y = higher_event.y - dst.h/2
		end
	end

	# Clear the screen with a spacy color
	renderer.draw_color = spacy
	renderer.clear

	# Draw the target box for the following `copy`
	renderer.draw_color = green
	renderer.draw_rect dst

	# Copy a texture to the screen
	renderer.draw_color = white
	renderer.copy(texture, src, dst)

	# Copy the back buffer to the screen
	renderer.present

	check_error "present"

	33.delay
end label out

# Free all resources
event.free
src.free
dst.free

texture.free
surface.free

window.destroy
sdl.img.quit
sys.sdl.quit
lib/sdl2/examples/minimal/src/minimal.nit:17,1--120,12