2015-08-19 2 views
-1

J'affiche un «budget» variable dont la valeur peut changer. Je veux afficher la valeur actuelle de "budget" sur mon écran.Redessiner le texte en utilisant sdl/ttf

Voici mes extraits de code pertinents:

else if (display_list_incredients) { 
      Csurface::OnDraw(surf_display, surf_list_incredients, 0, 0); 
      Csurface::OnDraw(surf_list_incredients, surf_budget, SCREEN_WIDTH/100 * 1, SCREEN_HEIGHT/100 * 2); 
      Csurface::OnDraw(surf_budget, surf_text_budget, 20, 80); 

      I_GameLogic i_game_logic; 
      int y = SCREEN_HEIGHT/100 *20; 
      int x_buttons = SCREEN_WIDTH/100 * 1; 
      int x_text = SCREEN_WIDTH/100 * 2; 
      int i_max = v_incredients.size(); 
      for (int i = 0; i < i_max; i++) 
      { 
       if (y > SCREEN_HEIGHT/100 * 95) { 
        x_buttons = SCREEN_WIDTH/100 * 40; 
        x_text = SCREEN_WIDTH/100 * 41; 
        y = SCREEN_HEIGHT/100 * 20; 
       } 

       Csurface::OnDraw(surf_list_incredients, surf_button_buy, x_buttons, y); 
       std::string text_incredient = i_game_logic.get_text_incredient(v_incredients[i]); 
       surf_text_incredient = Csurface::onLoadText(text_incredient.c_str(), font, text_color); 
       Csurface::OnDraw(surf_list_incredients, surf_text_incredient, x_text, y); 
       y+= SCREEN_HEIGHT/100 * 7; 
      } 
     } 

SDL_Surface * Csurface::onLoadText(const char* text, TTF_Font* font, SDL_Color text_color) { 
      //OutputDebugString("Csurface.cpp onLoadText called\n"); 
      SDL_Surface *surf_return = NULL; 

      surf_return = TTF_RenderText_Solid(font, text, text_color); 

      if (surf_return == NULL) { 
       return false; 
      }else { 
       return surf_return; 
      } 
     } 



    ----------------------------------- 


    else if (display_list_incredients == 1) 
     { 
      if (mX > (SCREEN_WIDTH/100 * 76) && 
       mY > (SCREEN_HEIGHT/100 * 3) && 
       mY < (SCREEN_HEIGHT/100 * 11) && 
       mX < (SCREEN_WIDTH/100 * 99)) 
      { 
       Capp::set_display_list_incredients(0); 
       Capp::set_show_office(1); 
      } 

      I_GameLogic i_game_logic; 
      Incredient incredient; 
      int y_min = SCREEN_HEIGHT/100 * 20; 
      int x_min = SCREEN_WIDTH/100 * 34.2; 
      int x_max = SCREEN_WIDTH/100 * 37.5; 

      //buying the incredients 
      int i_max = v_incredients.size(); 
      for (int i = 0; i < i_max; i++) 
      { 

       if (y_min > SCREEN_HEIGHT/100 * 95) { 
        x_min = SCREEN_WIDTH /100 * 69.8; 
        x_max = SCREEN_WIDTH /100 * 75.8; 
        y_min = SCREEN_HEIGHT/100 * 20; 
       } 

       int y_max = y_min + SCREEN_HEIGHT/100 * 6; 

       if (mX > x_min && 
        mY > y_min && 
        mY < y_max && 
        mX < x_max) 
       { 
        surf_text_budget = NULL; 

        incredient = v_incredients[i]; 
        i_game_logic.reduce_budget(seller, incredient.get_price()*100); 
        std::string text_budget = i_game_logic.get_text_budget(seller); 
        OutputDebugString(text_budget.c_str()); 
        surf_text_budget = Csurface::onLoadText(text_budget.c_str(), font, text_color); 
        OutputDebugString(incredient.get_name().c_str()); 
        break; 
       } 

       y_min += SCREEN_HEIGHT/100 * 7; 
      } 
     } 

bool Csurface::OnDraw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y) { 

     if (Surf_Dest == NULL || Surf_Src == NULL) { 
      return false; 
     } 

     SDL_Rect DestR; 

     DestR.x = X; 
     DestR.y = Y; 

     SDL_BlitSurface(Surf_Src, NULL, Surf_Dest, &DestR); 

     return true; 
    } 

J'ai deux questions:

1) J'ai le problème, que la valeur Le budget est seulement écrasé sur l'écran (vous pouvez voir l'ancienne valeur du e arrière-plan). Comment pouvez-vous actualiser le texte d'une surface?

2) Je libère toutes les ressources lorsque je ferme mon programme. Dois-je en quelque sorte «détacher» les surfaces de ma fenêtre lorsqu'elles ne sont pas affichées actuellement?

Répondre

0

Je suis maintenant l'arrière-plan remplissais de mon champ budgétaire blanc, et tout le reste dessin dessus:

SDL_FillRect(surf_budget, NULL, SDL_MapRGB(surf_budget->format, 0xFF, 0xFF, 0xFF)); 
    surf_budget = Csurface::OnLoad(budget_file); 
    std::string text_budget = i_gameLogic.get_text_budget(seller); 
    surf_text_budget = Csurface::onLoadText(text_budget.c_str(), font_large, text_color);