Like the big fat D too.
Yeah, everybody knows it. I got sick of having to do this sort of thing for what ought to be very, very basic:
void GameState::drawDebug()
{
Renderer& r = Utility<Renderer>::get();
stringstream str;
str << "FPS: " << mFps.fps();
r.drawText(mFont, str.str(), 10, 25, 255, 255, 255);
str.str("");
str << "Map Dimensions: " << mTileMap->width() << ", " << mTileMap->height();
r.drawText(mFont, str.str(), 10, 25 + mFont.height(), 255, 255, 255);
str.str("");
str << "Max Digging Depth: " << mTileMap->maxDepth();
r.drawText(mFont, str.str(), 10, 25 + mFont.height() * 2, 255, 255, 255);
str.str("");
str << "Map Mouse Hover Coords: " << mTileMap->tileMouseHoverX() << ", " << mTileMap->tileMouseHoverY();
r.drawText(mFont, str.str(), 10, 25 + mFont.height() * 3, 255, 255, 255);
str.str("");
str << "Current Depth: " << mTileMap->currentDepth();
r.drawText(mFont, str.str(), 10, 25 + mFont.height() * 4, 255, 255, 255);
str.str("");
str << "Structure Count: " << mStructureManager.count();
r.drawText(mFont, str.str(), 10, 25 + mFont.height() * 6, 255, 255, 255);
}
Back in the 90's when I was building games in QBasic for DOS, I could do things like this:
myString = "Tile X: " + tile.x + " Tile Y: " + tile.y
And it worked beautifully.
Bah, fuck C and C++. Something so basic can be so flipping frustrating and leads to retarded code like above.
Set about to fix it. Came up with something super simple using C++ templates that does the job. I'm sure there are ways this can be horribly broken, 100% sure this isn't exception or thread safe but for my purposes it does the trick:
template<typename ... Args>
std::string string_format(const std::string& format, Args ... args)
{
size_t size = snprintf(nullptr, 0, format.c_str(), args ...) + 1;
unique_ptr<char[]> buffer(new char[size]);
snprintf(buffer.get(), size, format.c_str(), args ...);
return string(buffer.get(), buffer.get() + size - 1);
}
Works a lot like C's printf function and reduces the above code to this:
void GameState::drawDebug()
{
Renderer& r = Utility<Renderer>::get();
r.drawText(mFont, string_format("FPS: %i", mFps.fps()), 10, 25, 255, 255, 255);
r.drawText(mFont, string_format("Map Dimensions: %i, %i", mTileMap->width(), mTileMap->height()), 10, 25 + mFont.height(), 255, 255, 255);
r.drawText(mFont, string_format("Max Digging Depth: %i", mTileMap->maxDepth()), 10, 25 + mFont.height() * 2, 255, 255, 255);
r.drawText(mFont, string_format("Map Mouse Hover Coords: %i, %i", mTileMap->tileMouseHoverX(), mTileMap->tileMouseHoverY()), 10, 25 + mFont.height() * 3, 255, 255, 255);
r.drawText(mFont, string_format("Current Depth: %i", mTileMap->currentDepth()), 10, 25 + mFont.height() * 4, 255, 255, 255);
r.drawText(mFont, string_format("Structure Count: %i", mStructureManager.count()), 10, 25 + mFont.height() * 6, 255, 255, 255);
}
I am unsure of any performance hits this may incur though my intuition tells me it should be roughly the same as it was before, maybe even a bit faster despite the construction/destruction of the string object in the templated function. Any decent optimizing compiler should be able to deal with this well enough and the game continues to perform as it did before. Will profile this later and determine if i need to perhaps optimize this some.