Arkadodge/state.odin

46 lines
742 B
Odin

package main
import "core:slice"
StateVariant :: union{^Game, ^Menu}
GameState :: struct {
update: proc(state: ^GameState, delta: f32),
draw: proc(state: ^GameState),
variant: StateVariant
}
new_state :: proc($T: typeid) -> ^T {
state := new(T)
state.variant = state
return state
}
STACK_SIZE :: 16
state_buf : [STACK_SIZE]^GameState
state_stack : [dynamic]^GameState
stack_init :: proc() {
state_stack = slice.into_dynamic(state_buf[:])
}
stack_push :: proc(state: ^GameState) -> (bool) {
if len(state_stack) == STACK_SIZE {
return false
}
append(&state_stack, state)
return true
}
stack_pop :: proc() -> (bool) {
if len(state_stack) == 0 {
return false
}
state := pop(&state_stack)
free(state)
return true
}