SDL

Simple DirectMedia Layer (SDL) is an API for cross-platform low-level access to graphics, audio, and input hardware. It is written in C and bindings for various programming languages exist. Using the ISO C binding interface f03sdl2 for SDL 2.0, computer games and multimedia applications can be written in Fortran easily.

SDL 2 must be installed with development headers. On FreeBSD, simply run:

# pkg install audio/sdl2_mixer devel/sdl20 graphics/sdl2_image graphics/sdl2_ttf

Example

Fortran SDL
Fig. 1: Filling a yellow rectangle with SDL

The example shows how to draw a coloured rectangle to the screen (fig. 1):

! demo.f90
program main
    use, intrinsic :: iso_fortran_env, only: stdout => output_unit, stderr => error_unit
    use, intrinsic :: iso_c_binding, only: C_NULL_CHAR, c_ptr
    use :: sdl2
    implicit none

    integer, parameter :: WIDTH  = 640
    integer, parameter :: HEIGHT = 480

    type(c_ptr)     :: window
    type(c_ptr)     :: renderer
    type(sdl_event) :: event
    type(sdl_rect)  :: r
    integer         :: rc

    ! Initialise SDL.
    rc = sdl_init(SDL_INIT_VIDEO)

    if (rc < 0) then
        write (stderr, *) 'SDL Error: ', sdl_get_error()
        stop
    end if

    ! Create the SDL window.
    window = sdl_create_window('SDL2 Fortran' // C_NULL_CHAR, &
                               SDL_WINDOWPOS_UNDEFINED, &
                               SDL_WINDOWPOS_UNDEFINED, &
                               WIDTH, &
                               HEIGHT, &
                               SDL_WINDOW_SHOWN)

    if (.not. c_associated(window)) then
        write (stderr, *) 'SDL Error: ', sdl_get_error()
        stop
    end if

    ! The rectangle.
    r%x = 50;  r%y = 50
    r%w = 150; r%h = 150

    ! Create the renderer.
    renderer = sdl_create_renderer(window, -1, 0)

    do while (.true.)
        rc = sdl_poll_event(event)

        if (rc > 0) then
            select case (event%type)
                case (SDL_QUITEVENT)
                    exit
            end select
        end if

        ! Clear screen.
        rc = sdl_set_render_draw_color(renderer, &
                                       int(0, 2), &
                                       int(0, 2), &
                                       int(0, 2), &
                                       int(255, 2))
        rc = sdl_render_clear(renderer)

        ! Fill a rectangle.
        rc = sdl_set_render_draw_color(renderer, &
                                       int(255, 2), &
                                       int(255, 2), &
                                       int(0, 2), &
                                       int(255, 2))
        rc = sdl_render_fill_rect(renderer, r)

        ! Render to screen.
        call sdl_render_present(renderer)
        call sdl_delay(20)
    end do

    ! Quit gracefully.
    call sdl_destroy_renderer(renderer)
    call sdl_destroy_window(window)
    call sdl_quit()
end program main

First, compile the SDL 2 interface. Then, compile and link the example program:

$ gfortran8 -c sdl2.f90
$ gfortran8 -Wl,-rpath=/usr/local/lib/gcc8/ `sdl2-config --cflags` \
  -o demo demo.f90 sdl2.o `sdl2-config --libs`
$ ./demo

References