Skip to content

Instantly share code, notes, and snippets.

@naranyala
Created June 21, 2025 23:27
Show Gist options
  • Save naranyala/b45c9c654837a5f07d62a7710e74f616 to your computer and use it in GitHub Desktop.
Save naranyala/b45c9c654837a5f07d62a7710e74f616 to your computer and use it in GitHub Desktop.
starter for your game levels using odin+raylib code
package main
import rl "vendor:raylib"
State :: enum {
LevelPicker,
LevelView,
}
main :: proc() {
screen_width :: 800
screen_height :: 600
rl.InitWindow(i32(screen_width), i32(screen_height), "Level Switcher with Back")
rl.SetTargetFPS(60)
box_width :: 100
box_height :: 60
spacing_x :: 20
spacing_y :: 20
cols :: 5
rows :: 3
start_x :: (screen_width - (cols * box_width + (cols - 1) * spacing_x)) / 2
start_y :: (screen_height - (rows * box_height + (rows - 1) * spacing_y)) / 2
state := State.LevelPicker
selected_level := -1
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.RAYWHITE)
mouse_pos := rl.GetMousePosition()
if state == State.LevelPicker {
for row in 0..<rows {
for col in 0..<cols {
i := row * cols + col
x := f32(start_x + col * (box_width + spacing_x))
y := f32(start_y + row * (box_height + spacing_y))
rect := rl.Rectangle{x, y, f32(box_width), f32(box_height)}
rl.DrawRectangleRec(rect, rl.LIGHTGRAY)
rl.DrawRectangleLinesEx(rect, 2, rl.DARKGRAY)
label := rl.TextFormat("%d", i32(i + 1))
text_width := rl.MeasureText(label, 20)
rl.DrawText(label,
i32(x + f32(box_width - int(text_width))/2),
i32(y + f32(box_height - 20)/2),
20,
rl.DARKBLUE)
if rl.CheckCollisionPointRec(mouse_pos, rect) && rl.IsMouseButtonPressed(rl.MouseButton.LEFT) {
selected_level = i + 1
state = State.LevelView
}
}
}
} else if state == State.LevelView {
message := rl.TextFormat("Level %d", i32(selected_level))
text_width := rl.MeasureText(message, 40)
rl.DrawText(message, i32((screen_width - int(text_width))/2), i32(screen_height/2 - 50), 40, rl.DARKGREEN)
// Back button
back_width :: 120
back_height :: 40
back_x := f32((screen_width - back_width)/2)
back_y := f32(screen_height - 100)
back_rect := rl.Rectangle{back_x, back_y, f32(back_width), f32(back_height)}
rl.DrawRectangleRec(back_rect, rl.SKYBLUE)
rl.DrawRectangleLinesEx(back_rect, 2, rl.BLUE)
rl.DrawText("Back", i32(back_x + 30), i32(back_y + 10), 20, rl.DARKBLUE)
if rl.CheckCollisionPointRec(mouse_pos, back_rect) && rl.IsMouseButtonPressed(rl.MouseButton.LEFT) {
state = State.LevelPicker
selected_level = -1
}
}
rl.EndDrawing()
}
rl.CloseWindow()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment