Skip to content

Commit a523dbf

Browse files
handle map texture creation errors
1 parent 7378967 commit a523dbf

File tree

2 files changed

+70
-23
lines changed

2 files changed

+70
-23
lines changed

Client/mods/deathmatch/logic/CPlayerMap.cpp

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ constexpr std::array<std::uint32_t, 2> MAP_IMAGE_SIZES = {1024, 2048};
2727

2828
CPlayerMap::CPlayerMap(CClientManager* pManager)
2929
{
30+
m_failedToLoadTextures = false;
31+
3032
// Setup our managers
3133
m_pManager = pManager;
3234
m_pRadarMarkerManager = pManager->GetRadarMarkerManager();
@@ -55,16 +57,8 @@ CPlayerMap::CPlayerMap(CClientManager* pManager)
5557
m_iHorizontalMovement = 0;
5658
m_iVerticalMovement = 0;
5759

58-
// Create the local player blip image
59-
m_playerMarkerTexture = g_pCore->GetGraphics()->GetRenderItemManager()->CreateTexture(CalcMTASAPath("MTA\\cgui\\images\\radarset\\02.png"));
60-
61-
// Create the map image
62-
m_mapImageTexture = nullptr;
63-
m_playerMapImageIndex = g_pCore->GetCVars()->GetValue<std::size_t>("mapimage");
64-
SetMapImage(m_playerMapImageIndex);
65-
66-
// Create the marker textures
67-
CreateMarkerTextures();
60+
// Create all map textures
61+
CreateAllTextures();
6862

6963
// Create the text displays for the help text
7064
const SColorRGBA colorWhiteTransparent(255, 255, 255, 200);
@@ -120,13 +114,61 @@ CPlayerMap::~CPlayerMap()
120114
// Don't need to delete the help texts as those are destroyed by the display manager
121115
}
122116

123-
void CPlayerMap::SetMapImage(std::size_t imageIndex)
117+
void CPlayerMap::CreateOrUpdateMapTexture()
124118
{
125-
std::uint32_t mapSize = MAP_IMAGE_SIZES[imageIndex];
126-
SString fileName("MTA\\cgui\\images\\map_%d.png", mapSize);
119+
const std::uint32_t mapSize = MAP_IMAGE_SIZES[m_playerMapImageIndex];
120+
const SString fileName("MTA\\cgui\\images\\map_%d.png", mapSize);
121+
122+
auto* newTexture = g_pCore->GetGraphics()->GetRenderItemManager()->CreateTexture(CalcMTASAPath(fileName), nullptr, false, mapSize, mapSize, RFORMAT_DXT1);
123+
if (!newTexture)
124+
throw std::runtime_error("Failed to load map image");
127125

128126
SAFE_RELEASE(m_mapImageTexture);
129-
m_mapImageTexture = g_pCore->GetGraphics()->GetRenderItemManager()->CreateTexture(CalcMTASAPath(fileName), nullptr, false, mapSize, mapSize, RFORMAT_DXT1);
127+
m_mapImageTexture = newTexture;
128+
}
129+
130+
void CPlayerMap::UpdateOrRevertMapTexture(std::size_t newImageIndex)
131+
{
132+
const std::size_t oldImageIndex = m_playerMapImageIndex;
133+
try
134+
{
135+
m_playerMapImageIndex = newImageIndex;
136+
CreateOrUpdateMapTexture();
137+
}
138+
catch (const std::exception& e)
139+
{
140+
m_playerMapImageIndex = oldImageIndex;
141+
g_pCore->GetConsole()->Printf("Problem updating map image: %s", e.what());
142+
}
143+
}
144+
145+
void CPlayerMap::CreatePlayerBlipTexture()
146+
{
147+
m_playerMarkerTexture = g_pCore->GetGraphics()->GetRenderItemManager()->CreateTexture(CalcMTASAPath("MTA\\cgui\\images\\radarset\\02.png"));
148+
if (!m_playerMarkerTexture)
149+
throw std::runtime_error("Failed to load player blip image");
150+
}
151+
152+
void CPlayerMap::CreateAllTextures()
153+
{
154+
try
155+
{
156+
// Create the map texture
157+
m_mapImageTexture = nullptr;
158+
m_playerMapImageIndex = g_pCore->GetCVars()->GetValue<std::size_t>("mapimage");
159+
CreateOrUpdateMapTexture();
160+
161+
// Create the player blip texture
162+
CreatePlayerBlipTexture();
163+
164+
// Create the other marker textures
165+
CreateMarkerTextures();
166+
}
167+
catch (const std::exception& e)
168+
{
169+
m_failedToLoadTextures = true;
170+
g_pCore->GetConsole()->Printf("Problem initializing player map: %s", e.what());
171+
}
130172
}
131173

132174
void CPlayerMap::DoPulse()
@@ -173,7 +215,7 @@ void CPlayerMap::DoPulse()
173215
//
174216
void CPlayerMap::CreateMarkerTextures()
175217
{
176-
assert(m_markerTextureList.empty());
218+
m_markerTextureList.clear();
177219
SString strRadarSetDirectory = CalcMTASAPath("MTA\\cgui\\images\\radarset\\");
178220

179221
// Load the 3 shapes
@@ -184,7 +226,8 @@ void CPlayerMap::CreateMarkerTextures()
184226
m_markerTextureList.push_back(pTextureItem);
185227
}
186228

187-
assert(m_markerTextureList.size() == MARKER_FIRST_SPRITE_INDEX);
229+
if (m_markerTextureList.size() != MARKER_FIRST_SPRITE_INDEX)
230+
throw std::runtime_error("Failed to load marker textures [1]");
188231

189232
// Load the icons
190233
for (uint i = 0; i < RADAR_MARKER_LIMIT; i++)
@@ -193,7 +236,8 @@ void CPlayerMap::CreateMarkerTextures()
193236
m_markerTextureList.push_back(pTextureItem);
194237
}
195238

196-
assert(m_markerTextureList.size() == MARKER_LAST_SPRITE_INDEX + 1);
239+
if (m_markerTextureList.size() != MARKER_LAST_SPRITE_INDEX + 1)
240+
throw std::runtime_error("Failed to load marker textures [2]");
197241
}
198242

199243
//
@@ -245,8 +289,8 @@ void CPlayerMap::DoRender()
245289
{
246290
bool isMapShowing = IsPlayerMapShowing();
247291

248-
// Render if showing
249-
if (isMapShowing)
292+
// Render if showing and textures are all loaded
293+
if (isMapShowing && !m_failedToLoadTextures)
250294
{
251295
// Get the alpha value from the settings
252296
int mapAlpha;
@@ -257,8 +301,7 @@ void CPlayerMap::DoRender()
257301
auto mapImageIndex = g_pCore->GetCVars()->GetValue<std::size_t>("mapimage");
258302
if (mapImageIndex != m_playerMapImageIndex)
259303
{
260-
m_playerMapImageIndex = mapImageIndex;
261-
SetMapImage(m_playerMapImageIndex);
304+
UpdateOrRevertMapTexture(mapImageIndex);
262305
}
263306

264307
g_pCore->GetGraphics()->DrawTexture(m_mapImageTexture, static_cast<float>(m_iMapMinX), static_cast<float>(m_iMapMinY),

Client/mods/deathmatch/logic/CPlayerMap.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class CPlayerMap
4242

4343
void CreateMarkerTextures();
4444
CTextureItem* GetMarkerTexture(CClientRadarMarker* pMarker, float fLocalZ, float* pfScale, SColor* pColor);
45+
void CreatePlayerBlipTexture();
46+
void CreateOrUpdateMapTexture();
47+
void UpdateOrRevertMapTexture(std::size_t imageIndex);
48+
void CreateAllTextures();
4549

4650
public:
4751
bool IsAttachedToLocalPlayer() const { return m_bIsAttachedToLocal; };
@@ -62,8 +66,6 @@ class CPlayerMap
6266
void ZoomIn();
6367
void ZoomOut();
6468

65-
void SetMapImage(std::size_t imageIndex);
66-
6769
SString GetBoundKeyName(const SString& strCommand);
6870

6971
private:
@@ -81,6 +83,8 @@ class CPlayerMap
8183
class CClientRadarMarkerManager* m_pRadarMarkerManager;
8284
class CClientRadarAreaManager* m_pRadarAreaManager;
8385

86+
bool m_failedToLoadTextures;
87+
8488
std::size_t m_playerMapImageIndex;
8589

8690
CTextureItem* m_mapImageTexture;

0 commit comments

Comments
 (0)