53 lines
		
	
	
		
			922 B
		
	
	
	
		
			Odin
		
	
	
	
	
	
			
		
		
	
	
			53 lines
		
	
	
		
			922 B
		
	
	
	
		
			Odin
		
	
	
	
	
	
package main
 | 
						|
 | 
						|
import "core:slice"
 | 
						|
 | 
						|
StateVariant :: union{^Game, ^Menu, ^Pause, ^GameOver, ^LevelDone, ^Howtoplay}
 | 
						|
 | 
						|
GameState :: struct {
 | 
						|
	update: proc(state: ^GameState, delta: f32),
 | 
						|
	draw: proc(state: ^GameState),
 | 
						|
	free: proc(state: ^GameState),
 | 
						|
	previous: ^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)
 | 
						|
	state->free()
 | 
						|
	return true
 | 
						|
}
 | 
						|
 | 
						|
stack_top :: proc() -> ^GameState {
 | 
						|
	return state_stack[len(state_stack)-1]
 | 
						|
}
 |