Jump to content
  • Advertisement
Sign in to follow this  
Kevn

wstring help

This topic is 2868 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having trouble trying to get my frames per second to show up in my DirectX 10 application. I am trying to store "FPS: #" in a wstring called frameStats and using it in the Direct3D function DrawText as frameStats.c_str(). I can't get anything to show up though.

the code for placing content into the wstring frameStats is:


void Timer::calcFPS(float dTime)
{
static int frameCount = 0;
static float timeBase = 0.0f;

frameCount++;

if ( (dTime - timeBase) >= 1.0f )
{
int fps = frameCount;

frameStats = L"FPS " + fps;

frameCount = 0;
timeBase += 1.0f;
}
}




in the Timer class under public i have std::wstring frameStats;
so i can call the DrawText function like as follows:

d3dBack.pFont->DrawText(0, timer.frameStats.c_str(), -1, &rc, DT_NOCLIP, D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f));

Everything compiles fine, it just leaves me with an application that has an all black screen. If I replace timer.frameStats.c_str() in the DrawText function with something like L"Hello World", the screen is still all black but Hello World shows up in my application on the top left. The all black part(there should be vertices and stuff I drew) is a different issue that I will try to fix after I can get the fps showing up, right now i can just comment out the DrawText function and its no longer all black.

Share this post


Link to post
Share on other sites
Advertisement
This line does not do what you think it does:

frameStats = L"FPS " + fps;

A string literal plus an integer will not concatenate the string with the literal.

Share this post


Link to post
Share on other sites

#include <sstream>
#include <string>

using namespace std;

int main() {
wstringstream wss;
wss << L"FPS: " << fps;

const wchar_t* wstr = wss.str().c_str();
}

Share this post


Link to post
Share on other sites
alright I tried it two ways:


void Timer::calcFPS(float dTime)
{
static int frameCount = 0;
static float timeBase = 0.0f;

frameCount++;

if ( (dTime - timeBase) >= 1.0f )
{
int fps = frameCount;

std::wostringstream outStats;
outStats << L"FPS " << fps;
frameStats = outStats.str();

frameCount = 0;
timeBase += 1.0f;
}
}


With fameStats being std::wstring frameStats;
and calling the draw function like so:
d3dBack.pFont->DrawText(0, timer.frameStats.c_str(), -1, &rc, DT_NOCLIP, D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f));

and like this:

void Timer::calcFPS(float dTime)
{
static int frameCount = 0;
static float timeBase = 0.0f;

frameCount++;

if ( (dTime - timeBase) >= 1.0f )
{
int fps = frameCount;

std::wostringstream outStats;
outStats << L"FPS " << fps;
frameStats = outStats.str().c_str();

frameCount = 0;
timeBase += 1.0f;
}
}


With frameStats being const wchar_t* frameStats;
and calling the draw function like:
d3dBack.pFont->DrawText(0, timer.frameStats, -1, &rc, DT_NOCLIP, D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f));

Also with both i tried std::wostringstream outStats; and std::wstringstream outStats;

the first code just gave me a black screen again, the second code made all my verticies and drawings come up like normal but no text was on the screen. Then after a little time passed the window would turn black and it displayed a lot of wierd symbols where the text should be.

Share this post


Link to post
Share on other sites

if ( (dTime - timeBase) >= 1.0f )
{
int fps = frameCount;

std::wostringstream outStats;
outStats << L"FPS " << fps;
frameStats = outStats.str().c_str(); // Ok, the pointer to char-array now points to outStats internal string buffer

frameCount = 0;
timeBase += 1.0f;
} // Boom! outStats is destroyed and the memory frameStats points to is no longer valid memory!




To fix, make frameStats a wstring.

Share this post


Link to post
Share on other sites
still only a black screen. Maybe that part is functioning correctly and i just need to fix the screen turning black when using the DrawText function first. Could it be that since something like L"Hello World" isn't changing it shows up but since fps is constantly changing it wont show up. Like if the screen isn't updating correctly cause I need to reset something probably after the DrawText function and if the first few frames on the fps output are blank then it would be stuck showing nothing. Whereas there is no blank at the beginning using L"Hello World".

if any of that made any sense...lol

EDIT - well.. I fixed the problem with the screen turning black when calling the DrawText function but when I try to use DrawText with frameStats no text is showing up.


EDIT2 - I commented out all the calculations and stuff and just set frameStats = L"testing" in the function i've been posting here and nothing showed up on the window. Then I put frameStats = L"Testing" in the constructor for the timer class and it showed up in my program. Why is it working in the constructor but not my calcFPS function?

EDIT3 - whoops... I figured out why nothing is showing up. The program is never running the if statement that frameStats is given a value in. Gotta dig around in my program now and figure out why.


awesome, i fixed it. Thank you _fastcall and SiCrane

[Edited by - Kevn on July 20, 2010 7:28:14 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!