static struct engine_State *_state;

void engine_state_pop(void) {
  if(_state == NULL) {
    return;
  }
  if(_state->end != NULL) {
    _state->end(_state->ud);
  }
  _state = _state->_prev;
  if(_state != NULL && _state->unpause != NULL) {
    _state->unpause(_state->ud);
  }
}

void engine_state_push(struct engine_State *state) {
  if(_state != NULL && _state->pause != NULL) {
    _state->pause(_state->ud);
  }
  state->_prev = _state;
  _state = state;
  if(_state->begin != NULL) {
    _state->begin(_state->ud);
  }
}

static bool _state_frame(void) {
  struct engine_State * current = _state;
  if(current == NULL) {
    return false;
  }
  while(current != NULL) {
    if(current->frame != NULL) {
      current->frame(current->ud);
    }
    current = current->_prev;
  }
  return true;
}

static void _state_ui(void) {
  struct engine_State * current = _state;
  if(current == NULL) {
    return;
  }
  while(current != NULL) {
    if(current->ui != NULL) {
      current->ui(current->ud);
    }
    current = current->_prev;
  }
}