Skip to content

Commit

Permalink
Refactor|libcommon: G_MapAuthor() and G_MapTitle() now return de::String
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Jun 30, 2014
1 parent d3752be commit 5810582
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 109 deletions.
12 changes: 12 additions & 0 deletions doomsday/plugins/common/include/g_common.h
Expand Up @@ -114,6 +114,18 @@ char const *G_InFineBriefing(de::Uri const *mapUri);
*/
char const *G_InFineDebriefing(de::Uri const *mapUri);

/**
* @param mapUri Identifier of the map to lookup the author of. Can be @c 0 in which
* case the author for the @em current map will be returned (if set).
*/
de::String G_MapAuthor(de::Uri const *mapUri, bool supressGameAuthor = false);

/**
* @param mapUri Identifier of the map to lookup the title of. Can be @c 0 in which
* case the title for the @em current map will be returned (if set).
*/
de::String G_MapTitle(de::Uri const *mapUri);

extern "C" {
#endif

Expand Down
12 changes: 0 additions & 12 deletions doomsday/plugins/common/include/p_mapsetup.h
Expand Up @@ -52,18 +52,6 @@ void P_SetupMap(Uri const *uri);
*/
void P_ResetWorldState();

/**
* @param mapUri Identifier of the map to lookup the author of. Can be @c 0 in which
* case the author for the @em current map will be returned (if set).
*/
char const *P_MapAuthor(Uri const *mapUri, dd_bool supressGameAuthor);

/**
* @param mapUri Identifier of the map to lookup the title of. Can be @c 0 in which
* case the title for the @em current map will be returned (if set).
*/
char const *P_MapTitle(Uri const *mapUri);

/**
* @param mapUri Identifier of the map to lookup the title of. Can be @c 0 in which
* case the title for the @em current map will be returned (if set).
Expand Down
44 changes: 18 additions & 26 deletions doomsday/plugins/common/src/g_game.cpp
Expand Up @@ -108,11 +108,6 @@ void G_StopDemo();
*/
void G_UpdateGSVarsForPlayer(player_t *pl);

/**
* Updates game status cvars for the current map.
*/
void G_UpdateGSVarsForMap();

void R_LoadVectorGraphics();

int Hook_DemoStop(int hookType, int val, void *parm);
Expand Down Expand Up @@ -1200,22 +1195,23 @@ static void printMapBanner()
{
App_Log(DE2_LOG_MESSAGE, DE2_ESC(R));

if(char const *title = P_MapTitle(0/*current map*/))
String const title = G_MapTitle(0/*current map*/);
if(!title.isEmpty())
{
de::String text = de::String("Map: ") + gameMapUri.path().asText();
String text = String("Map: ") + gameMapUri.path().asText();
#if __JHEXEN__
mapinfo_t const *mapInfo = P_MapInfo(0/*current map*/);
text += de::String(" (%1)").arg(mapInfo? mapInfo->warpTrans + 1 : 0);
text += String(" (%1)").arg(mapInfo? mapInfo->warpTrans + 1 : 0);
#endif
text += de::String(" - " DE2_ESC(b)) + title;
text += String(" - " DE2_ESC(b)) + title;
App_Log(DE2_LOG_NOTE, "%s", text.toUtf8().constData());
}

#if !__JHEXEN__
char const *author = P_MapAuthor(0/*current map*/, P_MapIsCustom(gameMapUri.compose().toUtf8().constData()));
if(!author) author = "Unknown";
String author = G_MapAuthor(0/*current map*/, P_MapIsCustom(gameMapUri.compose().toUtf8().constData()));
if(author.isEmpty()) author = "Unknown";

App_Log(DE2_LOG_NOTE, "Author: %s", author);
App_Log(DE2_LOG_NOTE, "Author: %s", author.toUtf8().constData());
#endif
App_Log(DE2_LOG_MESSAGE, DE2_ESC(R));
}
Expand All @@ -1231,7 +1227,15 @@ void G_BeginMap()
}

G_ControlReset(-1); // Clear all controls for all local players.
G_UpdateGSVarsForMap();

// Update the game status cvars for the current map.
String mapAuthor = G_MapAuthor(0/*current map*/);
if(mapAuthor.isEmpty()) mapAuthor = "Unknown";
Con_SetString2("map-author", mapAuthor.toUtf8().constData(), SVF_WRITE_OVERRIDE);

String mapTitle = G_MapTitle(0/*current map*/);
if(mapTitle.isEmpty()) mapTitle = "Unknown";
Con_SetString2("map-name", mapTitle.toUtf8().constData(), SVF_WRITE_OVERRIDE);

// Time can now progress in this map.
mapTime = actualMapTime = 0;
Expand Down Expand Up @@ -1355,18 +1359,6 @@ void G_UpdateGSVarsForPlayer(player_t *pl)
#endif
}

void G_UpdateGSVarsForMap()
{
char const *mapAuthor = P_MapAuthor(0/*current map*/, false/*don't supress*/);
char const *mapTitle = P_MapTitle(0/*current map*/);

if(!mapAuthor) mapAuthor = "Unknown";
Con_SetString2("map-author", mapAuthor, SVF_WRITE_OVERRIDE);

if(!mapTitle) mapTitle = "Unknown";
Con_SetString2("map-name", mapTitle, SVF_WRITE_OVERRIDE);
}

static sfxenum_t randomQuitSound()
{
#if __JDOOM__ || __JDOOM64__
Expand Down Expand Up @@ -2330,7 +2322,7 @@ de::String G_DefaultSavedSessionUserDescription(de::String const &saveName, bool
}

// Include the map title.
de::String mapTitle = de::String(P_MapTitle(0/*current map*/));
de::String mapTitle = G_MapTitle(0/*current map*/);
// No map title? Use the identifier. (Some tricksy modders provide us with an empty title).
/// @todo Move this logic engine-side.
if(mapTitle.isEmpty() || mapTitle.at(0) == ' ')
Expand Down
28 changes: 15 additions & 13 deletions doomsday/plugins/common/src/hu_stuff.cpp
Expand Up @@ -757,11 +757,12 @@ static void drawMapMetaData(float x, float y, float alpha)
{
#define BORDER 2

char const *title = P_MapTitle(0/*current map*/);
if(!title) title = "Unnamed";
de::String title = G_MapTitle(0/*current map*/);
if(title.isEmpty()) title = "Unnamed";

char buf[256];
dd_snprintf(buf, 256, "%s - %s", COMMON_GAMESESSION->rules().description().toLatin1().constData(), title);
dd_snprintf(buf, 256, "%s - %s", COMMON_GAMESESSION->rules().description().toLatin1().constData(),
title.toLatin1().constData());

FR_SetColorAndAlpha(1, 1, 1, alpha);
FR_DrawTextXY2(buf, x + BORDER, y - BORDER, ALIGN_BOTTOMLEFT);
Expand Down Expand Up @@ -1481,10 +1482,10 @@ int Hu_MapTitleFirstLineHeight()
}
#endif

dd_bool Hu_IsMapTitleAuthorVisible(void)
dd_bool Hu_IsMapTitleAuthorVisible()
{
char const *author = P_MapAuthor(0/*current map*/, cfg.hideIWADAuthor);
return author != 0 && (actualMapTime <= 6 * TICSPERSEC);
de::String const author = G_MapAuthor(0/*current map*/, CPP_BOOL(cfg.hideIWADAuthor));
return !author.isEmpty() && (actualMapTime <= 6 * TICSPERSEC);
}

int Hu_MapTitleHeight(void)
Expand All @@ -1502,8 +1503,8 @@ int Hu_MapTitleHeight(void)

void Hu_DrawMapTitle(float alpha, dd_bool mapIdInsteadOfAuthor)
{
char const *title = P_MapTitle(0/*current map*/);
char const *author = P_MapAuthor(0/*current map*/, cfg.hideIWADAuthor);
de::String const title = G_MapTitle(0/*current map*/);
de::String const author = G_MapAuthor(0/*current map*/, CPP_BOOL(cfg.hideIWADAuthor));

float y = 0;

Expand All @@ -1516,15 +1517,16 @@ void Hu_DrawMapTitle(float alpha, dd_bool mapIdInsteadOfAuthor)

#if __JDOOM__ || __JDOOM64__
patchid_t patchId = P_MapTitlePatch(0/*current map*/);
WI_DrawPatchXY3(patchId, Hu_ChoosePatchReplacement2(PRM_ALLOW_TEXT, patchId, title), 0, 0, ALIGN_TOP, 0, DTF_ONLY_SHADOW);
WI_DrawPatchXY3(patchId, Hu_ChoosePatchReplacement2(PRM_ALLOW_TEXT, patchId, title.toUtf8().constData()),
0, 0, ALIGN_TOP, 0, DTF_ONLY_SHADOW);

// Following line of text placed according to patch height.
y += Hu_MapTitleFirstLineHeight();

#elif __JHERETIC__ || __JHEXEN__
if(title)
if(!title.isEmpty())
{
FR_DrawTextXY3(title, 0, 0, ALIGN_TOP, DTF_ONLY_SHADOW);
FR_DrawTextXY3(title.toUtf8().constData(), 0, 0, ALIGN_TOP, DTF_ONLY_SHADOW);
y += 20;
}
#endif
Expand All @@ -1539,11 +1541,11 @@ void Hu_DrawMapTitle(float alpha, dd_bool mapIdInsteadOfAuthor)
#endif
FR_DrawTextXY3(gameMapUri.path().toUtf8().constData(), 0, y, ALIGN_TOP, DTF_ONLY_SHADOW);
}
else if(author)
else if(!author.isEmpty())
{
FR_SetFont(FID(GF_FONTA));
FR_SetColorAndAlpha(.5f, .5f, .5f, alpha);
FR_DrawTextXY3(author, 0, y, ALIGN_TOP, DTF_ONLY_SHADOW);
FR_DrawTextXY3(author.toUtf8().constData(), 0, y, ALIGN_TOP, DTF_ONLY_SHADOW);
}

DGL_Disable(DGL_TEXTURE_2D);
Expand Down
55 changes: 25 additions & 30 deletions doomsday/plugins/common/src/p_mapsetup.cpp
Expand Up @@ -1102,12 +1102,11 @@ void P_ResetWorldState()
#endif
}

char const *P_MapTitle(uri_s const *mapUri_)
String G_MapTitle(de::Uri const *mapUri)
{
de::Uri const *mapUri = reinterpret_cast<de::Uri const *>(mapUri_);
if(!mapUri) mapUri = &gameMapUri;

char const *title = 0;
String title;

// Perhaps a MapInfo definition exists for the map?
ddmapinfo_t mapInfo;
Expand All @@ -1130,7 +1129,7 @@ char const *P_MapTitle(uri_s const *mapUri_)

#if __JHEXEN__
// In Hexen we can also look in MAPINFO for the map title.
if(!title)
if(title.isEmpty())
{
if(mapinfo_t const *mapInfo = P_MapInfo(mapUri))
{
Expand All @@ -1139,49 +1138,45 @@ char const *P_MapTitle(uri_s const *mapUri_)
}
#endif

if(!title || !title[0])
return 0;

// Skip the "ExMx" part, if present.
if(char const *ptr = strchr(title, ':'))
int idSuffixAt = title.indexOf(':');
if(idSuffixAt >= 0)
{
title = ptr + 1;
while(*title && isspace(*title))
{
title++;
}
int subStart = idSuffixAt + 1;
while(subStart < title.length() && title.at(subStart).isSpace()) { subStart++; }

return title.substr(subStart);
}

return title;
}

char const *P_MapAuthor(uri_s const *mapUri, dd_bool supressGameAuthor)
String G_MapAuthor(de::Uri const *mapUri, bool supressGameAuthor)
{
if(!mapUri) mapUri = reinterpret_cast<uri_s const *>(&gameMapUri);
if(!mapUri) mapUri = &gameMapUri;

AutoStr *mapUriAsText = Uri_Resolved(mapUri);
if(!mapUriAsText || Str_IsEmpty(mapUriAsText))
return 0;
String mapUriAsText = mapUri->resolved();
if(mapUriAsText.isEmpty()) return ""; // Huh??

// Perhaps a MapInfo definition exists for the map?
ddmapinfo_t mapInfo;
char const *author = 0;
if(Def_Get(DD_DEF_MAP_INFO, Str_Text(mapUriAsText), &mapInfo))
String author;
if(Def_Get(DD_DEF_MAP_INFO, mapUriAsText.toUtf8().constData(), &mapInfo))
{
author = mapInfo.author;
}

if(!author || !author[0])
return 0;

// Should we suppress the author?
/// @todo Do not do this here.
GameInfo gameInfo;
DD_GameInfo(&gameInfo);
if(supressGameAuthor || P_MapIsCustom(Str_Text(mapUriAsText)))
if(!author.isEmpty())
{
if(!Str_CompareIgnoreCase(gameInfo.author, author))
return 0;
// Should we suppress the author?
/// @todo Do not do this here.
GameInfo gameInfo;
DD_GameInfo(&gameInfo);
if(supressGameAuthor || P_MapIsCustom(mapUriAsText.toUtf8().constData()))
{
if(!author.compareWithoutCase(Str_Text(gameInfo.author)))
return "";
}
}

return author;
Expand Down
15 changes: 5 additions & 10 deletions doomsday/plugins/doom/src/wi_stuff.cpp
Expand Up @@ -256,21 +256,14 @@ static void drawBackground()
DGL_Disable(DGL_TEXTURE_2D);
}

static void drawFinishedTitle(void)
static void drawFinishedTitle(int x = SCREENWIDTH / 2, int y = WI_TITLEY)
{
int x = SCREENWIDTH/2, y = WI_TITLEY;
patchid_t patchId;
patchinfo_t info;
char const *mapTitle;
uint mapNum;

if(gameModeBits & (GM_ANY_DOOM2|GM_DOOM_CHEX))
mapNum = wbs->currentMap;
else
mapNum = (wbs->episode * 9) + wbs->currentMap;

mapTitle = P_MapTitle(0/*current map*/);

DGL_Enable(DGL_TEXTURE_2D);
DGL_Color4f(1, 1, 1, 1);

Expand All @@ -279,8 +272,10 @@ static void drawFinishedTitle(void)
FR_SetColorAndAlpha(defFontRGB[CR], defFontRGB[CG], defFontRGB[CB], 1);

// Draw <MapName>
patchId = (mapNum < pMapNamesSize? pMapNames[mapNum] : 0);
WI_DrawPatchXY3(patchId, patchReplacementText(patchId, mapTitle), x, y, ALIGN_TOP, 0, DTF_NO_TYPEIN);
patchid_t const patchId = (mapNum < pMapNamesSize? pMapNames[mapNum] : 0);
de::String const mapTitle = G_MapTitle(0/*current map*/);
WI_DrawPatchXY3(patchId, patchReplacementText(patchId, mapTitle.toUtf8().constData()), x, y, ALIGN_TOP, 0, DTF_NO_TYPEIN);
patchinfo_t info;
if(R_GetPatchInfo(patchId, &info))
y += (5 * info.geometry.size.height) / 4;

Expand Down
14 changes: 6 additions & 8 deletions doomsday/plugins/doom64/src/wi_stuff.cpp
Expand Up @@ -118,30 +118,28 @@ void WI_Register(void)
Con_AddVariableList(cvars);
}

static void drawBackground(void)
static void drawBackground()
{
DGL_Enable(DGL_TEXTURE_2D);
DGL_Color4f(1, 1, 1, 1);
GL_DrawPatchXY3(pBackground, 0, 0, ALIGN_TOPLEFT, DPF_NO_OFFSET);
DGL_Disable(DGL_TEXTURE_2D);
}

static void drawFinishedTitle(void)
static void drawFinishedTitle(int x = SCREENWIDTH / 2, int y = WI_TITLEY)
{
int x = SCREENWIDTH/2, y = WI_TITLEY;
uint mapNum = wbs->currentMap;
char const *mapTitle = P_MapTitle(0/*current map*/);
patchid_t patchId;
patchinfo_t info;
de::String const mapTitle = G_MapTitle(0/*current map*/);

DGL_Enable(DGL_TEXTURE_2D);
DGL_Color4f(1, 1, 1, 1);
FR_SetFont(FID(GF_FONTB));
FR_LoadDefaultAttrib();

// Draw <MapName>
patchId = (mapNum < pMapNamesSize? pMapNames[mapNum] : 0);
WI_DrawPatchXY3(patchId, patchReplacementText(patchId, mapTitle), x, y, ALIGN_TOP, 0, DTF_NO_TYPEIN);
patchid_t patchId = (mapNum < pMapNamesSize? pMapNames[mapNum] : 0);
WI_DrawPatchXY3(patchId, patchReplacementText(patchId, mapTitle.toUtf8().constData()), x, y, ALIGN_TOP, 0, DTF_NO_TYPEIN);
patchinfo_t info;
if(R_GetPatchInfo(patchId, &info))
y += (5 * info.geometry.size.height) / 4;

Expand Down

0 comments on commit 5810582

Please sign in to comment.