Next: Input, Up: API Reference [Contents][Index]
At the very core of Chickadee, in the (chickadee)
module, lies
an event loop. This loop, or “kernel”, is responsible for creating
and managing the game window, dispatching input events, ensuring that
the game is updated at the desired interval, and rendering graphics.
The kernel implements what is known as a “fixed timestep” game loop,
meaning that the game simulation will be advanced by a fixed interval
of time and will never vary from frame to frame, unlike some other
styles of game loops. The appropriately named run-game
and
abort-game
procedures are the entry and exit points to the
Chickadee kernel.
On its own, the kernel does not do very much at all. In order to
actually respond to input events, update game state, or draw something
to the game window, a hacker with a penchant for game development must
latch onto extension points built into the kernel, called “hooks”,
and specify what action ought to be taken for any given event. For
example, the key-press-hook
can be used to respond to the
a
key being pressed by swinging the player’s mighty sword.
There are many hooks available, so read on to learn about all of them.
For information about using Guile’s hook API, see See Hooks in GNU Guile Reference Manual.
[#:window-width 640] [#:window-height 480] [#:window-fullscreen? #f]
[#:update-hz 60]
Start the event loop. This procedure will not return until
abort-game
is called.
The update-hook
will be run update-hz times per second.
A new graphical window will be opened with window-width x
window-height as its dimensions, window-title as its
title, and in fullscreen mode if window-fullscreen? is
#t
.
Stop the currently running Chickadee event loop.
Return the current game time in milliseconds.
A hook that is run once when the event loop boots, before any other hook is run. This hook is run with zero arguments.
(add-hook! load-hook (lambda () (display "hello!\n")))
A hook that is run every time the game simulation should be advanced. This hook is run with a single argument dt, the fixed timestep that was configured when the event loop was started, in milliseconds.
(add-hook! update-hook (lambda (dt) (display "tick!\n")))
A hook that is run before a frame is rendered. This hook is run with zero arguments.
(add-hook! before-draw-hook (lambda () (display "about to draw!\n")))
A hook that is run after a frame is rendered. This hook is run with zero arguments.
(add-hook! after-draw-hook (lambda () (display "done drawing!\n")))
Combined with before-draw-hook
, one can perform a frames per
second calculation to monitor game performance and stability.
A hook that is run each time a frame should be rendered. This hook is run with a single argument alpha, a value in the range [0, 1] which represents how much time has past since the last game state update relative to the upcoming game state update, as a percentage. Because the game state is updated independent of rendering, it is often the case that rendering is occuring between two updates. If the game is rendered as it was during the last update, a strange side-effect will occur that makes animation appear rough or “choppy”. To counter this, the alpha value can be used to perfrom a linear interpolation of a moving object between its current position and its previous position. This odd trick has the pleasing result of making the animation look smooth again, but requires keeping track of previous state.
(add-hook! draw-hook (lambda (alpha) (display "<(._.<) \n")))
A hook that is run when the user clicks the close button on the game window. This hook is run with zero arguments.
(add-hook! quit-hook (lambda () (display "bye!\n")))
A hook that is run when a key is pressed on the keyboard. This hook is run with four arguments:
backspace
. It’s called a virtual key because the
operating system may map a physical keyboard key to another key
entirely, such as how the author binds the “caps lock” key to mean
“control”.
ctrl
, alt
, and shift
.
#t
if this is a repeated press of the same key.
(add-hook! key-press-hook (lambda (key scancode modifiers repeat?) (display "pressed key: ") (display key) (newline)))
A hook that is run when a key is released on the keyboard. This hook is run with three arguments:
A hook that is run when printable text is typed on the keyboard. This hook is run with a single argument, text, a string containing the text that was entered.
A hook that is run when a mouse button is pressed. This hook is run with four arguments:
left
, middle
, or right
.
A hook that is run when a mouse button is released. This hook is run with three arguments:
A hook that is run when the mouse is moved. This hook is run with five arguments:
A hook that is run when a game controller is connected. This hook is run with a single argument, controller, the controller that was connected.
A hook that is run when a game controller is disconnected. This hook is run with a single argument, controller, the controller that was disconnected.
A hook that is run when a button on a game controller is pressed. This hook is run with two arguments:
a
b
x
y
back
guide
start
left-stick
right-stick
left-shoulder
right-shoulder
dpad-up
dpad-down
dpad-left
dpad-right
A hook that is run when a button on a game controller is released.
This hook is run with two arguments:
A hook that is run when an analog stick or trigger on a game controller is moved. This hook is run with three arguments
left-x
left-y
right-x
right-y
trigger-left
trigger-right
Next: Input, Up: API Reference [Contents][Index]