Jump to content
  • Advertisement
Sign in to follow this  

sstream help

This topic is 3766 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright, I am really confused.
// At function scope
    // This is a stringstream
    scoress << "Score:\n" << player->GetScore();
    this->printString( scorerect.x + 4,
                       scorerect.y + 4,
                       color[blackb] );

// ...

// Outside function scope
void Graphics::printString( int x, int y, SDL_Surface *dest, std::string sz, SDL_Color fgcolor, SDL_Color bgcolor )
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;

    int width = 0, height = 10;
    int lineskip = TTF_FontLineSkip( font );
    std::vector<std::string> vlines;

    int n = 0;
    std::string szsub;
    // Break up the lines
    while( n != -1 ) {
        // Find a newline
        n = sz.find( '\n', 0 );
        // Split up at the newline
        szsub = sz.substr( 0, n );
        if( n != -1 ) sz = sz.substr( n+1, 1 );
        // Add the line to the vector
        vlines.push_back( szsub );

        // Get the size of the rendered text
        int w = 0;
        TTF_SizeText( font, szsub.c_str(), &w, &height );
        if( w > width ) width = w;

    height = (vlines.size()-1) * lineskip + height;

    // Draw
    SDL_Surface *temp = NULL;
    SDL_Surface *text = NULL;

    text = SDL_CreateRGBSurface( SDL_SWSURFACE, width, height,
                                 dest->format->Amask );

    offset.w = width;
    offset.h = height;
    SDL_FillRect( text, NULL, SDL_MapColor( SDL_GetVideoSurface()->format, bgcolor ) );

    SDL_Rect rect;
    for( int i = 0; i < vlines.size(); i++ ) {
        temp = TTF_RenderText_Blended( font, vlines.c_str(), fgcolor );
        rect.x = 0;
        rect.y = i * lineskip;

        SDL_BlitSurface( temp, NULL, text, &rect );

    SDL_BlitSurface( text, NULL, dest, &offset );

player->GetScore(); returns an integer. By debugging into stdout.txt, I managed to find out that the players score is updated correctly. The game however displays the wrong score (roughly the score divided by 10), and sometimes resets the score randomly to 1. I have no idea how this is possible. Can someone please help me?

Share this post

Link to post
Share on other sites
Did you try creating a new stringstream each time to format your score rather than reusing an existing one?

Share this post

Link to post
Share on other sites
I found my error :) Thanks for your reply anyway though.

I mistyped something from this gamedev tutorial.

if( n != -1 ) sz = sz.substr( n+1, 1 );
should be according to the tutorial
if( n != -1 ) sz = sz.substr( n+1, -1 );
but that gives me a compiler warning. This line works though:
if( n != -1 ) sz = sz.substr( n+1 );

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!