使用sdl / ttf重绘文本

时间:2015-08-19 11:20:58

标签: c++ sdl sdl-ttf

我正在显示一个变量“预算”,其值可以更改。我想在屏幕上显示“预算”的当前值。

以下是我的相关代码段:

  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;
    }

我有两个问题:

1)我遇到的问题是,预算的价值只会在屏幕上覆盖(您可以在后台看到旧值)。如何刷新曲面文本?

2)当我关闭程序时,我释放了所有的资源。当它们当前没有显示时,我是否必须以某种方式从窗口“分离”表面?

1 个答案:

答案 0 :(得分:0)

我现在正在填写我的预算字段白色的背景,并再次绘制其他所有内容:

    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);