1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
* guile-2d
Guile-2d is a free software 2D game engine written in GNU Guile
Scheme. It provides an abstraction layer above SDL and OpenGL 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/engines 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
*** 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 an 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)
(2d game))
(coroutine
(while #t
(walk 'up)
(wait game-agenda 60)
(walk 'down)
(wait game-agenda 60)))
#+END_SRC
=coroutine= is a useful macro that evaluates a block of code as a
coroutine. =wait= aborts the procedure and schedules the
continuation inside of an agenda. =game-agenda= is the main
agenda that is ticked at each iteration of the game update loop.
In this example, the script is paused for 1 second after each
step. Since guile-2d enforces a fixed timestep and updates 60
times per second by default, 60 ticks is equivalent to 1 second.
You can also use the agenda to schedule the evaluation of any
thunk even if it isn't a coroutine.
#+BEGIN_SRC scheme
(define (hello)
(display "Hello, world! Sorry I'm late!\n"))
(schedule game-agenda hello 600)
#+END_SRC
=schedule= accepts a thunk (a procedure that takes no arguments)
and schedules it to be applied after a certain number of ticks, or
after 1 tick by default. In this example, the text "Hello, world!
Sorry I'm late!" is displayed after 10 seconds. There are other
ways to schedule procedures, too. =schedule-interval= applies a
thunk periodically, and =schedule-each= applies a thunk upon every
tick.
*** Functional Reactive Programming
Games are composed of values that evolve as time passes. The
player's score, the current stage, an enemy's hit points, etc. all
change in response to events that happen at discrete points in
time. Typically, this means that a number of callback procedures
are registered to react to events which mutate data structures
and/or assign to variables. However, this approach, while simple
and effective, comes at the price of readability and
comprehension. Instead of explicitly mutating data and entering
"callback hell", guile-2d abstracts and formalizes the process
using a functional reactive programming style.
Time-varying values are called "signals", and they are created in
a declarative and functional manner. Rather than describing the
process of mutation procedurally, one describes the relationship
between signals instead. Signal relationships are described in a
functional style using =signal-map=, =signal-fold=,
=signal-filter=, and others.
Example:
#+BEGIN_SRC scheme
(define-signal position
(signal-fold v+ (vector2 320 240)
(signal-map (lambda (v)
(vscale v 4))
(signal-sample game-agenda 1 key-arrows))))
#+END_SRC
This signal describes a relationship between the arrow keys on the
keyboard and the position of the player. =signal-sample= is used
to trigger a signal update upon every game tick that provides the
current state of the arrow keys. =key-arrows= is a vector2 that
maps to the current state of the arrow keys, allowing for 8
direction movement. This vector2 is then scaled 4x to make the
player move faster. Finally, the scaled vector is added to the
previous player position via =signal-fold=. The player's position
is at (320, 240) initially. As you can see, there are no
callbacks and explicit mutation needed. Those details have been
abstracted away, freeing the programmer to focus on more important
things.
As an added bonus, signals adapt to changes in their environment
when defined using the =define-signal= form. This means that a
signal can be re-defined at the REPL and other dependent signals
will take notice and re-evaluate themselves automagically.
*** 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 integrates Guile's cooperative REPL server with the game
loop. To activate this feature, import the =(2d repl)= module. To
connect to the REPL server, 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
** Using the Sandbox
If you want to quickly create a guile-2d environment and start
experimenting, run the =sandbox= script. It will import many
useful modules, start a REPL server, open a window, and start the
game loop. Simply connect to the REPL server and start hacking!
** 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
** License
GNU GPL v3+
See =COPYING= for the full license text.
|