From 8fb1ec1dcbd940e04e4a2ff71ca5f6f35b569df1 Mon Sep 17 00:00:00 2001 From: codegirl-007 Date: Wed, 1 Jul 2026 01:13:15 -0700 Subject: [PATCH] create basic tilemap --- constants.odin | 4 ++++ game.odin | 13 +++++++++++- main.odin | 1 + tilemap.odin | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 tilemap.odin diff --git a/constants.odin b/constants.odin index e0caab4..4647413 100644 --- a/constants.odin +++ b/constants.odin @@ -4,3 +4,7 @@ WINDOW_WIDTH :: 1280 WINDOW_HEIGHT :: 720 WINDOW_TITLE :: "Game 1" +TILE_SIZE :: 32 +MAP_WIDTH :: 40 +MAP_HEIGHT :: 25 + diff --git a/game.odin b/game.odin index 2538397..f50914b 100644 --- a/game.odin +++ b/game.odin @@ -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) +} + diff --git a/main.odin b/main.odin index ca2847d..2844992 100644 --- a/main.odin +++ b/main.odin @@ -9,6 +9,7 @@ main :: proc() { rl.SetTargetFPS(60) game := game_init() + defer game_shutdown(&game) for !rl.WindowShouldClose() { rl.BeginDrawing() diff --git a/tilemap.odin b/tilemap.odin new file mode 100644 index 0000000..f50da49 --- /dev/null +++ b/tilemap.odin @@ -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}) + } + } +} +