doodle - A minimal game 'framework'.
A minimal game 'framework' inspired by löve.
This is still a work in progress and subject to change!
The source code for doodle is hosted on bitbucket.
You can grab it with git:
git clone https://bitbucket.org/ckeen/doodle.git
General program flow of a doodle
A program creates a window, called a 'doodle' here and registers for any of these three events: world-inits, world-ends and world-changes. world-inits is called upon the first iteration of the event loop, world-ends last and world-changes for every iteration.
world-inits and world-ends are thunks, whereas world-changes has the following signature:[procedure] (world-changes (lambda (events dt escape-continuation) ...))
- holds the occured list of events of this loop iteration
- holds the time delta between the last and this iteration
- holds the continuation that will exit the loop
Please see below for a detailed list of supported event symbols.
dt is a flonum which can be used to adjust speed of animations for example.
The game loop is started with the (run-event-loop) procedure. Usually the game loop will run as fast as it can unless the keyword parameter minimum-wait has been given which adds that minimum delay between iterations.
This egg is still under development; the API might change a bit in future versions.
Procedures[procedure] (run-event-loop #!key (run-in-background #f) (minimum-wait *minimum-wait*))
Starts the event loop and runs world-inits. If run-in-background is #t a new thread is started. Within the event loop the procedure given with world-changes is called with the time delta of the last call and the events that occured. If minimum-wait is given and the delta is smaller than minimum-wait the thread will sleep for the remaining time. minimum-wait takes a value in seconds.[procedure] (world-inits (lambda () ...))
A thunk that is called once when the event loop is started.[procedure] (world-ends (lambda () ...))
A thunk that is called once when the even loop is exited.[procedure] (world-changes (lambda (events dt exit-continuation) ...))
A procedure that gets called every iteration of the event loop. The events parameter holds the list of events, dt is the time difference in milliseconds between the last and current iteration. exit-continuation can be used to jump out of the event-loop.[parameter] (world-update-delay flonum)
The minimum delay between updates. Aka, the minimum-wait property in the event-loop.
One event is a list containing information about the individual event. There are currently 3 types of handled events:
- The quit event has the following form (quit).
- key events
- The first element of the list is the symbol key followed by either the symbol pressed or released followed by either the integer for the key code or the symbols up, down, left or right representing cursor keys.
- mouse events
- The first element of this list is the symbol mouse. There are three types of events: pressed, released and moved. The first two are followed by three values x y and button, representing the coordinates of the pointer and the button number being pressed or released. Mouse buttons are numbered 1-5, with 4,5 being rotations of the mouse wheel. moved events have the coordinates as their only arguments.
- This will list all other events. The list contains the symbol unknown and the SDL event type. See the SDL egg documentation for hints on what this may be.
Colors in doodle are represented as simple lists representing RGBA values one number each. Currently there are two predefined colors:
- (0 0 0 1)
- (1 1 1 1)
Procedures[procedure] (new-doodle #!key (width 680) (height 460) (title "Doodle") (background solid-black) (fullscreen #f))
Initialises the internal state and createas a window with the given dimensions and title. If background is a string it will be interpreted as a filename pointing to a PNG file which will be loaded as background.
The background parameter can either be a doodle RGBA color list or a string pointing to a PNG file.[procedure] (show!)
Causes a redraw of the window.[procedure] (clear-screen #!optional (color (current-background)))
Fills the screen with the current-background color. Color can also be given as a string representing a filename to a PNG image which will be loaded instead.[procedure] (rectangle x y width height color)
Draws a rectangle at the given coordinates (x, y) with the dimensions width and height. The border is drawn in color.[procedure] (filled-rectangle x y width height color)
Draws a rectangle at the given coordinates (x, y) with the dimensions width and height. The border is drawn in color. The rectangle also is filled with color.[procedure] (circle x y diameter color)
Draws a circle at the point defined by (x,y) with the given diameter and color. The border is drawn in color.[procedure] (filled-circle x y diameter color)
Draws a circle at the point defined by (x,y) with the given diameter and color. The border is drawn in color. The circle is filled in color too.[procedure] (draw-line x1 y1 x2 y2 #!key (color solid-white) (style #:solid))
Draw a line between the two points (x1,y1) and (x2,y2) in the given style. Valid styles are either #:solid (the default) or #:dashed for dashed lines. The line is drawn in color.[procedure] (set-font! font size color)
Sets the font to font, given size and color. font is a string representing the font's name. Every X11 TTF font is applicable.[procedure] (text x y text #!key (align #:left))
Print the given text in one line starting on point (x,y). Alignment can be changed with the align parameter. Supported alignment values are #:left, #:center and #:right.[procedure] (text-width text)
Returns multiple values: The width and height in pixels of the given text when rendered with the current font settings. text is assumed to be a string.[procedure] (save-screenshot filename)
Saves the current screen content to a file called filename as a portable network graphics (PNG). It is up to the user to provide an appropriate extension to the filename.
doodle-width and doodle-height hold the width and height of the window, that has been given on a call to (new-doodle).
The following can be used to get or set the current values.[procedure] (font-color)
Holds the current font color. Default is black.[procedure] (font-size)
Holds the current font-size. Default is 12.[procedure] (current-background)
Holds the current background color. Default is black.[procedure] (line-width)
Holds the line width used for drawing. Default is 2.0.[procedure] (world-update-delay)
Amount of milliseconds that should pass at minimum before calling the world-changes procedure again.
Since version 0.4 doodle supports blitting images in. The initial form is define-resource. This registers an image and makes it available through the given symbol name to all procedures that take a resource argument below.[procedure] (define-resource name type filename . data )
Registers a resource under the name name. The resource is read from a file specified with filename. The type argument is a keyword and specifies how the data argument is handled and which form is expected.
The following types are handled:
A single image from a single file. It takes optional argumens x-offset, y-offset and scale-factor to take just a fraction out of the given image file and to be able to scale it.
The following example does this several times:
(define-resource 'water #:image "Water Block.png" *terrain-xoffset* *terrain-yoffset*) (define-resource 'stone #:image "Stone Block.png" *terrain-xoffset* *terrain-yoffset*) (define-resource 'princess #:image "Character Princess Girl.png" 0 -80) (define-resource 'bubble #:image "SpeechBubble.png" -15 -120) (define-resource 'bug #:image "Enemy Bug.png" 0 -80) (define-resource 'tree #:image "Tree Tall.png" 0 -80 ) (define-resource 'heart #:image "Heart.png" 0 0 0.5)
This assumes the image to consist of one or more square tiles. Then several images are referenced out of a single image file. The data argument is expected to be a list of two elements: the tile-size and a list consisting of (name number) pairs (proper lists). The following examples makes three tiles accessible through their names: wall, floor and hero. They can be drawn with the blit-image procedure.
(define-resource 'tiles #:tileset "../roguelike/tile.png" 32 '((wall 2) (floor 30) (hero 486)))[procedure] (blit-image name x y rotation: angle )
Blits in the image name at position (x, y). If angle is given the image is rotated. The angle has to be given in degrees, negative values indicate counter-clockwise rotation.
The following example will show a little circle painting program. It requires the matchable egg.
(use matchable doodle) (define *paint* #f) (define red '(1 0 0 0.3)) (world-inits (lambda () (clear-screen) (set-font! "Vollkorn" 18 red) (text (/ doodle-width 2) (/ doodle-height 2) '("Welcome to doodle!" "Click the left mouse button to draw circles" "Press ESC to leave") align: #:center))) (world-changes (lambda (events dt exit) (for-each (lambda (e) (match e (('mouse 'pressed x y 1) (set! *paint* #t) (filled-circle x y 10 red)) (('mouse 'released x y 1) (set! *paint* #f)) (('mouse 'moved x y) (when *paint* (filled-circle x y 10 red))) (('key 'pressed #\esc) (exit #t)) (else (void)))) events))) (new-doodle title: "Doodle paint" background: solid-white) (run-event-loop)
- Initial version
- bogus tag, sorry
- Fixed event handling, added mouse events, background images possible
- blit-image and define-resources added, text-width and line-width are exported
- Yours truly messing up the meta data, please ignore
- Correct dependencies to cairo and sdl
- Fixed text procedure, closed path on circles
- Changed dependency from sdl egg to sdl-base
- Fiddling with meta files
- callbacks are wrapped in exception handlers, parameters are no longer used, added world-update-delay
Copyright (c) 2012, Christian Kellermann All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.