0
\$\begingroup\$

I have a function that I use to print text PrintText(char* text) that stores the text to a vector, which is then displayed if the vector is not empty. I'm trying to figure out how can I correctly print multiple char* to the screen, one under the next?

For example:
This is line one.
This is line two.

Both of those are stored in separate char* elements in the vector. How can I use Direct3D or C++ to correctly display consecutive char* elements as it is in the above example? As in getting the correct screen coordinates to display the next element?

Code:

void DrawString(char* String, int x, int y, int r, int g, int b, ID3DXFont* ifont)
{
    RECT ShadowPos;
    ShadowPos.left = x + 1;
    ShadowPos.top = y + 1;
    RECT FontPos;
    FontPos.left = x;
    FontPos.top = y;
    ifont->DrawTextA(0, String, strlen(String), &ShadowPos, DT_NOCLIP, D3DCOLOR_ARGB(255, r / 3, g / 3, b / 3));
    ifont->DrawTextA(0, String, strlen(String), &FontPos, DT_NOCLIP, D3DCOLOR_ARGB(255, r, g, b));
}
\$\endgroup\$
4
  • \$\begingroup\$ Have you tried anything yet? \$\endgroup\$ – Vaillancourt Sep 3 '15 at 22:11
  • \$\begingroup\$ I haven't fully coded it yet, but I have an idea of how to do it. But I'm also stuck when it comes to using a text wrap and the one element uses two lines because of the text wrap restriction. \$\endgroup\$ – O.J. Sep 3 '15 at 22:15
  • \$\begingroup\$ Do you have code that can render a single string? \$\endgroup\$ – MooseBoys Sep 3 '15 at 23:55
  • \$\begingroup\$ Yes, I just added it. \$\endgroup\$ – O.J. Sep 4 '15 at 0:29
1
\$\begingroup\$

According to the docs for DrawTextA, "the return value is the height of the text in logical units". So you should modify your DrawString function to return this value (plus one, since you draw the shadow one pixel lower). You'd then offset the calling function's y-coordinate by the returned value, plus whatever padding you want. So something like this:

void DrawString(char* String, int x, int y, int r, int g, int b, ID3DXFont* ifont)
{
    RECT ShadowPos;
    ShadowPos.left = x + 1;
    ShadowPos.top = y + 1;
    RECT FontPos;
    FontPos.left = x;
    FontPos.top = y;
    ifont->DrawTextA(0, String, strlen(String), &ShadowPos, DT_NOCLIP, D3DCOLOR_ARGB(255, r / 3, g / 3, b / 3));
    int h = ifont->DrawTextA(0, String, strlen(String), &FontPos, DT_NOCLIP, D3DCOLOR_ARGB(255, r, g, b));
    return h + 1;
}

...

int y = 0, h = 0, padding = 4;
for(auto& s : vStrings)
{
    h = DrawString(s.c_str(), 0, y, ...);
    y += h + padding;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.