Tilemap and grid

This commit is contained in:
2026-02-15 18:53:45 -06:00
parent 671a5172ab
commit b5e07700b0
4 changed files with 151 additions and 26 deletions

View File

@@ -1,34 +1,56 @@
package main
import rl "vendor:raylib"
import "vendor:raylib"
TILE_SIZE :: 16
TILE_IMAGE_PATH :: "assets/tiles.png"
tilemap_image: rl.Texture2D
tilesX: i32
tilesY: i32
load_tilemap :: proc() {
tilemap_image = rl.LoadTexture(TILE_IMAGE_PATH)
tilesX = tilemap_image.width / TILE_SIZE
tilesY = tilemap_image.height / TILE_SIZE
TilemapSpritesheet :: struct {
texture: raylib.Texture2D,
tile_width: i32,
tile_height: i32,
tiles_x: i32,
tiles_y: i32,
}
unload_tilemap :: proc() {
rl.UnloadTexture(tilemap_image)
}
draw_tile :: proc(tilemap_pos: rl.Vector2, draw_pos: rl.Vector2, color: rl.Color) {
source_rect := rl.Rectangle {
x = tilemap_pos.x * TILE_SIZE,
y = tilemap_pos.y * TILE_SIZE,
width = TILE_SIZE,
height = TILE_SIZE,
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) -> [][]Tile {
grid: [][]Tile = make([][]Tile, height)
for y := 0; y < int(height); y += 1 {
grid[y] = make([]Tile, width)
for x := 0; x < int(width); x += 1 {
grid[y][x] = ground_tile
}
}
return grid
}
update_tile_grid :: proc(grid: [][]Tile, delta: f32) {
for y := 0; y < len(grid); y += 1 {
row := grid[y]
for x := 0; x < len(row); x += 1 {
update_tile_anim(&row[x], delta)
}
}
}
draw_tile_grid :: proc(sheet: ^TilemapSpritesheet, grid: [][]Tile) {
for y := 0; y < len(grid); y += 1 {
row := grid[y]
for x := 0; x < len(row); x += 1 {
tile := &row[x]
pos := raylib.Vector2{f32(x * int(sheet.tile_width)), f32(y * int(sheet.tile_height))}
draw_tile(sheet, tile, pos, raylib.Color{255, 136, 0, 255})
}
}
rl.DrawTextureRec(tilemap_image, source_rect, draw_pos, color)
}