146 lines
3.0 KiB
Odin
146 lines
3.0 KiB
Odin
// This file is compiled as part of the `odin.dll` file. It contains the
|
|
// procs that `game_hot_reload.exe` will call, such as:
|
|
//
|
|
// game_init: Sets up the game state
|
|
// game_update: Run once per frame
|
|
// game_shutdown: Shuts down game and frees memory
|
|
// game_memory: Run just before a hot reload, so game.exe has a pointer to the
|
|
// game's memory.
|
|
// game_hot_reloaded: Run after a hot reload so that the `g_mem` global variable
|
|
// can be set to whatever pointer it was in the old DLL.
|
|
//
|
|
// Note: When compiled as part of the release executable this whole package is imported as a normal
|
|
// odin package instead of a DLL.
|
|
|
|
package game
|
|
|
|
import "core:math/linalg"
|
|
import "core:fmt"
|
|
import rl "vendor:raylib"
|
|
|
|
PIXEL_WINDOW_HEIGHT :: 180
|
|
|
|
Game_Memory :: struct {
|
|
player_pos: rl.Vector2,
|
|
some_number: int,
|
|
}
|
|
|
|
g_mem: ^Game_Memory
|
|
|
|
game_camera :: proc() -> rl.Camera2D {
|
|
w := f32(rl.GetScreenWidth())
|
|
h := f32(rl.GetScreenHeight())
|
|
|
|
return {
|
|
zoom = h/PIXEL_WINDOW_HEIGHT,
|
|
target = g_mem.player_pos,
|
|
offset = { w/2, h/2 },
|
|
}
|
|
}
|
|
|
|
ui_camera :: proc() -> rl.Camera2D {
|
|
return {
|
|
zoom = f32(rl.GetScreenHeight())/PIXEL_WINDOW_HEIGHT,
|
|
}
|
|
}
|
|
|
|
update :: proc() {
|
|
input: rl.Vector2
|
|
|
|
if rl.IsKeyDown(.UP) || rl.IsKeyDown(.W) {
|
|
input.y -= 1
|
|
}
|
|
if rl.IsKeyDown(.DOWN) || rl.IsKeyDown(.S) {
|
|
input.y += 1
|
|
}
|
|
if rl.IsKeyDown(.LEFT) || rl.IsKeyDown(.A) {
|
|
input.x -= 1
|
|
}
|
|
if rl.IsKeyDown(.RIGHT) || rl.IsKeyDown(.D) {
|
|
input.x += 1
|
|
}
|
|
|
|
input = linalg.normalize0(input)
|
|
g_mem.player_pos += input * rl.GetFrameTime() * 100
|
|
g_mem.some_number += 1
|
|
}
|
|
|
|
draw :: proc() {
|
|
rl.BeginDrawing()
|
|
rl.ClearBackground(rl.BLACK)
|
|
|
|
rl.BeginMode2D(game_camera())
|
|
rl.DrawRectangleV(g_mem.player_pos, {10, 20}, rl.WHITE)
|
|
rl.DrawRectangleV({20, 20}, {10, 10}, rl.RED)
|
|
rl.DrawRectangleV({-30, -20}, {10, 10}, rl.GREEN)
|
|
rl.EndMode2D()
|
|
|
|
rl.BeginMode2D(ui_camera())
|
|
// Note: main_hot_reload.odin clears the temp allocator at end of frame.
|
|
rl.DrawText(fmt.ctprintf("some_number: %v\nplayer_pos: %v", g_mem.some_number, g_mem.player_pos), 5, 5, 8, rl.WHITE)
|
|
rl.EndMode2D()
|
|
|
|
rl.EndDrawing()
|
|
}
|
|
|
|
@(export)
|
|
game_update :: proc() -> bool {
|
|
update()
|
|
draw()
|
|
return !rl.WindowShouldClose()
|
|
}
|
|
|
|
@(export)
|
|
game_init_window :: proc() {
|
|
rl.SetConfigFlags({.WINDOW_RESIZABLE, .VSYNC_HINT})
|
|
rl.InitWindow(1280, 720, "Odin + Raylib + Hot Reload template!")
|
|
rl.SetWindowPosition(200, 200)
|
|
rl.SetTargetFPS(500)
|
|
}
|
|
|
|
@(export)
|
|
game_init :: proc() {
|
|
g_mem = new(Game_Memory)
|
|
|
|
g_mem^ = Game_Memory {
|
|
some_number = 100,
|
|
}
|
|
|
|
game_hot_reloaded(g_mem)
|
|
}
|
|
|
|
@(export)
|
|
game_shutdown :: proc() {
|
|
free(g_mem)
|
|
}
|
|
|
|
@(export)
|
|
game_shutdown_window :: proc() {
|
|
rl.CloseWindow()
|
|
}
|
|
|
|
@(export)
|
|
game_memory :: proc() -> rawptr {
|
|
return g_mem
|
|
}
|
|
|
|
@(export)
|
|
game_memory_size :: proc() -> int {
|
|
return size_of(Game_Memory)
|
|
}
|
|
|
|
@(export)
|
|
game_hot_reloaded :: proc(mem: rawptr) {
|
|
g_mem = (^Game_Memory)(mem)
|
|
}
|
|
|
|
@(export)
|
|
game_force_reload :: proc() -> bool {
|
|
return rl.IsKeyPressed(.F5)
|
|
}
|
|
|
|
@(export)
|
|
game_force_restart :: proc() -> bool {
|
|
return rl.IsKeyPressed(.F6)
|
|
}
|