summaryrefslogtreecommitdiff
path: root/doc/chickadee.texi
blob: 0585688f2f1c488170cdb2f645aacc5458eea0fe (plain)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
\input texinfo   @c -*-texinfo-*-
@c %**start of header
@setfilename chickadee.info
@settitle The Chickadee Game Toolkit
@c %**end of header
@copying
Copyright @copyright{} 2017-2021  David Thompson @email{davet@@gnu.org}

@quotation
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
A copy of the license is included in the section entitled ``GNU
Free Documentation License''.

A copy of the license is also available from the Free Software
Foundation Web site at @url{http://www.gnu.org/licenses/fdl.html}.

@end quotation

@dircategory The Algorithmic Language Scheme
@direntry
* Chickadee: (chickadee).     Game programming toolkit for Guile.
@end direntry

The document was typeset with
@uref{http://www.texinfo.org/, GNU Texinfo}.

@end copying

@titlepage
@title Chickadee 0.1
@subtitle Using the Chickadee game toolkit
@author David Thompson
@page
@vskip 0pt plus 1filll
@insertcopying
@end titlepage

@c Output the table of the contents at the beginning.
@contents

@ifnottex
@node Top
@top Chickadee

@insertcopying
@end ifnottex

@c Generate the nodes for this menu with `C-c C-u C-m'.
@menu
* Installation::                Installing Chickadee.
* Getting Started::             Writing your first Chickadee program.
* Command Line Interface::      Run Chickadee programs from the terminal.
* Live Coding::                 Tips for building games from the REPL.
* API Reference::               Chickadee API reference.
* Copying This Manual::         The GNU Free Documentation License and you!
* Index::
@end menu

@c Update all node entries with `C-c C-u C-n'.
@c Insert new nodes with `C-c C-c n'.

@node Installation
@chapter Installation

Chickadee is available for download from its website at
@url{dthompson.us/projects/chickadee.html}.  This section describes
the software requirements of Chickadee, as well as how to install it.

The build procedure for Chickadee is the same as for GNU software
packages, and is not covered here.  Please see the files @file{README}
and @file{INSTALL} for additional details.

@menu
* Requirements::                Software needed to build and run Chickadee.
@end menu

@node Requirements
@section Requirements

Chickadee depends on the following packages:

@itemize
@item @url{https://gnu.org/software/guile, GNU Guile}, version 3.0.0 or later;
@item @url{https://gnu.org/software/guile-opengl, GNU guile-opengl}, version 0.1 or later.
@item @url{https://dthompson.us/pages/software/guile-sdl2.html, guile-sdl2}, version 0.7.0 or later;
@item libfreetype
@item libmpg123
@item libopenal
@item libreadline
@item libvorbisfile
@end itemize

@node Getting Started
@chapter Getting Started

One of the simplest programs we can make with Chickadee is rendering
the text ``Hello, world'' on screen.  Here's what that looks like:

@example
(define (draw alpha)
  (draw-text "Hello, world!" (vec2 64.0 240.0)))
@end example

The @code{draw} procedure is called frequently to draw the game scene.
For the sake of simplicity, we will ignore the @code{alpha} variable
in this tutorial.

To run this program, we'll use the @command{chickadee play} command:

@example
chickadee play hello.scm
@end example

This is a good start, but it's boring.  Let's make the text move!

@example
(define position (vec2 0.0 240.0))

(define (draw alpha)
  (draw-text "Hello, world!" position))

(define (update dt)
  (set-vec2-x! position (+ (vec2-x position) (* 100.0 dt))))
@end example

The @code{vec2} type is used to store 2D coordinates
(@pxref{Vectors}.)  A variable named @code{position} contains the
position where the text should be rendered.  A new hook called
@code{update} has been added to handle the animation.  This hook is
called frequently to update the state of the game.  The variable
@code{dt} (short for ``delta-time'') contains the amount of time that
has passed since the last update, in seconds.  Putting it all
together, this update procedure is incrementing the x coordinate of
the position by 100 pixels per second.

This is neat, but after a few seconds the text moves off the screen
completely, never to be seen again.  It would be better if the text
bounced back and forth against the sides of the window.

@example
(define position (vec2 0.0 240.0))

(define (draw alpha)
  (draw-text "Hello, world!" position))

(define (update dt)
  (update-agenda dt))

(define (update-x x)
  (set-vec2-x! position x))

(let ((start 0.0)
      (end 536.0)
      (duration 4.0))
  (script
   (while #t
    (tween duration start end update-x)
    (tween duration end start update-x))))
@end example

This final example uses Chickadee's scripting features
(@pxref{Scripting}) to bounce the text between the edges of the window
indefinitely using the handy @code{tween} procedure.  The only thing
the @code{update} procedure needs to do now is advance the clock of
the ``agenda'' (the thing that runs scripts.)  The script takes care
of the rest.

This quick tutorial has hopefully given you a taste of what you can do
with Chickadee.  The rest of this manual gets into all of the details
that were glossed over, and much more.  Try rendering a sprite,
playing a sound effect, or handling keyboard input.  But most
importantly: Have fun!

@node Command Line Interface
@chapter Command Line Interface

While Chickadee is a library at heart, it also comes with a command
line utility to make it easier to get started.

@menu
* Invoking chickadee play::     Run Chickadee programs
@end menu

@node Invoking chickadee play
@section Invoking @command{chickadee play}

The @command{chickadee play} command is used to open a window and run
the Chickadee game contained within a Scheme source file.

@example
chickadee play the-legend-of-emacs.scm
@end example

In this file, special procedures may be defined to handle various
events from the game loop:

@itemize
@item load-game
@item quit-game
@item draw
@item update
@item key-press
@item key-release
@item text-input
@item mouse-press
@item mouse-release
@item mouse-move
@item mouse-wheel
@item controller-add
@item controller-remove
@item controller-press
@item controller-release
@item controller-move
@end itemize

See @ref{The Game Loop} for complete information on all of these
hooks, such as the arguments that each procedure receives.

In additional to evaluating the specified source file, the directory
containing that file is added to Guile's load path so that games can
easily be divided into many different files.  Furthermore, that
directory is entered prior to evaluating the file so that data files
(images, sounds, etc.) can be loaded relative to the main source file,
regardless of what the current directory was when @command{chickadee
play} was invoked.

Many aspects of the initial game window and environment can be
controlled via the following options:

@table @code
@item --title=@var{title}
@itemx -t @var{title}

Set the window title to @var{title}.

@item --width=@var{width}
@itemx -w @var{width}

Set the window width to @var{width} pixels.

@item --height=@var{height}
@itemx -h @var{height}

Set the window height to @var{height} pixels.

@item --fullscreen
@itemx -f

Open window in fullscreen mode.

@item --resizable
@itemx -r

Make window resizable.

@item --update-hz=@var{n}
@itemx -u @var{n}

Update the game @var{n} times per second.

@item --repl

Launch a REPL in the terminal.  This will allow the game environment
to debugged and modified without having to stop and restart the game
after each change.

@item --repl-server[=@var{port}]

Launch a REPL server on port @var{port}, or 37146 by default.

@command{telnet localhost 37146} (or whatever port number was given)
will do the trick, but using the @uref{https://www.nongnu.org/geiser/,
Geiser} extension for Emacs is by far the best way to develop at the
REPL with Guile.  Use @code{M-x connect-to-guile} to connect to the
REPL server.

@end table

@node Live Coding
@chapter Live Coding

One of the biggest appeals of any Lisp dialect is the ability to use
the ``read-eval-print loop'' (REPL for short) to build programs
iteratively and interactively while the program is running.  However,
programs that run in an event loop and respond to user input (such as
a game using the Chickadee library!) require special care for this
workflow to be pleasant.

If you are using the @command{chickadee play} command to run your
game, then the @code{--repl} or @code{--repl-server} arguments are all
you need to get a live coding environment running.

If, however, you are using @code{run-game} to start the game loop then
it's still fairly easy to hook up a special kind of REPL by yourself.

First, create a cooperative REPL server (It's important to use Guile's
cooperative REPL server instead of the standard REPL server in
@code{(system repl server)} to avoid thread synchronization issues).
Then, in the game loop's update procedure, call
@code{poll-coop-repl-server} and pass the REPL object.  Here is a
template to follow:

@example
(use-modules (chickadee)
             (system repl coop-server))

(define repl (spawn-coop-repl-server))

(define (update dt)
  (poll-coop-repl-server repl)
  ...)

(run-game #:update update ...)
@end example

To use the REPL, connect to it via port 37146.  Telnet will do the
trick, but using the @uref{https://www.nongnu.org/geiser/, Geiser}
extension for Emacs is by far the best way to develop at the REPL with
Guile.  Use @code{M-x connect-to-guile} to connect to the REPL server.

@node API Reference
@chapter API Reference

@include api.texi

@node Copying This Manual
@appendix Copying This Manual

@menu
* GNU Free Documentation License::  License for copying this manual.
@end menu

@c Get fdl.texi from http://www.gnu.org/licenses/fdl.html
@node GNU Free Documentation License
@section GNU Free Documentation License
@include fdl.texi

@node Index
@unnumbered Index

@syncodeindex tp fn
@syncodeindex vr fn
@printindex fn

@bye

@c chickadee.texi ends here