support full screen gameplay

This commit is contained in:
2026-06-21 01:27:45 -07:00
parent ff341cb834
commit fcfd1e95d8
5 changed files with 82 additions and 16 deletions
+13 -12
View File
@@ -3,29 +3,30 @@ package game
import rl "vendor:raylib"
run :: proc() {
rl.InitWindow(SCREEN_W, SCREEN_H, "Tower Defense")
defer rl.CloseWindow()
rl.SetTargetFPS(60)
init_display()
defer {
shutdown_display()
rl.CloseWindow()
}
world := init_world()
defer {
delete(world.path)
delete(world.towers)
delete(world.events)
}
for !rl.WindowShouldClose() {
dt := rl.GetFrameTime()
update_display()
update_world(&world, dt)
rl.BeginDrawing()
rl.ClearBackground(rl.Color{34, 139, 34, 255})
begin_game_draw()
render_world(&world)
render_overlay(&world)
render_controls(&world)
rl.EndDrawing()
end_game_draw()
}
delete(world.path)
delete(world.towers)
delete(world.events)
}
+1 -1
View File
@@ -23,7 +23,7 @@ gather_commands :: proc(world: ^World) -> Command {
}
if world.phase == .Build && rl.IsMouseButtonPressed(.LEFT) {
mouse := rl.GetMousePosition()
mouse := game_mouse()
gx, gy := screen_to_grid(mouse.x, mouse.y)
return Command{kind = .Place_Tower, gx = gx, gy = gy}
}
+3
View File
@@ -9,6 +9,9 @@ TILE_SIZE :: 32 // size of tiles in pixels
SCREEN_W :: MAP_W * TILE_SIZE
SCREEN_H :: MAP_H * TILE_SIZE
DEFAULT_WINDOW_W :: 1280
DEFAULT_WINDOW_H :: 720
MAX_ENEMIES :: 64
MAX_WAVES :: 5
MAX_TOWERS :: 32
+3 -3
View File
@@ -22,7 +22,7 @@ build_controls_layout :: proc() -> (archer, start_wave_btn: rl.Rectangle) {
poll_controls_command :: proc(world: ^World) -> Command {
if world.phase != .Build do return Command{kind = .None}
mouse := rl.GetMousePosition()
mouse := game_mouse()
if rl.IsMouseButtonPressed(.LEFT) {
archer, start_wave := build_controls_layout()
if rl.CheckCollisionPointRec(mouse, archer) {
@@ -38,7 +38,7 @@ poll_controls_command :: proc(world: ^World) -> Command {
draw_archer_button :: proc(world: ^World, archer: rl.Rectangle) {
arch := get_archetype(.Archer)
mouse := rl.GetMousePosition()
mouse := game_mouse()
arch_bg := CONTROLS_SHOP_IDLE
if world.selected_tower == .Archer do arch_bg = CONTROL_SHOP_SELECTED
@@ -57,7 +57,7 @@ draw_start_wave_button :: proc(start_wave_rect: rl.Rectangle, mouse: rl.Vector2)
draw_build_controls :: proc(world: ^World) {
if world.phase != .Build do return
mouse := rl.GetMousePosition()
mouse := game_mouse()
bar_y := bottom_bar_y()
rl.DrawRectangle(0, bar_y, SCREEN_W, CONTROL_BAR_H, CONTROL_BAR_BG)
+62
View File
@@ -0,0 +1,62 @@
package game
import rl "vendor:raylib"
game_target: rl.RenderTexture2D
init_display :: proc() {
rl.InitWindow(DEFAULT_WINDOW_W, DEFAULT_WINDOW_H, "Tower Defense")
rl.SetWindowState({.WINDOW_RESIZABLE})
rl.SetTargetFPS(60)
game_target = rl.LoadRenderTexture(i32(SCREEN_W), i32(SCREEN_H))
}
shutdown_display :: proc() {
rl.UnloadRenderTexture(game_target)
}
update_display :: proc() {
if rl.IsKeyPressed(.F11) {
rl.ToggleFullscreen()
}
}
game_viewport_dest :: proc() -> rl.Rectangle {
sw := f32(rl.GetRenderWidth())
sh := f32(rl.GetRenderHeight())
scale := min(sw / f32(SCREEN_W), sh / f32(SCREEN_H))
dw := f32(SCREEN_W) * scale
dh := f32(SCREEN_H) * scale
return {x = (sw - dw) * 0.5, y = (sh - dh) * 0.5, width = dw, height = dh}
}
game_mouse :: proc() -> rl.Vector2 {
m := rl.GetMousePosition()
dest := game_viewport_dest()
if dest.width <= 0 || dest.height <= 0 {
return m
}
return {
(m.x - dest.x) / dest.width * f32(SCREEN_W),
(m.y - dest.y) / dest.height * f32(SCREEN_H),
}
}
begin_game_draw :: proc() {
rl.BeginTextureMode(game_target)
rl.ClearBackground(rl.Color{34, 139, 34, 255})
}
end_game_draw :: proc() {
rl.EndTextureMode()
rl.BeginDrawing()
rl.ClearBackground(rl.BLACK)
dest := game_viewport_dest()
source := rl.Rectangle{0, 0, f32(SCREEN_W), -f32(SCREEN_H)}
rl.DrawTexturePro(game_target.texture, source, dest, {}, 0, rl.WHITE)
rl.EndDrawing()
}