From 58105820d156b7d6e66af40aab50e75d7ad803d2 Mon Sep 17 00:00:00 2001 From: danij Date: Mon, 30 Jun 2014 23:43:00 +0100 Subject: [PATCH] Refactor|libcommon: G_MapAuthor() and G_MapTitle() now return de::String --- doomsday/plugins/common/include/g_common.h | 12 +++++ doomsday/plugins/common/include/p_mapsetup.h | 12 ----- doomsday/plugins/common/src/g_game.cpp | 44 +++++++--------- doomsday/plugins/common/src/hu_stuff.cpp | 28 +++++----- doomsday/plugins/common/src/p_mapsetup.cpp | 55 +++++++++----------- doomsday/plugins/doom/src/wi_stuff.cpp | 15 ++---- doomsday/plugins/doom64/src/wi_stuff.cpp | 14 +++-- doomsday/plugins/heretic/src/in_lude.cpp | 20 +++---- 8 files changed, 91 insertions(+), 109 deletions(-) diff --git a/doomsday/plugins/common/include/g_common.h b/doomsday/plugins/common/include/g_common.h index 80a2ab904d..f8767d82a5 100644 --- a/doomsday/plugins/common/include/g_common.h +++ b/doomsday/plugins/common/include/g_common.h @@ -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 diff --git a/doomsday/plugins/common/include/p_mapsetup.h b/doomsday/plugins/common/include/p_mapsetup.h index ac2f32b8d2..7c8893e286 100644 --- a/doomsday/plugins/common/include/p_mapsetup.h +++ b/doomsday/plugins/common/include/p_mapsetup.h @@ -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). diff --git a/doomsday/plugins/common/src/g_game.cpp b/doomsday/plugins/common/src/g_game.cpp index fa25b9eeaa..1d328b8986 100644 --- a/doomsday/plugins/common/src/g_game.cpp +++ b/doomsday/plugins/common/src/g_game.cpp @@ -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); @@ -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)); } @@ -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; @@ -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__ @@ -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) == ' ') diff --git a/doomsday/plugins/common/src/hu_stuff.cpp b/doomsday/plugins/common/src/hu_stuff.cpp index 47d4192632..9aac35aa2c 100644 --- a/doomsday/plugins/common/src/hu_stuff.cpp +++ b/doomsday/plugins/common/src/hu_stuff.cpp @@ -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); @@ -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) @@ -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; @@ -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 @@ -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); diff --git a/doomsday/plugins/common/src/p_mapsetup.cpp b/doomsday/plugins/common/src/p_mapsetup.cpp index c90e9dbb12..41b1cfb44e 100644 --- a/doomsday/plugins/common/src/p_mapsetup.cpp +++ b/doomsday/plugins/common/src/p_mapsetup.cpp @@ -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(mapUri_); if(!mapUri) mapUri = &gameMapUri; - char const *title = 0; + String title; // Perhaps a MapInfo definition exists for the map? ddmapinfo_t mapInfo; @@ -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)) { @@ -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(&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; diff --git a/doomsday/plugins/doom/src/wi_stuff.cpp b/doomsday/plugins/doom/src/wi_stuff.cpp index 1803b9a4e4..4407766744 100644 --- a/doomsday/plugins/doom/src/wi_stuff.cpp +++ b/doomsday/plugins/doom/src/wi_stuff.cpp @@ -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); @@ -279,8 +272,10 @@ static void drawFinishedTitle(void) FR_SetColorAndAlpha(defFontRGB[CR], defFontRGB[CG], defFontRGB[CB], 1); // Draw - 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; diff --git a/doomsday/plugins/doom64/src/wi_stuff.cpp b/doomsday/plugins/doom64/src/wi_stuff.cpp index f8f9dc2a06..799b8cc293 100644 --- a/doomsday/plugins/doom64/src/wi_stuff.cpp +++ b/doomsday/plugins/doom64/src/wi_stuff.cpp @@ -118,7 +118,7 @@ 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); @@ -126,13 +126,10 @@ static void drawBackground(void) 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); @@ -140,8 +137,9 @@ static void drawFinishedTitle(void) FR_LoadDefaultAttrib(); // Draw - 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; diff --git a/doomsday/plugins/heretic/src/in_lude.cpp b/doomsday/plugins/heretic/src/in_lude.cpp index db66e3770d..9e4e54aa69 100644 --- a/doomsday/plugins/heretic/src/in_lude.cpp +++ b/doomsday/plugins/heretic/src/in_lude.cpp @@ -653,7 +653,7 @@ void IN_DrawOldLevel() FR_LoadDefaultAttrib(); FR_SetColorAndAlpha(defFontRGB[0], defFontRGB[1], defFontRGB[2], 1); - FR_DrawTextXY3(P_MapTitle(reinterpret_cast(&oldMapUri)), 160, 3, ALIGN_TOP, DTF_ONLY_SHADOW); + FR_DrawTextXY3(G_MapTitle(&oldMapUri).toUtf8().constData(), 160, 3, ALIGN_TOP, DTF_ONLY_SHADOW); FR_SetFont(FID(GF_FONTA)); FR_SetColor(defFontRGB3[0], defFontRGB3[1],defFontRGB3[2]); @@ -696,7 +696,7 @@ void IN_DrawOldLevel() void IN_DrawYAH() { - de::Uri nextMapUri = G_ComposeMapUri(wbs->episode, wbs->nextMap); + de::Uri const nextMapUri = G_ComposeMapUri(wbs->episode, wbs->nextMap); FR_SetFont(FID(GF_FONTA)); FR_LoadDefaultAttrib(); @@ -705,7 +705,7 @@ void IN_DrawYAH() FR_SetFont(FID(GF_FONTB)); FR_SetColor(defFontRGB[0], defFontRGB[1], defFontRGB[2]); - FR_DrawTextXY3(P_MapTitle(reinterpret_cast(&nextMapUri)), 160, 20, ALIGN_TOP, DTF_ONLY_SHADOW); + FR_DrawTextXY3(G_MapTitle(&nextMapUri).toUtf8().constData(), 160, 20, ALIGN_TOP, DTF_ONLY_SHADOW); DGL_Color4f(1, 1, 1, 1); for(uint i = 0; i < wbs->nextMap; ++i) @@ -731,8 +731,7 @@ void IN_DrawSingleStats() static int sounds; - de::Uri oldMapUri = G_ComposeMapUri(wbs->episode, wbs->currentMap); - char buf[20]; + de::Uri const oldMapUri = G_ComposeMapUri(wbs->episode, wbs->currentMap); DGL_Enable(DGL_TEXTURE_2D); @@ -743,7 +742,7 @@ void IN_DrawSingleStats() FR_DrawTextXY3("KILLS", 50, 65, ALIGN_TOPLEFT, DTF_ONLY_SHADOW); FR_DrawTextXY3("ITEMS", 50, 90, ALIGN_TOPLEFT, DTF_ONLY_SHADOW); FR_DrawTextXY3("SECRETS", 50, 115, ALIGN_TOPLEFT, DTF_ONLY_SHADOW); - FR_DrawTextXY3(P_MapTitle(reinterpret_cast(&oldMapUri)), 160, 3, ALIGN_TOP, DTF_ONLY_SHADOW); + FR_DrawTextXY3(G_MapTitle(&oldMapUri).toUtf8().constData(), 160, 3, ALIGN_TOP, DTF_ONLY_SHADOW); FR_SetFont(FID(GF_FONTA)); FR_SetColor(defFontRGB3[0], defFontRGB3[1], defFontRGB3[2]); @@ -766,6 +765,7 @@ void IN_DrawSingleStats() DGL_Enable(DGL_TEXTURE_2D); + char buf[20]; dd_snprintf(buf, 20, "%i", players[CONSOLEPLAYER].killCount); FR_SetFont(FID(GF_FONTB)); FR_SetTracking(TRACKING); @@ -847,7 +847,7 @@ void IN_DrawSingleStats() } else { - de::Uri nextMapUri = G_ComposeMapUri(wbs->episode, wbs->nextMap); + de::Uri const nextMapUri = G_ComposeMapUri(wbs->episode, wbs->nextMap); DGL_Enable(DGL_TEXTURE_2D); @@ -857,7 +857,7 @@ void IN_DrawSingleStats() FR_SetFont(FID(GF_FONTB)); FR_SetColorAndAlpha(defFontRGB[0], defFontRGB[1], defFontRGB[2], 1); - FR_DrawTextXY3(P_MapTitle(reinterpret_cast(&nextMapUri)), 160, 170, ALIGN_TOP, DTF_ONLY_SHADOW); + FR_DrawTextXY3(G_MapTitle(&nextMapUri).toUtf8().constData(), 160, 170, ALIGN_TOP, DTF_ONLY_SHADOW); DGL_Disable(DGL_TEXTURE_2D); @@ -873,7 +873,7 @@ void IN_DrawCoopStats() static int sounds; - de::Uri oldMapUri = G_ComposeMapUri(wbs->episode, wbs->currentMap); + de::Uri const oldMapUri = G_ComposeMapUri(wbs->episode, wbs->currentMap); DGL_Enable(DGL_TEXTURE_2D); @@ -884,7 +884,7 @@ void IN_DrawCoopStats() FR_DrawTextXY3("KILLS", 95, 35, ALIGN_TOPLEFT, DTF_ONLY_SHADOW); FR_DrawTextXY3("BONUS", 155, 35, ALIGN_TOPLEFT, DTF_ONLY_SHADOW); FR_DrawTextXY3("SECRET", 232, 35, ALIGN_TOPLEFT, DTF_ONLY_SHADOW); - FR_DrawTextXY3(P_MapTitle(reinterpret_cast(&oldMapUri)), SCREENWIDTH/2, 3, ALIGN_TOP, DTF_ONLY_SHADOW); + FR_DrawTextXY3(G_MapTitle(&oldMapUri).toUtf8().constData(), SCREENWIDTH/2, 3, ALIGN_TOP, DTF_ONLY_SHADOW); FR_SetFont(FID(GF_FONTA)); FR_SetColor(defFontRGB3[0], defFontRGB3[1], defFontRGB3[2]);