大多数游戏框架提供了两种您需要实现的方法(它们都在循环中调用):
这Update是你应该把所有东西放在哪里,它应该检查用户输入,状态变化,间隔动作等.例子是Physics,ButtonPressed等.没有什么能阻止你在这里处理事件(看看BoostLibrary信号).
void Game::update() {
if(pushedButton) {
this->cardsOnTable->add(this->hand->remove(0));
this->activePlayer = nextPlayer();
}
}
Run Code Online (Sandbox Code Playgroud)
本Draw应该只呈现当前,潜在的状态到屏幕上.因此,您必须确保您的基础状态/模型易于访问.
void Game::render() {
this->table->render();
this->cardsOnTable->render();
this->hand->render();
// ...
flipBuffers();
}
Run Code Online (Sandbox Code Playgroud)
您可以使用Scenes和SceneManager(可以是堆栈)解决您的Menu/SettingsMenu问题.因此,不是Game直接将逻辑放入,而是将其放入Scenes.您可以向/从管理器推送/弹出场景.
void Game::update() {
this->sceneManager->activeScene->update();
}
void MenuScene::update() {
if(settingsMenuItemSelected) {
game->sceneManager->push(new SettingsMenuScene));
// now the next time Game::update() is called
// the active scene of the SceneManager will be
// the settings menu. And once it is closed/cancelled
// it will pop itself from the manager and we are back
// in the regular menu
}
}
Run Code Online (Sandbox Code Playgroud)
如果你想从更高级的东西开始,你可以尝试将"事件"存储到一个巨大的列表中并在你输入Game::update方法时触发所有事件- 这就是VB确保你不能修改来自另一个线程的控件而不是UI线程 - 但我不认为这是你用C++做的事情.