diff --git a/examples/dejong.factor b/examples/dejong.factor index 03c0f58f17..6e668b0992 100644 --- a/examples/dejong.factor +++ b/examples/dejong.factor @@ -51,6 +51,17 @@ SYMBOL: d iterate-dejong 2dup scale-dejong rect> white rgb pixel ] times 2drop ; compiled +: event-loop ( event -- ) + dup SDL_WaitEvent [ + dup event-type SDL_QUIT = [ + drop + ] [ + event-loop + ] ifte + ] [ + drop + ] ifte ; compiled + : dejong ( -- ) ! Fiddle with these four values! 1.0 a set diff --git a/examples/mandel.factor b/examples/mandel.factor index b3084b0c99..b2bee6b648 100644 --- a/examples/mandel.factor +++ b/examples/mandel.factor @@ -30,6 +30,50 @@ USE: prettyprint USE: stdio USE: test +: f_ ( h s v i -- f ) >r swap rot >r 2dup r> 6 * r> - ; +: p ( v s x -- v p x ) >r dupd neg 1 + * r> ; +: q ( v s f -- q ) * neg 1 + * ; +: t_ ( v s f -- t_ ) neg 1 + * neg 1 + * ; + +: mod-cond ( p vector -- ) + #! Call p mod q'th entry of the vector of quotations, where + #! q is the length of the vector. The value q remains on the + #! stack. + [ dupd vector-length mod ] keep vector-nth call ; + +: hsv>rgb ( h s v -- r g b ) + pick 6 * >fixnum { + [ f_ t_ p swap ( v p t ) ] + [ f_ q p -rot ( q v p ) ] + [ f_ t_ p swapd ( p v t ) ] + [ f_ q p rot ( p q v ) ] + [ f_ t_ p swap rot ( t p v ) ] + [ f_ q p ( v p q ) ] + } mod-cond ; + +[ 1/2 1/2 1/2 ] [ 0 0 1/2 hsv>rgb ] unit-test + +[ 1/2 1/4 1/4 ] [ 0 1/2 1/2 hsv>rgb ] unit-test +[ 1/3 2/9 2/9 ] [ 0 1/3 1/3 hsv>rgb ] unit-test + +[ 24/125 1/5 4/25 ] [ 1/5 1/5 1/5 hsv>rgb ] unit-test +[ 29/180 1/6 5/36 ] [ 1/5 1/6 1/6 hsv>rgb ] unit-test + +[ 6/25 2/5 38/125 ] [ 2/5 2/5 2/5 hsv>rgb ] unit-test +[ 8/25 4/5 64/125 ] [ 2/5 3/5 4/5 hsv>rgb ] unit-test + +[ 6/25 48/125 3/5 ] [ 3/5 3/5 3/5 hsv>rgb ] unit-test +[ 0 0 0 ] [ 3/5 1/5 0 hsv>rgb ] unit-test + +[ 84/125 4/25 4/5 ] [ 4/5 4/5 4/5 hsv>rgb ] unit-test +[ 7/15 1/3 1/2 ] [ 4/5 1/3 1/2 hsv>rgb ] unit-test + +[ 5/6 5/36 5/6 ] [ 5/6 5/6 5/6 hsv>rgb ] unit-test +[ 1/6 0 1/6 ] [ 5/6 1 1/6 hsv>rgb ] unit-test + +[ 1 0 0 ] [ 1 1 1 hsv>rgb ] unit-test +[ 1/6 1/9 1/9 ] [ 1 1/3 1/6 hsv>rgb ] unit-test + : scale 255 * >fixnum ; : scale-rgb ( r g b a -- n ) @@ -88,6 +132,17 @@ SYMBOL: center ] ifte ] with-pixels ; compiled +: event-loop ( event -- ) + dup SDL_WaitEvent [ + dup event-type SDL_QUIT = [ + drop + ] [ + event-loop + ] ifte + ] [ + drop + ] ifte ; compiled + : mandel ( -- ) 1280 1024 0 SDL_HWSURFACE [ [ diff --git a/library/bootstrap/boot-stage2.factor b/library/bootstrap/boot-stage2.factor index b7eea21d53..571342adc1 100644 --- a/library/bootstrap/boot-stage2.factor +++ b/library/bootstrap/boot-stage2.factor @@ -121,7 +121,6 @@ IN: alien : add-library 3drop ; "/library/sdl/sdl-keyboard.factor" "/library/sdl/sdl-ttf.factor" "/library/sdl/sdl-utils.factor" - "/library/sdl/hsv.factor" "/library/bootstrap/image.factor" @@ -168,6 +167,7 @@ cpu "x86" = "mini" get not and [ "/library/compiler/x86/fixnum.factor" "/library/ui/shapes.factor" + "/library/ui/text.factor" "/library/ui/gadgets.factor" "/library/ui/paint.factor" "/library/ui/gestures.factor" diff --git a/library/compiler/alien.factor b/library/compiler/alien.factor index d02eaddac0..aa67da3dbe 100644 --- a/library/compiler/alien.factor +++ b/library/compiler/alien.factor @@ -15,6 +15,8 @@ unparser ; ! parameter, or a missing abi parameter indicates the cdecl ABI ! should be used, which is common on Unix. +: null? ( alien -- ? ) dup [ alien-address 0 = ] when ; + M: alien hashcode ( obj -- n ) alien-address >fixnum ; diff --git a/library/sdl/hsv.factor b/library/sdl/hsv.factor deleted file mode 100644 index 23a953d706..0000000000 --- a/library/sdl/hsv.factor +++ /dev/null @@ -1,35 +0,0 @@ -! Contains definition of the hsv>rgb word for converting -! Hue/Saturation/Value color values to RGB. - -! This thing is GPL, hsv->rgb is a translation to Common Lisp of a -! function found in color_sys.py in Python 2.3.0 - -! Translated to Factor by Slava Pestov. - -IN: sdl -USE: kernel -USE: lists -USE: math -USE: namespaces -USE: vectors - -: f_ ( h s v i -- f ) >r swap rot >r 2dup r> 6 * r> - ; -: p ( v s x -- v p x ) >r dupd neg 1 + * r> ; -: q ( v s f -- q ) * neg 1 + * ; -: t_ ( v s f -- t_ ) neg 1 + * neg 1 + * ; - -: mod-cond ( p vector -- ) - #! Call p mod q'th entry of the vector of quotations, where - #! q is the length of the vector. The value q remains on the - #! stack. - [ dupd vector-length mod ] keep vector-nth call ; - -: hsv>rgb ( h s v -- r g b ) - pick 6 * >fixnum { - [ f_ t_ p swap ( v p t ) ] - [ f_ q p -rot ( q v p ) ] - [ f_ t_ p swapd ( p v t ) ] - [ f_ q p rot ( p q v ) ] - [ f_ t_ p swap rot ( t p v ) ] - [ f_ q p ( v p q ) ] - } mod-cond ; diff --git a/library/sdl/sdl-event.factor b/library/sdl/sdl-event.factor index a51c4613d4..699b5785a2 100644 --- a/library/sdl/sdl-event.factor +++ b/library/sdl/sdl-event.factor @@ -1,34 +1,6 @@ -! :folding=indent:collapseFolds=1:sidekick.parser=none: - -! $Id$ -! ! Copyright (C) 2004, 2005 Slava Pestov. -! -! Redistribution and use in source and binary forms, with or without -! modification, are permitted provided that the following conditions are met: -! -! 1. Redistributions of source code must retain the above copyright notice, -! this list of conditions and the following disclaimer. -! -! 2. 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 ``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 -! DEVELOPERS AND 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. - -IN: sdl-event -USE: alien -USE: generic -USE: kernel +! See http://factor.sf.net/license.txt for BSD license. +IN: sdl USING: alien generic kernel ; BEGIN-ENUM: 0 ENUM: SDL_NOEVENT ! Unused (do not remove) diff --git a/library/sdl/sdl-gfx.factor b/library/sdl/sdl-gfx.factor index 822eb34b9f..a6b8a78180 100644 --- a/library/sdl/sdl-gfx.factor +++ b/library/sdl/sdl-gfx.factor @@ -1,32 +1,6 @@ -! :folding=indent:collapseFolds=1:sidekick.parser=none: - -! $Id$ -! -! Copyright (C) 2004 Slava Pestov. -! -! Redistribution and use in source and binary forms, with or without -! modification, are permitted provided that the following conditions are met: -! -! 1. Redistributions of source code must retain the above copyright notice, -! this list of conditions and the following disclaimer. -! -! 2. 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 ``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 -! DEVELOPERS AND 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. - -IN: sdl-gfx -USE: alien +! Copyright (C) 2004, 2005 Slava Pestov. +! See http://factor.sf.net/license.txt for BSD license. +IN: sdl USING: alien ; : pixelColor ( surface x y color -- ) "void" "sdl-gfx" "pixelColor" diff --git a/library/sdl/sdl-keyboard.factor b/library/sdl/sdl-keyboard.factor index 6de8f49c2f..ff1dd52497 100644 --- a/library/sdl/sdl-keyboard.factor +++ b/library/sdl/sdl-keyboard.factor @@ -1,8 +1,7 @@ ! Copyright (C) 2004, 2005 Slava Pestov. ! See http://factor.sf.net/license.txt for BSD license. -IN: sdl-keyboard -USING: alien lists sdl-keysym namespaces sdl-event kernel -math hashtables ; +IN: sdl +USING: alien lists namespaces kernel math hashtables ; : SDL_EnableUNICODE ( enable -- ) "int" "sdl" "SDL_EnableUNICODE" [ "int" ] alien-invoke ; diff --git a/library/sdl/sdl-keysym.factor b/library/sdl/sdl-keysym.factor index 7233ed2656..f21fb10fdf 100644 --- a/library/sdl/sdl-keysym.factor +++ b/library/sdl/sdl-keysym.factor @@ -1,32 +1,6 @@ -! :folding=indent:collapseFolds=1: - -! $Id$ -! -! Copyright (C) 2004 Slava Pestov. -! -! Redistribution and use in source and binary forms ; with or without -! modification ; are permitted provided that the following conditions are met: -! -! 1. Redistributions of source code must retain the above copyright notice ; -! this list of conditions and the following disclaimer. -! -! 2. 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 ``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 -! DEVELOPERS AND 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. - -IN: sdl-keyboard -USE: namespaces +! Copyright (C) 2004, 2005 Slava Pestov. +! See http://factor.sf.net/license.txt for BSD license. +IN: sdl USING: namespaces ; ! Here we smash left/right control/shift/alt for convinience. ! Later, something better needs to be done. diff --git a/library/sdl/sdl-ttf.factor b/library/sdl/sdl-ttf.factor index 6c2590247d..67cb7b48fc 100644 --- a/library/sdl/sdl-ttf.factor +++ b/library/sdl/sdl-ttf.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2004, 2005 Slava Pestov. ! See http://factor.sf.net/license.txt for BSD license. -IN: sdl-ttf +IN: sdl USE: alien : UNICODE_BOM_NATIVE HEX: FEFF ; diff --git a/library/sdl/sdl-utils.factor b/library/sdl/sdl-utils.factor index 4b10476ecd..122d75531a 100644 --- a/library/sdl/sdl-utils.factor +++ b/library/sdl/sdl-utils.factor @@ -1,27 +1,7 @@ ! Copyright (C) 2004, 2005 Slava Pestov. ! See http://factor.sf.net/license.txt for BSD license. IN: sdl -USING: alien math namespaces compiler words parser kernel errors -lists prettyprint sdl-event sdl-gfx sdl-keyboard sdl-video -streams strings sdl-ttf hashtables ; - -SYMBOL: surface -SYMBOL: width -SYMBOL: height -SYMBOL: bpp -SYMBOL: surface - -: init-screen ( width height bpp flags -- ) - >r 3dup bpp set height set width set r> - SDL_SetVideoMode surface set ; - -: with-screen ( width height bpp flags quot -- ) - #! Set up SDL graphics and call the quotation. - SDL_INIT_EVERYTHING SDL_Init drop TTF_Init - 1 SDL_EnableUNICODE drop - SDL_DEFAULT_REPEAT_DELAY SDL_DEFAULT_REPEAT_INTERVAL - SDL_EnableKeyRepeat drop - [ >r init-screen r> call SDL_Quit ] with-scope ; inline +USING: kernel lists math namespaces ; : rgb ( [ r g b ] -- n ) 3unlist @@ -45,9 +25,6 @@ SYMBOL: surface : green [ 0 255 0 ] ; : blue [ 0 0 255 ] ; -: clear-surface ( color -- ) - >r surface get 0 0 width get height get r> boxColor ; - : with-pixels ( quot -- ) width get [ height get [ @@ -66,80 +43,3 @@ SYMBOL: surface slip ] ifte SDL_Flip drop ] with-scope ; inline - -: event-loop ( event -- ) - dup SDL_WaitEvent [ - dup event-type SDL_QUIT = [ - drop - ] [ - event-loop - ] ifte - ] [ - drop - ] ifte ; - -SYMBOL: fonts - -: null? ( alien -- ? ) - dup [ alien-address 0 = ] when ; - -: ( name ptsize -- font ) - >r resource-path swap cat2 r> TTF_OpenFont ; - -SYMBOL: logical-fonts - -: logical-font ( name -- name ) - dup logical-fonts get hash dup [ nip ] [ drop ] ifte ; - -global [ - {{ - [[ "Monospaced" "/fonts/VeraMono.ttf" ]] - [[ "Serif" "/fonts/VeraSe.ttf" ]] - [[ "Sans Serif" "/fonts/Vera.ttf" ]] - }} logical-fonts set -] bind - -: (lookup-font) ( [[ name ptsize ]] -- font ) - unswons logical-font swons dup get dup alien? [ - dup alien-address 0 = [ - drop f - ] when - ] when ; - -: lookup-font ( [[ name ptsize ]] -- font ) - fonts get [ - (lookup-font) [ - nip - ] [ - [ uncons dup ] keep set - ] ifte* - ] bind ; - -: make-rect ( x y w h -- 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 -- ) - surface get SDL_UnlockSurface - [ - [ surface-rect ] keep swap surface get 0 0 - ] keep surface-rect swap rot SDL_UpperBlit drop - surface get dup must-lock-surface? [ - SDL_LockSurface - ] when drop ; - -: size-string ( font text -- w h ) - >r lookup-font r> filter-nulls dup string-length 0 = [ - drop TTF_FontHeight 0 swap - ] [ - [ TTF_SizeUNICODE drop ] 2keep - swap int-box-i swap int-box-i - ] ifte ; - -global [ fonts set ] bind diff --git a/library/sdl/sdl-video.factor b/library/sdl/sdl-video.factor index fcec0f5414..fe4d989552 100644 --- a/library/sdl/sdl-video.factor +++ b/library/sdl/sdl-video.factor @@ -1,7 +1,6 @@ ! Copyright (C) 2004, 2005 Slava Pestov. ! See http://factor.sf.net/license.txt for BSD license. -IN: sdl-video -USING: alien kernel math ; +IN: sdl USING: alien kernel math ; ! These are the currently supported flags for the SDL_surface ! Available for SDL_CreateRGBSurface() or SDL_SetVideoMode() diff --git a/library/sdl/sdl.factor b/library/sdl/sdl.factor index 19dcbd38b7..3d6756d79d 100644 --- a/library/sdl/sdl.factor +++ b/library/sdl/sdl.factor @@ -1,33 +1,6 @@ -! :folding=indent:collapseFolds=1: - -! $Id$ -! -! Copyright (C) 2004 Slava Pestov. -! -! Redistribution and use in source and binary forms, with or without -! modification, are permitted provided that the following conditions are met: -! -! 1. Redistributions of source code must retain the above copyright notice, -! this list of conditions and the following disclaimer. -! -! 2. 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 ``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 -! DEVELOPERS AND 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. - -IN: sdl -USE: alien -USE: compiler +! Copyright (C) 2004, 2005 Slava Pestov. +! See http://factor.sf.net/license.txt for BSD license. +IN: sdl USING: alien ; : SDL_INIT_TIMER HEX: 00000001 ; : SDL_INIT_AUDIO HEX: 00000010 ; diff --git a/library/test/hsv.factor b/library/test/hsv.factor deleted file mode 100644 index 9f404815b2..0000000000 --- a/library/test/hsv.factor +++ /dev/null @@ -1,26 +0,0 @@ -IN: scratchpad -USE: sdl -USE: test - -[ 1/2 1/2 1/2 ] [ 0 0 1/2 hsv>rgb ] unit-test - -[ 1/2 1/4 1/4 ] [ 0 1/2 1/2 hsv>rgb ] unit-test -[ 1/3 2/9 2/9 ] [ 0 1/3 1/3 hsv>rgb ] unit-test - -[ 24/125 1/5 4/25 ] [ 1/5 1/5 1/5 hsv>rgb ] unit-test -[ 29/180 1/6 5/36 ] [ 1/5 1/6 1/6 hsv>rgb ] unit-test - -[ 6/25 2/5 38/125 ] [ 2/5 2/5 2/5 hsv>rgb ] unit-test -[ 8/25 4/5 64/125 ] [ 2/5 3/5 4/5 hsv>rgb ] unit-test - -[ 6/25 48/125 3/5 ] [ 3/5 3/5 3/5 hsv>rgb ] unit-test -[ 0 0 0 ] [ 3/5 1/5 0 hsv>rgb ] unit-test - -[ 84/125 4/25 4/5 ] [ 4/5 4/5 4/5 hsv>rgb ] unit-test -[ 7/15 1/3 1/2 ] [ 4/5 1/3 1/2 hsv>rgb ] unit-test - -[ 5/6 5/36 5/6 ] [ 5/6 5/6 5/6 hsv>rgb ] unit-test -[ 1/6 0 1/6 ] [ 5/6 1 1/6 hsv>rgb ] unit-test - -[ 1 0 0 ] [ 1 1 1 hsv>rgb ] unit-test -[ 1/6 1/9 1/9 ] [ 1 1/3 1/6 hsv>rgb ] unit-test diff --git a/library/test/test.factor b/library/test/test.factor index 992934a8e1..7f05b89dd6 100644 --- a/library/test/test.factor +++ b/library/test/test.factor @@ -90,7 +90,6 @@ prettyprint stdio strings words vectors unparser ; "inference" "dataflow" "interpreter" - "hsv" "alien" "line-editor" "gadgets" diff --git a/library/ui/paint.factor b/library/ui/paint.factor index 041f697f18..c89de4f57a 100644 --- a/library/ui/paint.factor +++ b/library/ui/paint.factor @@ -103,34 +103,6 @@ M: plain-ellipse draw-shape ( ellipse -- ) >r surface get r> ellipse>screen bg rgb filledEllipseColor ; -! Strings are shapes too. This is somewhat of a hack and strings -! do not have x/y co-ordinates. -M: string shape-x drop 0 ; -M: string shape-y drop 0 ; -M: string shape-w - font get swap size-string ( h -) drop ; - -M: string shape-h ( text -- h ) - #! This is just the height of the current font. - drop font get lookup-font TTF_FontHeight ; - -: filter-nulls ( str -- str ) - "\0" over string-contains? [ - [ dup CHAR: \0 = [ drop CHAR: \s ] when ] string-map - ] when ; - -M: string draw-shape ( text -- ) - dup string-length 0 = [ - drop - ] [ - filter-nulls font get lookup-font swap - fg 3unlist make-color - bg 3unlist make-color - TTF_RenderUNICODE_Shaded - [ >r x get y get r> draw-surface ] keep - SDL_FreeSurface - ] ifte ; - ! Clipping SYMBOL: clip diff --git a/library/ui/text.factor b/library/ui/text.factor new file mode 100644 index 0000000000..c170c82b29 --- /dev/null +++ b/library/ui/text.factor @@ -0,0 +1,94 @@ +! Strings are shapes too. This is somewhat of a hack and strings +! do not have x/y co-ordinates. +IN: gadgets +USING: alien hashtables kernel lists namespaces sdl +sdl-ttf sdl-video streams strings ; + +SYMBOL: fonts + +: ( name ptsize -- font ) + >r resource-path swap cat2 r> TTF_OpenFont ; + +SYMBOL: logical-fonts + +: logical-font ( name -- name ) + dup logical-fonts get hash dup [ nip ] [ drop ] ifte ; + +global [ + {{ + [[ "Monospaced" "/fonts/VeraMono.ttf" ]] + [[ "Serif" "/fonts/VeraSe.ttf" ]] + [[ "Sans Serif" "/fonts/Vera.ttf" ]] + }} logical-fonts set +] bind + +: (lookup-font) ( [[ name ptsize ]] -- font ) + unswons logical-font swons dup get dup alien? [ + dup alien-address 0 = [ + drop f + ] when + ] when ; + +: lookup-font ( [[ name ptsize ]] -- font ) + fonts get [ + (lookup-font) [ + nip + ] [ + [ uncons dup ] keep set + ] ifte* + ] bind ; + +: make-rect ( x y w h -- 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 -- ) + surface get SDL_UnlockSurface + [ + [ surface-rect ] keep swap surface get 0 0 + ] keep surface-rect swap rot SDL_UpperBlit drop + surface get dup must-lock-surface? [ + SDL_LockSurface + ] when drop ; + +: size-string ( font text -- w h ) + >r lookup-font r> filter-nulls dup string-length 0 = [ + drop TTF_FontHeight 0 swap + ] [ + [ TTF_SizeUNICODE drop ] 2keep + swap int-box-i swap int-box-i + ] ifte ; + +global [ fonts set ] bind + +M: string shape-x drop 0 ; +M: string shape-y drop 0 ; +M: string shape-w + font get swap size-string ( h -) drop ; + +M: string shape-h ( text -- h ) + #! This is just the height of the current font. + drop font get lookup-font TTF_FontHeight ; + +: filter-nulls ( str -- str ) + "\0" over string-contains? [ + [ dup CHAR: \0 = [ drop CHAR: \s ] when ] string-map + ] when ; + +M: string draw-shape ( text -- ) + dup string-length 0 = [ + drop + ] [ + filter-nulls font get lookup-font swap + fg 3unlist make-color + bg 3unlist make-color + TTF_RenderUNICODE_Shaded + [ >r x get y get r> draw-surface ] keep + SDL_FreeSurface + ] ifte ;