使用 sdl/ttf 重绘文本
Redrawing text using sdl/ttf
我正在显示一个变量 "budget",它的值可以改变。我想在屏幕上显示 "budget" 的当前值。
这是我的相关代码片段:
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)我在关闭程序时释放了所有资源。当前未显示时,我是否必须以某种方式 "detach" 来自 window 的表面?
我现在将预算字段的背景填充为白色,并再次在其上绘制其他所有内容:
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);
我正在显示一个变量 "budget",它的值可以改变。我想在屏幕上显示 "budget" 的当前值。
这是我的相关代码片段:
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)我在关闭程序时释放了所有资源。当前未显示时,我是否必须以某种方式 "detach" 来自 window 的表面?
我现在将预算字段的背景填充为白色,并再次在其上绘制其他所有内容:
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);