Skip to content

Commit

Permalink
Refactor|Games: Updated for DENG2_PIMPL, added game change audience
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed May 21, 2013
1 parent 90e1fbe commit a54b169
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
12 changes: 9 additions & 3 deletions doomsday/client/include/games.h
Expand Up @@ -25,6 +25,7 @@
#include "dd_share.h"
#include <de/types.h>
#include <de/str.h>
#include <de/Observers>
#include <QList>

namespace de {
Expand Down Expand Up @@ -61,9 +62,13 @@ class Games
/// Game instances.
typedef QList<Game *> All;

/**
* Notified when the current game is changed.
*/
DENG2_DEFINE_AUDIENCE(GameChange, void currentGameChanged(Game &newGame))

public:
Games();
~Games();

/// Register the console commands, variables, etc..., of this module.
static void consoleRegister();
Expand Down Expand Up @@ -110,6 +115,8 @@ class Games
*/
Game &byIndex(int idx) const;

void clear();

/**
* Add a new Game to this collection. If @a game is already present in the
* collection this is no-op.
Expand Down Expand Up @@ -148,8 +155,7 @@ class Games
void locateStartupResources(Game &game);

private:
struct Instance;
Instance *d;
DENG2_PRIVATE(d)
};

} // namespace de
Expand Down
44 changes: 27 additions & 17 deletions doomsday/client/src/games.cpp
Expand Up @@ -40,30 +40,33 @@ DENG2_PIMPL(Games)
/// Special "null-game" object for this collection.
NullGame *nullGame;

Instance(Public &a)
: Base(a), games(), currentGame(0), nullGame(0)
{}
Instance(Public *i) : Base(i), games(), currentGame(0), nullGame(0)
{
/*
* One-time creation and initialization of the special "null-game"
* object (activated once created).
*/
currentGame = nullGame = new NullGame;
}

~Instance()
{
clear();
delete nullGame;
}

void clear()
{
DENG2_ASSERT(nullGame != 0);

qDeleteAll(games);
if(nullGame) delete nullGame;
games.clear();
currentGame = nullGame;
}
};

Games::Games() : d(new Instance(*this))
{
/*
* One-time creation and initialization of the special "null-game"
* object (activated once created).
*/
d->currentGame = d->nullGame = new NullGame();
}

Games::~Games()
{
delete d;
}
Games::Games() : d(new Instance(this))
{}

Game &Games::current() const
{
Expand All @@ -80,6 +83,8 @@ void Games::setCurrent(Game &game)
// Ensure the specified game is actually in this collection (NullGame is implicitly).
DENG_ASSERT(isNullGame(game) || id(game) > 0);
d->currentGame = &game;

DENG2_FOR_AUDIENCE(GameChange, i) i->currentGameChanged(game);
}

int Games::numPlayable() const
Expand Down Expand Up @@ -149,6 +154,11 @@ Game &Games::byIndex(int idx) const
return *d->games[idx];
}

void Games::clear()
{
d->clear();
}

Games::All const &Games::all() const
{
return d->games;
Expand Down

0 comments on commit a54b169

Please sign in to comment.