aboutsummaryrefslogtreecommitdiff
path: root/src/draw/animation.odin
diff options
context:
space:
mode:
authorXander Swan <email>2025-12-03 09:52:13 -0500
committerXander Swan <email>2025-12-03 09:52:13 -0500
commitb6cf1d104da53ec9c30dc45c35d9de48812f0afc (patch)
tree49421206d8d866e82a8a743cba5ced494cbfc785 /src/draw/animation.odin
Initial commit
Diffstat (limited to 'src/draw/animation.odin')
-rw-r--r--src/draw/animation.odin84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/draw/animation.odin b/src/draw/animation.odin
new file mode 100644
index 0000000..ac947fb
--- /dev/null
+++ b/src/draw/animation.odin
@@ -0,0 +1,84 @@
+package draw
+
+import "core:encoding/json"
+import "core:fmt"
+import "core:os"
+import "core:path/filepath"
+import "core:strings"
+
+FrameRect :: struct {
+ x: i32 `json:"x"`,
+ y: i32 `json:"y"`,
+ w: i32 `json:"w"`,
+ h: i32 `json:"h"`,
+}
+
+Frame :: struct {
+ rect: FrameRect `json:"frame"`,
+ duration: i32 `json:"duration"`,
+}
+
+Tag :: struct {
+ name: string `json:"name"`,
+ from: i32 `json:"from"`,
+ to: i32 `json:"to"`,
+ direction: string `json:"direction"`,
+}
+
+AnimationMeta :: struct {
+ image_path: string `json:"image"`,
+ frame_tags: []Tag `json:"frameTags"`,
+ frame_tags_indices: map[string]u32,
+}
+
+Animation :: struct {
+ frames: []Frame `json:"frames"`,
+ using meta: AnimationMeta `json:"meta"`,
+}
+
+@(require_results)
+init_anim_data :: proc(anim: ^Animation, path: string) -> bool {
+ data, ok := os.read_entire_file(path)
+ if !ok {
+ return false
+ }
+ defer delete(data)
+
+ json_err := json.unmarshal(data, anim)
+ if json_err != nil {
+ fmt.println("could not unmarshal data")
+ return false
+ }
+
+ for tag, i in anim.frame_tags {
+ anim.frame_tags_indices[tag.name] = u32(i)
+ }
+
+ partial_img_path := anim.image_path
+ defer delete(partial_img_path)
+ anim_dir := filepath.dir(path)
+ defer delete(anim_dir)
+
+ anim.image_path = strings.concatenate({anim_dir, "/", partial_img_path})
+
+ return true
+}
+
+delete_anim_data :: proc(anim: Animation) {
+ delete(anim.frames)
+
+ for tag in anim.frame_tags {
+ delete(tag.name)
+ delete(tag.direction)
+ }
+
+ // sg.destroy_image(anim.image)
+
+ delete(anim.frame_tags)
+ delete(anim.frame_tags_indices)
+ delete(anim.image_path)
+}
+
+get_anim_tag :: proc(anim: Animation, tag_name: string) -> Tag {
+ return anim.frame_tags[anim.frame_tags_indices[tag_name]]
+}