0

How do I go about drawing the value of a variable onto the screen with DirectX9? I know I can use the DrawText() method to draw text like this:

m_pD3DFont->DrawText(0, "text", -1, &rect, DT_TOP | DT_RIGHT | DT_NOCLIP, D3DCOLOR_ARGB(255, 255, 255, 255));

But how do I get it to draw the value of a variable? An integer, specifically. I've tried converting the integer to a string, but then I get an error saying

cannot convert parameter 2 from 'std::string *' to 'LPCWSTR'

Any help is appreciated.

Neevo
  • 1
  • 1

2 Answers2

0

To convert integers to strings, strings to integers and so on you may use std::stringstream like that

std::stringstream ss;
std::string s = "123";
ss << s;
int result; 
ss >> result;

More information here http://cplusplus.com/reference/iostream/stringstream/

Or you can use std::wstringstream and std::wstring if you need Unicode-strings for your application.

JustSomeGuy
  • 3,677
  • 1
  • 23
  • 31
0

As the previous poster mentioned, you can use stringstreams to convert ints to strings and vise versa, but with C++11 there are some new features that eliminate the need to do this. You can use std::to_string or std::to_wstring to convert ints to strings or wstrings, and you can use c_str() to get the raw char* or wchar_t* from the string object. You should be able to convert the appropriate type of pointer to your winapi string, but some can depend on your compiler's settings.

For your reference, here's what the winapi strings mean:

LPSTR = char*
LPCSTR = const char*
LPWSTR = wchar_t*
LPCWSTR = const wchar_t*
LPTSTR = char* or wchar_t* depending on _UNICODE
LPCTSTR = const char* or const wchar_t* depending on _UNICODE

And here is a quick sample of how to assign them to C++ strings:

#include <iostream>
#include <string>
#include <Windows.h>
#include <tchar.h>

int main()
{
    // Declare winapi strings
    LPSTR str_charPtr;
    LPCSTR str_constCharPtr;
    LPWSTR str_wcharPtr;
    LPCWSTR str_constWcharPtr;
    LPTSTR str_tcharPtr;
    LPCTSTR str_constTcharPtr;

    // Declare a test integer
    int num = 5001;

    // Convert the integer to a string and to a wstring
    std::string regString = std::to_string(num);
    std::wstring wideString = std::to_wstring(num);

    // Attempt to assign the winapi strings to the C++ standard strings
    str_charPtr = const_cast<char*>(regString.c_str());         // NOTE: removing const to store in non-const LPSTR
    str_constCharPtr = regString.c_str();
    str_wcharPtr = const_cast<wchar_t*>(wideString.c_str());    // NOTE: removing const to store in non-cost LPWSTR
    str_constWcharPtr = wideString.c_str();
    str_tcharPtr = const_cast<TCHAR*>(regString.c_str());       // Error if set to Unicode
    str_tcharPtr = const_cast<TCHAR*>(wideString.c_str());      // Error if NOT set to Unicode
    str_constTcharPtr = regString.c_str();                      // Error if set to Unicode
    str_constTcharPtr = wideString.c_str();                     // Error if NOT set to Unicde

    return 0;
}

If you've moved on to Visual Studio 2012 by now, you can adjust your settings like so:
1. Right-click the project in the solution explorer.
2. Click on Properties.
3. In the left pane under Configuration Properties, select General
4. In the right pane, look in the Project Defaults section.
5. Next to Character Set, choose Unicode or whichever option is appropriate for you.

derpface
  • 1,611
  • 1
  • 10
  • 20