From fac15b9bbcf99eab95475381db23b364b342b304 Mon Sep 17 00:00:00 2001 From: Macoy Madson Date: Wed, 7 Sep 2022 19:29:58 -0400 Subject: [PATCH] Support 4k scaling, tweak slides * Add comparison with projectiles, make index modified clear * Use SDL scaling to let positioning be virtual when at >1440p resolutions * Create separate font atlases for 4K * Remove unused text-x --- data/DrivingCodeWithData.cakedata | 16 +- src/Presentation.cake | 257 ++++++++++++++++++------------ 2 files changed, 168 insertions(+), 105 deletions(-) diff --git a/data/DrivingCodeWithData.cakedata b/data/DrivingCodeWithData.cakedata index 126d79c..be48c6b 100644 --- a/data/DrivingCodeWithData.cakedata +++ b/data/DrivingCodeWithData.cakedata @@ -207,6 +207,14 @@ End") if word == \"IfEqual\": operations.append({IfEqual, ...}); else if word == \"MakeParticle\" + ... + +vs. + + if projectile.isFireball: + if projectile.x > 500: + projectile.velocityX = 5; + else if projectile.isIceStorm: ...") (slide-data :heading "We can do better!" :is-code true @@ -229,9 +237,9 @@ OperationReader readers = Operation operation; operation.type = reader.type; operation.arguments = - readArguments(reader.numArguments, file, index) + readArguments(reader.numArguments, file, &index) -function readArguments(numArguments, file, wordIndex): +function readArguments(numArguments, file, *wordIndex): arguments = [] each in range(numArguments): arguments.append(file[wordIndex]) @@ -296,11 +304,11 @@ system before knowing it is necessary?") (slide-data :heading "In conclusion" :body "Doing this is not hard, knowing when to do this, and to what extent, -is hard.") +can be hard.") (slide-data :heading "In conclusion" :body "Doing this is not hard, knowing when to do this, and to what extent, -is hard. +can be hard. That doesn't mean it shouldn't be done, but does mean it should be done carefully.") diff --git a/src/Presentation.cake b/src/Presentation.cake index fbdd84d..ca0718a 100644 --- a/src/Presentation.cake +++ b/src/Presentation.cake @@ -908,12 +908,6 @@ (SetProcessDpiAwarenessContext DPI_AWARENESS_CONTEXT_SYSTEM_AWARE))) (data-bundle-load-all-resources) - (var device-dpi (unsigned char) 144) - ;; 1 point = 1/72". Accurate if device-dpi is properly set - (var large-font-size-points (unsigned char) 38) - (var small-font-size-points (unsigned char) 20) - (var code-font-size-points (unsigned char) 20) - (SDL_GL_SetAttribute SDL_GL_CONTEXT_MAJOR_VERSION 4) (SDL_GL_SetAttribute SDL_GL_CONTEXT_MINOR_VERSION 6) (SDL_SetHint SDL_HINT_RENDER_VSYNC "1") @@ -930,43 +924,73 @@ (return 1)) (defer (SDL_DestroyRenderer renderer)) - (var ubuntu-regular-large-font-atlas font-atlas (array 0)) - (defer (free-font-atlas (addr ubuntu-regular-large-font-atlas))) - (var ubuntu-regular-large-texture (* SDL_Texture) null) - (defer (when ubuntu-regular-large-texture (SDL_DestroyTexture ubuntu-regular-large-texture))) - (unless (make-font-atlas-and-texture renderer - (addr ubuntu-regular-large-font-atlas) - (addr ubuntu-regular-large-texture) - s-start-ubuntu-regular-font - (- s-end-ubuntu-regular-font s-start-ubuntu-regular-font) - device-dpi - large-font-size-points) - (return 1)) + ;; + ;; Fonts + ;; - (var ubuntu-regular-small-font-atlas font-atlas (array 0)) - (defer (free-font-atlas (addr ubuntu-regular-small-font-atlas))) - (var ubuntu-regular-small-texture (* SDL_Texture) null) - (defer (when ubuntu-regular-small-texture (SDL_DestroyTexture ubuntu-regular-small-texture))) - (unless (make-font-atlas-and-texture renderer - (addr ubuntu-regular-small-font-atlas) - (addr ubuntu-regular-small-texture) - s-start-ubuntu-regular-font - (- s-end-ubuntu-regular-font s-start-ubuntu-regular-font) - device-dpi - small-font-size-points) - (return 1)) + (var device-dpi (unsigned char) 144) + ;; 1 point = 1/72". Accurate if device-dpi is properly set + + (defstruct font-atlas-texture + atlas font-atlas + texture (* SDL_Texture) + + ;; Only needed before it is built + start-file-data (* (unsigned char)) + end-file-data (* (unsigned char)) + font-size-points (unsigned char)) + (var font-atlases ([] font-atlas-texture) + (array + (array ;; Heading font, 1080p + (array 0) null + s-start-ubuntu-regular-font s-end-ubuntu-regular-font + 38) + (array ;; Body font, 1080p + (array 0) null + s-start-ubuntu-regular-font s-end-ubuntu-regular-font + 20) + (array ;; Body code font, 1080p + (array 0) null + s-start-ubuntu-mono-font s-end-ubuntu-mono-font + 20) + + (array ;; Heading font, 4k + (array 0) null + s-start-ubuntu-regular-font s-end-ubuntu-regular-font + 58) + (array ;; Body font, 4k + (array 0) null + s-start-ubuntu-regular-font s-end-ubuntu-regular-font + 34) + (array ;; Body code font, 4k + (array 0) null + s-start-ubuntu-mono-font s-end-ubuntu-mono-font + 34))) + (var heading-font-index-1080p (const int) 0) + (var body-font-index-1080p (const int) 1) + (var body-code-font-index-1080p (const int) 2) + (var heading-font-index-4k (const int) 3) + (var body-font-index-4k (const int) 4) + (var body-code-font-index-4k (const int) 5) + (var heading-font-index int heading-font-index-1080p) + (var body-font-index int body-font-index-1080p) + (var body-code-font-index int body-code-font-index-1080p) - (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) + (defer + (each-item-addr-in-array font-atlases i font (* font-atlas-texture) + (free-font-atlas (addr (path font > atlas))) + (when (path font > texture) + (SDL_DestroyTexture (path font > texture))))) + + (each-item-addr-in-array font-atlases i font (* font-atlas-texture) + (unless (make-font-atlas-and-texture renderer + (addr (path font > atlas)) + (addr (path font > texture)) + (path font > start-file-data) + (- (path font > end-file-data) (path font > start-file-data)) device-dpi - code-font-size-points) - (return 1)) + (path font > font-size-points)) + (return 1))) (unless (power-system-initialize) (return 1)) @@ -1065,6 +1089,7 @@ (var last-power-cycle-ticks Uint64 0) (var enable-fullscreen bool false) + (var upscale-to-4k bool false) (var exit-reason (* (const char)) null) (while true @@ -1123,12 +1148,36 @@ (when (keybind-tapped (addr s-toggle-debug-overlay-keybind) (addr s-key-states)) (set s-enable-debug-overlay (not s-enable-debug-overlay))) - (var window-width int 0) - (var window-height int 0) - (SDL_GetWindowSize window (addr window-width) (addr window-height)) + (var true-window-width int 0) + (var true-window-height int 0) + (SDL_GetWindowSize window (addr true-window-width) (addr true-window-height)) + + (if (> true-window-height 1440) + (scope + (unless upscale-to-4k + (preslog "Now upscaling 2x\n")) + (set heading-font-index heading-font-index-4k) + (set body-font-index body-font-index-4k) + (set body-code-font-index body-code-font-index-4k) + (set upscale-to-4k true)) + (scope + (when upscale-to-4k + (preslog "No longer upscaling\n")) + (set heading-font-index heading-font-index-1080p) + (set body-font-index body-font-index-1080p) + (set body-code-font-index body-code-font-index-1080p) + (set upscale-to-4k false))) + + (var virtual-window-width int true-window-width) + (var virtual-window-height int true-window-height) - (set (field g-wizard-hero x) (type-cast (* window-width 0.08f) int)) - (set (field g-wizard-hero y) (- window-height (/ ground-height 2) wizard-ground-height)) + ;; We always master to 1080p in "game space". Text is all true window relative + (when upscale-to-4k + (set virtual-window-width 1920) + (set virtual-window-height 1080)) + + (set (field g-wizard-hero x) (type-cast (* virtual-window-width 0.08f) int)) + (set (field g-wizard-hero y) (- virtual-window-height (/ ground-height 2) wizard-ground-height)) (when (and (!= (field g-wizard-hero anim) (addr anim-wizard-attack)) @@ -1197,10 +1246,10 @@ atlas (* font-atlas) texture (* SDL_Texture)) (var atlases-to-draw ([] atlas-set) - (array (array (addr ubuntu-regular-large-font-atlas) - ubuntu-regular-large-texture) - (array (addr ubuntu-mono-font-atlas) - ubuntu-mono-texture))) + (array (array (addr (field (at heading-font-index font-atlases) atlas)) + (field (at heading-font-index font-atlases) texture)) + (array (addr (field (at body-code-font-index font-atlases) atlas)) + (field (at body-code-font-index font-atlases) texture)))) (each-in-array atlases-to-draw i (var source-rectangle SDL_Rect (array 0 0 @@ -1215,32 +1264,30 @@ (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-time-seconds float (/ (- (SDL_GetPerformanceCounter) slide-start-ticks) + (type-cast (SDL_GetPerformanceFrequency) float))) (when should-render-ground - (var source-rectangle SDL_Rect - (array 0 0 - ground-width - ground-height)) - (var destination-rectangle SDL_Rect - (array 0 (- window-height ground-height) - window-width - ground-height)) - (unless (= 0 (SDL_RenderCopy renderer ground-texture - (addr source-rectangle) (addr destination-rectangle))) - (sdl-print-error) - (set exit-reason "SDL failed to render ground"))) + (when upscale-to-4k + (SDL_RenderSetScale renderer 2.f 2.f)) + (defer (SDL_RenderSetScale renderer 1.f 1.f)) + (var source-rectangle SDL_Rect + (array 0 0 + ground-width + ground-height)) + (var destination-rectangle SDL_Rect + (array 0 (- virtual-window-height ground-height) + virtual-window-width + ground-height)) + (unless (= 0 (SDL_RenderCopy renderer ground-texture + (addr source-rectangle) (addr destination-rectangle))) + (sdl-print-error) + (set exit-reason "SDL failed to render ground"))) (render-string - renderer (addr ubuntu-regular-large-font-atlas) ubuntu-regular-large-texture - (+ text-x (/ window-width 4.5f)) (/ window-height 8) + renderer (addr (field (at heading-font-index font-atlases) atlas)) + (field (at heading-font-index font-atlases) texture) + (/ true-window-width 4.5f) (/ true-window-height 8) (path slide > heading)) (if (path slide > is-code) @@ -1254,39 +1301,46 @@ (set power-update-str (path current-power > on-update)) (break)) (render-string - renderer (addr ubuntu-mono-font-atlas) ubuntu-mono-texture - (+ text-x (/ window-width 4)) (/ window-height 4) + renderer (addr (field (at body-code-font-index font-atlases) atlas)) + (field (at body-code-font-index font-atlases) texture) + (/ true-window-width 4) (/ true-window-height 4) power-update-str)) (render-string - renderer (addr ubuntu-mono-font-atlas) ubuntu-mono-texture - (+ text-x (/ window-width 4)) (/ window-height 4) + renderer (addr (field (at body-code-font-index font-atlases) atlas)) + (field (at body-code-font-index font-atlases) texture) + (/ true-window-width 4) (/ true-window-height 4) (path slide > body)))) (render-string - renderer (addr ubuntu-regular-small-font-atlas) ubuntu-regular-small-texture - (+ text-x (/ window-width 4)) (/ window-height 4) + renderer (addr (field (at body-font-index font-atlases) atlas)) + (field (at body-font-index font-atlases) texture) + (/ true-window-width 4) (/ true-window-height 4) (path slide > body))) - (when should-render-wizard - (unless (field g-wizard-hero animation-start-ticks) - (set (field g-wizard-hero animation-start-ticks) (SDL_GetPerformanceCounter))) - (render-animation - renderer spritesheet-wizard (addr (field g-wizard-hero anim)) - (field g-wizard-hero animation-start-ticks) c-animation-frame-rate - (field g-wizard-hero x) (field g-wizard-hero y))) - - (set (field g-boar-enemy x) (type-cast (* window-width 0.70f) int)) - (set (field g-boar-enemy y) (- window-height (/ ground-height 2) boar-ground-height)) - - (when should-render-boar - (unless (field g-boar-enemy animation-start-ticks) - (set (field g-boar-enemy animation-start-ticks) (SDL_GetPerformanceCounter))) - (render-animation - renderer spritesheet-boar (addr (field g-boar-enemy anim)) - (field g-boar-enemy animation-start-ticks) c-animation-frame-rate - (field g-boar-enemy x) - (field g-boar-enemy y))) - - (update-effects renderer (addr g-boar-enemy)) + (scope + (when upscale-to-4k + (SDL_RenderSetScale renderer 2.f 2.f)) + (defer (SDL_RenderSetScale renderer 1.f 1.f)) + (when should-render-wizard + (unless (field g-wizard-hero animation-start-ticks) + (set (field g-wizard-hero animation-start-ticks) (SDL_GetPerformanceCounter))) + (render-animation + renderer spritesheet-wizard (addr (field g-wizard-hero anim)) + (field g-wizard-hero animation-start-ticks) c-animation-frame-rate + (field g-wizard-hero x) (field g-wizard-hero y))) + + (set (field g-boar-enemy x) (type-cast (* virtual-window-width 0.70f) int)) + (set (field g-boar-enemy y) (- virtual-window-height (/ ground-height 2) boar-ground-height)) + + (when should-render-boar + (unless (field g-boar-enemy animation-start-ticks) + (set (field g-boar-enemy animation-start-ticks) (SDL_GetPerformanceCounter))) + (render-animation + renderer spritesheet-boar (addr (field g-boar-enemy anim)) + (field g-boar-enemy animation-start-ticks) c-animation-frame-rate + (field g-boar-enemy x) + (field g-boar-enemy y))) + + (update-effects renderer (addr g-boar-enemy))) (when s-enable-debug-overlay (var buffer ([] 256 char) (array 0)) @@ -1295,11 +1349,12 @@ (SDL_GetMouseState (addr mouse-x) (addr mouse-y)) (snprintf buffer (- (array-size buffer) 1) "Mouse: %4d %4d Percent of window: %.2f %.2f Wizard relative: %4d %4d" mouse-x mouse-y - (/ mouse-x (type-cast window-width float)) (/ mouse-y (type-cast window-height float)) + (/ mouse-x (type-cast true-window-width float)) (/ mouse-y (type-cast true-window-height float)) (- mouse-x (field g-wizard-hero x)) (- mouse-y (field g-wizard-hero y))) (render-string - renderer (addr ubuntu-mono-font-atlas) ubuntu-mono-texture - 10 (- window-height 20) + renderer (addr (field (at body-code-font-index font-atlases) atlas)) + (field (at body-code-font-index font-atlases) texture) + 10 (- true-window-height 20) buffer)) ;; #"# ;; result= FT_Set_Char_Size(typeface, 0, (16 * 64), 300, 300);