]> gitweb.factorcode.org Git - factor.git/commitdiff
added bitstream vera fonts, sdl console uses sdl-ttf for text rendering
authorSlava Pestov <slava@factorcode.org>
Mon, 24 Jan 2005 02:00:52 +0000 (02:00 +0000)
committerSlava Pestov <slava@factorcode.org>
Mon, 24 Jan 2005 02:00:52 +0000 (02:00 +0000)
fonts/VeraMoBI.ttf [new file with mode: 0644]
fonts/VeraMoBd.ttf [new file with mode: 0644]
fonts/VeraMoIt.ttf [new file with mode: 0644]
fonts/VeraMono.ttf [new file with mode: 0644]
library/sdl/sdl-ttf.factor
library/sdl/sdl-utils.factor
library/ui/console.factor

diff --git a/fonts/VeraMoBI.ttf b/fonts/VeraMoBI.ttf
new file mode 100644 (file)
index 0000000..8624542
Binary files /dev/null and b/fonts/VeraMoBI.ttf differ
diff --git a/fonts/VeraMoBd.ttf b/fonts/VeraMoBd.ttf
new file mode 100644 (file)
index 0000000..9be6547
Binary files /dev/null and b/fonts/VeraMoBd.ttf differ
diff --git a/fonts/VeraMoIt.ttf b/fonts/VeraMoIt.ttf
new file mode 100644 (file)
index 0000000..2404924
Binary files /dev/null and b/fonts/VeraMoIt.ttf differ
diff --git a/fonts/VeraMono.ttf b/fonts/VeraMono.ttf
new file mode 100644 (file)
index 0000000..139f0b4
Binary files /dev/null and b/fonts/VeraMono.ttf differ
index a84417bb815581769a2d4feda7b480fd2d21a5bc..693fae82ddc3bf4f4d56005fb9cf69b8a317d9f9 100644 (file)
@@ -78,6 +78,13 @@ USE: alien
 : TTF_FontFaceStyleName ( font -- n )
     "char*" "sdl-ttf" "TTF_FontFaceStyleName" [ "void*" ] alien-invoke ;
 
+BEGIN-STRUCT: int-box
+    FIELD: int i
+END-STRUCT
+
+: TTF_SizeText ( font text w h -- ? )
+    "bool" "sdl-ttf" "TTF_SizeText" [ "void*" "char*" "int-box*" "int-box*" ] alien-invoke ;
+
 : TTF_RenderText_Solid ( font text fg -- surface )
     "surface*" "sdl-ttf" "TTF_RenderText_Solid" [ "void*" "char*" "int" ] alien-invoke ;
 
index 2e7f1b406cf71465d464b9a7c6391944bb82ccb2..0f3b252445c0bbd0db414b62d4830a51bb8a4ae2 100644 (file)
@@ -2,7 +2,7 @@
 
 ! $Id$
 !
-! Copyright (C) 2004 Slava Pestov.
+! 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:
@@ -39,6 +39,9 @@ USE: prettyprint
 USE: sdl-event
 USE: sdl-gfx
 USE: sdl-video
+USE: streams
+USE: strings
+USE: sdl-ttf
 
 SYMBOL: surface
 SYMBOL: width
@@ -60,6 +63,14 @@ SYMBOL: surface
     swap 16 shift bitor
     swap 24 shift bitor ;
 
+: 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 ;
+
 : black 0 0 0 ;
 : white 255 255 255 ;
 : red 255 0 0 ;
@@ -98,3 +109,55 @@ SYMBOL: surface
     ] [
         drop
     ] ifte ;
+
+SYMBOL: fonts
+
+: null? ( alien -- ? )
+    dup [ alien-address 0 = ] when ;
+
+: <font> ( name ptsize -- font )
+    >r resource-path swap cat2 r> TTF_OpenFont ;
+
+: font ( name ptsize -- font )
+    fonts get [
+        2dup cons get [
+            2nip
+        ] [
+            2dup cons >r <font> dup r> set
+        ] 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 -- )
+    [
+        [ surface-rect ] keep swap surface get 0 0
+    ] keep surface-rect swap rot SDL_UpperBlit drop ;
+
+: draw-string ( x y font text fg bg -- width )
+    pick str-length 0 = [
+        2drop 2drop 2drop 0
+    ] [
+        TTF_RenderText_Shaded
+        [ draw-surface ] keep
+        [ surface-w ] keep
+        SDL_FreeSurface
+    ] ifte ;
+
+: size-string ( font text -- w h )
+    dup str-length 0 = [
+        drop TTF_FontHeight 0 swap
+    ] [
+        <int-box> <int-box> [ TTF_SizeText drop ] 2keep
+        swap int-box-i swap int-box-i
+    ] ifte ;
+
+global [ <namespace> fonts set ] bind
index 2c2ff83c1f5a34d0b608af8dd151d2786c43dfab..94188269631bd5e70c26bf6de422f681c4dda070 100644 (file)
@@ -63,6 +63,7 @@ USE: errors
 USE: line-editor
 USE: hashtables
 USE: lists
+USE: sdl-ttf
 
 #! A namespace holding console state.
 SYMBOL: console
@@ -78,13 +79,16 @@ SYMBOL: y
 SYMBOL: output-line
 #! A line editor object.
 SYMBOL: input-line
+#! A TTF_Font* value.
+SYMBOL: console-font
+#! Font height.
+SYMBOL: line-height
 
 #! The font size is hardcoded here.
-: line-height 8 ;
 : char-width 8 ;
 
 ! Scrolling
-: visible-lines ( -- n ) height get line-height /i ;
+: visible-lines ( -- n ) height get line-height get /i ;
 : total-lines ( -- n ) lines get vector-length ;
 : available-lines ( -- ) total-lines first-line get - ;
 
@@ -105,19 +109,20 @@ SYMBOL: input-line
     total-lines fix-first-line first-line set ;
 
 ! Rendering
-: background white rgb ;
-: foreground black rgb ;
-: cursor     red   rgb ;
+: background white ;
+: foreground black ;
+: cursor     red   ;
 
 : next-line ( -- )
-    0 x set  line-height y [ + ] change ;
+    0 x set  line-height get y [ + ] change ;
 
 : draw-line ( str -- )
-    [ surface get x get y get ] keep foreground stringColor
-    str-length char-width * x [ + ] change ;
+    >r x get y get console-font get r>
+    foreground make-color background make-color draw-string
+    x [ + ] change ;
 
 : clear-display ( -- )
-    surface get 0 0 width get height get background boxColor ;
+    surface get 0 0 width get height get background rgb boxColor ;
 
 : draw-lines ( -- )
     visible-lines available-lines min [
@@ -133,14 +138,17 @@ SYMBOL: input-line
     swap
     y get
     over 1 +
-    y get line-height +
-    cursor boxColor ;
+    y get line-height get +
+    cursor rgb boxColor ;
 
 : draw-current ( -- )
     output-line get sbuf>str draw-line ;
 
 : caret-x ( -- x )
-    x get input-line get [ caret get char-width * + ] bind ;
+    x get input-line get [
+        console-font get caret get line-text get str-head
+        size-string drop +
+    ] bind ;
 
 : draw-input ( -- )
     caret-x >r
@@ -341,7 +349,13 @@ M: alien handle-event ( event -- ? )
         drop t
     ] ifte ;
 
+: set-console-font ( font ptsize )
+    font dup console-font set
+    TTF_FontHeight line-height set ;
+
 : init-console ( -- )
+    TTF_Init
+    "/fonts/VeraMono.ttf" 14 set-console-font
     <event> event set
     0 first-line set
     80 <vector> lines set