Browse Source

Add animation state changing

master
Macoy Madson 5 months ago
parent
commit
bf2a8f9255
  1. 62
      src/Presentation.cake

62
src/Presentation.cake

@ -36,6 +36,8 @@
(define-keybind s-toggle-kerning-keybind (array SDL_SCANCODE_F2))
(define-keybind s-toggle-debug-overlay-keybind (array SDL_SCANCODE_F1))
(define-keybind s-attack-keybind (array SDL_SCANCODE_1))
(var s-key-states sdl-key-states (array 0))
(defmacro preslog (format string &optional &rest arguments any)
@ -86,33 +88,41 @@
(defstruct-local animation
start-frame-index (unsigned char)
end-frame-index (unsigned char)
flip SDL_RendererFlip)
flip SDL_RendererFlip
next-animation (* animation))
(var spritesheet-wizard spritesheet (array null
966 636
6 6
2))
(var anim-wizard-idle animation (array 0 5 SDL_FLIP_HORIZONTAL))
(var anim-wizard-damage animation (array 31 35 SDL_FLIP_HORIZONTAL))
(var anim-wizard-attack animation (array 14 31 SDL_FLIP_HORIZONTAL))
(var anim-wizard-idle animation (array 0 5 SDL_FLIP_HORIZONTAL null))
(var anim-wizard-damage animation (array 31 35 SDL_FLIP_HORIZONTAL
(addr anim-wizard-idle)))
(var anim-wizard-attack animation (array 14 31 SDL_FLIP_HORIZONTAL
(addr anim-wizard-idle)))
(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-jump-in animation (array 23 34 SDL_FLIP_HORIZONTAL))
(var anim-boar-damage animation (array (+ (* 3 6) 3) (+ (* 3 6) 5) SDL_FLIP_HORIZONTAL))
(var anim-boar-idle animation (array 0 6 SDL_FLIP_HORIZONTAL
null))
(var anim-boar-jump-in animation (array 23 34 SDL_FLIP_HORIZONTAL
(addr anim-boar-idle)))
(var anim-boar-damage animation (array (+ (* 3 6) 3) (+ (* 3 6) 5) SDL_FLIP_HORIZONTAL
(addr anim-boar-idle)))
(var c-animation-frame-rate (const float) 0.1f)
(defun-local render-animation (renderer (* SDL_Renderer)
sprite (* spritesheet) anim (* animation)
sprite (* spritesheet)
;; This can be modified if there is a (anim > next-animation)
anim (* (* (const 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 num-frames int (- (+ 1 (path (deref anim) > end-frame-index)) ;; TODO: Off by one?
(path (deref 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)))
@ -120,15 +130,24 @@
(var animation-time-seconds float
(/ (- (SDL_GetPerformanceCounter) animation-start-ticks)
(type-cast (SDL_GetPerformanceFrequency) float)))
(when (and (path (deref anim) > next-animation)
(> animation-time-seconds loop-rate))
(set (deref anim) (path (deref anim) > next-animation))
;; TODO: Copy paste
(set num-frames (- (+ 1 (path (deref anim) > end-frame-index)) ;; TODO: Off by one?
(path (deref anim) > start-frame-index)))
(set loop-rate (* frame-rate num-frames)))
(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))
(var frame-x int (mod (+ current-frame (path (deref anim) > start-frame-index))
(path sprite > frame-width)))
(var frame-y int (/ (+ current-frame (path anim > start-frame-index))
(var frame-y int (/ (+ current-frame (path (deref anim) > start-frame-index))
(path sprite > frame-width)))
(var source-rectangle SDL_Rect
@ -142,7 +161,7 @@
(unless (= 0 (SDL_RenderCopyEx renderer (path sprite > texture)
(addr source-rectangle) (addr destination-rectangle)
0.f null ;; no rotation
(path anim > flip)))
(path (deref anim) > flip)))
(sdl-print-error)))
;;
@ -359,7 +378,9 @@
(var current-slide-index int 0)
(var slide-start-ticks Uint64 0)
(var animation-start-ticks Uint64 (SDL_GetPerformanceCounter))
(var animation-start-ticks Uint64 0)
(var current-wizard-animation (* (const animation)) (addr anim-wizard-attack))
(var current-boar-animation (* (const animation)) (addr anim-boar-jump-in))
(var exit-reason (* (const char)) null)
(while true
@ -393,6 +414,11 @@
(when (keybind-tapped (addr s-toggle-debug-overlay-keybind) (addr s-key-states))
(set s-enable-debug-overlay (not s-enable-debug-overlay)))
(when (keybind-tapped (addr s-attack-keybind) (addr s-key-states))
(set animation-start-ticks (SDL_GetPerformanceCounter))
(set current-wizard-animation (addr anim-wizard-attack))
(set current-boar-animation (addr anim-boar-jump-in)))
(SDL_SetRenderDrawColor renderer 17 17 17 255)
(SDL_RenderClear renderer)
@ -467,16 +493,16 @@
;; }#"#)
(when (>= current-slide-index 1)
(unless animation-start-ticks
(set animation-start-ticks (SDL_GetPerformanceCounter)))
(render-animation
renderer (addr spritesheet-wizard) (addr anim-wizard-idle)
renderer (addr spritesheet-wizard) (addr current-wizard-animation)
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
renderer (addr spritesheet-boar) (addr current-boar-animation)
animation-start-ticks c-animation-frame-rate
(type-cast (* window-width 0.70f) int)
(type-cast (* window-height 0.63f) int)))

Loading…
Cancel
Save