Skip to content

Commit

Permalink
Refactor: Improved GameCollection and Game interfaces for C++ usage
Browse files Browse the repository at this point in the history
The internal C wrappers will ultimately be dropped altogether...
  • Loading branch information
skyjake committed Feb 10, 2013
1 parent 40bfe97 commit dfa8a94
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 99 deletions.
2 changes: 1 addition & 1 deletion doomsday/client/include/dd_games.h
Expand Up @@ -61,7 +61,7 @@ namespace de
/// @return @c true= this game's title is lexically less than that of @a other.
bool operator < (GameListItem const& other) const
{
return Str_CompareIgnoreCase(&game->title(), Str_Text(&other.game->title())) < 0;
return Str_CompareIgnoreCase(game->title(), Str_Text(other.game->title())) < 0;
}
};
typedef QList<GameListItem> GameList;
Expand Down
36 changes: 12 additions & 24 deletions doomsday/client/include/dd_main.h
Expand Up @@ -24,24 +24,24 @@
#ifndef LIBDENG_MAIN_H
#define LIBDENG_MAIN_H

#ifndef __cplusplus
# error "C++ required"
#endif

#include "dd_types.h"
#include "dd_games.h"
#include "api_plugin.h"
#include "api_gameexport.h"
#include "resource/materials.h"
#include "resource/textures.h"
#include "filesys/sys_direc.h"
#include <de/c_wrapper.h>

#ifdef __cplusplus

#include <QList>
#include <QMap>
#include <de/String>
#include "resourceclass.h"

extern "C" {
#endif

// Verbose messages.
#define VERBOSE(code) { if(verbose >= 1) { code; } }
#define VERBOSE2(code) { if(verbose >= 2) { code; } }
Expand All @@ -56,7 +56,6 @@ extern "C" {
# define DEBUG_VERBOSE2_Message(code)
#endif

struct gamecollection_s;
struct game_s;

extern int verbose;
Expand All @@ -78,10 +77,10 @@ extern GETGAMEAPI GetGameAPI;
extern int gameDataFormat;

/// @return The Game collection.
struct gamecollection_s* App_GameCollection();
de::GameCollection& App_GameCollection();

/// @return The current Game in the collection.
struct game_s* App_CurrentGame();
de::Game *App_CurrentGame();

int DD_EarlyInit(void);
void DD_FinishInitializationAfterWindowReady(void);
Expand Down Expand Up @@ -140,16 +139,12 @@ void DD_CreateResourceClasses();

void DD_ClearResourceClasses();

#ifdef __cplusplus
} // extern "C"

namespace de {

typedef QList<ResourceClass*> ResourceClasses;

/// Map of symbolic file type names to file types.
typedef QMap<String, FileType*> FileTypes;
namespace de
{
typedef QList<ResourceClass*> ResourceClasses;

/// Map of symbolic file type names to file types.
typedef QMap<String, FileType*> FileTypes;
}

/**
Expand Down Expand Up @@ -192,9 +187,6 @@ de::ResourceClass& DD_ResourceClassByName(de::String name);
/// @return Symbolic name of the material scheme associated with @a textureSchemeName.
de::String DD_MaterialSchemeNameForTextureScheme(de::String textureSchemeName);

extern "C" {
#endif // __cplusplus

fontschemeid_t DD_ParseFontSchemeName(char const *str);

/// @return Symbolic name of the material scheme associated with @a textureSchemeName.
Expand All @@ -215,8 +207,4 @@ D_CMD(Unload);
D_CMD(Reset);
D_CMD(ReloadGame);

#ifdef __cplusplus
} // extern "C"
#endif

#endif /* LIBDENG_MAIN_H */
11 changes: 6 additions & 5 deletions doomsday/client/include/game.h
Expand Up @@ -53,6 +53,7 @@ struct gamedef_s;

#ifdef __cplusplus

#include <de/Error>
#include <QMultiMap>

namespace de {
Expand Down Expand Up @@ -90,19 +91,19 @@ class Game
pluginid_t pluginId() const;

/// @return String containing the identity key.
ddstring_t const& identityKey() const;
ddstring_t const *identityKey() const;

/// @return String containing the default title.
ddstring_t const& title() const;
ddstring_t const *title() const;

/// @return String containing the default author.
ddstring_t const& author() const;
ddstring_t const *author() const;

/// @return String containing the name of the main config file.
ddstring_t const& mainConfig() const;
ddstring_t const *mainConfig() const;

/// @return String containing the name of the binding config file.
ddstring_t const& bindingConfig() const;
ddstring_t const *bindingConfig() const;

/**
* Change the identfier of the plugin associated with this.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/client/src/client/cl_main.cpp
Expand Up @@ -133,7 +133,7 @@ void Cl_SendHello(void)

// The game mode is included in the hello packet.
memset(buf, 0, sizeof(buf));
strncpy(buf, Str_Text(Game_IdentityKey(App_CurrentGame())), sizeof(buf) - 1);
strncpy(buf, Str_Text(App_CurrentGame()->identityKey()), sizeof(buf) - 1);

#ifdef _DEBUG
Con_Message("Cl_SendHello: game mode = %s\n", buf);
Expand Down
3 changes: 2 additions & 1 deletion doomsday/client/src/con_config.cpp
Expand Up @@ -278,7 +278,8 @@ boolean Con_WriteState(const char* fileName, const char* bindingsFileName)
*/
void Con_SaveDefaults(void)
{
Con_WriteState(cfgFile, (!isDedicated? Str_Text(Game_BindingConfig(App_CurrentGame())) : 0));
Con_WriteState(cfgFile, (!isDedicated && App_CurrentGame()?
Str_Text(App_CurrentGame()->bindingConfig()) : 0));
}

D_CMD(WriteConsole)
Expand Down
20 changes: 12 additions & 8 deletions doomsday/client/src/con_data.cpp
Expand Up @@ -253,7 +253,7 @@ static int compareKnownWordByName(void const* a, void const* b)
textAString = CVar_ComposePath((cvar_t*)wA->data);
textA = Str_Text(textAString);
break;
case WT_GAME: textA = Str_Text(&reinterpret_cast<de::Game*>(wA->data)->identityKey()); break;
case WT_GAME: textA = Str_Text(reinterpret_cast<de::Game*>(wA->data)->identityKey()); break;
default:
Con_Error("compareKnownWordByName: Invalid type %i for word A.", wA->type);
exit(1); // Unreachable
Expand All @@ -267,7 +267,7 @@ static int compareKnownWordByName(void const* a, void const* b)
textBString = CVar_ComposePath((cvar_t*)wB->data);
textB = Str_Text(textBString);
break;
case WT_GAME: textB = Str_Text(&reinterpret_cast<de::Game*>(wB->data)->identityKey()); break;
case WT_GAME: textB = Str_Text(reinterpret_cast<de::Game*>(wB->data)->identityKey()); break;
default:
Con_Error("compareKnownWordByName: Invalid type %i for word B.", wB->type);
exit(1); // Unreachable
Expand Down Expand Up @@ -369,7 +369,11 @@ static void updateKnownWords(void)
}

// Build the known words table.
numKnownWords = numUniqueNamedCCmds + countCVarParams.count + numCAliases + GameCollection_Count(App_GameCollection());
numKnownWords =
numUniqueNamedCCmds +
countCVarParams.count +
numCAliases +
App_GameCollection().count();
size_t len = sizeof(knownword_t) * numKnownWords;
knownWords = (knownword_t*) M_Realloc(knownWords, len);
memset(knownWords, 0, len);
Expand Down Expand Up @@ -409,7 +413,7 @@ static void updateKnownWords(void)
}

// Add games?
de::GameCollection& gameCollection = reinterpret_cast<de::GameCollection&>( *App_GameCollection() );
de::GameCollection& gameCollection = App_GameCollection();
DENG2_FOR_EACH_CONST(de::GameCollection::Games, i, gameCollection.games())
{
knownWords[knownWordIdx].type = WT_GAME;
Expand Down Expand Up @@ -1294,7 +1298,7 @@ static AutoStr* textForKnownWord(knownword_t const* word)
case WT_CALIAS: Str_Set(text = AutoStr_NewStd(), ((calias_t*)word->data)->name); break;
case WT_CCMD: Str_Set(text = AutoStr_NewStd(), ((ccmd_t*)word->data)->name); break;
case WT_CVAR: text = CVar_ComposePath((cvar_t*)word->data); break;
case WT_GAME: Str_Set(text = AutoStr_NewStd(), Str_Text(&reinterpret_cast<de::Game*>(word->data)->identityKey())); break;
case WT_GAME: Str_Set(text = AutoStr_NewStd(), Str_Text(reinterpret_cast<de::Game*>(word->data)->identityKey())); break;
default:
Con_Error("textForKnownWord: Invalid type %i for word.", word->type);
exit(1); // Unreachable
Expand Down Expand Up @@ -1465,7 +1469,7 @@ static int aproposPrinter(knownword_t const* word, void* matching)
}
else if(word->type == WT_GAME)
{
Str_Set(&tmp, Str_Text(&reinterpret_cast<de::Game*>(word->data)->title()));
Str_Set(&tmp, Str_Text(reinterpret_cast<de::Game*>(word->data)->title()));
}

// Truncate.
Expand Down Expand Up @@ -1558,7 +1562,7 @@ static void printHelpAbout(char const* query)
{
try
{
de::Game& game = reinterpret_cast<de::GameCollection&>( *App_GameCollection() ).byIdentityKey(query);
de::Game& game = App_GameCollection().byIdentityKey(query);
de::Game::print(game, PGF_EVERYTHING);
found = true;
}
Expand Down Expand Up @@ -1627,7 +1631,7 @@ static int printKnownWordWorker(knownword_t const* word, void* parameters)

case WT_GAME: {
de::Game* game = (de::Game*) word->data;
Con_FPrintf(CPF_LIGHT|CPF_BLUE, " %s\n", Str_Text(&game->identityKey()));
Con_FPrintf(CPF_LIGHT|CPF_BLUE, " %s\n", Str_Text(game->identityKey()));
break; }

default:
Expand Down
20 changes: 10 additions & 10 deletions doomsday/client/src/dd_games.cpp
@@ -1,4 +1,4 @@
/** @file dd_games.cpp Game collection.
/** @file dd_games.cpp Game collection.
* @ingroup core
*
* @authors Copyright &copy; 2012-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
Expand Down Expand Up @@ -145,7 +145,7 @@ Game& GameCollection::byIdentityKey(char const* identityKey) const
DENG2_FOR_EACH_CONST(GameCollection::Games, i, d->games)
{
Game* game = *i;
if(!Str_CompareIgnoreCase(&game->identityKey(), identityKey))
if(!Str_CompareIgnoreCase(game->identityKey(), identityKey))
return *game;
}
}
Expand Down Expand Up @@ -227,7 +227,7 @@ static int locateAllResourcesWorker(void* parameters)
{
Game* game = *i;

Con_Message("Locating \"%s\"...\n", Str_Text(&game->title()));
Con_Message("Locating \"%s\"...\n", Str_Text(game->title()));

gameCollection->locateStartupResources(*game);
Con_SetProgress((n + 1) * 200 / gameCollection->count() - 1);
Expand All @@ -252,8 +252,8 @@ D_CMD(ListGames)
{
DENG_UNUSED(src); DENG_UNUSED(argc); DENG_UNUSED(argv);

de::GameCollection* games = reinterpret_cast<de::GameCollection*>(App_GameCollection());
if(!games || !games->count())
de::GameCollection& games = App_GameCollection();
if(!games.count())
{
Con_Printf("No Registered Games.\n");
return true;
Expand All @@ -264,7 +264,7 @@ D_CMD(ListGames)
Con_PrintRuler();

de::GameCollection::GameList found;
games->findAll(found);
games.findAll(found);
// Sort so we get a nice alphabetical list.
qSort(found.begin(), found.end());

Expand All @@ -273,17 +273,17 @@ D_CMD(ListGames)
{
de::Game* game = i->game;

Con_Printf(" %s %-16s %s (%s)\n", games->isCurrentGame(*game)? "*" :
Con_Printf(" %s %-16s %s (%s)\n", games.isCurrentGame(*game)? "*" :
!game->allStartupFilesFound()? "!" : " ",
Str_Text(&game->identityKey()), Str_Text(&game->title()),
Str_Text(&game->author()));
Str_Text(game->identityKey()), Str_Text(game->title()),
Str_Text(game->author()));

if(game->allStartupFilesFound())
numCompleteGames++;
}

Con_PrintRuler();
Con_Printf("%i of %i games playable.\n", numCompleteGames, games->count());
Con_Printf("%i of %i games playable.\n", numCompleteGames, games.count());
Con_Printf("Use the 'load' command to load a game. For example: \"load gamename\".\n");

return true;
Expand Down
27 changes: 14 additions & 13 deletions doomsday/client/src/dd_main.cpp
Expand Up @@ -1155,7 +1155,7 @@ static int DD_ActivateGameWorker(void* parameters)
}
else
{
configFileName = &games->currentGame().mainConfig();
configFileName = games->currentGame().mainConfig();
}

Con_Message("Parsing primary config \"%s\"...\n", F_PrettyPath(Str_Text(configFileName)));
Expand All @@ -1171,7 +1171,7 @@ static int DD_ActivateGameWorker(void* parameters)
B_BindGameDefaults();

// Read bindings for this game and merge with the working set.
Con_ParseCommands2(Str_Text(&games->currentGame().bindingConfig()), CPCF_ALLOW_SAVE_BINDINGS);
Con_ParseCommands2(Str_Text(games->currentGame().bindingConfig()), CPCF_ALLOW_SAVE_BINDINGS);
}
#endif

Expand Down Expand Up @@ -1226,22 +1226,23 @@ static int DD_ActivateGameWorker(void* parameters)
return 0;
}

struct gamecollection_s* App_GameCollection()
de::GameCollection &App_GameCollection()
{
return reinterpret_cast<struct gamecollection_s*>(games);
DENG2_ASSERT(games != 0);
return *games;
}

struct game_s* App_CurrentGame()
de::Game *App_CurrentGame()
{
if(!games) return 0;
return reinterpret_cast<struct game_s*>(&games->currentGame());
return &games->currentGame();
}

static void populateGameInfo(GameInfo& info, de::Game& game)
{
info.identityKey = Str_Text(&game.identityKey());
info.title = Str_Text(&game.title());
info.author = Str_Text(&game.author());
info.identityKey = Str_Text(game.identityKey());
info.title = Str_Text(game.title());
info.author = Str_Text(game.author());
}

/// @note Part of the Doomsday public API.
Expand Down Expand Up @@ -1362,7 +1363,7 @@ bool DD_ChangeGame(de::Game& game, bool allowReload = false)
if(!allowReload)
{
if(DD_GameLoaded())
Con_Message("%s (%s) - already loaded.\n", Str_Text(&game.title()), Str_Text(&game.identityKey()));
Con_Message("%s (%s) - already loaded.\n", Str_Text(game.title()), Str_Text(game.identityKey()));
return true;
}
// We are re-loading.
Expand Down Expand Up @@ -1492,7 +1493,7 @@ bool DD_ChangeGame(de::Game& game, bool allowReload = false)
VERBOSE(
if(!isNullGame(game))
{
Con_Message("Selecting game '%s'...\n", Str_Text(&game.identityKey()));
Con_Message("Selecting game '%s'...\n", Str_Text(game.identityKey()));
}
else if(!isReload)
{
Expand Down Expand Up @@ -2734,7 +2735,7 @@ D_CMD(Load)
{
Con_Message("Failed to locate all required startup resources:\n");
de::Game::printFiles(game, FF_STARTUP);
Con_Message("%s (%s) cannot be loaded.\n", Str_Text(&game.title()), Str_Text(&game.identityKey()));
Con_Message("%s (%s) cannot be loaded.\n", Str_Text(game.title()), Str_Text(game.identityKey()));
Str_Free(&searchPath);
return true;
}
Expand Down Expand Up @@ -2871,7 +2872,7 @@ D_CMD(Unload)
return DD_ChangeGame(games->nullGame());
}

Con_Message("%s is not currently loaded.\n", Str_Text(&game.identityKey()));
Con_Message("%s is not currently loaded.\n", Str_Text(game.identityKey()));
return true;
}
catch(de::GameCollection::NotFoundError const&)
Expand Down
3 changes: 1 addition & 2 deletions doomsday/client/src/dd_pinit.cpp
Expand Up @@ -75,8 +75,7 @@ void DD_ComposeMainWindowTitle(char* title)
if(DD_GameLoaded() && gx.GetVariable)
{
sprintf(title, DOOMSDAY_NICENAME " " DOOMSDAY_VERSION_TEXT "%s - %s",
(isDedicated? " (Dedicated)" : ""), Str_Text(Game_Title(App_CurrentGame()))/*,
(char*) gx.GetVariable(DD_PLUGIN_NICENAME), (char*) gx.GetVariable(DD_PLUGIN_VERSION_SHORT)*/);
(isDedicated? " (Dedicated)" : ""), Str_Text(App_CurrentGame()->title()));
}
else
{
Expand Down

0 comments on commit dfa8a94

Please sign in to comment.