* guile-2d 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: - Sprites - Animation - Tilesets - Tile maps - Scene graph - Input handling - Scripting ** Inspiration Every programming language should have a fun, easy to use 2D game library. Guile-2d draws its inspiration from great libraries/frameworks such as [[http://love2d.org/][LÖVE]], [[http://pygame.org/][Pygame]], and [[http://pyglet.org/][Pyglet]]. ** Example Here is the simplest guile-2d application (so far). #+BEGIN_SRC scheme (use-modules (2d game) (2d sprite) (2d vector2) (2d window)) (define sprite (load-sprite "images/p1_front.png" #:position (vector2 320 240))) (add-hook! draw-hook (lambda (dt alpha) (draw-sprite sprite))) (with-window (make-window #:title "Simple Sprite Demo") (run-game-loop)) #+END_SRC ** Features *** The Game Loop Guile-2d's game loop doesn't tie drawing and updating together. Instead, updates happen on a fixed timestep (60 ticks per second by default) while drawing happens as many times as possible. A framerate indepedent loop mitigates slow down that the user might experience when updating the game takes longer than drawing a frame at the desired rate. Instead of slowing to a crawl, some frames are dropped and the loop tries to catch up on updates. Additionally, a fixed timestep allows for a more deterministic simulation than a variable timestep. To start up the game loop, simply call =(run-game-loop)=. It's a good idea to set up the game window prior to starting the loop via the =with-window= form. #+BEGIN_SRC scheme (with-window (make-window #:title "Best Game Ever" #:resolution (vector2 640 480)) (run-game-loop)) #+END_SRC *** Sprites 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 [[http://freeimage.sourceforge.net/features.html][features page]] for a full list of supported formats. #+BEGIN_SRC scheme (use-modules (2d sprite)) (define sprite (load-sprite "cirno.png" #:position (vector2 320 240) #:scale (1 1) #:rotation 45 #:color white #:anchor 'center)) #+END_SRC Alternatively, you can make a sprite from an existing texture. The same keyword arguments in =load-sprite= are also available here. #+BEGIN_SRC scheme (define sprite (make-sprite (load-texture "cirno.png"))) #+END_SRC Position, scale, rotation, color, and anchor are mutable. #+BEGIN_SRC scheme (set-sprite-position! sprite (vector2 100 100)) #+END_SRC Drawing a sprite is simple. #+BEGIN_SRC scheme (draw-sprite sprite) #+END_SRC *** Sprite Batches 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. #+BEGIN_SRC scheme (define sprites (make-a-ton-of-sprites)) (define batch (make-sprite-batch)) (with-sprite-batch batch (for-each draw-sprite sprites)) #+END_SRC *** Keyboard and Mouse Input There are hooks within the =(2d keyboard)= and =(2d mouse)= modules that can be used to respond to user input. #+BEGIN_SRC scheme (use-modules (2d keyboard) (2d mouse)) ;; Quit when ESC is pressed. (add-hook! key-press-hook (lambda (key unicode) (when (eq? key 'escape) (quit-game)))) ;; Print coordinates when the mouse is moved. (add-hook! mouse-move-hook (lambda (x y) (format #t "pos: (~d, ~d)\n" x y))) #+END_SRC In the future, there will be more convenient ways to respond to user input similar to how keymaps work in Emacs. *** Coroutines and Agendas The ability to write scripts is very important for most games. A script for an RPG NPC could look like this: #+BEGIN_SRC scheme ;; Walk up one tile and then down one tile, forever. (while #t (walk 'up) (walk 'down)) #+END_SRC 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 exists a =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. #+BEGIN_SRC scheme (use-modules (2d agenda) (2d coroutine)) (schedule-next (colambda () (while #t (walk 'up) (wait 60) (walk 'down) (wait 60)))) #+END_SRC =colambda= is a useful macro that is syntactic sugar for a lambda expression executed as a coroutine. =schedule-next= accepts a thunk (a procedure that takes 0 arguments) and schedules it to be executed the next time the agenda is ticked. There are other ways to schedule procedures such as =schedule=, =schedule-interval=, and =schedule-every=. Since guile-2d enforces a fixed timestep and updates 60 times per second by default, waiting for 60 updates means that the NPC will wait one second in between each step. ** 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 [[http://www.nongnu.org/geiser/][Geiser]] extension for GNU Emacs or telnet. *Geiser* #+BEGIN_SRC fundamental M-x connect-to-guile #+END_SRC Use the default host and port settings. *Telnet* #+BEGIN_SRC sh telnet localhost 37146 #+END_SRC ** Building Guile-2d uses the typical GNU build system. First run =autogen.sh= and then do the usual incantations. #+BEGIN_SRC sh ./autogen.sh ./configure make sudo make install #+END_SRC See =INSTALL.org= for more detailed installation instructions. ** Running Examples To run an example when guile-2d has been installed: #+BEGIN_SRC sh cd examples guile simple.scm #+END_SRC To run an example using the modules in the source directory (useful when developing): #+BEGIN_SRC sh cd examples guile -L .. simple.scm #+END_SRC To quit an example: - Close the window - Press the =ESCAPE= key ** Platforms 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. ** Dependencies - GNU Guile >= 2.0.9 - [[https://gitorious.org/guile-figl/guile-figl][guile-figl]] (git master branch) - [[https://www.gnu.org/software/guile-sdl/index.html][guile-sdl]] >= 0.5.0 - SDL 1.2 - FreeImage >= 3.0 - FTGL >= 2.1 ** License GNU LGPL v3+