2005-01-23 21:00:52 -05:00
|
|
|
! 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-02-11 19:09:48 -05:00
|
|
|
USING: alien math namespaces compiler words parser kernel errors
|
|
|
|
lists prettyprint sdl-event sdl-gfx sdl-keyboard sdl-video
|
|
|
|
streams strings sdl-ttf hashtables ;
|
2004-10-17 19:10:46 -04:00
|
|
|
|
|
|
|
SYMBOL: surface
|
|
|
|
SYMBOL: width
|
|
|
|
SYMBOL: height
|
2004-11-08 22:36:51 -05:00
|
|
|
SYMBOL: bpp
|
|
|
|
SYMBOL: surface
|
|
|
|
|
2004-12-26 18:52:58 -05:00
|
|
|
: init-screen ( width height bpp flags -- )
|
|
|
|
>r 3dup bpp set height set width set r>
|
|
|
|
SDL_SetVideoMode surface set ;
|
|
|
|
|
2004-11-08 22:36:51 -05:00
|
|
|
: with-screen ( width height bpp flags quot -- )
|
|
|
|
#! Set up SDL graphics and call the quotation.
|
2005-02-01 20:14:03 -05:00
|
|
|
SDL_INIT_EVERYTHING SDL_Init drop TTF_Init
|
2005-02-11 19:09:48 -05:00
|
|
|
1 SDL_EnableUNICODE drop
|
|
|
|
SDL_DEFAULT_REPEAT_DELAY SDL_DEFAULT_REPEAT_INTERVAL
|
|
|
|
SDL_EnableKeyRepeat drop
|
2004-12-26 18:52:58 -05:00
|
|
|
[ >r init-screen r> call SDL_Quit ] with-scope ; inline
|
2004-10-17 19:10:46 -04:00
|
|
|
|
2005-02-03 22:21:51 -05:00
|
|
|
: rgb ( [ r g b ] -- n )
|
|
|
|
3unlist
|
2005-01-23 16:47:28 -05:00
|
|
|
255
|
2004-10-17 19:10:46 -04:00
|
|
|
swap 8 shift bitor
|
|
|
|
swap 16 shift bitor
|
|
|
|
swap 24 shift bitor ;
|
|
|
|
|
2005-01-23 21:00:52 -05: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.
|
|
|
|
255 24 shift
|
|
|
|
swap 16 shift bitor
|
|
|
|
swap 8 shift bitor
|
|
|
|
swap bitor ;
|
|
|
|
|
2005-02-03 22:21:51 -05:00
|
|
|
: black [ 0 0 0 ] ;
|
|
|
|
: white [ 255 255 255 ] ;
|
|
|
|
: red [ 255 0 0 ] ;
|
|
|
|
: green [ 0 255 0 ] ;
|
|
|
|
: blue [ 0 0 255 ] ;
|
2004-11-08 22:36:51 -05:00
|
|
|
|
|
|
|
: clear-surface ( color -- )
|
|
|
|
>r surface get 0 0 width get height get r> boxColor ;
|
|
|
|
|
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
|
|
|
|
|
|
|
: with-surface ( quot -- )
|
|
|
|
#! Execute a quotation, locking the current surface if it
|
|
|
|
#! is required (eg, hardware surface).
|
|
|
|
[
|
2004-11-08 22:36:51 -05:00
|
|
|
surface get dup must-lock-surface? [
|
2004-12-25 18:08:20 -05:00
|
|
|
dup SDL_LockSurface drop slip dup SDL_UnlockSurface
|
2004-10-17 19:10:46 -04:00
|
|
|
] [
|
|
|
|
slip
|
2004-11-08 22:36:51 -05:00
|
|
|
] ifte SDL_Flip drop
|
2004-12-25 18:08:20 -05:00
|
|
|
] with-scope ; inline
|
2004-10-17 19:10:46 -04:00
|
|
|
|
2004-10-20 21:49:10 -04:00
|
|
|
: event-loop ( event -- )
|
2005-02-08 22:02:44 -05:00
|
|
|
dup SDL_WaitEvent [
|
2004-10-20 21:49:10 -04:00
|
|
|
dup event-type SDL_QUIT = [
|
|
|
|
drop
|
|
|
|
] [
|
|
|
|
event-loop
|
|
|
|
] ifte
|
|
|
|
] [
|
|
|
|
drop
|
|
|
|
] ifte ;
|
2005-01-23 21:00:52 -05:00
|
|
|
|
|
|
|
SYMBOL: fonts
|
|
|
|
|
|
|
|
: null? ( alien -- ? )
|
|
|
|
dup [ alien-address 0 = ] when ;
|
|
|
|
|
|
|
|
: <font> ( name ptsize -- font )
|
|
|
|
>r resource-path swap cat2 r> TTF_OpenFont ;
|
|
|
|
|
2005-02-01 22:48:04 -05:00
|
|
|
SYMBOL: logical-fonts
|
|
|
|
|
|
|
|
: logical-font ( name -- name )
|
|
|
|
dup logical-fonts get hash dup [ nip ] [ drop ] ifte ;
|
|
|
|
|
|
|
|
global [
|
|
|
|
{{
|
|
|
|
[[ "Monospaced" "/fonts/VeraMono.ttf" ]]
|
2005-02-11 12:45:24 -05:00
|
|
|
[[ "Serif" "/fonts/VeraSe.ttf" ]]
|
|
|
|
[[ "Sans Serif" "/fonts/Vera.ttf" ]]
|
2005-02-01 22:48:04 -05:00
|
|
|
}} logical-fonts set
|
|
|
|
] bind
|
|
|
|
|
2005-02-07 11:51:22 -05:00
|
|
|
: (lookup-font) ( [[ name ptsize ]] -- font )
|
2005-02-07 12:16:39 -05:00
|
|
|
unswons logical-font swons dup get dup alien? [
|
|
|
|
dup alien-address 0 = [
|
|
|
|
drop f
|
|
|
|
] when
|
2005-02-07 11:51:22 -05:00
|
|
|
] when ;
|
|
|
|
|
2005-02-01 22:48:04 -05:00
|
|
|
: lookup-font ( [[ name ptsize ]] -- font )
|
2005-01-23 21:00:52 -05:00
|
|
|
fonts get [
|
2005-02-07 11:51:22 -05:00
|
|
|
(lookup-font) [
|
2005-02-01 22:48:04 -05:00
|
|
|
nip
|
2005-01-23 21:00:52 -05:00
|
|
|
] [
|
2005-02-01 22:48:04 -05:00
|
|
|
[ uncons <font> dup ] keep set
|
2005-01-23 21:00:52 -05:00
|
|
|
] ifte*
|
|
|
|
] bind ;
|
|
|
|
|
|
|
|
: make-rect ( x y w h -- rect )
|
|
|
|
<rect>
|
|
|
|
[ set-rect-h ] keep
|
|
|
|
[ set-rect-w ] keep
|
|
|
|
[ set-rect-y ] keep
|
|
|
|
[ set-rect-x ] keep ;
|
|
|
|
|
|
|
|
: surface-rect ( x y surface -- rect )
|
|
|
|
dup surface-w swap surface-h make-rect ;
|
|
|
|
|
|
|
|
: draw-surface ( x y surface -- )
|
2005-02-01 22:48:04 -05:00
|
|
|
surface get SDL_UnlockSurface
|
2005-01-23 21:00:52 -05:00
|
|
|
[
|
|
|
|
[ surface-rect ] keep swap surface get 0 0
|
2005-02-01 22:48:04 -05:00
|
|
|
] keep surface-rect swap rot SDL_UpperBlit drop
|
|
|
|
surface get dup must-lock-surface? [
|
|
|
|
SDL_LockSurface
|
|
|
|
] when drop ;
|
2005-01-23 21:00:52 -05:00
|
|
|
|
2005-02-20 00:01:05 -05:00
|
|
|
: filter-nulls ( str -- str )
|
|
|
|
"\0" over str-contains? [
|
|
|
|
[ dup CHAR: \0 = [ drop CHAR: \s ] when ] str-map
|
|
|
|
] when ;
|
|
|
|
|
2005-02-02 22:00:46 -05:00
|
|
|
: draw-string ( x y font text fg -- width )
|
2005-02-20 00:01:05 -05:00
|
|
|
>r filter-nulls r> over str-length 0 = [
|
2005-02-02 22:00:46 -05:00
|
|
|
2drop 3drop 0
|
2005-01-23 21:00:52 -05:00
|
|
|
] [
|
2005-02-11 19:35:50 -05:00
|
|
|
>r >r lookup-font r> r>
|
2005-02-08 22:02:44 -05:00
|
|
|
TTF_RenderUNICODE_Blended
|
2005-01-23 21:00:52 -05:00
|
|
|
[ draw-surface ] keep
|
|
|
|
[ surface-w ] keep
|
|
|
|
SDL_FreeSurface
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: size-string ( font text -- w h )
|
2005-02-20 00:01:05 -05:00
|
|
|
>r lookup-font r> filter-nulls dup str-length 0 = [
|
2005-01-23 21:00:52 -05:00
|
|
|
drop TTF_FontHeight 0 swap
|
|
|
|
] [
|
2005-02-08 22:02:44 -05:00
|
|
|
<int-box> <int-box> [ TTF_SizeUNICODE drop ] 2keep
|
2005-01-23 21:00:52 -05:00
|
|
|
swap int-box-i swap int-box-i
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
global [ <namespace> fonts set ] bind
|