0

I'm trying to show the score on the screen. Following code works fine:

g_Font = NULL;
D3DXFONT_DESC f = {fontSize,
                    0,
                    400,
                    0,
                    false,
                    DEFAULT_CHARSET,
                    OUT_TT_PRECIS,
                    CLIP_DEFAULT_PRECIS,
                    DEFAULT_PITCH,
                    fontName};
fontDesc = f;
fontPosition.top = top;
fontPosition.left = left;
fontPosition.right = right;
fontPosition.bottom = bottom;
text = t;
D3DXCreateFontIndirect(device,&fontDesc,&g_Font);

Following part is rendered for each frame:

g_Font->DrawText(NULL,
        text,
        -1,
        &fontPosition,
        DT_CENTER,
        0xffffffff); //draw text

What I want to do is, update the text during runtime. I simply update the text variable since drawing code runs for each frame, but it doesn't work. A simple text works but following construction doesn't work:

const size_t buflen = 100;
TCHAR buf[buflen];
_sntprintf(buf, buflen - 1, TEXT("Point: %d"), point);
text = (LPCTSTR)buf;

I tried almost every solution I could have found online, but they don't work. I can see that the integer is converted successfully, but there are absurd characters in the following rendering. Any solutions?

mtyurt
  • 3,369
  • 6
  • 38
  • 57
  • Where is `pointLabel` coming from? can't you simply `DrawText` your new value? – emartel Jan 24 '13 at 02:36
  • Yes I can, but it's another class implemented for abstraction; so it's not the point of the question. – mtyurt Jan 24 '13 at 02:43
  • Without us telling us how `setText` on `pointLabel` is implemented there's not much we can tell you... is `pointLabel` rendered? – emartel Jan 24 '13 at 02:54
  • " I simply update the text variable" and I updated the code. It's simply assignment operation, forget `pointLabel` – mtyurt Jan 24 '13 at 06:36

1 Answers1

0

The code you posted is incomplete, but I will still try to provide you with an answer that should help you solve the problem.

The first issue is I think you're mixing concepts and you're handling DrawText like if it was a UI element of a WinForms or something. Everytime you want to update the text, DrawText needs to be called. It doesn't store a pointer to the buffer you pass and automatically update the text when this buffer is changed, see documentation here. If you wanted to add a "label wrapper", you will need to have it call DrawText, most likely in a Render method, between calls to BeginScene and EndScene.

As for the absurd characters, if your string is converted properly, are you sure you Clear your RenderTarget before drawing again on it?

emartel
  • 7,712
  • 1
  • 30
  • 58