Sign in to follow this  
c4c0d3m0n

sstream help

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,
                       SDL_GetVideoSurface(),
                       scoress.str(),
                       color[whitea],
                       color[blackb] );
    scoress.str("");

// ...

// 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->BitsPerPixel,
                                 dest->format->Rmask,
                                 dest->format->Gmask,
                                 dest->format->Bmask,
                                 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[i].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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this