98 lines
1.6 KiB
Odin
98 lines
1.6 KiB
Odin
package main
|
|
|
|
import "core:fmt"
|
|
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
|
|
|
|
at.tex = rl.LoadTexture("./assets/3rdparty/tileset/Tiles/Tileset.png")
|
|
at.tile_size = rl.Vector2{16, 16}
|
|
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
|
|
}
|