@ -13,6 +13,9 @@
( bundle-file s-start-ubuntu-regular-font s-end-ubuntu-regular-font
( unsigned char ) "data/Fonts/Ubuntu-R.ttf" )
( bundle-file s-start-ubuntu-mono-font s-end-ubuntu-mono-font
( unsigned char ) "data/Fonts/UbuntuMono-R.ttf" )
( define-keybind s-quit-keybind ( array SDL_SCANCODE_Q keybind-modifier-flags-ctrl ) )
( var s-key-states sdl-key-states ( array 0 ) )
@ -27,12 +30,23 @@
( fprintf stderr ( token-splice format ) ) ) ) )
( return true ) )
;;
;; Text rendering
;;
( defun-local render-string ( renderer ( * SDL_Renderer ) font ( * font-atlas ) font-texture ( * SDL_Texture )
x int y int str ( * ( const char ) ) )
( var write-x int x )
( var write-y int y )
( var tab-width int 100 )
( scope ;; Get tab width based on space advance
( var space-key char ' ' )
( var glyph ( * glyph-entry ) ( dict-ptr-at ( path font > glyph-lookup-table ) space-key ) )
( when glyph
( set tab-width ( * 4 ( path glyph > advance-x ) ) ) ) )
( var line-height int 100 )
( each-char-in-string-const str current-char
( cond
( ( = ( deref current-char ) '\n ' )
@ -65,6 +79,35 @@
( addr source-rectangle ) ( addr destination-rectangle ) )
( set write-x ( + write-x ( path glyph > advance-x ) ) ) ) )
( defun-local make-font-atlas-and-texture ( renderer ( * SDL_Renderer )
font-atlas-out ( * font-atlas ) font-texture-out ( * ( * SDL_Texture ) )
font-data ( * ( const ( unsigned char ) ) ) font-data-size ( unsigned int )
font-size-points ( unsigned char )
&return bool )
( unless ( = 0 ( build-font-atlas font-data font-data-size
font-size-points
font-atlas-out ) )
( return false ) )
( scope
( var font-surface ( * SDL_Surface )
( SDL_CreateRGBSurfaceWithFormatFrom ( path font-atlas-out > pixel-buffer )
( path font-atlas-out > width )
( path font-atlas-out > height )
32 ;; bit depth of each pixel (RGBA)
( * ( path font-atlas-out > width ) 4 ) ;; pitch (width of a row in bytes)
SDL_PIXELFORMAT_RGBA32 ) )
( defer ( SDL_FreeSurface font-surface ) ) ;; We don't need this after making the texture
( set ( deref font-texture-out ) ( SDL_CreateTextureFromSurface renderer font-surface ) )
( unless ( deref font-texture-out )
( sdl-print-error )
( return false ) ) )
( return true ) )
;;
;; Main
;;
( defun main ( &return int )
( data-bundle-load-all-resources )
@ -85,28 +128,27 @@
( defer ( SDL_DestroyRenderer renderer ) )
( var ubuntu-regular-font-atlas font-atlas ( array 0 ) )
( var font-size-points ( unsigned char ) 12 )
( unless ( = 0 ( build-font-atlas s-start-ubuntu-regular-font
( - s-end-ubuntu-regular-font s-start-ubuntu-regular-font )
font-size-points
( addr ubuntu-regular-font-atlas ) ) )
( return 1 ) )
( defer ( free-font-atlas ( addr ubuntu-regular-font-atlas ) ) )
( var font-size-points ( unsigned char ) 12 )
( var ubuntu-regular-texture ( * SDL_Texture ) null )
( defer ( when ubuntu-regular-texture ( SDL_DestroyTexture ubuntu-regular-texture ) ) )
( scope
( var font-surface ( * SDL_Surface )
( SDL_CreateRGBSurfaceFrom ( field ubuntu-regular-font-atlas pixel-buffer )
( field ubuntu-regular-font-atlas width )
( field ubuntu-regular-font-atlas height )
32 ;; bit depth of each pixel (RGBA)
( * ( field ubuntu-regular-font-atlas width ) 4 ) ;; pitch (width of a row in bytes)
0xff000000 0x00ff0000 0x0000ff00 0x000000ff ) )
( defer ( SDL_FreeSurface font-surface ) ) ;; We don't need this after making the texture
( set ubuntu-regular-texture ( SDL_CreateTextureFromSurface renderer font-surface ) )
( unless ubuntu-regular-texture
( sdl-print-error )
( return 1 ) ) )
( unless ( make-font-atlas-and-texture renderer
( addr ubuntu-regular-font-atlas ) ( addr ubuntu-regular-texture )
s-start-ubuntu-regular-font
( - s-end-ubuntu-regular-font s-start-ubuntu-regular-font )
font-size-points )
( return 1 ) )
( var ubuntu-mono-font-atlas font-atlas ( array 0 ) )
( defer ( free-font-atlas ( addr ubuntu-mono-font-atlas ) ) )
( var ubuntu-mono-texture ( * SDL_Texture ) null )
( defer ( when ubuntu-mono-texture ( SDL_DestroyTexture ubuntu-mono-texture ) ) )
( unless ( make-font-atlas-and-texture renderer
( addr ubuntu-mono-font-atlas ) ( addr ubuntu-mono-texture )
s-start-ubuntu-mono-font
( - s-end-ubuntu-mono-font s-start-ubuntu-mono-font )
font-size-points )
( return 1 ) )
;; current-key-states is owned by SDL, but we own last-frame-states
( defer ( dynarray-free ( field s-key-states last-frame-states ) ) )
@ -123,7 +165,7 @@
( when ( keybind-tapped ( addr s-quit-keybind ) ( addr s-key-states ) )
( set exit-reason "Quit keybind pressed" ) )
( SDL_SetRenderDrawColor renderer 85 85 85 255 )
( SDL_SetRenderDrawColor renderer 17 17 17 255 )
( SDL_RenderClear renderer )
( scope
@ -142,6 +184,11 @@
( render-string
renderer ( addr ubuntu-regular-font-atlas ) ubuntu-regular-texture
200 400
"Hello, text renderer!" )
( render-string
renderer ( addr ubuntu-mono-font-atlas ) ubuntu-mono-texture
200 600
# " # result= FT_Set_Char_Size ( typeface, 0 , ( 16 * 64 ) , 300 , 300 ) ;
if ( ! ( ( result== FT_Err_Ok ) ) )