factor/library/sdl/sdl-utils.factor

55 lines
1.5 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 ;
: 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
2005-10-14 04:05:02 -04:00
init-keyboard init-surface ;
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
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