aboutsummaryrefslogtreecommitdiff
path: root/src/tiled/tiled.odin
blob: d84c2967faf1ffedb7766702b58375a222b4fa30 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
package tiled

import os "core:os/os2"
import mem "core:mem"
import strings "core:strings"
import "core:path/filepath"
import "core:log"
import "core:encoding/json"

import rl "vendor:raylib"

Color :: [4]f32

Error :: enum {
  NONE = 0,
  COULD_NOT_LOAD,
  NON_ORTHOGONAL_NOT_SUPPORTED,
  INFINITE_NOT_SUPPORTED,
  ELLIPSE_OBJ_NOT_SUPPORTED,
  POLYGON_OBJ_NOT_SUPPORTED,
  POLYLINE_OBJ_NOT_SUPPORTED,
  TEXT_OBJ_NOT_SUPPORTED,
}

Axis :: enum {
  X,
  Y,
}

Properties :: map[string]any

Map :: struct {
  // Currently, only orthogonal maps are allowed. Any non-orthogonal maps
  // throw an error.
  // Infinite maps will also throw an error for now.

  background_color: Color,
  class: string,

  width: i32,
  height: i32,

  layers: []Layer,

  next_layer_id: i32,
  next_object_id: i32,

  parallax_origin_x: f64,
  parallax_origin_y: f64,

  properties: Properties,

  // stagger_axis: Axis,
  // stagger_offset: i32, // To replace stagger index.

  tile_width: i32,
  tile_height: i32,

  tile_sets: []Tile_Set,

  tiles: [dynamic]Tile,
}

Layer :: struct {
  // Layers that aren't visible are entirely ignored.
  name: string,
  properties: Properties,

  parallax_x: f64,
  parallax_y: f64,

  tint: Color,
  id: i32,

  layer: union #no_nil {
    Tile_Layer,
    Image_Layer,
    Object_Layer,
  }
}

Tile_Layer :: struct {
  // Data will automatically become uncompressed.
  width: i32,
  height: i32,
  data: []i32,
}

Image_Layer :: struct {
  texture: rl.Texture2D,
  transparent_color: Color,
  repeat_x: bool,
  repeat_y: bool,
}

Object_Layer :: []Object

Object_Type :: enum {
  POINT,
  TILE,
  RECT,
}

Object :: struct {
  // For now, anything that *isn't* one of these is ignored and 
  // a warning is thrown:
  // - Point
  // - Tile
  // - Rectangle
  // 
  // Objects that aren't visible will be ignored entirely.

  type: Object_Type,
  name: string,
  class: string,
  properties: Properties,
  position: [2]f32,
  size: [2]f32,
  tile_id: i32,
}

Tile_Set :: struct {
  // For now, any autotiling information is ignored.

  properties: Properties,
  
  rows: i32,
  columns: i32,

  first_gid: i32,

  texture: rl.Texture2D,
  margin: i32,
  spacing: i32,

  tile_count: i32,
  tile_width: i32,
  tile_height: i32,
  tile_offset: [2]i32, // in pixels

  tiles: []i32, // array of GIDs
}

Tile :: struct {
  // For now, animations are ignored. Support in the future is somewhat likely.
  // And similar to above, autotiling information is ignored.

  is_image_collection: bool,

  tile_set: i32, // index for the tile set in Map

  id: i32,
  gid: i32,

  texture: Maybe(rl.Texture2D), // If the tile is part of an image collection.

  // Where on the texture is this tile
  src_start: [2]i32,
  src_size: [2]i32,

  object_group: Layer, // Object layer for collisions.

  properties: Properties,
}

load_map :: proc(path: string) -> (Map, Error) {
  defer free_all(context.temp_allocator)

  jmap_text, read_err := os.read_entire_file(path, context.temp_allocator)
  if read_err != nil {
    log.errorf("Failed to read file %v (%v)", path, read_err)
    return {}, .COULD_NOT_LOAD
  }

  jmap: Json_Map
  unmarshal_err := json.unmarshal(
    jmap_text,
    &jmap,
    allocator = context.temp_allocator,
  )
  if unmarshal_err != nil {
    log.errorf("Failed to unmarshal file %v (%v)", path, unmarshal_err)
    return {}, .COULD_NOT_LOAD
  }

  // log.debug(jmap)

  tmap: Map
  err := convert_json_map(jmap, &tmap, path)
  if err != .NONE {
    return {}, err
  }
  return tmap, .NONE
}

delete_map :: proc(tmap: Map) {
  delete(tmap.class)
  delete(tmap.properties)

  for layer in tmap.layers {
    delete_layer(layer)
  }
  delete(tmap.layers)

  for tile_set in tmap.tile_sets {
    delete(tile_set.properties)
    delete(tile_set.tiles) // all tiles are deleted later
    rl.UnloadTexture(tile_set.texture)
  }
  delete(tmap.tile_sets)

  for tile in tmap.tiles {
    delete(tile.properties)

    if tile.texture != nil {
      rl.UnloadTexture(tile.texture.(rl.Texture2D))
    }

    delete_layer(tile.object_group)
  }
  delete(tmap.tiles)
}

@(private)
delete_layer :: proc(layer: Layer) {
  delete(layer.name)
  delete(layer.properties)

  switch l in layer.layer {
  case Tile_Layer:
    delete(l.data)
  case Image_Layer:
    rl.UnloadTexture(l.texture)
  case Object_Layer:
    for obj in l {
      delete(obj.name)
      delete(obj.class)
      delete(obj.properties)
    }
    delete(l)
  }
}

@(private)
convert_json_color :: proc(jcolor: string) -> Color {
  return Color{1, 1, 1, 1}
}

@(private)
convert_json_properties :: proc(jprops: []Json_Property) -> Properties {
  props := make(Properties)

  for jprop in jprops {
    props[jprop.name] = jprop.value
  }

  return props
}

@(private)
convert_json_map :: proc(jmap: Json_Map, tmap: ^Map, path: string) -> Error {
  // ensure the map is orthogonal
  if strings.compare(jmap.orientation, "orthogonal") != 0 {
    return .NON_ORTHOGONAL_NOT_SUPPORTED
  }

  // ensure the map is not infinite
  if jmap.infinite {
    return .INFINITE_NOT_SUPPORTED
  }

  tmap.background_color = convert_json_color(jmap.backgroundcolor)
  tmap.class = strings.clone(jmap.class)

  tmap.width = jmap.width
  tmap.height = jmap.height

  tmap.tile_width = jmap.tilewidth
  tmap.tile_height = jmap.tileheight

  tmap.next_layer_id = jmap.nextlayerid
  tmap.next_object_id = jmap.nextobjectid

  tmap.parallax_origin_x = jmap.parallaxoriginx
  tmap.parallax_origin_y = jmap.parallaxoriginy

  tmap.properties = convert_json_properties(jmap.properties)

  // TODO: flatten groups
  layer_count := len(jmap.layers)
  for jlayer in jmap.layers {
    layer_count += len(jlayer.layers) // take into consideration groups
  }

  tmap.layers = make([]Layer, layer_count)
  current_index := 0

  for jlayer in jmap.layers {
    if !jlayer.visible {
      continue // ignore hidden layers
    }

    // FIXME: Some duplicated code down here
    if strings.compare(jlayer.type, "group") == 0 {
      for jlayer2 in jlayer.layers {
        if !jlayer2.visible {
          continue // ignore hidden layers
        }

        layer, err := convert_json_layer(jlayer2)
        if err != .NONE {
          return err
        }
        tmap.layers[current_index] = layer
        current_index += 1
      }
      continue
    }

    layer, err := convert_json_layer(jlayer)
    if err != .NONE {
      return err
    }
    tmap.layers[current_index] = layer
    current_index += 1
  }

  tmap.tile_sets = make([]Tile_Set, len(jmap.tilesets))

  res_dir := filepath.dir(path, allocator = context.temp_allocator)

  for jtile_set, i in jmap.tilesets {
    tile_set, err := convert_json_tile_set(tmap, res_dir, jtile_set)
    if err != .NONE {
      return err
    }
    tmap.tile_sets[i] = tile_set

    for j in 0..<len(tmap.tiles) {
      tmap.tiles[int(tile_set.first_gid) + j].tile_set = i32(i)
    }
  }

  return .NONE
}

Map_Iterate_Callback :: proc(i32, i32, Tile, Tile_Set)

iterate_map_tiles :: proc(tmap: Map, callback: Map_Iterate_Callback) {
  for layer in tmap.layers {
    tile_layer, ok := layer.layer.(Tile_Layer)
    if !ok {
      continue
    }

    x: i32 = 0
    y: i32 = 0

    for cell, i in tile_layer.data {
      if cell > 0 {
        tile := tmap.tiles[cell - 1]
        tile_set := tmap.tile_sets[tile.tile_set]
        callback(x, y, tile, tile_set)
      }

      x += tmap.tile_width
      if x > (tmap.width - 1) * tmap.tile_width {
        x = 0
        y += tmap.tile_height
      }
    }
  }
}

test_cells :: proc(tmap: Map) {
  for layer in tmap.layers {
    tile_layer, ok := layer.layer.(Tile_Layer)
    if !ok {
      continue
    }

    x: i32 = 0
    y: i32 = 0

    for cell, i in tile_layer.data {
      if cell > 0 {
        log.debug(i, [2]i32{x, y}, cell)
      }

      x += tmap.tile_width
      if x > (tmap.width - 1) * tmap.tile_width {
        x = 0
        y += tmap.tile_height
      }
    }
  }
}

draw_map :: proc(tmap: Map) {
  draw_tile :: proc(x: i32, y: i32, tile: Tile, tile_set: Tile_Set) {
    rl.DrawTexturePro(
      tile_set.texture,
      rl.Rectangle{
        x = f32(tile.src_start.x),
        y = f32(tile.src_start.y),
        width = f32(tile.src_size.x),
        height = f32(tile.src_size.y),
      },
      rl.Rectangle{
        x = f32(x),
        y = f32(y),
        width = f32(tile.src_size.x),
        height = f32(tile.src_size.y),
      },
      {0, 0},
      0,
      rl.WHITE,
    )
  }
  
  iterate_map_tiles(tmap, draw_tile)
}

@(private)
load_image :: proc(filename: string, res_dir: string) -> rl.Texture2D {
  path := strings.concatenate({res_dir, "/", filename})
  defer delete(path)

  byte_path := make([]u8, len(path) + 1, context.temp_allocator)
  copy(byte_path, path)
  byte_path[len(path)] = 0
  return rl.LoadTexture(transmute(cstring)&byte_path[0])
}

@(private)
convert_json_layer :: proc(jlayer: Json_Layer) -> (Layer, Error) {
  // TODO: uncompress tile data

  layer: Layer

  layer.name = strings.clone(jlayer.name)

  layer.parallax_x = jlayer.parallaxx
  layer.parallax_y = jlayer.parallaxy

  layer.id = jlayer.id

  layer.tint = convert_json_color(jlayer.tintcolor)

  layer.properties = convert_json_properties(jlayer.properties)

  switch jlayer.type {
  case "tilelayer":
    tile_layer := Tile_Layer{
      width = jlayer.width,
      height = jlayer.height,
    }

    data := jlayer.data.([]i32)
    tile_layer.data = make([]i32, len(data))
    copy(tile_layer.data, data)

    layer.layer = tile_layer

  case "objectgroup":
    obj_layer := make(Object_Layer, len(jlayer.objects))

    for jobj, i in jlayer.objects {
      if !jobj.visible {
        continue // ignore hidden objects
      }

      obj, err := convert_json_object(jobj)
      if err != .NONE {
        return {}, err
      }
      obj_layer[i] = obj
    }

    layer.layer = obj_layer

  case "imagelayer":
    image_layer := Image_Layer{
      texture = load_image(jlayer.image, "res"),
      transparent_color = convert_json_color(jlayer.transparentcolor),
      repeat_x = jlayer.repeatx,
      repeat_y = jlayer.repeaty,
    }

    layer.layer = image_layer

  case "group":
    // groups are handled elsewhere
  }

  return layer, .NONE
}

@(private)
convert_json_object :: proc(jobj: Json_Object) -> (Object, Error) {
  obj: Object

  if jobj.ellipse {
    return {}, .ELLIPSE_OBJ_NOT_SUPPORTED
  }
  if jobj.polygon != nil {
    return {}, .POLYGON_OBJ_NOT_SUPPORTED
  }
  if jobj.polyline != nil {
    return {}, .POLYLINE_OBJ_NOT_SUPPORTED
  }
  if jobj.text != nil {
    return {}, .TEXT_OBJ_NOT_SUPPORTED
  }

  obj.position = {
    f32(jobj.x),
    f32(jobj.y),
  }
  obj.size = {
    f32(jobj.width),
    f32(jobj.height),
  }
  obj.tile_id = jobj.gid

  obj.name = strings.clone(jobj.name)
  obj.class = strings.clone(jobj.type)
  obj.properties = convert_json_properties(jobj.properties)

  return obj, .NONE
}

@(private)
convert_json_tile_set :: proc(
  tmap: ^Map,
  res_dir: string,
  jtile_set: Json_Tile_Set,
) -> (Tile_Set, Error) {
  if len(jtile_set.source) > 0 {
    // Tileset links somewhere else
    path := strings.concatenate({res_dir, "/", jtile_set.source})
    defer delete(path)

    data, err := os.read_entire_file(path, allocator = context.temp_allocator)
    if err != nil {
      log.errorf("Could not load external tile set '%v'", path)
      return {}, .COULD_NOT_LOAD
    }
    
    new_jtile_set: Json_Tile_Set
    marshal_err := json.unmarshal(
      data,
      &new_jtile_set,
      allocator = context.temp_allocator,
    )
    if marshal_err != nil {
      log.errorf("Could not unmarshal external tile set '%v'", path)
      return {}, .COULD_NOT_LOAD
    }

    return convert_json_tile_set(tmap, res_dir, new_jtile_set)
  }

  tile_set: Tile_Set

  tile_set.rows = jtile_set.rows
  tile_set.columns = jtile_set.columns

  tile_set.first_gid = jtile_set.firstgid

  tile_set.texture = load_image(jtile_set.image, res_dir)
  tile_set.margin = jtile_set.margin
  tile_set.spacing = jtile_set.spacing

  tile_set.tile_count = jtile_set.tilecount
  tile_set.tile_width = jtile_set.tilewidth
  tile_set.tile_height = jtile_set.tileheight
  tile_set.tile_offset = {
    jtile_set.tileoffset.x,
    jtile_set.tileoffset.y,
  }

  tile_set.properties = convert_json_properties(jtile_set.properties)

  tile_set.tiles = make([]i32, jtile_set.tilecount)

  // Ignore margin and spacing for now
  // TODO: account for margin and spacing
  tile_x := tile_set.texture.width / tile_set.tile_width
  tile_y := tile_set.texture.height / tile_set.tile_height

  current_index := 0
  for y in 0..<tile_y {
    for x in 0..<tile_x {
      tile: Tile

      tile.id = i32(current_index)  

      tile.src_start = {
        x * tile_set.tile_width,
        y * tile_set.tile_height,
      }
      tile.src_size = {
        tile_set.tile_width,
        tile_set.tile_height,
      }

      current_index += 1

      tile.gid = i32(len(tmap.tiles))
      append(&tmap.tiles, tile)

      tile_set.tiles[current_index - 1] = tile.gid
    }
  }

  for jtile in jtile_set.tiles {
    ttile := &tmap.tiles[tile_set.tiles[jtile.id]]
    object_group, err := convert_json_layer(jtile.objectgroup)
    if err != .NONE {
      return {}, err
    }
    ttile.object_group = object_group
    ttile.properties = convert_json_properties(jtile.properties)
  }

  return tile_set, .NONE
}