Browse Source

Progress on effects system

master
Macoy Madson 5 months ago
parent
commit
9344d6958b
  1. 156
      src/Presentation.cake

156
src/Presentation.cake

@ -81,6 +81,9 @@
(bundle-file s-start-ground-bmp s-end-ground-bmp
(unsigned char) "/home/macoy/Documents/Ground.bmp")
(bundle-file s-start-fireball-spritesheet s-end-fireball-spritesheet
(unsigned char) "/home/macoy/Documents/fireball.bmp")
(defstruct-local spritesheet
texture (* SDL_Texture)
width uint16_t
@ -107,6 +110,7 @@
(addr anim-wizard-idle)))
(var anim-wizard-fast-attack animation (array 7 13 SDL_FLIP_HORIZONTAL
(addr anim-wizard-idle)))
(var wizard-fast-attack-effect-frame int 11)
(var spritesheet-boar spritesheet (array null
1434 1068
@ -119,32 +123,33 @@
(var anim-boar-damage animation (array (+ (* 3 6) 3) (+ (* 3 6) 5) SDL_FLIP_HORIZONTAL
(addr anim-boar-idle)))
(var spritesheet-fireball spritesheet (array null
237 46
3 1
2))
(var anim-fireball-idle animation (array 0 2 SDL_FLIP_NONE
null))
(var c-animation-frame-rate (const float) 0.1f)
(defun-local render-animation (renderer (* SDL_Renderer)
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 (deref anim) > end-frame-index)) ;; TODO: Off by one?
(path (deref anim) > start-frame-index)))
;; Returns whether the next animation should be started instead
(defun-local animation-get-current-frame (;; This can be modified if there is a (anim > next-animation)
anim (* (const animation))
animation-start-ticks Uint64
frame-rate float
;; Not valid if the next animation should be played
frame-index-out (* int)
&return bool)
(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)))
(when (and (path (deref anim) > next-animation)
(when (and (path 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)))
(return false))
(var-cast-to current-frame int
(interpolate-range
@ -152,9 +157,27 @@
0.f loop-rate
(- animation-time-seconds
(* loop-rate (truncf (/ animation-time-seconds loop-rate))))))
(var frame-x int (mod (+ current-frame (path (deref anim) > start-frame-index))
(set (deref frame-index-out) (+ current-frame (path anim > start-frame-index)))
(return true))
(defun-local render-animation (renderer (* SDL_Renderer)
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 sprite-width int (/ (path sprite > width) (path sprite > frame-width)))
(var sprite-height int (/ (path sprite > height) (path sprite > frame-height)))
(var current-frame int 0)
(while (not (animation-get-current-frame (deref anim) animation-start-ticks frame-rate
(addr current-frame)))
(set (deref anim) (path (deref anim) > next-animation)))
(var frame-x int (mod current-frame
(path sprite > frame-width)))
(var frame-y int (/ (+ current-frame (path (deref anim) > start-frame-index))
(var frame-y int (/ current-frame
(path sprite > frame-width)))
(var source-rectangle SDL_Rect
@ -171,6 +194,42 @@
(path (deref anim) > flip)))
(sdl-print-error)))
(defstruct-local effect
sprite (* spritesheet)
anim (* (const animation))
animation-start-ticks Uint64
x int
y int
velocity-x int
velocity-y int)
(var s-effects ([] 16 effect) (array 0))
(defun-local update-effects (renderer (* SDL_Renderer))
(each-in-array s-effects i
(var current-effect (* effect) (addr (at i s-effects)))
(unless (path current-effect > sprite)
(continue))
;; TODO: Frame independence
(set (path current-effect > x) (+ (path current-effect > x) (path current-effect > velocity-x)))
(set (path current-effect > y) (+ (path current-effect > y) (path current-effect > velocity-y)))
(render-animation renderer (path current-effect > sprite)
(addr (path current-effect > anim))
(path current-effect > animation-start-ticks)
c-animation-frame-rate
(path current-effect > x) (path current-effect > y))
(when (> (path current-effect > x) 3000)
(set (path current-effect > sprite) null))))
(defun-local get-free-effect (&return (* effect))
(each-in-array s-effects i
(var current-effect (* effect) (addr (at i s-effects)))
(unless (path current-effect > sprite)
(memset current-effect 0 (sizeof (type effect)))
(return current-effect)))
(return null))
;;
;; Text rendering
;;
@ -282,6 +341,10 @@
;; Main
;;
(defenum effect-id
effect-id-none
effect-id-fireball)
(defun main (&return int)
(data-bundle-load-all-resources)
@ -350,16 +413,22 @@
(unless wizard-texture
(return 1))
(defer (SDL_DestroyTexture wizard-texture))
(set (field spritesheet-wizard texture) wizard-texture)
(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)
(var fireball-texture (* SDL_Texture)
(sdl-texture-from-bmp-data renderer s-start-fireball-spritesheet s-end-fireball-spritesheet))
(unless fireball-texture
(return 1))
(defer (SDL_DestroyTexture fireball-texture))
(set (field spritesheet-fireball texture) fireball-texture)
(var ground-texture (* SDL_Texture)
(sdl-texture-from-bmp-data renderer s-start-ground-bmp s-end-ground-bmp))
(unless ground-texture
@ -406,6 +475,8 @@
(var should-render-boar bool false)
(var should-render-ground bool false)
(var queued-effect effect-id effect-id-none)
(var exit-reason (* (const char)) null)
(while true
(var event SDL_Event)
@ -459,13 +530,41 @@
(when (keybind-tapped (addr s-toggle-debug-overlay-keybind) (addr s-key-states))
(set s-enable-debug-overlay (not s-enable-debug-overlay)))
(var wizard-x int (type-cast (* window-width 0.08f) int))
(var wizard-y int (- window-height (/ ground-height 2) wizard-ground-height))
(when (and
(!= current-wizard-animation (addr anim-wizard-fast-attack))
(keybind-tapped (addr s-attack-keybind) (addr s-key-states)))
(set wizard-animation-start-ticks (SDL_GetPerformanceCounter))
(set current-wizard-animation (addr anim-wizard-fast-attack))
(set boar-animation-start-ticks (SDL_GetPerformanceCounter))
(set current-boar-animation (addr anim-boar-jump-in)))
(set current-boar-animation (addr anim-boar-jump-in))
(set queued-effect effect-id-fireball))
(when (and (= current-wizard-animation (addr anim-wizard-fast-attack))
(= queued-effect effect-id-fireball))
(var current-frame int 0)
(var current-anim (* (const animation)) current-wizard-animation)
(when (or (not (animation-get-current-frame current-anim
wizard-animation-start-ticks c-animation-frame-rate
(addr current-frame)))
(= current-frame wizard-fast-attack-effect-frame))
(var new-effect (* effect) (get-free-effect))
(set (path new-effect > sprite) (addr spritesheet-fireball))
(set (path new-effect > anim) (addr anim-fireball-idle))
(var mouse-x int 0)
(var mouse-y int 0)
(SDL_GetMouseState (addr mouse-x) (addr mouse-y))
(if (and mouse-x mouse-y s-enable-debug-overlay)
(scope
(set (path new-effect > x) mouse-x)
(set (path new-effect > y) mouse-y))
(scope
(set (path new-effect > x) (+ 186 wizard-x))
(set (path new-effect > y) (+ 64 wizard-y))))
(set (path new-effect > velocity-x) 25)
(set queued-effect effect-id-none)))
(SDL_SetRenderDrawColor renderer 11 19 40 255)
(SDL_RenderClear renderer)
@ -539,8 +638,7 @@
(render-animation
renderer (addr spritesheet-wizard) (addr current-wizard-animation)
wizard-animation-start-ticks c-animation-frame-rate
(type-cast (* window-width 0.08f) int)
(- window-height (/ ground-height 2) wizard-ground-height)))
wizard-x wizard-y))
(when should-render-boar
(unless boar-animation-start-ticks
@ -551,13 +649,17 @@
(type-cast (* window-width 0.70f) int)
(- window-height (/ ground-height 2) boar-ground-height)))
(update-effects renderer)
(when s-enable-debug-overlay
(var buffer ([] 256 char) (array 0))
(var mouse-x int 0)
(var mouse-y int 0)
(SDL_GetMouseState (addr mouse-x) (addr mouse-y))
(snprintf buffer (- (array-size buffer) 1) "Mouse: %4d %4d Percent of window: %.2f %.2f" mouse-x mouse-y
(/ mouse-x (type-cast window-width float)) (/ mouse-y (type-cast window-height float)))
(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 wizard-x) (- mouse-y wizard-y))
(render-string
renderer (addr ubuntu-mono-font-atlas) ubuntu-mono-texture
10 (- window-height 20)

Loading…
Cancel
Save