factor/library/sdl/sdl-utils.factor

129 lines
3.7 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2005 Slava Pestov.
2005-02-11 19:09:48 -05:00
! See http://factor.sf.net/license.txt for BSD license.
2004-10-17 19:10:46 -04:00
IN: sdl
2005-10-07 20:26:21 -04:00
USING: alien arrays errors hashtables io kernel lists math
namespaces sequences styles ;
2004-10-17 19:10:46 -04:00
2005-03-07 00:39:57 -05:00
SYMBOL: surface
SYMBOL: width
SYMBOL: height
SYMBOL: bpp
2005-10-07 20:26:21 -04:00
: sdl-error ( 0/-1 -- )
0 = [ SDL_GetError throw ] unless ;
: ttf-name ( font style -- name )
cons {{
[[ [[ "Monospaced" plain ]] "VeraMono" ]]
[[ [[ "Monospaced" bold ]] "VeraMoBd" ]]
[[ [[ "Monospaced" bold-italic ]] "VeraMoBI" ]]
[[ [[ "Monospaced" italic ]] "VeraMoIt" ]]
[[ [[ "Sans Serif" plain ]] "Vera" ]]
[[ [[ "Sans Serif" bold ]] "VeraBd" ]]
[[ [[ "Sans Serif" bold-italic ]] "VeraBI" ]]
[[ [[ "Sans Serif" italic ]] "VeraIt" ]]
[[ [[ "Serif" plain ]] "VeraSe" ]]
[[ [[ "Serif" bold ]] "VeraSeBd" ]]
[[ [[ "Serif" bold-italic ]] "VeraBI" ]]
[[ [[ "Serif" italic ]] "VeraIt" ]]
}} hash ;
: ttf-path ( name -- string )
[ "/fonts/" % % ".ttf" % ] "" make resource-path ;
: open-font ( { font style ptsize } -- alien )
first3 >r ttf-name ttf-path r> TTF_OpenFont
dup alien-address 0 = [ SDL_GetError throw ] when ;
SYMBOL: open-fonts
: lookup-font ( font style ptsize -- font )
3array open-fonts get [ open-font ] cache ;
: init-ttf ( -- )
TTF_Init sdl-error
global [
open-fonts [ [ cdr expired? not ] hash-subset ] change
] bind ;
: init-keyboard ( -- )
1 SDL_EnableUNICODE drop
SDL_DEFAULT_REPEAT_DELAY SDL_DEFAULT_REPEAT_INTERVAL
SDL_EnableKeyRepeat drop ;
: init-surface ( width height bpp flags -- )
2005-03-07 00:39:57 -05:00
>r 3dup bpp set height set width set r>
SDL_SetVideoMode surface set ;
2005-10-07 20:26:21 -04:00
: init-sdl ( width height bpp flags -- )
SDL_INIT_EVERYTHING SDL_Init sdl-error
init-keyboard init-surface init-ttf ;
2005-09-24 23:21:09 -04:00
2005-03-07 00:39:57 -05:00
: with-screen ( width height bpp flags quot -- )
#! Set up SDL graphics and call the quotation.
2005-10-07 20:26:21 -04:00
[ [ >r init-sdl r> call ] [ SDL_Quit ] cleanup ] with-scope ;
inline
2005-03-07 00:39:57 -05:00
: rgb ( [ r g b ] -- n )
first3
2005-01-23 16:47:28 -05:00
255
swap >fixnum 8 shift bitor
swap >fixnum 16 shift bitor
swap >fixnum 24 shift bitor ;
2004-10-17 19:10:46 -04:00
: make-color ( r g b -- color )
#! Make an SDL_Color struct. This will go away soon in favor
#! of pass-by-value support in the FFI.
2005-10-11 21:46:14 -04:00
<sdl-color>
[ set-sdl-color-b ] keep
[ set-sdl-color-g ] keep
[ set-sdl-color-r ] keep
0 alien-unsigned-4 ;
2005-03-07 00:39:57 -05:00
: make-rect ( x y w h -- rect )
2005-08-24 00:30:07 -04:00
<sdl-rect>
[ set-sdl-rect-h ] keep
[ set-sdl-rect-w ] keep
[ set-sdl-rect-y ] keep
[ set-sdl-rect-x ] keep ;
2005-03-07 00:39:57 -05:00
2005-01-23 16:47:28 -05:00
: with-pixels ( quot -- )
width get [
height get [
[ rot dup slip swap surface get swap ] 2keep
[ rot pixelColor ] 2keep
] repeat
] repeat drop ; inline
2004-10-17 19:10:46 -04:00
2005-10-07 20:26:21 -04:00
: must-lock-surface? ( -- ? )
2005-08-24 01:09:36 -04:00
#! This is a macro in SDL_video.h.
2005-10-07 20:26:21 -04:00
surface get dup surface-offset 0 = [
2005-08-24 10:19:09 -04:00
surface-flags
2005-08-24 01:09:36 -04:00
SDL_HWSURFACE SDL_ASYNCBLIT bitor SDL_RLEACCEL bitor
bitand 0 = not
] [
drop t
2005-09-24 15:21:17 -04:00
] if ;
2005-08-24 01:09:36 -04:00
2005-10-07 20:26:21 -04:00
: lock-surface ( -- )
2005-10-13 00:30:44 -04:00
must-lock-surface? [ surface get SDL_LockSurface drop ] when ;
2005-10-07 20:26:21 -04:00
: unlock-surface ( -- )
2005-10-13 00:30:44 -04:00
must-lock-surface? [ surface get SDL_UnlockSurface ] when ;
2005-10-07 20:26:21 -04:00
2004-10-17 19:10:46 -04:00
: with-surface ( quot -- )
#! Execute a quotation, locking the current surface if it
#! is required (eg, hardware surface).
2005-10-13 00:30:44 -04:00
[ lock-surface call ]
[ unlock-surface surface get SDL_Flip ]
cleanup ; inline
2005-10-07 20:26:21 -04:00
: with-unlocked-surface ( quot -- )
must-lock-surface?
[ unlock-surface call lock-surface ] [ call ] if ; inline
2005-08-24 00:30:07 -04:00
2005-08-24 10:19:09 -04:00
: surface-rect ( x y surface -- rect )
dup surface-w swap surface-h make-rect ;
2005-10-07 20:26:21 -04:00
{{ }} clone open-fonts global set-hash