From b6cf1d104da53ec9c30dc45c35d9de48812f0afc Mon Sep 17 00:00:00 2001 From: Xander Swan Date: Wed, 3 Dec 2025 09:52:13 -0500 Subject: Initial commit --- src/draw/animation.odin | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/draw/animation.odin (limited to 'src/draw/animation.odin') 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]] +} -- cgit v1.3-2-g0d8e