90 lines
2.3 KiB
Odin
90 lines
2.3 KiB
Odin
package game
|
|
|
|
import rl "vendor:raylib"
|
|
|
|
VIEWPORT_W :: 1280
|
|
VIEWPORT_H :: 720
|
|
WINDOW_W :: 2560
|
|
WINDOW_H :: 1440
|
|
WINDOW_TITLE :: "Pong"
|
|
|
|
Game :: struct {
|
|
viewport: rl.RenderTexture2D,
|
|
scene: Scene,
|
|
overlay_scene: Maybe(Scene),
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------
|
|
// Game Lifecycle
|
|
// ------------------------------------------------------------------------------
|
|
game_init :: proc(game: ^Game) {
|
|
rl.InitWindow(WINDOW_W, WINDOW_H, WINDOW_TITLE)
|
|
rl.SetTargetFPS(60)
|
|
|
|
game.viewport = rl.LoadRenderTexture(VIEWPORT_W, VIEWPORT_H)
|
|
game.scene = make_gameplay_scene()
|
|
// game.scene = make_main_menu()
|
|
|
|
debug_init()
|
|
}
|
|
|
|
game_update :: proc(game: ^Game) {
|
|
dt := rl.GetFrameTime()
|
|
input := poll_input()
|
|
|
|
// Pass direct pointers (^Scene) so modifications persist
|
|
if overlay, ok := &game.overlay_scene.?; ok {
|
|
scene_update(overlay, game, input, dt)
|
|
} else {
|
|
scene_update(&game.scene, game, input, dt)
|
|
}
|
|
|
|
// flush any events triggered in this loop
|
|
flush_events()
|
|
}
|
|
|
|
game_render :: proc(game: ^Game) {
|
|
rl.BeginDrawing()
|
|
rl.ClearBackground(rl.BLACK)
|
|
|
|
rl.BeginTextureMode(game.viewport)
|
|
rl.ClearBackground(rl.BLACK)
|
|
|
|
// Render base scene
|
|
scene_render(&game.scene)
|
|
|
|
// Render active overlay on top if present
|
|
if overlay, ok := &game.overlay_scene.?; ok {
|
|
scene_render(overlay)
|
|
}
|
|
rl.EndTextureMode()
|
|
|
|
render_game_to_window(game.viewport.texture)
|
|
|
|
rl.EndDrawing()
|
|
}
|
|
|
|
game_quit :: proc(game: ^Game) {
|
|
rl.UnloadRenderTexture(game.viewport)
|
|
rl.CloseWindow()
|
|
}
|
|
|
|
game_should_run :: proc() -> bool {
|
|
return !rl.WindowShouldClose()
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------
|
|
// Window Upscale Render
|
|
// ------------------------------------------------------------------------------
|
|
render_game_to_window :: proc(texture: rl.Texture) {
|
|
rl.DrawTexturePro(
|
|
texture,
|
|
// src: inverted y coordinate flips opengl texture buffer correctly
|
|
rl.Rectangle{0, 0, f32(texture.width), -f32(texture.height)},
|
|
// dst: scales viewport canvas to fill window resolution
|
|
rl.Rectangle{0, 0, f32(WINDOW_W), f32(WINDOW_H)},
|
|
rl.Vector2{},
|
|
0,
|
|
rl.WHITE,
|
|
)
|
|
} |