Browse Source

WIP data-driven power system

* Fixed memory leak due to kerning table
master
Macoy Madson 5 months ago
parent
commit
0b13376d27
  1. 12
      data/PowerSystem.cakedata
  2. 1
      src/FontAtlas.cake
  3. 148
      src/Presentation.cake

12
data/PowerSystem.cakedata

@ -16,4 +16,14 @@
(spritesheet :id "smoke-explosion"
:width 630 :height 116
:frame-width 5 :frame-height 1
:scale-factor 2)))
:scale-factor 2))
:powers
(array
(power
:id "fireball"
:operations
"PlayEffect fireball
SetValue velocity 25
IfEquals positionX enemyX
PlayEffect smoke-explosion
EndIf")))

1
src/FontAtlas.cake

@ -35,6 +35,7 @@
(defun free-font-atlas (font-atlas-to-free (* font-atlas))
(dict-free (path font-atlas-to-free > glyph-lookup-table))
(dict-free (path font-atlas-to-free > kerning-lookup-table))
(free (path font-atlas-to-free > pixel-buffer)))
(defun font-atlas-make-character-pair (character-a char

148
src/Presentation.cake

@ -200,14 +200,140 @@
(path (deref anim) > flip)))
(sdl-print-error)))
;;
;; Power and effect system
;;
(defenum operation-type
operation-type-play-effect
operation-type-set-value
operation-type-if-equals
operation-type-end-if)
(def-introspect-struct operation
type int ;; operation-type
stringA ([] 32 char)
stringB ([] 32 char))
(defun-local parse-power-operations (operations (* (const char))
&return (* operation)) ;; dynarray
(var new-operations (* operation) null)
(defstruct-local operation-reader
type operation-type
keyword (* (const char))
num-arguments char)
(var readers ([] operation-reader)
(array
(array
operation-type-play-effect
"PlayEffect" 1)
(array
operation-type-set-value
"SetValue" 2)
(array
operation-type-if-equals
"IfEquals" 2)
(array
operation-type-end-if
"EndIf" 0)))
(defenum read-state
read-state-looking-for-keyword
read-state-reading-arguments)
(var state read-state read-state-looking-for-keyword)
(var new-operation operation)
(var current-operation-reader (* operation-reader) null)
(var argument-index int 0)
(each-char-in-string-const operations current-char
(cond
((= state read-state-looking-for-keyword)
(when (!= ' ' (deref current-char))
(var argument-end (* (const char)) null)
(each-char-in-string-const current-char end-char
(set argument-end end-char)
(when (or (= ' ' (deref end-char))
(= '\n' (deref end-char)))
(break)))
(each-item-addr-in-array readers i reader (* operation-reader)
(when (= 0 (strncmp (path reader > keyword) current-char (- argument-end current-char)))
(preslog "Found keyword %s\n" (path reader > keyword))
(memset (addr new-operation) 0 (sizeof new-operation))
(set (field new-operation type) (path reader > type))
(if (path reader > num-arguments)
(scope
(set argument-index 0)
(set state read-state-reading-arguments)
(set current-operation-reader reader))
;; No arguments, push now
(dynarray-push new-operations new-operation))
(set current-char argument-end)
(break)))))
((= state read-state-reading-arguments)
(when (!= ' ' (deref current-char))
(var argument-end (* (const char)) null)
(each-char-in-string-const current-char end-char
(set argument-end end-char)
(when (or (= ' ' (deref end-char))
(= '\n' (deref end-char)))
(break)))
(preslog "Argument [%d] found: " argument-index)
(fwrite current-char (sizeof char) (- argument-end current-char) stderr)
(preslog "\n")
(cond
((= argument-index 0)
(strncpy (field new-operation stringA) current-char (- argument-end current-char)))
((= argument-index 1)
(strncpy (field new-operation stringB) current-char (- argument-end current-char))))
(incr argument-index)
(when (>= argument-index (path current-operation-reader > num-arguments))
(dynarray-push new-operations new-operation)
(set state read-state-looking-for-keyword))
(set current-char argument-end)))))
(return new-operations))
(def-introspect-struct power
id ([] 32 char)
operations ([] 1024 char)
;; Parsed at runtime, though they could be written by hand if you wanted
parsed-operations (* operation) ('dynarray))
(def-introspect-struct power-system
sprites ([] 16 spritesheet) ('array-allow-subset))
sprites ([] 16 spritesheet) ('array-allow-subset)
powers ([] 16 power) ('array-allow-subset))
(var s-power-system power-system (array 0))
(bundle-file s-start-power-system s-end-power-system
(const char) "data/PowerSystem.cakedata")
(defun-local power-system-initialize (&return bool)
(scope
;; TODO: Take string size rather than requiring null terminator
(var power-system-string-length int (- s-end-power-system s-start-power-system))
(var-cast-to power-system-string-null-terminated (* char)
(malloc (+ 1 power-system-string-length)))
(defer (free power-system-string-null-terminated))
(strncpy power-system-string-null-terminated s-start-power-system power-system-string-length)
(set (at power-system-string-length power-system-string-null-terminated) 0)
(unless (read-introspect-struct-s-expr
power-system--metadata
(addr s-power-system)
power-system-string-null-terminated
malloc
null)
(preslog "Failed to read power system data\n")
(return false)))
(each-item-addr-in-array (field s-power-system powers) i current-power (* power)
(unless (at 0 (path current-power > id))
(continue))
(set (path current-power > parsed-operations)
(parse-power-operations (path current-power > operations))))
(return true))
(defstruct-local effect
sprite (* spritesheet) ;; null = empty slot
anim (* (const animation))
@ -451,22 +577,8 @@
code-font-size-points)
(return 1))
(scope
;; TODO: Take string size rather than requiring null terminator
(var power-system-string-length int (- s-end-power-system s-start-power-system))
(var-cast-to power-system-string-null-terminated (* char)
(malloc (+ 1 power-system-string-length)))
(defer (free power-system-string-null-terminated))
(strncpy power-system-string-null-terminated s-start-power-system power-system-string-length)
(set (at power-system-string-length power-system-string-null-terminated) 0)
(unless (read-introspect-struct-s-expr
power-system--metadata
(addr s-power-system)
power-system-string-null-terminated
malloc
null)
(preslog "Failed to read power system data\n")
(return false)))
(unless (power-system-initialize)
(return 1))
(defer (free-introspect-struct-fields power-system--metadata (addr s-power-system) free))
(defstruct spritesheets-to-prepare
@ -528,7 +640,7 @@
malloc
null)
(preslog "Failed to read presentation data\n")
(return false)))
(return 1)))
(defer (free-introspect-struct-fields presentation-data--metadata (addr presentation) free))
(var current-slide-index int 0)

Loading…
Cancel
Save