Arkadodge/state.odin

47 lines
743 B
Odin
Raw Normal View History

2024-09-04 22:43:27 +03:00
package main
2024-09-05 22:39:52 +03:00
import "core:slice"
StateVariant :: union{^Game, ^Menu}
2024-09-04 22:43:27 +03:00
GameState :: struct {
2024-09-05 22:39:52 +03:00
update: proc(state: ^GameState, delta: f32),
draw: proc(state: ^GameState),
2024-09-04 22:43:27 +03:00
2024-09-05 22:39:52 +03:00
variant: StateVariant
2024-09-04 22:43:27 +03:00
}
new_state :: proc($T: typeid) -> ^T {
state := new(T)
state.variant = state
return state
}
2024-09-05 22:39:52 +03:00
STACK_SIZE :: 16
state_buf : [STACK_SIZE]^GameState
state_stack : [dynamic]^GameState
2024-09-05 23:42:18 +03:00
2024-09-05 22:39:52 +03:00
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
}