0

I've recently started some programming in C++ using DirectX. I'm not new to C++ as I've used Allegro & SDL before. So far, I can draw text to the screen. However, now I have a slight problem, where I cannot draw a variable to the screen. Ideally, I want to draw a string + an int value. However I have no idea how to do that. This is a snippet of my code so far:

font->DrawTextA(sprite, "Score: ", -1, scoreR, DT_CALCRECT, 0xFFFFFFFF);
font->DrawTextA(sprite, "Score: ", -1, scoreR, 0, 0xFFFFFFFF);

As you might expect, this would write "Score: " to the screen. I need it to write the 'score' variable after that.

Any help would be appreciated.

  • Why not use sprintf to write a formatted string to memory and then send that string to DrawTextA? – tohava Jul 21 '13 at 15:09

1 Answers1

1

You may use sprintf to format a string into a memory string, and then print it using DrawText

Example: (not tested)

char formatted_string[100];
sprintf(formatted_string, "Score: %d", score);
font->DrawTextA(sprite, formatted_string, -1, scoreR, DT_CALCRECT, 0xFFFFFFFF);

Obviously this is just illustrative, as this could be much more polished.

Gallium Nitride
  • 193
  • 1
  • 5
  • Thank you, this worked. For some reason when I tried this before it didn't, it might have been because I was using a char pointer instead of a char array. –  Jul 21 '13 at 15:09
  • For sure. sprintf requires an allocated string to write to. Best luck for you! – Gallium Nitride Jul 21 '13 at 15:23