0

I'm attempting to create a vector of surfaces, which will contain messages the user inputs (its a chat box). But whenever I add new surfaces to the vector and display them, the other surfaces get displayed in the surface vector. I don't know why this is. I assumed it was because the vector keeps being resized with .push_Back(); and the computer wasn't doing this before the other surfaces get moved, so they get "stuck in the middle" and displayed in place of the surfaces. But when I checked against that, it just didn't add any at all! I even tried .reserve( myVect.size() + 1); but alas that only gave me the same issue.

The problem rests with handle_input(); and how the messages are displayed I think. I cannot find anything on this issue anywhere on the web!

Here's my source:

    ofstream logFile;


    SDL_Surface *text = NULL;


void apply_surface (int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip = NULL) {

    SDL_Rect offset;
    offset.x = x;
    offset.y = y;

    SDL_BlitSurface(source, clip, screen, &offset);
}


vector <SDL_Surface*> surface_vect;

bool handle_input() {

if (event.type == SDL_KEYDOWN) {

   std::string temp = str;

   if ( str.length() <= 48) {
      logFile << "str's length is <=48.\n";
      if ( event.key.keysym.unicode == (Uint16) ' ' ) {

         str += (char)event.key.keysym.unicode;
         logFile << "Space bar was pressed.\n";
         charcount += 1;
      }
      else if ( ( event.key.keysym.unicode >= (Uint16)'0') && (event.key.keysym.unicode <= (Uint16) '9' ) ) //Finally works!
      {
         charcount += 1;
         str += (char)event.key.keysym.unicode;
         logFile << "Number key: " <<(char)event.key.keysym.unicode << " pressed, and added to str.\n";
      }
      else if (( event.key.keysym.unicode >= (Uint16)'A' )&& (event.key.keysym.unicode <= (Uint16) 'Z') )
      {
         charcount += 1;
         str += (char)event.key.keysym.unicode;
         logFile << "Number key: " <<(char)event.key.keysym.unicode << " pressed, and added to str.\n";
      }
      else if ( (event.key.keysym.unicode >= (Uint16)'a' )&& (event.key.keysym.unicode <= (Uint16) 'z' ) )
      {
         charcount += 1;
         str += (char)event.key.keysym.unicode;
         logFile << "Number key: " <<(char)event.key.keysym.unicode << " pressed, and added to str.\n";
      }
      else if ( (event.key.keysym.unicode == SDLK_RETURN ) )
      {
         str = "";
         surface_vect.reserve ( surface_vect.size() + 1);

         if ( surface_vect.size() < surface_vect.capacity() ) { //PREVENTS AN ACCESS READING VIOLATION ERROR! THE ELEMENTS IN THE VECTOR WERE BECOMING TOO LARGE FOR THE VECTOR!!
         surf_count += 1;
         surface_vect.push_back ( text );
         logFile << "surface_vect has: " << surf_count << " surfaces!";
     }

     }
      if (event.key.keysym.unicode == SDLK_BACKSPACE && str.length() != 0) {
            charcount -= 1;
        str.erase(str.length() - 1);
         logFile << "Backspace was pressed.\n";
      }
      if (str != temp) {
          SDL_FreeSurface ( text );  //This frees up the text surface whenever the str changes.

          text = TTF_RenderText_Solid(font, str.c_str(), textcolor); //Renders the text whenever that change occurs.

         logFile << "Full text rendered: " << str.c_str() << " Number of surfaces in existence: " << c << " Number of characters in current surface: " << charcount  << "\n"; 
      }


      //OK SO: the program separates the surfaces! Now to just make each surface different for each different message! To do this, we will put the "text" surface into the top of surface_vect, because that vector will be our messages body. Text will display all messages being typed, only when return is pressed is it added to the message surfaces body!
   }
}
return true;
}


int main ( int argc, char* args[] )
{
    time(&currentTime);

    int count = 0;

    text = TTF_RenderText_Solid(font, "First surface message!", textcolor);

    surface_vect.push_back( text );

    Timer myTime;
    Timer fps;

    if (init() == false ) {
        return false;
    }

    logFile << "Everything initialized fine. \n" ;
    if (load_files() == false) {

        return false;
    }

    logFile << "All files loaded successfully.\n";

myTime.start(); //The time the loop started. Keeps track of the time.

while (quit == false )
{
    fps.start(); //Frame timer.
    std::stringstream time; //Something to convert the time into a string, along with a message.
    while (SDL_PollEvent( &event ) ) {
        count += 1;
    if ( handle_input() == false ) {
        logFile << "Handle_input() returned false on the: " << count << "th run through the while loop!";
        return false;
    }

        if ( event.type  == SDL_QUIT ) {
            fps.stop();
            myTime.stop();
            quit = true; //Duh.
        }

    }

    apply_surface(0,0, background, screen);
    apply_surface ( 25, 435, text, screen ); //Render the text surface because its separate from the other surfaces! It needs its own render command.

for ( int c = 0; c <= surface_vect.size() - 1; c+=1 ) {
        apply_surface(25, newY - (13 * c), surface_vect.at(c), screen);
    }

    if (SDL_Flip (screen) == - 1) {
        return false;
    }
    frame += 1; //Frame counter. Used to 
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }

}
clean_up();
return 0;

}

Is there a better way to do this? Its supposed to just take the users text, put it into a vector of surfaces, and display them above.

4

1 回答 1

0

I don't understand your description of the symptoms but looking at the code I guess the text you push into the surface_vect vector isn't displayed afterwards, because when the user pushes the return key you are effectively doing (I keep only the relevant lines) :

surface_vect.push_back ( text );
SDL_FreeSurface ( text );
text = TTF_RenderText_Solid(font, str.c_str(), textcolor);

Remember the text variable and the surface_vect vector don't hold actual surfaces but pointers to surfaces, so when you do the push_back you don't put a copy of the surface into the vector, just a copy of the pointer. When you SDL_FreeSurface(text) the surface itself is cleared which means the pointer you just inserted into the vector is now pointing to a cleared surface which won't display anything ... you can solve this by doing a text=NULL after the push_back (SDL_FreeSurface(text) accepts NULL and won't do anything)

Note there are a lot of other little problems in your code (like your use vector as if it was a C array, or the fact you should use a list instead) but it's not in the scope of the question ;)

于 2012-08-06T17:52:13.340 回答