Goose
Goose

Reputation: 2250

WxWidgets DrawText to Print a Variable on Screen

I am trying to use wxWidgets in C++ to use DrawText to draw a string that says "Game Over" for my game and also displays the ending game score which is an int variable.

Here is my code:

void CFunction::Draw(wxDC &dc)
{
    if (mGame != NULL && mGame->IsGameOver())
    {

        wxFont font(75, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false);
        dc.SetFont(font);
        dc.SetTextForeground(wxColour(221, 34, 34));
        dc.DrawText(L"GAME OVER!", 250, 100);
    }
}

Right now it prints "GAME OVER!" but I want it to print the variable stored in mGame->GetScore(), so for example if the score was 10 at the end of the game it should show on the screen:

"GAME OVER! Score: 10"

Anyone know how to do this in wxWidgets with wxDraw?

Upvotes: 0

Views: 1276

Answers (1)

Goose
Goose

Reputation: 2250

I figured it out:

if ( mGame != NULL && mGame->IsGameOver() )
{
    wstringstream str;
    str << L"GAME OVER! YOUR SCORE: "  << mGame->GetScore() << ends;

    wxFont font(45, wxFONTFAMILY_SWISS, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false);
    dc.SetFont(font);
    dc.SetTextForeground(wxColour(255, 102, 0));
    dc.DrawText(str.str().c_str(), 300, 200);
}

You have to use wstringstream, and use that to pass in your variables apparently.

Upvotes: 1

Related Questions