• Advertisement
Sign in to follow this  

Trying to display text using D3D

This topic is 2256 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

Hi everyone,

I've been trying to figure out this issue all day and can't find the problem. Last night I tried incorporating a frame limiter in my code but I can't verify whether or not my frames per second is actually limited because I can't seem to output my [font="Courier New"][color="#0000ff"]double [/font][font="Courier New"][color="#0000ff"]fps [/font]variable. I have, however, verified this code in a test project where I was outputting the frames, or loops per second to the console window.

I have set up a [font="Courier New"][color="#ff0000"]frames [/font]class with a run function inside. The run function is called inside my main loop, but it has a game loop within it where my code is executed. I am calculating frames based on timeGetTime() and have limited to 30 FPS. I am passing in the [font="Courier New"][color="#0000ff"]double fps [/font][font="Arial"]variable to my render function where I want to use [/font][font="Courier New"]LPD3DXFONT [/font][font="Arial"]to display it on screen. My code is:[/font]

Frames.h

#define SPEED 33.3333

class frames
{
public:
bool run(WINDOW_PROPERTIES* wp);
bool timer_update(void);
private:
double frame_count;
double start_time;
double last_time;
double fps;
};



[font="Arial"]Frames.cpp[/font]
[font="Arial"]
#include "global.h"

bool frames::timer_update(void)
{
double current_time = timeGetTime() - last_time;

if(current_time < SPEED)
{
return true;
}

frame_count++;

last_time = timeGetTime();

return false;
}

bool frames::run(WINDOW_PROPERTIES* wp)
{
while(handle_messages()) // If handle_messages returns false, the program is over.
{
render(wp, fps);
timer_update();
fps = frame_count / ((timeGetTime() - start_time) / 1000);
}
return false;
}
[/font]

[font="Arial"]Render.cpp[/font]
[font="Arial"]
#include "global.h"

void render(WINDOW_PROPERTIES* wp, double fps)
{
begin_render(wp);
draw_grid();
show_font(fps);
end_render();
}
[/font]


[font="Arial"]Direct3D.cpp -- Please note the code for this file is 400 lines, I am posting the problem code only.

void show_font(double fps)
{
static RECT textbox;
SetRect(&textbox, 0, 0, 90, 30);


stringstream my_string;
my_string << fps;
string frames = my_string.str();

if(FAILED(font->DrawTextA(NULL,
(LPCSTR)&frames,
strlen((LPCSTR)&frames),
&textbox,
DT_CENTER | DT_VCENTER,
D3DCOLOR_ARGB(255, 255, 255, 255))))
{
MessageBox(NULL, L"The 'DrawTextA' function has failed.", L"Error!", MB_OK);
PostQuitMessage(0);
}
}
[/font]

[font="Arial"]Also in Direct3D.cpp I have a init_d3d function, where I call:[/font]
[font="Arial"]
// Inisialise the font face.
if(FAILED(D3DXCreateFont(d3ddev,
20, 0,
FW_NORMAL,
1,
FALSE,
DEFAULT_CHARSET,
OUT_DEFAULT_PRECIS,
DEFAULT_QUALITY,
DEFAULT_PITCH || FF_DONTCARE,
L"Arial",
&font)))
{
MessageBox(NULL, L"The D3DXCreateFont function has failed!", L"Error", MB_OK);
}
[/font]

[font="Arial"]This does not work. Instead of displaying the frames per second, it displays random junk. I thought it may be something to do with the draw font function so I tried outputting frames to a message box, I'll show the results in screen shots:[/font]

[font="Arial"]Without messagebox: http://img23.imagesh.../randomjunk.png[/font]
[font="Arial"]With messagebox: http://img42.imagesh...kmessagebox.png[/font]

[font="Arial"]I've also tried using [/font][font="Courier New"]_itoa_s() [/font][font="Arial"]to convert the double to a string,[/font] [font="Arial"]but got an assertion error when I tried. I have also tried using a MessageBox to display the [/font][font="Courier New"][color="#0000ff"]double fps [/font][font="Arial"]variable within the timer::run function with the same output.[/font]

[font="Arial"]Why is this happening? I can output the double to the console window in a test project, but I can't output the double in a MessageBox in Win32/D3D. How may I achieve the result I want?[/font]

[font="Arial"]Thanks for reading! :)[/font]

Share this post


Link to post
Share on other sites
Advertisement
I'm pretty sure you can't just cast a string object to a LPCSTR. You want to use the c_str function of the string class, which produces a null-terminated C-style string, which I believe is what an LPCSTR is.

Share this post


Link to post
Share on other sites
You were correct. I have now converted the string object to a C-style string. For anyone looking at this in the future, the working code is:



void show_font(int fps)
{
static RECT textbox;
SetRect(&textbox, 0, 0, 150, 100);

stringstream my_string;
my_string << fps;
string frames = my_string.str();

const char *c_str1 = frames.c_str();

if(FAILED(font->DrawTextA(NULL,
(LPCSTR)c_str1,
strlen((LPCSTR)c_str1),
&textbox,
DT_CENTER | DT_VCENTER,
D3DCOLOR_ARGB(255, 255, 255, 255))))
{
MessageBox(NULL, L"The 'DrawTextA' function has failed.", L"Error!", MB_OK);
PostQuitMessage(0);
}
}



FYI my frame limiter is working fine :) Thanks for your help!

http://img834.images...rateworking.png

Share this post


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

  • Advertisement