首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SDL_Surface向量

SDL_Surface向量
EN

Stack Overflow用户
提问于 2012-08-05 09:26:23
回答 1查看 929关注 0票数 0

我正在尝试创建一个曲面矢量,它将包含用户输入的消息(它是一个聊天框)。但每当我向矢量中添加新曲面并显示它们时,其他曲面就会显示在曲面矢量中。我不知道为什么会这样。我假设这是因为向量不断地用.push_Back();调整大小,而计算机在其他表面移动之前没有这样做,所以它们被“卡在中间”,并显示在表面的位置。但是当我检查它的时候,它根本没有添加任何东西!我甚至尝试过.reserve( myVect.size() + 1);,但遗憾的是,这只给了我同样的问题。

我认为问题出在handle_input();和消息的显示方式上。我在网上任何地方都找不到关于这个问题的任何东西!

这是我的资料来源:

代码语言:javascript
复制
    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;

}

有没有更好的方法来做这件事?它应该只是将用户的文本放入一个曲面矢量中,并在上面显示它们。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-07 01:52:13

我不理解你对症状的描述,但是看一下代码,我猜你推入surface_vect向量的文本之后不会显示出来,因为当用户按下return键时,你实际上是在做这件事(我只保留了相关的行):

代码语言:javascript
复制
surface_vect.push_back ( text );
SDL_FreeSurface ( text );
text = TTF_RenderText_Solid(font, str.c_str(), textcolor);

请记住,text变量和surface_vect向量并不包含实际的曲面,而是指向曲面的指针,所以当您执行push_back操作时,您不会将曲面的副本放入向量中,而只是指针的副本。当你SDL_FreeSurface(text)的时候,表面本身就被清除了,这意味着你刚刚插入到向量中的指针现在正指向一个清除的表面,这个表面不会显示任何东西……您可以通过在push_back之后执行text=NULL来解决此问题(SDL_FreeSurface(text)接受NULL,不做任何事情)

注意在你的代码中还有很多其他的小问题(比如你使用vector就好像它是一个C数组,或者你应该改用list ),但这不在问题的范围内;)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11813264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档