ATTENTION: Guile-2d has a new home at my own web server under a new name: Sly
Guile-2d is a 2D game programming library for GNU Guile. It is a layer above SDL and OpenGL that provides abstractions for common 2D game programming requirements such as:
- Tile maps
- Scene graph
- Input handling
Here is the simplest Guile-2d application (so far).
(use-modules (2d game) (2d scene) (2d sprite) (2d vector2)) (define (make-demo-sprite) (load-sprite "images/ghost.png" #:position (vector2 320 240))) (define simple-scene (make-scene "Simple" #:init make-demo-sprite #:draw draw-sprite)) (define simple-demo (make-game #:title "Simple Demo" #:first-scene simple-scene)) (run-game simple-demo)
Game objects are used to define the basic aspects of a Guile-2D game such as the window title, resolution, whether or not it is fullscreen, and what the first scene is.
(define my-game (make-game #:title "Simple Demo" #:resolution (vector2 640 480) #:fullscreen? #f #:first-scene main-menu))
Games can be divided into several smaller pieces, called scenes. A scene describes how a particular part of a game is initialized, drawn, updated, etc.
(define main-menu (make-scene "Main Menu" #:init create-menu #:enter menu-enter #:exit menu-exit #:draw draw-menu #:update update-menu #:events `((key-down . ,menu-key-down))))
In addition to the essential callbacks (draw, update, enter,
exit), scenes can specify an alist of additional arbitrary event
handlers. Some events such as
key-down are emitted by the game
loop when input events are received.
Scenes live in a place called the stage. There can be many stages,
but only one is active at any given time. When a stage enters
focus, the scene’s enter procedure is applied. When a stage loses
focus, the exit procedure is applied. Stages are stored in a
stack, and they can pushed and popped as needed. To change the
current scene, and thus the current stage, use
(push-scene tetris-clone) (pop-scene) (replace-scene high-scores)
Sprites encapsulate the presentation of an image or a region of an image.
The simplest way to get started with sprites is to use the
load-sprite procedure. All arguments except the filename are
optional keyword arguments.
Guile-2d uses the FreeImage library and can load many different image formats. See the FreeImage features page for a full list of supported formats.
(define sprite (load-sprite "cirno.png" #:position #(0 0) #:scale (1 1) #:rotation (0) #:color white #:anchor 'center))
Alternatively, you can make a sprite from an existing texture. The
same keyword arguments in
load-sprite are also available here.
(define sprite (make-sprite (load-texture "cirno.png")))
Position, scale, rotation, color, and anchor are mutable.
(set-sprite-position! sprite #(100 100))
Drawing a sprite is simple.
When drawing many sprites, it is inefficient to send them to the GPU individually. Sprite batches resolve this issue by sending sprites to the GPU in large chunks.
To take advantage of this, create a sprite batch and use
with-sprite-batch. All calls to
draw-sprite will use the
sprite batch within this form.
(define sprites (make-a-ton-of-sprites)) (define batch (make-sprite-batch)) (with-sprite-batch batch (for-each draw-sprite sprites))
Coroutines and Agendas
The ability to write scripts is very important for most games. A script for an RPG NPC could look like this:
;; Walk up one tile and then down one tile, forever. (while #t (walk 'up) (walk 'down))
Unfortunately, running this script as it is means completely locking up the program in an unbounded loop. However, coroutines (and a scheduler known as the “agenda”) are here to save the day! Coroutines are procedures that can be exited at any point and resumed later.
It would be nice if after every call to
walk, the NPC would wait
for one second before taking its next step. This is where the
agenda comes in. The agenda is used to schedule procedures to be
run after an arbitrary number of game updates (1 by
default). Since coroutines and the agenda go hand in hand, there
wait procedure to pause a coroutine and schedule it to
be resumed later.
Using a coroutine and the agenda, the NPC script can be rewritten such that it does not halt further program execution.
(agenda-schedule (colambda () (while #t (walk 'up) (wait 60) (walk 'down) (wait 60))))
colambda is a useful macro that is syntactic sugar for a lambda
expression executed as a coroutine.
agenda-schedule accepts a
thunk (a procedure that takes 0 arguments) and schedules it to be
executed later. In this example we do not provide a second
agenda-schedule, which means that the thunk will be
executed upon the next game update.
Since guile-2d enforces a fixed timestep and updates 60 times per second, waiting for 60 updates means that the NPC will wait one second in between each step.
Actions encapsulate a procedure that operates over a certain period of time. Action objects have two properties: an arbitrary procedure and a duration in game ticks. Action procedures accept one argument: a time delta in the range [0, 1]. Use actions in combination with coroutines for things that are a function of time, such as moving a sprite across the screen.
(schedule-action ;; Move horizontally across the screen, starting at x=0 and moving to ;; x=800, in 60 ticks. (lerp (lambda (x) (set-sprite-position! sprite (vector2 x (/ window-height 2)))) 0 800 60))
schedule-action is used to schedule a coroutine that will
perform the given action in the current agenda.
lerp is a type
of action, short for linear interpolation.
lerp takes an
arbitrary procedure to apply at each tick, a start value, an end
value, and like all other actions, a duration. The code above
interpolates from 0 to 800 over 60 ticks. The result of this
action is a sprite moving across the screen from left to right.
Actions can be combined to run in a sequence or in parallel.
(schedule-action (action-parallel (lerp (lambda (x) (set-sprite-position! sprite (vector2 x (/ window-height 2)))) 0 800 60) ;; Rotate sprite 1080 degrees in 120 ticks. (lerp (lambda (angle) (set-sprite-rotation! sprite angle)) 0 1080 120)))
action-parallel will combine many actions into one action that
does everything at the same time. In the example above, the sprite
will still move across the screen from left to right, but while
it’s doing so (and for 60 ticks after), it will be rotating from 0
to 1080 degrees.
REPL Driven Development
The read-eval-print-loop present in Guile allows you to develop your game while it is running! This allows you to see in real time what your changes do to the game without having to restart the program every time.
Guile-2d uses a modified REPL server that is integrated with the game loop. A REPL server is started when the game loop starts. To connect to it, use the Geiser extension for GNU Emacs or telnet.
Use the default host and port settings.
telnet localhost 37146
guile-2d uses the typical GNU build system. First run `autogen.sh` and then do the usual incantations.
./autogen.sh ./configure make sudo make install
INSTALL.org for more detailed installation instructions.
To run an example when guile-2d has been installed:
cd examples guile simple.scm
To run an example using the not-yet-installed files (useful when developing):
cd examples guile -L .. simple.scm
To quit an example:
- Close the window
- Press the
Guile-2d supports GNU/Linux currently. OS X support is in the works, but there are problems with guile-sdl. See https://github.com/davexunit/guile-2d/issues/2 for more details.
- GNU Guile >= 2.0.9
- guile-figl (git master branch)
- guile-sdl >= 0.5.0
- SDL 1.2
- FreeImage >= 3.0
- FTGL >= 2.1
GNU LGPL v3+