SFML community forums
General => General discussions => Topic started by: pixel_pusher on September 01, 2010, 10:57:09 pm
-
Hi, I just started messing around with SFML, and for the heck of it, I'm working on the game engine from the tutorials page. Mainly, I'm just trying to replace the win32 function calls with more cross-platform stuff, so I can play with this thing on Linux.
What's giving me trouble right now is the use of _snprintf_s, lines from IState.hpp in the Draw() method:
char frames[20];
....
_snprintf_s(frames, 20, 20, "FPS: %4.2f", (float)mFrames / mFrameClock.GetElapsedTime());
Yeah, im embarrassed, I don't know my c library..... is there a quick replacement func that does the same thing? I know there's a function for this somewhere, but offhand all I can think of is printf and its variants, which wont do the trick AFAIK.
Any suggestions?
-
What about true C++, like std::string and std::ostringstream?
-
What about true C++, like std::string and std::ostringstream?
Well, I was shooting for as small of a change as possible... just while I get up and running. sprintf seems to work as a quick-fix, for now.
But, if I were to use string and osstream, wouldn't that be a little costly inside of update and render methods? I'd think I'd at least need to restructure the logging so that I wasn't calling these constructors twice every frame.
-
But, if I were to use string and osstream, wouldn't that be a little costly inside of update and render methods? I'd think I'd at least need to restructure the logging so that I wasn't calling these constructors twice every frame.
If you really have performance problems, measure the required time and/or use a profiler. The stream operations probably become negligible, as soon as your game logic or graphic gets more complex.
Choosing a bad design based on speed assumptions (not facts) is also called premature optimization. ;)
-
Hi. I converted and run GQE on Linux with NetBeans/G++
char updates[20]; --> std::ostringstream updates;
_snprintf_s(updates.. --> updates << "UPS: " << std::fixed ..
in IState.hpp:
/**
* Update is responsible for handling all State update needs for this
* State when it is the active State.
*/
virtual void Update(void) {
//char updates[20];
std::ostringstream updates;
// Check our App pointer
assert(NULL != mApp && "IState::Update() bad app pointer");
// Increment our update counter
mUpdates++;
if(mUpdateClock.GetElapsedTime() > 1.0f)
{
// Update our UPS string to be displayed
//_snprintf_s(updates, 20, 20, "UPS: %4.2f", (float)mUpdates / mUpdateClock.GetElapsedTime());
updates << "UPS: " << std::fixed << std::setprecision(2) << std::setw(7)
<< (float)mUpdates / mUpdateClock.GetElapsedTime();
mUPS.SetText(updates.str());
// Reset our Update clock and update counter
mUpdates = 0;
mUpdateClock.Reset();
}
};
/**
* Draw is responsible for handling all Drawing needs for this State
* when it is the Active State.
*/
virtual void Draw(void) {
//char frames[20];
std::ostringstream frames;
// Check our mApp pointer
assert(NULL != mApp && "IState::Draw() invalid app pointer provided");
// Increment our frame counter
mFrames++;
if(mFrameClock.GetElapsedTime() > 1.0f)
{
// Get our FramesPerSecond value
//_snprintf_s(frames, 20, 20, "FPS: %4.2f", (float)mFrames / mFrameClock.GetElapsedTime());
//mFPS.SetString(frames);
frames << "FPS: " << std::fixed << std::setprecision(2) << std::setw(7)
<< (float)mFrames / mFrameClock.GetElapsedTime();
mFPS.SetText(frames.str());
// Reset our Frames clock and frame counter
mFrames = 0;
mFrameClock.Reset();
}
// Draw the Frames Per Second debug value on the screen
mApp->mWindow.Draw(mFPS);
// Draw the Updates Per Second debug value on the screen
mApp->mWindow.Draw(mUPS);
};