8.4.4.2. LibSDL: High-level Interface to SDL
(packages/sdl/libsdl.lsh)


libsdl is a library of "high level" functions and classes built on top of the SDL (Simple Directmedia Layer) library to facilitate its use from Lush. It includes objects such as screens and sprites with pixel-accurate collision detection. It also includes access functions to keyboard and mouse events, as well as to several common SDL data structures.

8.4.4.2.0. (sdl-initialize)
(packages/sdl/libsdl.lsh)


initializes the SDL engine with all its subsystems (timer, audio, video, cdrom, joystick). This function MUST BE CALLED before any other SDL function or the interpreter will probably crash.

this function sets the global variable *sdl-initialized* to true, and only executes the initialization code if this variable is nil. Returns true on success. If you want to initialize only certain subsystems, call the lower-level function SDL_Init with the appropriate flags and set *sdl-initialized* to true before opening an sdl-screen.



8.4.4.2.1. (sdl-terminate)
(packages/sdl/libsdl.lsh)


This shuts down the SDL subsystem. sdl-initialize must be called before any new SDL call can be performed again. A call to sdl-terminate will close any open SDL screen. In fact, calling sdl-terminate is the only way to close an SDL screen.

8.4.4.2.2. sdl-screen
(packages/sdl/libsdl.lsh)


a class that creates and manipulates SDL Screens. This class can handle pixel-accurate collision detection. It maintains a timer that measures the time between screen flips. This allows to do time-accurate real-time physical simulation. An SDL Screens is not closed when the corresponding sdl-screen object is freed. Closing a screen can be done with a call to (sdl-terminate) .

8.4.4.2.2.0. (new sdl-screen w h cap)
[CLASS] (packages/sdl/libsdl.lsh)


open a new SDL Screen of size w , h with title cap . This attempts to open a double-buffered 32 bit-per-pixel surface in the video hardware. It reverts to 24 or 16 bits (with 32 bit emulation) if a 32 bit screen cannot be allocated. The function sdl-intialize MUST be called before opening an sdl-screen, or the interpreter will crash!

8.4.4.2.2.1. (==> sdl-screen flip)
[MSG] (packages/sdl/libsdl.lsh)


flip the buffers. The content of the visible screen is replaced by the content of the back buffer (in which drawing operations are performed). This returns the time since the last call to flip in seconds (as a floating point number). The collision detection array is cleared by this call.

8.4.4.2.2.2. (==> sdl-screen toggle-fullscreen)
[MSG] (packages/sdl/libsdl.lsh)


Toggle full screen mode. Returns 1 on success, 0 on failure.

8.4.4.2.2.3. (==> sdl-screen clear)
[MSG] (packages/sdl/libsdl.lsh)


fill the screen with back. Nothing actually happens on the screen until the next call to flip.

8.4.4.2.2.4. (new sdlgl-screen w h cap)
[CLASS] (packages/sdl/libsdl.lsh)


just like sdl-screen, except that OpenGL drawing is enabled. Always opens a 32 bit window.

8.4.4.2.3. sdlidx-screen
(packages/sdl/libsdl.lsh)


A subclass of sdl-screen that associates an SDL screen with an IDX of the same size. Whatever is contained in the IDX can drawn on the screen by calling the "flip" method. Unlike sdl-screen, this class does not use SDL's double buffering mechanism, but uses its own back buffer in which all drawing operations take place. sdlidx-screen can be used with sprites. Sprites drawn into the screen will appear in the IDX. No collision detection between sprites and the background directly drawn into the IDX can be performed (but it works between sprites).

8.4.4.2.3.0. (new sdlidx-screen m caption)
[CLASS] (packages/sdl/libsdl.lsh)


opens a new SDL screen where IDX m (and idx3 of ubytes) is used as a back buffer in which images can be drawn. The last dimension of m should be 3 or 4. If it is 3, the RGB image in m will be drawn opaque, if it is 4, the RGBA image in m will be drawn on top of the current image using the A channel as transparency. The screen is updated by a call to the method flip.

8.4.4.2.3.1. (==> sdlidx-screen flip)
[MSG] (packages/sdl/libsdl.lsh)


Update screen with content of the IDX back buffer. return number of milliseconds since the last call to flip.

8.4.4.2.4. sdl-event
(packages/sdl/libsdl.lsh)


a high-level class to manipulate SDL events.

8.4.4.2.4.0. (new sdl-event)
[CLASS] (packages/sdl/libsdl.lsh)


create a new sdl-event object.

8.4.4.2.4.1. (==> sdle type)
[MSG] (packages/sdl/libsdl.lsh)


return type of sdl-even sdle .

8.4.4.2.4.2. (==> sdle key-keysym-sym)
[MSG] (packages/sdl/libsdl.lsh)


return symbol of keyboard key symbol of an sdl-event.

8.4.4.2.4.3. (==> sdle button-x)
[MSG] (packages/sdl/libsdl.lsh)


return mouse button 1 value of an sdl-event.

8.4.4.2.4.4. (==> sdle button-y)
[MSG] (packages/sdl/libsdl.lsh)


return mouse button 2 value of an sdl-event.

8.4.4.2.4.5. (==> sdle motion-x)
[MSG] (packages/sdl/libsdl.lsh)


return horizontal mouse motion of an sdl-event.

8.4.4.2.4.6. (==> sdle motion-y)
[MSG] (packages/sdl/libsdl.lsh)


return vertical mouse motion of an sdl-event.

8.4.4.2.4.7. (==> sdle free)
[MSG] (packages/sdl/libsdl.lsh)


sdl-event destructor

8.4.4.2.4.8. (==> sdl-event get-arrows xyk)
[MSG] (packages/sdl/libsdl.lsh)


xyk is an idx1 of ints of size 3. (xyk 0) is set to +1 if right-arrow is pressed and -1 if left-arrow is pressed (xyk 1) is set to +1 if up-arrow is pressed and -1 if down-arrow is pressed (xyk 2) is set to the keysym of any other key that's pressed

8.4.4.2.4.9. (==> sdl-event get-keys keymap keystate)
[MSG] (packages/sdl/libsdl.lsh)


keymap is an idx1 of -int- containing a list keys to be polled. The keys are identified by their SDLK code. keystate is an -idx1- of -int- of the same size. Upon exit, each element of keystate whose corresponding key (as specified in keymap) is pressed down is set to 1. If the corresponding key is not pressed, it is set to 0. This method can be used for games when simultaneous keypresses of all the keys specified in keymap must be detected.
 (libload "sdl/libsdl")
 (sdl-initialize)
 (setq scr (new sdl-screen 640 480 "Key Test"))
 (setq ev (new sdl-event))
 (setq kmap (int-matrix 4))
 (kmap () '(@@SDLK_a @@SDLK_d @@SDLK_w @@SDLK_s))
 (setq ks (int-matrix 4))
 (while t
   (==> ev get-keys kmap ks)
   (when (<> 0 ((idx-sum ks)))
    (print ks)
    (sleep 0.25)))


8.4.4.2.5. sdl-sprite
(packages/sdl/libsdl.lsh)


a sdl-sprite class for drawing movable objects in an SDL screen with pixel-accurate collision detection. Sprites can contain multiple frames that can be flipped through to make animations. or rotations. Each sprite object can be given an ID (which is preferably between 1 and 32, but can be larger), which identifies it during collision detection. For example, all background sprites can be given ID 1, spaceship 1 ID 2, spaceship 2 ID 3, and missiles, ID 4. IDs do not need to be unique (several sprites can share the same ID), but then the collision detection mechanism won't be able to distinguish them.

8.4.4.2.5.0. (==> sdl-sprite set-hotpoint hx hy)
[MSG] (packages/sdl/libsdl.lsh)


sets the "hot point" (the handle) of the sprite to coordinates hx , hy .

8.4.4.2.5.1. (==> sdl-sprite get-hotpoint)
[MSG] (packages/sdl/libsdl.lsh)


get the "hot point" (the handle) of the sprite as a list ( hx , hy ).

8.4.4.2.5.2. (==> sdl-sprite get-width)
[MSG] (packages/sdl/libsdl.lsh)


return width of current frame

8.4.4.2.5.3. (==> sdl-sprite get-height)
[MSG] (packages/sdl/libsdl.lsh)


return height of current frame

8.4.4.2.5.4. (new sdl-sprite screen id)
[CLASS] (packages/sdl/libsdl.lsh)


create a new sprite on screen screen with ID id .

8.4.4.2.5.5. (==> sdl-sprite alloc-frame)
[MSG] (packages/sdl/libsdl.lsh)


private method for allocating new frames in a sprite

8.4.4.2.5.6. (==> sdl-sprite make-frame img i hx hy)
[MSG] (packages/sdl/libsdl.lsh)


private method to set frame i to SDL_Surface img . the screen must be initialized before calling this.

8.4.4.2.5.7. (==> sdl-sprite load-frame file i hx hy)
[MSG] (packages/sdl/libsdl.lsh)


load image file file into frame i , and set the hot point (handle) to hx hy . The images file can be in any format that sdlimage can handle (see img-load). The screen must be initialized before calling this

8.4.4.2.5.8. (==> sdl-sprite rotscale-frame src dst angle coeff)
[MSG] (packages/sdl/libsdl.lsh)


take frame src , rotate it by angle degreed, and scale it by coefficient coeff , then sets the dst -th frame to the resulting image. This function is convenient for generating views of a sprite at all possible angles automatically. This must be done in advance as this functions is too slow to generate frames on-the-fly while the game is running.

8.4.4.2.5.9. (==> sdl-sprite make-frame-idx m i hx hy)
[MSG] (packages/sdl/libsdl.lsh)


set frame i to the image contained in idx m . The idx will be interpreted as an heightxwidthx4 idx of ubytes containing an RGBA images. NOTE: the alpha channel of m must be set to 255 for opaque pixels. If m was filled up with image-read-rgb the alpha channel is likely to be all zero. The screen must be initialized before calling this

8.4.4.2.5.10. (==> sdl-sprite get-frame-idx i)
[MSG] (packages/sdl/libsdl.lsh)


Return an idx containing the image of the i -th frame.

8.4.4.2.5.11. (==> sdl-sprite set-frame i)
[MSG] (packages/sdl/libsdl.lsh)


make frame i the current frame (i.e. the one that will be drawn next).

8.4.4.2.5.12. (==> sdl-sprite get-frame)
[MSG] (packages/sdl/libsdl.lsh)


return index of current frame.

8.4.4.2.5.13. (==> sdl-sprite move lx ly)
[MSG] (packages/sdl/libsdl.lsh)


move sprite to lx , ly . This does not actually draw anything.

8.4.4.2.5.14. (==> sdl-sprite moverel dx dy)
[MSG] (packages/sdl/libsdl.lsh)


move sprite by dx , dy relative to the current position. This does not actually draw anything.

8.4.4.2.5.15. (==> sdl-sprite draw)
[MSG] (packages/sdl/libsdl.lsh)


draw the sprite on the screen using the current frame and position. This does not do any collision detection.

8.4.4.2.5.16. (==> sdl-sprite drawc)
[MSG] (packages/sdl/libsdl.lsh)


draw the sprite on the screen using the current frame and position. This performs all the operation necessary to do pixel-accurate collision detection. Any pixel with a non-zero alpha value drawn on top of another previously drawn object triggers the collision detection.

8.4.4.2.5.17. (==> sdl-sprite test-collision)
[MSG] (packages/sdl/libsdl.lsh)


Prepare to test if the sprite would collide with any sprite already drawn on the screen. Unlike drawc , this method does not draw the sprite and does not modify the screen's collision map. It provides a way to test for collision and take actions before the collisio actually happens. The function collided must be subsequently called to actually test if the current sprite would have collided with any other already drawn sprites.

8.4.4.2.5.18. (collided sp1 sp2)
(packages/sdl/libsdl.lsh)


returns true any sprite with the same id as sp1 has collided with any sprite with the same id as sp2 . For this to return a meaningful result, both sprites must have been drawn with the drawc , or the one must have been drawn with drawc , and the other one must have been sent the message test-collision . This must be called before the screen flip (but after the calls to drawc or test-collision ) since the screen flip clears the collision detection array.

8.4.4.2.6. mover
(packages/sdl/libsdl.lsh)


a class that can move sprites according to 2D Newtonian dynamics. The simplest use consists in creating a mover, then go around a loop that:
- gets input from the keyboard, mouse or joystick.
- calls the mover methods push, and update
- calls the mover method move with a sprite as argument
- draws the sprite
- flips the screen
- sets the deltat of the mover to the value
  returned by the screen flip method.


8.4.4.2.6.0. (==> mover set-mass m)
[MSG] (packages/sdl/libsdl.lsh)


set the mass of the mover.

8.4.4.2.6.1. (==> mover get-mass)
[MSG] (packages/sdl/libsdl.lsh)


return mass of mover

8.4.4.2.6.2. (==> mover set-deltat dt)
[MSG] (packages/sdl/libsdl.lsh)


sets the time increment between updates (in seconds) to dt .

8.4.4.2.6.3. (==> mover get-deltat)
[MSG] (packages/sdl/libsdl.lsh)


gets the time increment between updates.

8.4.4.2.6.4. (==> mover set-state lx ly lvx lvy lax lay)
[MSG] (packages/sdl/libsdl.lsh)




8.4.4.2.6.5. (==> mover get-state)
[MSG] (packages/sdl/libsdl.lsh)




8.4.4.2.6.6. (==> mover set-state x y vx vy ax ay mass deltat)
[MSG] (packages/sdl/libsdl.lsh)


create a new sprite mover with initial position x , y , initial velocity vx , vy , initial acceleration ax , ay , mass mass , and time increment deltat .

8.4.4.2.6.7. (==> mover push fx fy)
[MSG] (packages/sdl/libsdl.lsh)


apply a force vector fx fy to the mover. the unit is mass untis times pixels per second per second. In other words, a mover of mass 1, with a force vector of length 1, will accelerate 1 pixel per second per second (assuming the time increment is correct).

8.4.4.2.6.8. (==> mover update)
[MSG] (packages/sdl/libsdl.lsh)


updates the positions and velocity according to 2D Newtonian mechanics.

8.4.4.2.6.9. (==> mover move-sprite sp)
[MSG] (packages/sdl/libsdl.lsh)


move sprite sp to the position of the mover.

8.4.4.2.7. SDL_Rect function
(packages/sdl/libsdl.lsh)


functions to manipulate SDL_Rect structures Most users will prefer to use the higher level sdl-rect class rather than these functions

8.4.4.2.7.0. low-level functions on SDL-Rect
(packages/sdl/libsdl.lsh)




8.4.4.2.7.0.0. (new-sdl-rect x y w h)
(packages/sdl/libsdl.lsh)


allocate a new SDL_Rect structure, fill it up with x , y , w , h (position, width, height), and return a gptr to it. The result must be freed with free-sdl-rect.

8.4.4.2.7.0.1. (set-sdl-rect r x y w h)
(packages/sdl/libsdl.lsh)


sets the position width and height of SDL_Rect pointed to by r to the arguments.

8.4.4.2.7.0.2. (free-sdl-rect r)
(packages/sdl/libsdl.lsh)


deallocate SDL_Rect structure pointed to by r .

8.4.4.2.7.1. sdl-rect
(packages/sdl/libsdl.lsh)


a high-level class to manipulate SDL_Rect with automatic garbage collection and the like.

8.4.4.2.7.1.0. (new sdl-rect x y w h)
[CLASS] (packages/sdl/libsdl.lsh)


create a new sdl-rect.

8.4.4.2.8. SDL_Surface
(packages/sdl/libsdl.lsh)


manipulating SDL_Surface structures

8.4.4.2.8.0. low level functions on SDL_Surface
(packages/sdl/libsdl.lsh)




8.4.4.2.8.1. (sdl-surface-w s)
(packages/sdl/libsdl.lsh)


return width of SDL_Surface pointed to by s .

8.4.4.2.8.2. (sdl-surface-h s)
(packages/sdl/libsdl.lsh)


return height of SDL_Surface pointed to by s .

8.4.4.2.8.3. (sdl-surface-pitch s)
(packages/sdl/libsdl.lsh)


return pitch of SDL_Surface pointed to by s .

8.4.4.2.8.4. (sdl-surface-pixels s)
(packages/sdl/libsdl.lsh)


return a pointer to the pixel data of the SDL_Surface pointed to by s .

8.4.4.2.8.5. (sdl-surface-offset s)
(packages/sdl/libsdl.lsh)




8.4.4.2.8.6. (sdl-surface-ptr-idx m)
(packages/sdl/libsdl.lsh)


create an SDL_Surface from an IDX. the pixel area of the SDL_Surface points to the data of the IDX. CAUTION: this function is a bit dangerous because the idx can be deallocated without the sdl_surface knowing about it.

8.4.4.2.8.7. (idx-to-sdl-surface m)
(packages/sdl/libsdl.lsh)


Return an RGB SDL_Surface whose pixels are filled up with the content of IDX m . m must be an idx3 of ubyte whose last dimension must be 4. It will be interpreted as an RGBA image.

8.4.4.2.8.8. (sdl-surface-to-idx surface)
(packages/sdl/libsdl.lsh)


returns an idx3 of ubytes filled up with the RGBA pixel values of SDL_surface surface .

8.4.4.2.9. SDL Drawing Functions
(packages/sdl/libsdl.lsh)


to draw into SDL_Surfaces.

8.4.4.2.9.0. (sdl-fill-rect dst x y w h r g <b)
(packages/sdl/libsdl.lsh)


fills a rectangle defined by x y w h of an SDL_Surface dst with RGB values r g b).

8.4.4.2.10. Collision Detection (low level functions)
(packages/sdl/libsdl.lsh)


a set of function to manage pixel-accurate collision detection between object drawn on an SDL_Surface.

8.4.4.2.10.0. (sdl-collide id src srcrect dst dstrect coll)
(packages/sdl/libsdl.lsh)


this is a low-level function for pixel-accurate collision detection. The arguments src srcrect dst dstrect play the samee role as the corresponding arguments of SDL_BlitSurface. dst is an idx3 of ints. The vector of ints obtained by fixing the first two indexes and varying the last is interpreted as a bitmap. The id -th bit of the bitmap at location i , j is set when a non-transparent pixel of an object whose ID is id has been painted at the corresponding location. src is an RLE-encoded mask of an object (a list of runs) as returned by rle-encode-alpha which indicates which pixels trigger collision. srcrect is a subrectangle in src that is actually painted (must be NULL in current implementation). dstrect is the clipping rectangle in dst . coll is an idx1 of int interpreted as a bitmap, which on exit contains a set bit for every already painted object that collided with the current object.

8.4.4.2.10.1. (sdl-test-collision id src srcrect dst dstrect coll)
(packages/sdl/libsdl.lsh)


this is a low-level function for pixel-accurate collision detection. This function is essentially identical to sdl-collide , except it doesn't modify the destination bitmap dst . This function can be used to get a list of which sprites would be collided if a sprite were drawn. On output, the i-th bit of coll (idx1 of ubyte) is set if the any pixel of src would collide with any non-transparent pixel of dst . The arguments src srcrect dst dstrect play the samee role as the corresponding arguments of SDL_BlitSurface. dst is an idx3 of ints. The vector of ints obtained by fixing the first two indexes and varying the last is interpreted as a bitmap. src is an RLE-encoded mask of an object (a list of runs) as returned by rle-encode-alpha which indicates which pixels trigger collision. srcrect is a subrectangle in src that is actually painted (must be NULL in current implementation). dstrect is the clipping rectangle in dst . coll is an idx1 of int interpreted as a bitmap, which on exit contains a set bit for every already painted object that collided with the current object.

8.4.4.2.11. SDL_Event manipulation
(packages/sdl/libsdl.lsh)


functions to create and manipulate SDL_Event structures for keyboard/mouse/joystick event grabbing.

8.4.4.2.11.0. low level access to SDL_event structures
(packages/sdl/libsdl.lsh)




8.4.4.2.11.0.0. (new-sdl-event)
(packages/sdl/libsdl.lsh)


allocate and return a pointer to a new SDL_Event structure.

8.4.4.2.11.0.1. (sdl-event-type e)
(packages/sdl/libsdl.lsh)


return the type field of an SDL_Event pointed to by e .

8.4.4.2.11.0.2. (sdl-event-key-keysym-sym e)
(packages/sdl/libsdl.lsh)


return the field key.keysym.sym of an SDL_Event pointed to by e .

8.4.4.2.11.0.3. (sdl-event-button-x e)
(packages/sdl/libsdl.lsh)


return the button.x field of an SDL_Event pointed to by e .

8.4.4.2.11.0.4. (sdl-event-button-y e)
(packages/sdl/libsdl.lsh)


return the button.y field of an SDL_Event pointed to by e .

8.4.4.2.11.0.5. (sdl-event-motion-x e)
(packages/sdl/libsdl.lsh)


return the motion.x field of an SDL_Event pointed to by e .

8.4.4.2.11.0.6. (sdl-event-motion-y e)
(packages/sdl/libsdl.lsh)


return the motion.y field of an SDL_Event pointed to by e .

8.4.4.2.12. Miscellaneous/internal functions
(packages/sdl/libsdl.lsh)


these are not meant to be called directly.

8.4.4.2.12.0. (rle-encode-alpha image thres)
(packages/sdl/libsdl.lsh)


private returns a gptr on an RLE-encoded binary mask (used for collision detection) where any pixel of SDL_Surface image whose alpha value is less than thres is considered transparent