Files

106 lines
1.8 KiB
Odin

package main
import "core:fmt"
import os "core:os"
import "core:strings"
import rl "vendor:raylib"
Atlas :: struct {
tex: rl.Texture2D,
tile_size: rl.Vector2,
cols: i32,
rows: i32,
frames: i32,
}
TILE_MULT :: 4
Tile :: struct {
atlas: ^Atlas,
pos: rl.Vector2,
frame: i32,
}
load_atlas :: proc() -> Atlas {
at: Atlas
ts, err := load_tileset("./Tiled/tiles.json", context.allocator)
if err.type != .None {
fmt.println(err.msg)
os.exit(1)
}
at.tex = rl.LoadTexture(strings.clone_to_cstring(ts.texture_path))
at.tile_size = rl.Vector2{f32(ts.tile_width), f32(ts.tile_height)}
at.rows = at.tex.height / (i32(at.tile_size.y))
at.cols = at.tex.width / i32(at.tile_size.x)
at.frames = at.cols * at.rows
fmt.printf(
"Atlas rows: %v, Atlas cols: %v, Frames: %v\n",
at.rows,
at.cols,
at.frames,
)
return at
}
//
// draw_atlas :: proc(at: ^Atlas) {
// rl.DrawTexturePro(
// at^.tex,
// rl.Rectangle{0, 0, at.tile_size.x, at.tile_size.y},
// rl.Rectangle{0, 0, 64, 64},
// rl.Vector2{0, 0},
// 0,
// rl.WHITE,
// )
// }
draw_tile :: proc(tile: Tile) {
offset, empty := get_offset(tile.atlas, tile.frame)
if empty do return
rl.DrawTexturePro(
tile.atlas^.tex,
rl.Rectangle {
offset.x,
offset.y,
tile.atlas.tile_size.x,
tile.atlas.tile_size.y,
},
rl.Rectangle {
tile.pos.x * TILE_MULT,
tile.pos.y * TILE_MULT,
tile.atlas.tile_size.x * TILE_MULT,
tile.atlas.tile_size.y * TILE_MULT,
},
rl.Vector2{0, 0},
// rl.Vector2{tile.pos.x, tile.pos.y},
0,
rl.WHITE,
)
}
get_offset :: proc(
atlas: ^Atlas,
f: i32,
) -> (
offset: rl.Vector2,
empty: bool,
) {
if f == 0 do return {}, true
col := (f - 1) % atlas.cols
row := (f - 1) / atlas.cols
offset = rl.Vector2 {
f32(col) * atlas.tile_size.x,
f32(row) * atlas.tile_size.y,
}
return
}