Neatened up comments and got game using scene manager.

This commit is contained in:
2026-09-08 08:27:15 +02:00
parent 2ce0d52e69
commit 655219738d
2 changed files with 64 additions and 45 deletions
+59 -42
View File
@@ -2,67 +2,84 @@ package game
import rl "vendor:raylib"
WINDOW_TITLE :: "Pong"
WINDOW_WIDTH :: 2560
WINDOW_HEIGHT :: 1440
WINDOW_TITLE :: "Pong"
State :: enum {
IN_GAME,
MENU,
LOADING,
PAUSED,
FINISHED
}
// good 16:9 baseline res, scales up to 1080 or 4k clean.
VIEWPORT_WIDTH :: 1280
VIEWPORT_HEIGHT :: 720
// Window size is what the viewport scales to
//WINDOW_WIDTH :: 2560
// WINDOW_HEIGHT :: 1440
WINDOW_WIDTH :: 1280
WINDOW_HEIGHT :: 720
Game :: struct {
state: State,
player: Entity,
enemy: Entity
current_scene: Scene,
is_paused: bool,
score_player: int,
score_enemy: int,
}
// @NOTE: procs ie: game_init, game should run etc is a convention to work with wasm
game_should_run :: proc() -> bool { return !rl.WindowShouldClose( )}
// ------------------------------------------------------------------------------
// Game initializes and sets up any needed state
// - Then we load in the initial scene
// ------------------------------------------------------------------------------
game_init :: proc(game: ^Game) {
rl.InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
// Window configs
rl.InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
rl.SetTargetFPS(60)
game.state = .IN_GAME
game.player = init_player()
// initial state
game.score_player = 0
game.score_enemy = 0
game.is_paused = false
// initial scene (should be menu when setup)
game.current_scene = init_gameplay_scene()
// init any systems/managers
// game.input_manager = ....
// game.audio_manager = ....
}
// ------------------------------------------------------------------------------
// Game loop update phase
// - TODO: get inputs pressed and store them
// - apply any updates to the current scene
// ------------------------------------------------------------------------------
game_update :: proc(game: ^Game) {
// get delta time for frame
dt := rl.GetFrameTime()
dt := rl.GetFrameTime()
// TODO: Get an input manager here
#partial switch game.state {
case .IN_GAME: update_gameplay(dt, game)
}
// Update the current scene
scene_update(game, dt)
}
// ------------------------------------------------------------------------------
// Game loop integration phase
// - after the states and scene has been updated render whats active
// ------------------------------------------------------------------------------
game_render :: proc(game: ^Game) {
rl.BeginDrawing()
rl.ClearBackground(rl.DARKGRAY)
rl.BeginDrawing()
rl.ClearBackground(rl.BLACK)
#partial switch game.state {
case .IN_GAME: render_gameplay(game)
}
rl.EndDrawing()
// render the current scene
scene_render(game)
rl.EndDrawing()
}
// ------------------------------------------------------------------------------
// Game shutdown
// - before closing windows etc we cleanup free anything that was allocated
// ------------------------------------------------------------------------------
game_quit :: proc(game: ^Game) {
rl.CloseWindow()
// TODO: any cleanup goes here before
// shutting down the window
rl.CloseWindow()
}
update_gameplay :: proc(dt: f32, game: ^Game) {
// game not paused for each component, ie player, enemies, environment
// run update functions here.
update_player(dt, &game.player)
// update_camera
// update_entities
}
render_gameplay :: proc(game: ^Game) {
render_player(game.player)
game_should_run :: proc() -> bool {
return !rl.WindowShouldClose()
}