Browse Source

Add slide data, slide controls

* Bind various keys for advancing slides
* Disable debug font atlas rendering
* Add Handmade-Math dependency for no reason. I only wanted Math for
interpolate. Oh well.
* Add presentation data structure and an example presentation
* Tweaked text placement
master
Macoy Madson 3 months ago
parent
commit
d85590fc19
  1. 3
      .gitmodules
  2. 1
      Dependencies/Handmade-Math
  3. 6
      data/DrivingCodeWithData.cakedata
  4. 145
      src/Presentation.cake

3
.gitmodules

@ -13,3 +13,6 @@
[submodule "Dependencies/FreeType"]
path = Dependencies/FreeType
url = https://gitlab.freedesktop.org/freetype/freetype.git
[submodule "Dependencies/Handmade-Math"]
path = Dependencies/Handmade-Math
url = https://github.com/HandmadeMath/Handmade-Math

1
Dependencies/Handmade-Math

@ -0,0 +1 @@
Subproject commit ba5405ac0fb3f899845fe47d80364e4d1fd5c866

6
data/DrivingCodeWithData.cakedata

@ -0,0 +1,6 @@
(presentation-data
:slides
(array
(slide-data :heading "Driving code with data" :body "Macoy Madson")
(slide-data :heading "Setting the stage")
(slide-data :heading "Slide 2" :body "More blah")))

145
src/Presentation.cake

@ -3,13 +3,17 @@
(add-cakelisp-search-directory "Dependencies/cakelisp/runtime")
(add-cakelisp-search-directory "src")
(import "FontAtlas.cake"
(import
"FontAtlas.cake"
;; GameLib
"SDL.cake" "DynamicArray.cake" "Dictionary.cake" "DataBundle.cake")
"Introspection.cake" "SDL.cake" "DynamicArray.cake" "Dictionary.cake" "DataBundle.cake"
"Math.cake")
(c-import "<stdio.h>"
&with-decls "<stdint.h>")
(var s-draw-atlases bool false)
(bundle-file s-start-ubuntu-regular-font s-end-ubuntu-regular-font
(unsigned char) "data/Fonts/Ubuntu-R.ttf")
@ -17,6 +21,13 @@
(unsigned char) "data/Fonts/UbuntuMono-R.ttf")
(define-keybind s-quit-keybind (array SDL_SCANCODE_Q keybind-modifier-flags-ctrl))
(define-keybind s-next-slide-keybind (array SDL_SCANCODE_RIGHT)
(array SDL_SCANCODE_SPACE)
(array SDL_SCANCODE_RETURN)
(array SDL_SCANCODE_PAGEDOWN))
(define-keybind s-previous-slide-keybind (array SDL_SCANCODE_LEFT)
(array SDL_SCANCODE_BACKSPACE)
(array SDL_SCANCODE_PAGEUP))
(var s-key-states sdl-key-states (array 0))
@ -30,6 +41,20 @@
(fprintf stderr (token-splice format)))))
(return true))
;;
;; Slides
;;
(def-introspect-struct slide-data
heading ([] 256 char)
body ([] 1024 char))
(def-introspect-struct presentation-data
slides ([] 256 slide-data) ('array-allow-subset))
(bundle-file s-start-presentation s-end-presentation
(const char) "data/DrivingCodeWithData.cakedata")
;;
;; Text rendering
;;
@ -166,6 +191,28 @@
;; current-key-states is owned by SDL, but we own last-frame-states
(defer (dynarray-free (field s-key-states last-frame-states)))
(var presentation presentation-data (array 0))
(scope
;; TODO: Take string size rather than requiring null terminator
(var presentation-string-length int (- s-end-presentation s-start-presentation))
(var-cast-to presentation-string-null-terminated (* char)
(malloc (+ 1 presentation-string-length)))
(defer (free presentation-string-null-terminated))
(strncpy presentation-string-null-terminated s-start-presentation presentation-string-length)
(set (at presentation-string-length presentation-string-null-terminated) 0)
(unless (read-introspect-struct-s-expr
presentation-data--metadata
(addr presentation)
presentation-string-null-terminated
malloc
null)
(preslog "Failed to read presentation data\n")
(return false)))
(defer (free-introspect-struct-fields presentation-data--metadata (addr presentation) free))
(var current-slide int 0)
(var slide-start-ticks Uint64 0)
(var exit-reason (* (const char)) null)
(while true
(var event SDL_Event)
@ -173,53 +220,81 @@
(when (= (field event type) SDL_QUIT)
(set exit-reason "Window event")))
(var window-width int 0)
(var window-height int 0)
(SDL_GetWindowSize window (addr window-width) (addr window-height))
(var num-keys int 0)
(set (field s-key-states this-frame-states) (SDL_GetKeyboardState (addr num-keys)))
(when (keybind-tapped (addr s-quit-keybind) (addr s-key-states))
(set exit-reason "Quit keybind pressed"))
(when (keybind-tapped (addr s-next-slide-keybind) (addr s-key-states))
(set slide-start-ticks (SDL_GetPerformanceCounter))
(incr current-slide)
(unless (at 0 (field (at current-slide (field presentation slides)) heading))
(set current-slide (- current-slide 1))))
(when (keybind-tapped (addr s-previous-slide-keybind) (addr s-key-states))
(set slide-start-ticks (SDL_GetPerformanceCounter))
(decr current-slide)
(when (< current-slide 0)
(set current-slide 0)))
(SDL_SetRenderDrawColor renderer 17 17 17 255)
(SDL_RenderClear renderer)
(defstruct atlas-set
atlas (* font-atlas)
texture (* SDL_Texture))
(var atlases-to-draw ([] atlas-set)
(array (array (addr ubuntu-regular-font-atlas)
ubuntu-regular-texture)
(array (addr ubuntu-mono-font-atlas)
ubuntu-mono-texture)))
(each-in-array atlases-to-draw i
(var source-rectangle SDL_Rect
(array 0 0
(path (at i atlases-to-draw) . atlas > width)
(path (at i atlases-to-draw) . atlas > height)))
(var destination-rectangle SDL_Rect
(array (* (+ 50 (path (at i atlases-to-draw) . atlas > width)) i) 0
(path (at i atlases-to-draw) . atlas > width)
(path (at i atlases-to-draw) . atlas > height)))
(unless (= 0 (SDL_RenderCopy renderer (path (at i atlases-to-draw) . texture)
(addr source-rectangle) (addr destination-rectangle)))
(sdl-print-error)
(set exit-reason "SDL failed to render font atlas")))
(when s-draw-atlases
(defstruct atlas-set
atlas (* font-atlas)
texture (* SDL_Texture))
(var atlases-to-draw ([] atlas-set)
(array (array (addr ubuntu-regular-font-atlas)
ubuntu-regular-texture)
(array (addr ubuntu-mono-font-atlas)
ubuntu-mono-texture)))
(each-in-array atlases-to-draw i
(var source-rectangle SDL_Rect
(array 0 0
(path (at i atlases-to-draw) . atlas > width)
(path (at i atlases-to-draw) . atlas > height)))
(var destination-rectangle SDL_Rect
(array (* (+ 50 (path (at i atlases-to-draw) . atlas > width)) i) 0
(path (at i atlases-to-draw) . atlas > width)
(path (at i atlases-to-draw) . atlas > height)))
(unless (= 0 (SDL_RenderCopy renderer (path (at i atlases-to-draw) . texture)
(addr source-rectangle) (addr destination-rectangle)))
(sdl-print-error)
(set exit-reason "SDL failed to render font atlas"))))
;; (var slide-time-seconds float (/ (- (SDL_GetPerformanceCounter) slide-start-ticks)
;; (type-cast (SDL_GetPerformanceFrequency) float)))
;; (var text-x int (- 0 window-width))
;; (if (< slide-time-seconds 0.1f)
;; (set text-x (interpolate-range -400 200 0.f 0.1f slide-time-seconds))
;; (set text-x 200))
(var text-x int 0)
(var slide (* slide-data) (addr (at current-slide (field presentation slides))))
(render-string
renderer (addr ubuntu-regular-font-atlas) ubuntu-regular-texture
200 400
"Hello, text renderer!\nWe have a problem")
(+ text-x (/ window-width 5)) (/ window-height 8)
(path slide > heading))
(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)))
{
fprintf(stderr, "error: encountered error %d while %s\n", result, "setting character size");
FT_Done_Face(typeface);
FT_Done_FreeType(freetypeLibrary);
return 1;
}#"#)
(+ text-x (/ window-width 5)) (/ window-height 3)
(path slide > body))
;; #"#
;; result= FT_Set_Char_Size(typeface, 0, (16 * 64), 300, 300);
;; if (!((result== FT_Err_Ok)))
;; {
;; fprintf(stderr, "error: encountered error %d while %s\n", result, "setting character size");
;; FT_Done_Face(typeface);
;; FT_Done_FreeType(freetypeLibrary);
;; return 1;
;; }#"#)
(SDL_RenderPresent renderer)
(SDL_UpdateWindowSurface window)

Loading…
Cancel
Save