I've been programming in c++ for a bit now and Im very familiar with the syntax. I'm trying to use Allegro to make a card game. I understand everything I need to do for the game logic and what not. What puzzles me is how to drive the game. I'm sort of new to loop based applications. I got used to event based programming in VB .Net. I'm just not sure the proper way of for example switching players and raising "events" without having lots of ifs and bools. Also right now I have an array of bool to check which card is in play. And my game iterates through the whole bool array every time and it seems messy to me. Also, if I want to go from my menu loop to my settings loop, how is that done without a big bool? Thanks
These are many different questions. My first suggestions would be to look for online source code for similar games. If you need help with the code please post it along with the question.
You will most likely need to have a bunch of ifs and loops going something like this:
while(1){
initYourLevel();
while(levelNotOver){
//do things
}
}
Then it all depends what you're trying to do and your question is kind of blurry to me. Start looking at tutorials to wrap your head around how people are doing things. There is a lot of sample code out there!
Hope that helps
Most gaming frameworks provide two methods you need to implement (both of them are called in a loop):
- Update
- Draw
The Update
is where you should put all that stuff, which should check for User input, state changes, intervalled actions etc. Examples would be Physics, ButtonPressed, etc. Nothing prevents you from working with events here (have a look at BoostLibrary Signals).
void Game::update() {
if(pushedButton) {
this->cardsOnTable->add(this->hand->remove(0));
this->activePlayer = nextPlayer();
}
}
The Draw
should just render the current, underlying state to the screen. So you have to make sure your underlying state/model is easy to access.
void Game::render() {
this->table->render();
this->cardsOnTable->render();
this->hand->render();
// ...
flipBuffers();
}
You can solve your Menu/SettingsMenu issue with Scenes
and a SceneManager
(which can be a Stack). So instead of putting the logic into the Game
directly, you put it into Scenes
. And you can push/pop scenes to/from the Manager.
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
}
}
If you want to start with more advanced stuff, you could try to store "events" into a huge list and fire all events when you enter the Game::update
method - this is how VB makes sure that you can't modify controls from another thread than the UI thread - but I don't think that this is something you would do using C++.
In general you might want to look into a book about programming idioms and design patterns, which are already discussed in other questions here. Even if not directly related to games, they will help you.
Game-Engines are a nice example of state machines (state in-game, state in-settings and sub-states) - you can do it all with conditional branching etc., but to not lose your head if it gets bigger you need to abstract.
There are different approaches to implementing state machines in c++, based on polymorphous classes (consult your favourite c++ pattern book), template-based (boost.statechart), ...
Whats common is, when using a central loop you forward events to some handler (representing states or substates) which will do what fits in the current situation. Need to change behaviour (e.g. from game to options)? Switch out the handlers.
You'll also want to wrap and abstract the current game-state - there is no need for explicit handling of e.g. cards in every place. Wrap things up in e.g. a card- or a player-manager which internally handles it.
Also utilize the containers and algorithms C++ gives you for free, you could e.g. manage the cards or players in std::map
s or std::set
s. When you hit the limits of the standard library check out boost.