Browse Source

Move animations into data and function, do slides

master
Macoy Madson 1 year ago
parent
commit
e927620502
  1. 47
      data/DrivingCodeWithData.cakedata
  2. 132
      src/Presentation.cake

47
data/DrivingCodeWithData.cakedata

@ -3,4 +3,49 @@
(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")))
(slide-data :heading "First pass"
:body "if shouldShootFireball():
{
makeFireball();
}
for each fireball:
{
updateFireballs();
}")
(slide-data :heading "What about ice storms?" :body "if shouldShootFireball():
{
makeFireball();
}
else if shouldShootIceStore():
{
makeIceStorm();
}
for each fireball in fireballs:
{
updateFireball(fireball);
}
for each iceStorm in iceStorms:
{
updateIceStorm(iceStorm);
}")
(slide-data :heading "We want" :body "slime sling
laser blast
heat seeker
...")
(slide-data :heading "We have a problem")
(slide-data :heading "Second pass: sharing code"
:body "for each projectile in projectiles:
{
updateProjectile(projectile);
}")
(slide-data :heading "It works, but..."
:body "The code is a mess:
- Special cases everywhere
- No easy way to add new types or classes of things
- No clear centralization of functionality
- Complexity spreads")
(slide-data :heading "What are computers good at?")
(slide-data :heading "Storing and manipulating data")
(slide-data :heading "Describe the behavior in data")))

132
src/Presentation.cake

@ -66,6 +66,73 @@
(bundle-file s-start-boar-spritesheet s-end-boar-spritesheet
(unsigned char) "/home/macoy/Documents/boar.bmp")
(defstruct-local spritesheet
texture (* SDL_Texture)
width uint16_t
height uint16_t
frame-width uint16_t
frame-height uint16_t
scale-factor (unsigned char))
(defstruct-local animation
start-frame-index (unsigned char)
end-frame-index (unsigned char)
flip SDL_RendererFlip)
(var spritesheet-wizard spritesheet (array null
966 636
6 6
2))
(var anim-wizard-idle animation (array 0 5 SDL_FLIP_HORIZONTAL))
(var spritesheet-boar spritesheet (array null
1434 1068
6 6
2))
(var anim-boar-idle animation (array 0 6 SDL_FLIP_HORIZONTAL))
(var anim-boar-damage animation (array (+ (* 3 6) 3) (+ (* 3 6) 5) SDL_FLIP_HORIZONTAL))
(var c-animation-frame-rate (const float) 0.1f)
(defun-local render-animation (renderer (* SDL_Renderer)
sprite (* spritesheet) anim (* animation)
animation-start-ticks Uint64
frame-rate float
x int y int)
(var num-frames int (- (+ 1 (path anim > end-frame-index)) ;; TODO: Off by one?
(path anim > start-frame-index)))
(var loop-rate float (* frame-rate num-frames))
(var sprite-width int (/ (path sprite > width) (path sprite > frame-width)))
(var sprite-height int (/ (path sprite > height) (path sprite > frame-height)))
(var animation-time-seconds float
(/ (- (SDL_GetPerformanceCounter) animation-start-ticks)
(type-cast (SDL_GetPerformanceFrequency) float)))
(var-cast-to current-frame int
(interpolate-range
0.f (type-cast num-frames float)
0.f loop-rate
(- animation-time-seconds
(* loop-rate (truncf (/ animation-time-seconds loop-rate))))))
(var frame-x int (mod (+ current-frame (path anim > start-frame-index))
(path sprite > frame-width)))
(var frame-y int (/ (+ current-frame (path anim > start-frame-index))
(path sprite > frame-width)))
(var source-rectangle SDL_Rect
(array (* frame-x sprite-width) (* frame-y sprite-height)
sprite-width
sprite-height))
(var destination-rectangle SDL_Rect
(array x y
(* (path sprite > scale-factor) sprite-width)
(* (path sprite > scale-factor) sprite-height)))
(unless (= 0 (SDL_RenderCopyEx renderer (path sprite > texture)
(addr source-rectangle) (addr destination-rectangle)
0.f null ;; no rotation
(path anim > flip)))
(sdl-print-error)))
;;
;; Text rendering
;;
@ -217,22 +284,21 @@
code-font-size-points)
(return 1))
(var wizard-width int 161)
(var wizard-height int 106)
(var wizard-texture (* SDL_Texture)
(sdl-texture-from-bmp-data renderer s-start-wizard-spritesheet s-end-wizard-spritesheet))
(unless wizard-texture
(return 1))
(defer (SDL_DestroyTexture wizard-texture))
(var boar-width int 239)
(var boar-height int 178)
(var boar-texture (* SDL_Texture)
(sdl-texture-from-bmp-data renderer s-start-boar-spritesheet s-end-boar-spritesheet))
(unless boar-texture
(return 1))
(defer (SDL_DestroyTexture boar-texture))
(set (field spritesheet-wizard texture) wizard-texture)
(set (field spritesheet-boar texture) boar-texture)
;; current-key-states is owned by SDL, but we own last-frame-states
(defer (dynarray-free (field s-key-states last-frame-states)))
@ -355,50 +421,20 @@
;; return 1;
;; }#"#)
(var animation-time-seconds float
(/ (- (SDL_GetPerformanceCounter) animation-start-ticks)
(type-cast (SDL_GetPerformanceFrequency) float)))
(var loop-rate float 0.7f)
(var num-frames int 6)
(var-cast-to current-frame int
(interpolate-range
0.f (type-cast num-frames float)
0.f loop-rate
(- animation-time-seconds
(* loop-rate (truncf (/ animation-time-seconds loop-rate))))))
(when (> current-slide-index 0)
(var source-rectangle SDL_Rect
(array (* current-frame wizard-width) 0
wizard-width
wizard-height))
(var destination-rectangle SDL_Rect
(array (type-cast (* window-width 0.08f) int)
(type-cast (* window-height 0.77f) int)
(* 2 wizard-width)
(* 2 wizard-height)))
(unless (= 0 (SDL_RenderCopyEx renderer wizard-texture
(addr source-rectangle) (addr destination-rectangle)
0.f null ;; no rotation
SDL_FLIP_HORIZONTAL))
(sdl-print-error)
(set exit-reason "SDL failed to render wizard")))
(when (> current-slide-index 0)
(var source-rectangle SDL_Rect
(array (* current-frame boar-width) 0
boar-width
boar-height))
(var destination-rectangle SDL_Rect
(array (type-cast (* window-width 0.70f) int)
(type-cast (* window-height 0.63f) int)
(* 2 boar-width)
(* 2 boar-height)))
(unless (= 0 (SDL_RenderCopyEx renderer boar-texture
(addr source-rectangle) (addr destination-rectangle)
0.f null ;; no rotation
SDL_FLIP_HORIZONTAL))
(sdl-print-error)
(set exit-reason "SDL failed to render boar")))
(when (>= current-slide-index 1)
(render-animation
renderer (addr spritesheet-wizard) (addr anim-wizard-idle)
animation-start-ticks c-animation-frame-rate
(type-cast (* window-width 0.08f) int)
(type-cast (* window-height 0.77f) int))
(var-static boar-state (* animation) (addr anim-boar-idle))
;; (when (> current-slide-index 1) (set boar-state (addr anim-boar-damage)))
(render-animation
renderer (addr spritesheet-boar) boar-state
animation-start-ticks c-animation-frame-rate
(type-cast (* window-width 0.70f) int)
(type-cast (* window-height 0.63f) int)))
(SDL_RenderPresent renderer)
(SDL_UpdateWindowSurface window)

Loading…
Cancel
Save