Neatened up comments and got game using scene manager.
This commit is contained in:
+53
-36
@@ -3,66 +3,83 @@ package game
|
||||
import rl "vendor:raylib"
|
||||
|
||||
WINDOW_TITLE :: "Pong"
|
||||
WINDOW_WIDTH :: 2560
|
||||
WINDOW_HEIGHT :: 1440
|
||||
|
||||
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) {
|
||||
// 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()
|
||||
// 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.ClearBackground(rl.BLACK)
|
||||
|
||||
// render the current scene
|
||||
scene_render(game)
|
||||
|
||||
#partial switch game.state {
|
||||
case .IN_GAME: render_gameplay(game)
|
||||
}
|
||||
rl.EndDrawing()
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// Game shutdown
|
||||
// - before closing windows etc we cleanup free anything that was allocated
|
||||
// ------------------------------------------------------------------------------
|
||||
game_quit :: proc(game: ^Game) {
|
||||
// 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()
|
||||
}
|
||||
+5
-3
@@ -17,9 +17,11 @@ main :: proc() {
|
||||
defer mem.tracking_allocator_destroy(&track)
|
||||
context.allocator = mem.tracking_allocator(&track)
|
||||
}
|
||||
// ------------------------------------------------------
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
// Game Start
|
||||
// ------------------------------------------------------------------------------
|
||||
// Game Loop
|
||||
// ------------------------------------------------------------------------------
|
||||
game := Game{}
|
||||
game_init(&game)
|
||||
|
||||
@@ -29,11 +31,11 @@ main :: proc() {
|
||||
game_render(&game)
|
||||
}
|
||||
|
||||
// Game Stop
|
||||
game_quit(&game)
|
||||
|
||||
// ------------------------------------------------------
|
||||
// Check for any leaks!
|
||||
// ------------------------------------------------------
|
||||
when DEBUG {
|
||||
if len(track.allocation_map) > 0 {
|
||||
log.errorf("\nMEMORY LEAKS: %v allocation(s)", len(track.allocation_map))
|
||||
|
||||
Reference in New Issue
Block a user