aboutsummaryrefslogtreecommitdiff
path: root/src/resources.odin
blob: 8134ccb2aa195b708f838e9a8c15c7354653b2b8 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package demonchime

import "core:image"
import "core:image/qoi"
import "core:log"

import "fw"

Tiled_Property :: union {
  string,
  i32,
  f32,
  bool,
}

Image_Resource :: struct {
  texture: fw.Texture,
  anim: Animation_Id,
  data: []u8,
}

Tag_Resource :: struct {
  from: i32,
  to: i32,
}

Animation_Resource :: struct {
  frame_count: i32,
  frame_durations: []i32,
  tags: map[string]Tag_Resource,
}

Object_Resource :: struct {
  type: Object_Type,
  tile_id: Maybe(u32),
  pos: Vec2,
  size: Vec2,
  parallax: Vec2,
  properties: map[string]Tiled_Property,
}

Backgrond_Image :: struct {
  image_id: Image_Id,
  parallax: Vec2,
}

Room_Resource :: struct {
  width: i32,
  height: i32,
  tile_width: i32,
  tile_height: i32,
  layers: [][]u32,
  objects: []Object_Resource,
  background_color: Color,
  background_images: []Backgrond_Image,
}

Tile_Resource :: struct {
  image: Image_Id,
  rect: Rect,
  id: u32,
  collisions: []Rect,
}

Tileset_Resource :: struct {
  tiles: []u32,
  image: Image_Id,
}

Room_Position_Resource :: struct {
  id: Room_Id,
  x: i32,
  y: i32,
  width: i32,
  height: i32,
}

get_image :: proc(id: Image_Id) -> fw.Texture {
  img_res := &images[id]

  if img_res.texture.handle == 0 {
    img, qoi_err := qoi.load_from_bytes(img_res.data)
    defer image.destroy(img)

    img_res.texture = fw.create_texture_from_image(img)
  }

  return img_res.texture
}

get_room :: proc(id: Room_Id) -> Room_Resource {
  return rooms[id]
}

convert_path_to_resource :: proc(
  obj: Object_Resource,
  property: string,
) -> Resource_Id {
  path, has_path := obj.properties[property].(string)
  if !has_path {
    log.warnf("Property '%v' must be a path or string", property)
    return nil
  }
  return path_to_resource_id[path]
}