create basic tilemap

This commit is contained in:
2026-07-01 01:13:15 -07:00
parent bb25247c43
commit 8fb1ec1dcb
4 changed files with 74 additions and 1 deletions
+4
View File
@@ -4,3 +4,7 @@ WINDOW_WIDTH :: 1280
WINDOW_HEIGHT :: 720
WINDOW_TITLE :: "Game 1"
TILE_SIZE :: 32
MAP_WIDTH :: 40
MAP_HEIGHT :: 25
+12 -1
View File
@@ -5,10 +5,16 @@ import rl "vendor:raylib"
Game :: struct {
running: bool,
time: f32, // total time elapsed
tilemap: Tilemap,
}
game_init :: proc() -> Game {
return Game{running = true, time = 0}
g := Game {
running = true,
time = 0,
}
g.tilemap = tilemap_init()
return g
}
game_update :: proc(g: ^Game, dt: f32) {
@@ -20,8 +26,13 @@ game_update :: proc(g: ^Game, dt: f32) {
}
game_draw :: proc(g: ^Game) {
tilemap_draw(&g.tilemap)
fps := rl.GetFPS()
rl.DrawText(rl.TextFormat("FPS: %i", fps), 10, 36, 20, rl.GREEN)
rl.DrawText(rl.TextFormat("Time: %.1fs", g.time), 10, 62, 20, rl.GRAY)
}
game_shutdown :: proc(g: ^Game) {
tilemap_destroy(&g.tilemap)
}
+1
View File
@@ -9,6 +9,7 @@ main :: proc() {
rl.SetTargetFPS(60)
game := game_init()
defer game_shutdown(&game)
for !rl.WindowShouldClose() {
rl.BeginDrawing()
+57
View File
@@ -0,0 +1,57 @@
package main
import rl "vendor:raylib"
Tilemap :: struct {
width: int,
height: int,
tiles: []u8,
}
tilemap_init :: proc() -> Tilemap {
return Tilemap {
width = MAP_WIDTH,
height = MAP_HEIGHT,
tiles = make([]u8, MAP_WIDTH * MAP_HEIGHT),
}
}
tilemap_destroy :: proc(tilemap: ^Tilemap) {
delete(tilemap.tiles)
}
tile_in_bounds :: proc(tilemap: ^Tilemap, x: int, y: int) -> bool {
// learning from another game, this will become handy
return x >= 0 && x < tilemap.width && y >= 0 && y < tilemap.height
}
tile_index :: proc(tilemap: ^Tilemap, x, y: int) -> int {
return y * tilemap.width + x
}
tile_color :: proc(type_id: u8) -> rl.Color {
// going to make this a table at some point but for now, we do this
switch type_id {
case 0:
return {72, 112, 68, 255}
case:
return rl.MAGENTA
}
}
tilemap_draw :: proc(tilemap: ^Tilemap) {
for y in 0 ..< tilemap.height {
for x in 0 ..< tilemap.width {
index := tile_index(tilemap, x, y)
type_id := tilemap.tiles[index]
color := tile_color(type_id)
px := i32(x * TILE_SIZE)
py := i32(y * TILE_SIZE)
rl.DrawRectangle(px, py, TILE_SIZE, TILE_SIZE, color)
rl.DrawRectangleLines(px, py, TILE_SIZE, TILE_SIZE, {40, 50, 38, 255})
}
}
}