Compare commits

..
6 Commits
Author SHA1 Message Date
jasonhilder 133c328c32 Big rework for menus. 2026-09-15 07:59:52 +02:00
jasonhilder 86c792e39c Passed game for should close flag. 2026-09-15 07:59:31 +02:00
jasonhilder efcb9325fc New events for event manager. 2026-09-15 07:59:00 +02:00
jasonhilder e0ff1a51fd Integrated events and should close flag. 2026-09-15 07:58:51 +02:00
jasonhilder 676a24e883 Small clean up and win score set. 2026-09-15 07:58:28 +02:00
jasonhilder c82c51aabf Cleaned/improved input manager.
Added single frame actions for menus etc
Cleaned up continuous actions and set both to be captured.
2026-09-15 07:58:19 +02:00
6 changed files with 200 additions and 70 deletions
+3
View File
@@ -2,7 +2,10 @@ package game
// all possible game events // all possible game events
Event_Type :: enum { Event_Type :: enum {
Game_Start,
Game_Over, Game_Over,
Game_Quit,
Score_Change, Score_Change,
Ball_Collision, Ball_Collision,
} }
+11 -6
View File
@@ -12,6 +12,7 @@ Game :: struct {
viewport: rl.RenderTexture2D, viewport: rl.RenderTexture2D,
scene: Scene, scene: Scene,
overlay_scene: Maybe(Scene), overlay_scene: Maybe(Scene),
should_close: bool,
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
@@ -21,11 +22,13 @@ game_init :: proc(game: ^Game) {
rl.InitWindow(WINDOW_W, WINDOW_H, WINDOW_TITLE) rl.InitWindow(WINDOW_W, WINDOW_H, WINDOW_TITLE)
rl.SetTargetFPS(60) rl.SetTargetFPS(60)
game.viewport = rl.LoadRenderTexture(VIEWPORT_W, VIEWPORT_H) // allocate some event capacity to stop early dynamic reallocations
game.scene = make_gameplay_scene() GLOBAL_EVENTS = make([dynamic]Event, 0, 16)
// game.scene = make_main_menu()
debug_init() game.viewport = rl.LoadRenderTexture(VIEWPORT_W, VIEWPORT_H)
game.scene = make_main_menu()
game.overlay_scene = nil
game.should_close = false
} }
game_update :: proc(game: ^Game) { game_update :: proc(game: ^Game) {
@@ -64,13 +67,15 @@ game_render :: proc(game: ^Game) {
rl.EndDrawing() rl.EndDrawing()
} }
game_quit :: proc(game: ^Game) { game_quit :: proc(game: ^Game) {
delete(GLOBAL_EVENTS)
rl.UnloadRenderTexture(game.viewport) rl.UnloadRenderTexture(game.viewport)
rl.CloseWindow() rl.CloseWindow()
} }
game_should_run :: proc() -> bool { game_should_run :: proc(game: Game) -> bool {
return !rl.WindowShouldClose() return !game.should_close && !rl.WindowShouldClose()
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
+18 -11
View File
@@ -4,12 +4,17 @@ import rl "vendor:raylib"
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// Input Manager, captures input events and stores them into a struct. // Input Manager, captures input events and stores them into a struct.
// Passing the struct down allows for components to use inputs independantly // Passing the struct down allows for components to use inputs independently
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
Input_Event :: struct { Input_Event :: struct {
// Normalized and merged from keyboard + controller // Continuous movement normalized
move_dir: rl.Vector2, move_dir: rl.Vector2,
// single frame actions UI and triggers
up_pressed: bool,
down_pressed: bool,
confirm_pressed: bool,
pause_pressed: bool, pause_pressed: bool,
} }
@@ -17,22 +22,24 @@ poll_input :: proc() -> Input_Event {
input_event := Input_Event{} input_event := Input_Event{}
input_dir := rl.Vector2{0, 0} input_dir := rl.Vector2{0, 0}
// ------------------------------------------------------------------------------------------------------ // --------------------------------------------------------------------------
// Keyboard // Continuous Input ie movement code
// ------------------------------------------------------------------------------------------------------ // --------------------------------------------------------------------------
if rl.IsKeyDown(.W) || rl.IsKeyDown(.UP) { input_dir.y -= 1 } if rl.IsKeyDown(.W) || rl.IsKeyDown(.UP) { input_dir.y -= 1 }
if rl.IsKeyDown(.S) || rl.IsKeyDown(.DOWN) { input_dir.y += 1 } if rl.IsKeyDown(.S) || rl.IsKeyDown(.DOWN) { input_dir.y += 1 }
// single-fire == IsKeyPressed, held == IsKeyDown
input_event.pause_pressed = rl.IsKeyPressed(.P)
// ------------------------------------------------------------------------------------------------------
// Movement/direction needs update
// ------------------------------------------------------------------------------------------------------
if input_dir.x != 0 || input_dir.y != 0 { if input_dir.x != 0 || input_dir.y != 0 {
input_dir = rl.Vector2Normalize(input_dir) input_dir = rl.Vector2Normalize(input_dir)
} }
input_event.move_dir = input_dir input_event.move_dir = input_dir
// --------------------------------------------------------------------------
// actions single press ie menu navigation etc
// --------------------------------------------------------------------------
input_event.up_pressed = rl.IsKeyPressed(.W) || rl.IsKeyPressed(.UP)
input_event.down_pressed = rl.IsKeyPressed(.S) || rl.IsKeyPressed(.DOWN)
input_event.confirm_pressed = rl.IsKeyPressed(.ENTER) || rl.IsKeyPressed(.SPACE)
input_event.pause_pressed = rl.IsKeyPressed(.P)
return input_event return input_event
} }
+4 -4
View File
@@ -6,6 +6,9 @@ import rl "vendor:raylib"
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// LEVEL specifics // LEVEL specifics
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
FIELD_BORDER :: 3
SERVE_WAIT_TIME :: 1.0
Level :: struct { Level :: struct {
field_bounds: rl.Rectangle, field_bounds: rl.Rectangle,
player: Entity, player: Entity,
@@ -18,9 +21,6 @@ Level :: struct {
score_win: int, score_win: int,
} }
FIELD_BORDER :: 3
SERVE_WAIT_TIME :: 1.0
init_level :: proc() -> Level { init_level :: proc() -> Level {
// positions work off viewport resolution, independent of target display size // positions work off viewport resolution, independent of target display size
// 10% padding around the field // 10% padding around the field
@@ -49,9 +49,9 @@ init_level :: proc() -> Level {
ball = init_ball(), ball = init_ball(),
serve_timer = SERVE_WAIT_TIME, serve_timer = SERVE_WAIT_TIME,
wait_time = SERVE_WAIT_TIME, wait_time = SERVE_WAIT_TIME,
score_win = 1,
score_player = 0, score_player = 0,
score_enemy = 0, score_enemy = 0,
score_win = 2,
} }
} }
+1 -1
View File
@@ -26,7 +26,7 @@ main :: proc() {
game_init(&game) game_init(&game)
// Game loop // Game loop
for game_should_run() { for game_should_run(game) {
game_update(&game) game_update(&game)
game_render(&game) game_render(&game)
} }
+152 -37
View File
@@ -15,23 +15,149 @@ Scene_Type :: enum {
Scene :: struct { Scene :: struct {
type: Scene_Type, type: Scene_Type,
level: Maybe(Level), level: Maybe(Level),
canvas: Maybe(UI_Canvas), canvas: Maybe(Canvas),
winner_is_player: bool, // Store win state for rendering winner_is_player: bool,
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// UI Definitions // UI Definitions
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
UI_Canvas :: struct { UI_Menu :: struct {
items: [8]cstring,
count: int,
selected_index: int,
}
Canvas :: struct {
bounds: rl.Rectangle, bounds: rl.Rectangle,
padding: f32, padding: f32,
item_height: f32, item_height: f32,
item_spaceing: f32, item_spaceing: f32,
menu: Maybe(UI_Menu),
}
get_canvas_item_rec :: proc(canvas: Canvas, index: int, top_margin: f32 = 120) -> rl.Rectangle {
y_pos := (canvas.bounds.y + top_margin) + (f32(index) * (canvas.item_height + canvas.item_spaceing))
return rl.Rectangle{
x = canvas.bounds.x + canvas.padding,
y = y_pos,
width = canvas.bounds.width - (canvas.padding * 2.0),
height = canvas.item_height
}
}
render_main_menu :: proc(scene: Scene) {
canvas, ok := scene.canvas.?
// Assert or log error if canvas is unexpectedly missing
if !ok { return }
// Safely unwrap the embedded menu state
menu, menu_ok := canvas.menu.?
if !menu_ok { return }
// draw canvas base background
rl.DrawRectangleRec(canvas.bounds, rl.BLACK)
// draw the menu header
t_text: cstring = "PONG"
t_width := rl.MeasureText(t_text, 40)
t_x := i32(canvas.bounds.width * 0.5) - (t_width / 2)
rl.DrawText(t_text, t_x, i32(canvas.padding), 40, rl.WHITE)
// Iterate up to menu.count using the data inside UI_Menu
for i in 0 ..< menu.count {
item := menu.items[i]
item_rec := get_canvas_item_rec(canvas, i, top_margin = 150.0)
text_width := rl.MeasureText(item, 20)
text_x := i32(item_rec.x + (item_rec.width * 0.5)) - (text_width / 2)
text_y := i32(item_rec.y + (item_rec.height * 0.5)) - 10
// Highlight selected item box & text
is_selected := (i == menu.selected_index)
border_color := is_selected ? rl.ORANGE : rl.DARKGRAY
text_color := is_selected ? rl.ORANGE : rl.WHITE
rl.DrawRectangleLinesEx(item_rec, 3, border_color)
rl.DrawText(item, text_x, text_y, 20, text_color)
}
}
update_main_menu :: proc(scene: ^Scene, input: Input_Event) {
// pointer to Canvas
canvas, canvas_ok := &scene.canvas.?
if !canvas_ok do return
// pointer to UI_Menu
menu, menu_ok := &canvas.menu.?
if !menu_ok do return
// get some navigation events
if input.up_pressed {
menu.selected_index = (menu.selected_index - 1 + menu.count) % menu.count
}
if input.down_pressed {
menu.selected_index = (menu.selected_index + 1) % menu.count
}
if input.confirm_pressed {
switch menu.selected_index {
case 0: // Start Game
emit_event(Event{ type = .Game_Start })
case 1: // Options
// @todo open options scene/overlay
case 2: // Quit
emit_event(Event{ type = .Game_Quit })
}
}
}
// @todo
render_game_over :: proc(scene: Scene) {
rl.DrawRectangleRec(
rl.Rectangle{0, 0, f32(VIEWPORT_W), f32(VIEWPORT_H)},
rl.Color{0, 0, 0, 200},
)
msg :cstring = scene.winner_is_player ? "YOU WIN!" : "GAME OVER"
text_color := scene.winner_is_player ? rl.GREEN : rl.RED
rl.DrawText(msg, VIEWPORT_W / 2 - 100, VIEWPORT_H / 2 - 40, 40, text_color)
rl.DrawText("Press P to Play Again", VIEWPORT_W / 2 - 110, VIEWPORT_H / 2 + 10, 20, rl.LIGHTGRAY)
}
// @todo
render_pause_menu :: proc(scene: Scene) {
rl.DrawRectangleRec(
rl.Rectangle{0, 0, f32(VIEWPORT_W), f32(VIEWPORT_H)},
rl.Color{0, 0, 0, 140},
)
rl.DrawText("PAUSED", VIEWPORT_W / 2 - 80, VIEWPORT_H / 2 - 40, 40, rl.WHITE)
rl.DrawText("Press P to Resume", VIEWPORT_W / 2 - 90, VIEWPORT_H / 2 + 10, 20, rl.LIGHTGRAY)
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// Scene Constructors // Scene Constructors
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
make_main_menu :: proc() -> Scene {
return Scene{
type = .Main_Menu,
canvas = Canvas{
bounds = rl.Rectangle{0, 0, VIEWPORT_W, VIEWPORT_H},
padding = 20,
item_height = 40,
item_spaceing = 15,
menu = UI_Menu{
items = {0 = "Start Game", 1 = "Options", 2 = "Quit"},
count = 3,
selected_index = 0,
},
},
}
}
// @todo
make_gameplay_scene :: proc() -> Scene { make_gameplay_scene :: proc() -> Scene {
return Scene{ return Scene{
type = .Gameplay, type = .Gameplay,
@@ -39,24 +165,19 @@ make_gameplay_scene :: proc() -> Scene {
} }
} }
// @todo
make_pause_overlay :: proc() -> Scene { make_pause_overlay :: proc() -> Scene {
return Scene{ return Scene{
type = .Pause_Menu, type = .Pause_Menu,
canvas = UI_Canvas{}, canvas = Canvas{},
}
}
make_main_menu :: proc() -> Scene {
return Scene{
type = .Main_Menu,
canvas = UI_Canvas{},
} }
} }
// @todo
make_game_over_overlay :: proc(winner_is_player: bool) -> Scene { make_game_over_overlay :: proc(winner_is_player: bool) -> Scene {
return Scene{ return Scene{
type = .Game_Over, type = .Game_Over,
canvas = UI_Canvas{}, canvas = Canvas{},
winner_is_player = winner_is_player, winner_is_player = winner_is_player,
} }
} }
@@ -72,6 +193,9 @@ scene_update :: proc(scene: ^Scene, game: ^Game, input: Input_Event, dt: f32) {
// dispatch scene behavior // dispatch scene behavior
#partial switch scene.type { #partial switch scene.type {
case .Main_Menu:
update_main_menu(scene, input)
case .Gameplay: case .Gameplay:
if input.pause_pressed { if input.pause_pressed {
game.overlay_scene = make_pause_overlay() game.overlay_scene = make_pause_overlay()
@@ -83,20 +207,27 @@ scene_update :: proc(scene: ^Scene, game: ^Game, input: Input_Event, dt: f32) {
} }
case .Game_Over: case .Game_Over:
if input.pause_pressed { // Press P or your start key to restart // Press P or your start key to restart
if input.pause_pressed {
game.overlay_scene = nil game.overlay_scene = nil
game.scene = make_gameplay_scene() // Reset the base level game.scene = make_gameplay_scene()
} }
case .Main_Menu:
// @todo: handle menu selection / start game
} }
// Loop over global frame events // Loop over global frame events
for event in GLOBAL_EVENTS { for event in GLOBAL_EVENTS {
#partial switch event.type { #partial switch event.type {
case .Game_Start:
// clear old state
game.overlay_scene = nil
// swap to the gameplay scene
// @todo consider different levels here?
game.scene = make_gameplay_scene()
case .Game_Over: case .Game_Over:
game.overlay_scene = make_game_over_overlay(event.winner_is_player) game.overlay_scene = make_game_over_overlay(event.winner_is_player)
case .Game_Quit:
game.should_close = true
} }
} }
} }
@@ -111,27 +242,11 @@ scene_render :: proc(scene: ^Scene) {
// render scene/overlay ui elements // render scene/overlay ui elements
#partial switch scene.type { #partial switch scene.type {
case .Pause_Menu: case .Pause_Menu:
rl.DrawRectangleRec( render_pause_menu(scene^)
rl.Rectangle{0, 0, f32(VIEWPORT_W), f32(VIEWPORT_H)},
rl.Color{0, 0, 0, 140},
)
rl.DrawText("PAUSED", VIEWPORT_W / 2 - 80, VIEWPORT_H / 2 - 40, 40, rl.WHITE)
rl.DrawText("Press P to Resume", VIEWPORT_W / 2 - 90, VIEWPORT_H / 2 + 10, 20, rl.LIGHTGRAY)
case .Game_Over: case .Game_Over:
rl.DrawRectangleRec( render_game_over(scene^)
rl.Rectangle{0, 0, f32(VIEWPORT_W), f32(VIEWPORT_H)},
rl.Color{0, 0, 0, 200},
)
msg :cstring = scene.winner_is_player ? "YOU WIN!" : "GAME OVER"
text_color := scene.winner_is_player ? rl.GREEN : rl.RED
rl.DrawText(msg, VIEWPORT_W / 2 - 100, VIEWPORT_H / 2 - 40, 40, text_color)
rl.DrawText("Press P to Play Again", VIEWPORT_W / 2 - 110, VIEWPORT_H / 2 + 10, 20, rl.LIGHTGRAY)
case .Main_Menu: case .Main_Menu:
rl.DrawText("PONG", VIEWPORT_W / 2 - 50, VIEWPORT_H / 3, 40, rl.WHITE) render_main_menu(scene^)
rl.DrawText("Press ENTER to Start", VIEWPORT_W / 2 - 110, VIEWPORT_H / 2, 20, rl.LIGHTGRAY)
} }
} }