Skip to content

Commit 4fb7cd8

Browse files
author
hikerstk
committed
Applied r14491 and r14505 from trunk: improve warning messages,
avoid crashes on some missing textures. git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/0.8.1@14519 178a84e3-b1eb-0310-8ba1-8eac791a3b58
1 parent bb8f64c commit 4fb7cd8

File tree

14 files changed

+224
-55
lines changed

14 files changed

+224
-55
lines changed

src/graphics/irr_driver.cpp

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,32 @@ void IrrDriver::removeCameraSceneNode(scene::ICameraSceneNode *camera)
10691069
camera->remove();
10701070
} // removeCameraSceneNode
10711071

1072+
// ----------------------------------------------------------------------------
1073+
/** Sets an error message to be displayed when a texture is not found. This
1074+
* error message is shown before the "Texture '%s' not found" message. It can
1075+
* be used to supply additional details like what kart is currently being
1076+
* loaded.
1077+
* \param error Error message, potentially with a '%' which will be replaced
1078+
* with detail.
1079+
* \param detail String to replace a '%' in the error message.
1080+
*/
1081+
void IrrDriver::setTextureErrorMessage(const std::string &error,
1082+
const std::string &detail)
1083+
{
1084+
if(detail=="")
1085+
m_texture_error_message = error;
1086+
else
1087+
m_texture_error_message = StringUtils::insertValues(error, detail);
1088+
} // setTextureErrorMessage
1089+
1090+
// ----------------------------------------------------------------------------
1091+
/** Disables the texture error message again.
1092+
*/
1093+
void IrrDriver::unsetTextureErrorMessage()
1094+
{
1095+
m_texture_error_message = "";
1096+
} // unsetTextureErrorMessage
1097+
10721098
// ----------------------------------------------------------------------------
10731099
/** Loads a texture from a file and returns the texture object.
10741100
* \param filename File name of the texture to load.
@@ -1098,7 +1124,7 @@ video::ITexture *IrrDriver::getTexture(const std::string &filename,
10981124
// PNGs are non premul, but some are used for premul tasks, so convert
10991125
// http://home.comcast.net/~tom_forsyth/blog.wiki.html#[[Premultiplied%20alpha]]
11001126
// FIXME check param, not name
1101-
if(is_premul &&
1127+
if(img && is_premul &&
11021128
StringUtils::hasSuffix(filename.c_str(), ".png") &&
11031129
(img->getColorFormat() == video::ECF_A8R8G8B8) &&
11041130
img->lock())
@@ -1121,7 +1147,7 @@ video::ITexture *IrrDriver::getTexture(const std::string &filename,
11211147
} // if png and ColorFOrmat and lock
11221148
// Other formats can be premul, but the tasks can be non premul
11231149
// So divide to get the separate RGBA (only possible if alpha!=0)
1124-
else if(is_prediv &&
1150+
else if(img && is_prediv &&
11251151
(img->getColorFormat() == video::ECF_A8R8G8B8) &&
11261152
img->lock())
11271153
{
@@ -1150,9 +1176,12 @@ video::ITexture *IrrDriver::getTexture(const std::string &filename,
11501176

11511177
if (complain_if_not_found && out == NULL)
11521178
{
1153-
Log::error("irr_driver", "Texture '%s' not found; Put a breakpoint "
1154-
"at line %s:%i to debug!\n",
1155-
filename.c_str(), __FILE__, __LINE__);
1179+
1180+
if(m_texture_error_message.size()>0)
1181+
{
1182+
Log::error("irr_driver", m_texture_error_message.c_str());
1183+
}
1184+
Log::error("irr_driver", "Texture '%s' not found.", filename.c_str());
11561185
}
11571186

11581187
return out;

src/graphics/irr_driver.hpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ class IrrDriver : public IEventReceiver, public NoCopy
7575
/** Post-processing. */
7676
PostProcessing *m_post_processing;
7777

78+
/** Additional details to be shown in case that a texture is not found.
79+
* This is used to specify details like: "while loading kart '...'" */
80+
std::string m_texture_error_message;
81+
7882
/** Flag to indicate if a resolution change is pending (which will be
7983
* acted upon in the next update). None means no change, yes means
8084
* change to new resolution and trigger confirmation dialog.
@@ -193,6 +197,9 @@ class IrrDriver : public IEventReceiver, public NoCopy
193197
void printRenderStats();
194198
bool supportsSplatting();
195199
void requestScreenshot();
200+
void setTextureErrorMessage(const std::string &error,
201+
const std::string &detail="");
202+
void unsetTextureErrorMessage();
196203

197204
void draw2dTriangle(const core::vector2df &a, const core::vector2df &b,
198205
const core::vector2df &c,
@@ -203,6 +210,55 @@ class IrrDriver : public IEventReceiver, public NoCopy
203210

204211

205212

213+
// ------------------------------------------------------------------------
214+
/** Convenience function that loads a texture with default parameters
215+
* but includes an error message.
216+
* \param filename File name of the texture to load.
217+
* \param error Error message, potentially with a '%' which will be replaced
218+
* with detail.
219+
* \param detail String to replace a '%' in the error message.
220+
*/
221+
video::ITexture* getTexture(const std::string &filename,
222+
const std::string &error_message,
223+
const std::string &detail="")
224+
{
225+
setTextureErrorMessage(error_message, detail);
226+
video::ITexture *tex = getTexture(filename);
227+
unsetTextureErrorMessage();
228+
return tex;
229+
} // getTexture
230+
231+
// ------------------------------------------------------------------------
232+
/** Convenience function that loads a texture with default parameters
233+
* but includes an error message.
234+
* \param filename File name of the texture to load.
235+
* \param error Error message, potentially with a '%' which will be replaced
236+
* with detail.
237+
* \param detail String to replace a '%' in the error message.
238+
*/
239+
video::ITexture* getTexture(const std::string &filename,
240+
char *error_message,
241+
char *detail=NULL)
242+
{
243+
if(!detail)
244+
return getTexture(filename, std::string(error_message),
245+
std::string(""));
246+
247+
return getTexture(filename, std::string(error_message),
248+
std::string(detail));
249+
} // getTexture
250+
251+
// ------------------------------------------------------------------------
252+
/** Returns the currently defined texture error message, which is used
253+
* by event_handler.cpp to print additional info about irrlicht
254+
* internal errors or warnings. If no error message is currently
255+
* defined, the error message is "".
256+
*/
257+
const std::string &getTextureErrorMessage()
258+
{
259+
return m_texture_error_message;
260+
} // getTextureErrorMessage
261+
206262
// ------------------------------------------------------------------------
207263
/** Returns a list of all video modes supports by the graphics card. */
208264
const std::vector<VideoMode>& getVideoModes() const { return m_modes; }

src/graphics/material.cpp

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,9 @@ Material::Material(const XMLNode *node, int index, bool deprecated)
430430
else if (s=="" || s=="none")
431431
m_adjust_image = ADJ_NONE;
432432
else
433-
printf("Incorrect adjust-image specification: '%s' - ignored.\n",
434-
s.c_str());
433+
Log::warn("material",
434+
"Incorrect adjust-image specification: '%s' - ignored.",
435+
s.c_str());
435436
node->get("alpha", &m_alpha_blending );
436437
node->get("light", &m_lighting );
437438

@@ -464,7 +465,8 @@ Material::Material(const XMLNode *node, int index, bool deprecated)
464465
}
465466
else if (creaction.size() > 0)
466467
{
467-
fprintf(stderr, "[Material] WARNING: Unknown collision reaction '%s'\n", creaction.c_str());
468+
Log::warn("Material","Unknown collision reaction '%s'",
469+
creaction.c_str());
468470
}
469471

470472
node->get("below-surface", &m_below_surface );
@@ -573,9 +575,9 @@ Material::Material(const XMLNode *node, int index, bool deprecated)
573575
}
574576
else if (s != "")
575577
{
576-
fprintf(stderr,
577-
"Invalid graphical effect specification: '%s' - ignored.\n",
578-
s.c_str());
578+
Log::warn("material",
579+
"Invalid graphical effect specification: '%s' - ignored.",
580+
s.c_str());
579581
}
580582
else
581583
{
@@ -595,7 +597,8 @@ Material::Material(const XMLNode *node, int index, bool deprecated)
595597
}
596598
else
597599
{
598-
fprintf(stderr, "[Material] WARNING: could not find normal map image in materials.xml\n");
600+
Log::warn("material",
601+
"Could not find normal map image in materials.xml");
599602
}
600603

601604
node->get("normal-light-map", &m_normal_map_shader_lightmap);
@@ -618,9 +621,8 @@ Material::Material(const XMLNode *node, int index, bool deprecated)
618621
else if (s == "additive") m_add = true;
619622
else if (s == "coverage") m_alpha_to_coverage = true;
620623
else if (s != "none")
621-
fprintf(stderr,
622-
"[Material] WARNING: Unknown compositing mode '%s'\n",
623-
s.c_str());
624+
Log::warn("material", "Unknown compositing mode '%s'",
625+
s.c_str());
624626
}
625627

626628

@@ -665,10 +667,9 @@ Material::Material(const XMLNode *node, int index, bool deprecated)
665667
}
666668
else
667669
{
668-
fprintf(stderr,
669-
"[Material] WARNING: unknown node type '%s' for texture "
670-
"'%s' - ignored.\n",
671-
child_node->getName().c_str(), m_texname.c_str());
670+
Log::warn("material", "Unknown node type '%s' for texture "
671+
"'%s' - ignored.",
672+
child_node->getName().c_str(), m_texname.c_str());
672673
}
673674

674675
} // for i <node->getNumNodes()
@@ -756,14 +757,16 @@ void Material::install(bool is_full_path, bool complain_if_not_found)
756757

757758
if (complain_if_not_found && full_path.size() == 0)
758759
{
759-
fprintf(stderr, "[Material] WARNING, cannot find texture '%s'\n", m_texname.c_str());
760+
Log::error("material", "Cannot find texture '%s'.", m_texname.c_str());
761+
m_texture = NULL;
762+
}
763+
else
764+
{
765+
m_texture = irr_driver->getTexture(full_path,
766+
isPreMul(),
767+
isPreDiv(),
768+
complain_if_not_found);
760769
}
761-
762-
763-
m_texture = irr_driver->getTexture(full_path,
764-
isPreMul(),
765-
isPreDiv(),
766-
complain_if_not_found);
767770

768771
if (m_texture == NULL) return;
769772

@@ -780,8 +783,8 @@ void Material::install(bool is_full_path, bool complain_if_not_found)
780783
}
781784
else
782785
{
783-
fprintf(stderr, "Applying mask failed for '%s'!\n",
784-
m_texname.c_str());
786+
Log::warn("material", "Applying mask failed for '%s'!",
787+
m_texname.c_str());
785788
}
786789
}
787790
m_texture->grab();
@@ -833,8 +836,8 @@ void Material::initCustomSFX(const XMLNode *sfx)
833836

834837
if (filename.empty())
835838
{
836-
fprintf(stderr, "[Material] WARNING: sfx node has no 'filename' "
837-
"attribute, sound effect will be ignored\n");
839+
Log::warn("material", "Sfx node has no 'filename' "
840+
"attribute, sound effect will be ignored.");
838841
return;
839842
}
840843

@@ -912,10 +915,10 @@ void Material::initParticlesEffect(const XMLNode *node)
912915

913916
if (count == 0)
914917
{
915-
fprintf(stderr, "[Material::initParticlesEffect] WARNING: Particles "
916-
"'%s' for material '%s' are declared but not used "
917-
"(no emission condition set)\n",
918-
base.c_str(), m_texname.c_str());
918+
Log::warn("material", "initParticlesEffect: Particles "
919+
"'%s' for material '%s' are declared but not used "
920+
"(no emission condition set).",
921+
base.c_str(), m_texname.c_str());
919922
}
920923

921924
for (int c=0; c<count; c++)
@@ -930,8 +933,8 @@ void Material::initParticlesEffect(const XMLNode *node)
930933
}
931934
else
932935
{
933-
fprintf(stderr, "[Material::initParticlesEffect] WARNING: Unknown "
934-
"condition '%s' for material '%s'\n",
936+
Log::warn("material", "initParticlesEffect: Unknown "
937+
"condition '%s' for material '%s'",
935938
conditions[c].c_str(), m_texname.c_str());
936939
}
937940
}
@@ -985,9 +988,12 @@ void Material::setMaterialProperties(video::SMaterial *m, scene::IMeshBuffer* m
985988
// materials.xml, if you want to set flags for all surfaces, see
986989
// 'MaterialManager::setAllMaterialFlags'
987990

988-
if (m_deprecated || (m->getTexture(0) != NULL && ((core::stringc)m->getTexture(0)->getName()).find("deprecated") != -1))
991+
if (m_deprecated ||
992+
(m->getTexture(0) != NULL &&
993+
((core::stringc)m->getTexture(0)->getName()).find("deprecated") != -1))
989994
{
990-
fprintf(stderr, "WARNING: track uses deprecated texture <%s>\n", m_texname.c_str());
995+
Log::warn("material", "Track uses deprecated texture '%s'\n",
996+
m_texname.c_str());
991997
}
992998

993999

src/graphics/referee.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ void Referee::init()
124124
scene::IMeshBuffer *mb = m_st_referee_mesh->getMeshBuffer(i);
125125
video::SMaterial &irrMaterial = mb->getMaterial();
126126
video::ITexture* t=irrMaterial.getTexture(0);
127+
if(!t) continue;
128+
127129
std::string name=StringUtils::getBasename(t->getName()
128130
.getInternalName().c_str());
129131
if(name==colors[0] || name==colors[1] ||name==colors[2] )

src/guiengine/engine.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,11 @@ namespace GUIEngine
12611261
ITexture* loading =
12621262
irr_driver->getTexture(file_manager->getGUIDir()+"loading.png");
12631263

1264+
if(!loading)
1265+
{
1266+
Log::fatal("Engine", "Can not find loading.png texture, aborting.");
1267+
exit(-1);
1268+
}
12641269
const int texture_w = loading->getSize().Width;
12651270
const int texture_h = loading->getSize().Height;
12661271

src/guiengine/event_handler.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <IGUIEnvironment.h>
2323
#include <IGUIListBox.h>
2424

25+
#include "graphics/irr_driver.hpp"
2526
#include "guiengine/abstract_state_manager.hpp"
2627
#include "guiengine/engine.hpp"
2728
#include "guiengine/modaldialog.hpp"
@@ -192,14 +193,18 @@ bool EventHandler::OnEvent (const SEvent &event)
192193
#else
193194
return true; // EVENT_BLOCK
194195
#endif
195-
196+
const std::string &error_info = irr_driver->getTextureErrorMessage();
196197
if (event.LogEvent.Level == irr::ELL_WARNING)
197198
{
198-
printf("[Irrlicht Warning] %s\n", event.LogEvent.Text);
199+
if(error_info.size()>0)
200+
Log::warn("EventHandler", error_info.c_str());
201+
Log::warn("Irrlicht", event.LogEvent.Text);
199202
}
200203
else if (event.LogEvent.Level == irr::ELL_ERROR)
201204
{
202-
printf("[Irrlicht Error] %s\n", event.LogEvent.Text);
205+
if(error_info.size()>0)
206+
Log::error("EventHandler", error_info.c_str());
207+
Log::error("Irrlicht", event.LogEvent.Text);
203208
}
204209
}
205210
return true;

0 commit comments

Comments
 (0)