Files
game-jam/src/tilemap.odin
2026-02-16 15:29:10 -06:00

127 lines
3.0 KiB
Odin

package main
import "vendor:raylib"
TilemapSpritesheet :: struct {
texture: raylib.Texture2D,
tile_width: i32,
tile_height: i32,
tiles_x: i32,
tiles_y: i32,
}
VisibleTileRange :: struct {
start_x, start_y: int,
end_x, end_y: int,
}
Grid :: struct {
width: int,
height: int,
tiles: []Tile,
}
load_tilemap_sheet :: proc(path: cstring, tile_width, tile_height: i32) -> TilemapSpritesheet {
tex := raylib.LoadTexture(path)
return TilemapSpritesheet {
texture = tex,
tile_width = tile_width,
tile_height = tile_height,
tiles_x = tex.width / tile_width,
tiles_y = tex.height / tile_height,
}
}
create_tile_grid :: proc(width, height: i32, fill_tile: Tile) -> Grid {
w, h := int(width), int(height)
grid_slice := make([]Tile, w * h)
for i := 0; i < len(grid_slice); i += 1 {
grid_slice[i] = fill_tile
}
return Grid{width = w, height = h, tiles = grid_slice}
}
update_tile_grid :: proc(grid: ^Grid, camera: ^raylib.Camera2D, tile_w, tile_h: f32, delta: f32) {
range := get_visible_tile_range(grid, tile_w, tile_h, camera)
for y := range.start_y; y <= range.end_y; y += 1 {
for x := range.start_x; x <= range.end_x; x += 1 {
tile := &grid.tiles[y * grid.width + x]
update_tile_anim(tile, delta)
}
}
}
draw_tile_grid :: proc(
sheet: ^TilemapSpritesheet,
grid: ^Grid,
camera: ^raylib.Camera2D,
color: raylib.Color,
) {
tile_w := f32(sheet.tile_width)
tile_h := f32(sheet.tile_height)
range := get_visible_tile_range(grid, tile_w, tile_h, camera)
for y := range.start_y; y <= range.end_y; y += 1 {
for x := range.start_x; x <= range.end_x; x += 1 {
tile := &grid.tiles[y * grid.width + x]
if tile.type == .NOTHING do continue
pos := raylib.Vector2{f32(x) * tile_w, f32(y) * tile_h}
draw_tile(sheet, tile, pos, color)
}
}
}
get_visible_tile_range :: proc(
grid: ^Grid,
tile_w, tile_h: f32,
camera: ^raylib.Camera2D,
padding: int = 1,
) -> VisibleTileRange {
screen_w := f32(raylib.GetScreenWidth())
screen_h := f32(raylib.GetScreenHeight())
world_w := screen_w / camera.zoom
world_h := screen_h / camera.zoom
min_x := camera.target.x - world_w * 0.5
min_y := camera.target.y - world_h * 0.5
max_x := min_x + world_w
max_y := min_y + world_h
start_x := int(min_x / tile_w) - padding
start_y := int(min_y / tile_h) - padding
end_x := int(max_x / tile_w) + padding
end_y := int(max_y / tile_h) + padding
return VisibleTileRange {
start_x = clamp(start_x, 0, grid.width - 1),
start_y = clamp(start_y, 0, grid.height - 1),
end_x = clamp(end_x, 0, grid.width - 1),
end_y = clamp(end_y, 0, grid.height - 1),
}
}
get_tile :: proc(grid: ^Grid, x, y: int) -> ^Tile {
if x < 0 || x >= grid.width || y < 0 || y >= grid.height do return nil
return &grid.tiles[y * grid.width + x]
}
set_tile :: proc(grid: ^Grid, x: int, y: int, tile: Tile) {
if x < 0 || x >= grid.width || y < 0 || y >= grid.height do return
grid.tiles[y * grid.width + x] = tile
}
delete_tile_grid :: proc(grid: ^Grid) {
delete(grid.tiles)
grid.width = 0
grid.height = 0
}