aboutsummaryrefslogtreecommitdiff
path: root/src/draw/animation.odin
blob: e6b50d11ad1be3729dfd3324a0378efd117195dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package draw

import "core:log"
import "core:encoding/json"
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 {
    log.error("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})

  log.debugf("loaded animation '%v'", 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]]
}