Neatened up comments and got game using scene manager.
This commit is contained in:
+59
-42
@@ -2,67 +2,84 @@ package game
|
|||||||
|
|
||||||
import rl "vendor:raylib"
|
import rl "vendor:raylib"
|
||||||
|
|
||||||
WINDOW_TITLE :: "Pong"
|
WINDOW_TITLE :: "Pong"
|
||||||
WINDOW_WIDTH :: 2560
|
|
||||||
WINDOW_HEIGHT :: 1440
|
|
||||||
|
|
||||||
State :: enum {
|
// good 16:9 baseline res, scales up to 1080 or 4k clean.
|
||||||
IN_GAME,
|
VIEWPORT_WIDTH :: 1280
|
||||||
MENU,
|
VIEWPORT_HEIGHT :: 720
|
||||||
LOADING,
|
// Window size is what the viewport scales to
|
||||||
PAUSED,
|
//WINDOW_WIDTH :: 2560
|
||||||
FINISHED
|
// WINDOW_HEIGHT :: 1440
|
||||||
}
|
WINDOW_WIDTH :: 1280
|
||||||
|
WINDOW_HEIGHT :: 720
|
||||||
|
|
||||||
Game :: struct {
|
Game :: struct {
|
||||||
state: State,
|
current_scene: Scene,
|
||||||
player: Entity,
|
is_paused: bool,
|
||||||
enemy: Entity
|
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) {
|
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)
|
rl.SetTargetFPS(60)
|
||||||
|
|
||||||
game.state = .IN_GAME
|
// initial state
|
||||||
game.player = init_player()
|
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) {
|
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 {
|
// Update the current scene
|
||||||
case .IN_GAME: update_gameplay(dt, game)
|
scene_update(game, dt)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------
|
||||||
|
// Game loop integration phase
|
||||||
|
// - after the states and scene has been updated render whats active
|
||||||
|
// ------------------------------------------------------------------------------
|
||||||
game_render :: proc(game: ^Game) {
|
game_render :: proc(game: ^Game) {
|
||||||
rl.BeginDrawing()
|
rl.BeginDrawing()
|
||||||
rl.ClearBackground(rl.DARKGRAY)
|
rl.ClearBackground(rl.BLACK)
|
||||||
|
|
||||||
#partial switch game.state {
|
// render the current scene
|
||||||
case .IN_GAME: render_gameplay(game)
|
scene_render(game)
|
||||||
}
|
|
||||||
rl.EndDrawing()
|
rl.EndDrawing()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------
|
||||||
|
// Game shutdown
|
||||||
|
// - before closing windows etc we cleanup free anything that was allocated
|
||||||
|
// ------------------------------------------------------------------------------
|
||||||
game_quit :: proc(game: ^Game) {
|
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_should_run :: proc() -> bool {
|
||||||
// game not paused for each component, ie player, enemies, environment
|
return !rl.WindowShouldClose()
|
||||||
// run update functions here.
|
|
||||||
|
|
||||||
update_player(dt, &game.player)
|
|
||||||
// update_camera
|
|
||||||
// update_entities
|
|
||||||
}
|
|
||||||
|
|
||||||
render_gameplay :: proc(game: ^Game) {
|
|
||||||
render_player(game.player)
|
|
||||||
}
|
}
|
||||||
+5
-3
@@ -17,9 +17,11 @@ main :: proc() {
|
|||||||
defer mem.tracking_allocator_destroy(&track)
|
defer mem.tracking_allocator_destroy(&track)
|
||||||
context.allocator = mem.tracking_allocator(&track)
|
context.allocator = mem.tracking_allocator(&track)
|
||||||
}
|
}
|
||||||
// ------------------------------------------------------
|
// ------------------------------------------------------------------------------
|
||||||
|
|
||||||
// Game Start
|
// ------------------------------------------------------------------------------
|
||||||
|
// Game Loop
|
||||||
|
// ------------------------------------------------------------------------------
|
||||||
game := Game{}
|
game := Game{}
|
||||||
game_init(&game)
|
game_init(&game)
|
||||||
|
|
||||||
@@ -29,11 +31,11 @@ main :: proc() {
|
|||||||
game_render(&game)
|
game_render(&game)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Game Stop
|
|
||||||
game_quit(&game)
|
game_quit(&game)
|
||||||
|
|
||||||
// ------------------------------------------------------
|
// ------------------------------------------------------
|
||||||
// Check for any leaks!
|
// Check for any leaks!
|
||||||
|
// ------------------------------------------------------
|
||||||
when DEBUG {
|
when DEBUG {
|
||||||
if len(track.allocation_map) > 0 {
|
if len(track.allocation_map) > 0 {
|
||||||
log.errorf("\nMEMORY LEAKS: %v allocation(s)", len(track.allocation_map))
|
log.errorf("\nMEMORY LEAKS: %v allocation(s)", len(track.allocation_map))
|
||||||
|
|||||||
Reference in New Issue
Block a user