Browse Source

Start progress on more advanced text layout

master
Macoy Madson 11 months ago
parent
commit
80e418acc5
  1. 9
      src/SDL.cake
  2. 78
      src/SDLFontAtlas.cake

9
src/SDL.cake

@ -517,7 +517,14 @@ Note that you can also build SDL manually. This can be useful if you are porting
(Log "SDL: Successfully built\n")
(return true))
(add-dependency-git-submodule clone-sdl2 "https://github.com/libsdl-org/SDL" "Dependencies/SDL")
(add-dependency-git-submodule
clone-sdl2
"https://github.com/libsdl-org/SDL"
"Dependencies/SDL"
;; They changed their build system.
;; TODO: Update to SDL3?
"2e9f5b5989c5140f47ce3ada603c1ae32d555f01")
(add-compile-time-hook-module pre-build build-sdl)
(export-and-evaluate

78
src/SDLFontAtlas.cake

@ -3,8 +3,9 @@
;; SDLFontAtlas.cake: Render font atlases generated by FreeType.cake. This is kept separate from
;; both libraries in order to not force a dependency on either.
(import "SDL.cake" "FreeType.cake" "Dictionary.cake"
"CHelpers.cake")
(import "SDL.cake" "Dictionary.cake"
"CHelpers.cake"
&with-decls "FreeType.cake")
(c-import &with-decls "<stdbool.h>")
@ -18,11 +19,12 @@
;; The min and max arguments can be null. Otherwise, they are expected to be initialized to
;; reasonable values, e.g. positive infinity and negative infinity, respectively. This way, you can
;; render several strings separately and keep the same running bounds.
;; When length-limit is 0, render until null terminator
(defun render-string-internal (renderer (addr SDL_Renderer) font (addr font-atlas) font-texture (addr SDL_Texture)
write-x (addr int) write-y (addr int)
min-x-out (addr int) min-y-out (addr int)
max-x-out (addr int) max-y-out (addr int)
str (addr (const char)))
str (addr (const char)) length-limit (unsigned int))
;; TODO: Support UTF-8
(var tab-width int 100)
(scope ;; Get tab width based on space advance
@ -39,6 +41,9 @@
(var start-x int (deref write-x))
(each-char-in-string-const str current-char
(when (and length-limit
(>= (- current-char str) length-limit))
(break))
(cond
((= (deref current-char) '\r')
(continue))
@ -107,7 +112,49 @@
(var write-y int y)
(render-string-internal renderer font font-texture (addr write-x) (addr write-y)
null null null null
str))
str 0))
(defun render-string-length (renderer (addr SDL_Renderer) font (addr font-atlas) font-texture (addr SDL_Texture)
x int y int
str (addr (const char)) length-limit (unsigned int))
(var write-x int x)
(var write-y int y)
(render-string-internal renderer font font-texture (addr write-x) (addr write-y)
null null null null
str length-limit))
(defstruct font-render-settings
renderer (addr SDL_Renderer)
font (addr font-atlas) font-texture (addr SDL_Texture)
start-x int
;; Relative to start-x
wrap-width-pixels int
r (unsigned char)
g (unsigned char)
b (unsigned char))
(defstruct font-layout-state
x int
y int)
;; If str-length-limit is 0, render string until null terminator
(defun layout-render-string (render-settings (addr font-render-settings)
state (addr font-layout-state)
str (addr (const char)) str-length-limit (unsigned int))
(SDL_SetTextureColorMod
(path render-settings > font-texture)
(path render-settings > r) (path render-settings > g) (path render-settings > b))
(render-string-internal (path render-settings > renderer)
(path render-settings > font)
(path render-settings > font-texture)
(addr (path state > x)) (addr (path state > y))
null null null null
str str-length-limit)
(ignore (when (>= (path state > x)
(+ (path render-settings > start-x)
(path render-settings > wrap-width-pixels)))
(set (path state > x) (path render-settings > start-x))
(set (path state > y) (+ (path state > y) (path render-settings > font > font-height))))))
;; Returns the minimum and maximum bounds of the rendered string relative to a (0, 0) baseline
;; Note that due to kerning, the width of 'l' + 'w' != 'lw', for example.
@ -124,7 +171,7 @@
(addr write-x) (addr write-y)
min-x-out min-y-out
max-x-out max-y-out
str))
str 0))
(defun make-font-atlas-and-texture (renderer (addr SDL_Renderer)
font-atlas-out (addr font-atlas) font-texture-out (addr (addr SDL_Texture))
@ -158,6 +205,27 @@
(return false)))
(return true))
(forward-declare (struct SDL_Texture))
(defstruct font-atlas-texture
atlas font-atlas
texture (addr SDL_Texture))
(defun font-atlas-texture-from-bundle-file (renderer (addr SDL_Renderer)
font-atlas-texture-out (addr font-atlas-texture)
font-data-start (addr (const (unsigned char)))
font-data-end (addr (const (unsigned char)))
device-dpi (unsigned int)
font-size-points (unsigned char)
&return bool)
(make-font-atlas-and-texture renderer
(addr (path font-atlas-texture-out > atlas))
(addr (path font-atlas-texture-out > texture))
font-data-start
(- font-data-end
font-data-start)
device-dpi
font-size-points))
(comptime-cond
('auto-test
(import "DataBundle.cake")

Loading…
Cancel
Save