From fcfd1e95d8c6e48811e872570127021b70cadfdd Mon Sep 17 00:00:00 2001 From: codegirl-007 Date: Sun, 21 Jun 2026 01:27:45 -0700 Subject: [PATCH] support full screen gameplay --- game/app.odin | 25 +++++++++--------- game/commands.odin | 2 +- game/constants.odin | 3 +++ game/controls.odin | 6 ++--- game/display.odin | 62 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 game/display.odin diff --git a/game/app.odin b/game/app.odin index 89df930..2445fe5 100644 --- a/game/app.odin +++ b/game/app.odin @@ -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) - } diff --git a/game/commands.odin b/game/commands.odin index a812a2b..4e45811 100644 --- a/game/commands.odin +++ b/game/commands.odin @@ -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} } diff --git a/game/constants.odin b/game/constants.odin index 1a4f41b..f0d4301 100644 --- a/game/constants.odin +++ b/game/constants.odin @@ -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 diff --git a/game/controls.odin b/game/controls.odin index ec11f06..55472b7 100644 --- a/game/controls.odin +++ b/game/controls.odin @@ -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) diff --git a/game/display.odin b/game/display.odin new file mode 100644 index 0000000..643e24e --- /dev/null +++ b/game/display.odin @@ -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() +} +