使用 ttf 显示循环中的文本
Displaying text from a loop using ttf
我想使用 ttf 显示矢量中的文本。到目前为止我的代码:
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) {
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 *25;
int i_max = v_incredients.size();
for (int i = 0; i < i_max; i++) {
Csurface::OnDraw(surf_list_incredients, surf_button_buy, SCREEN_WIDTH / 100 * 5, y);
std::string text_incredient = i_game_logic.get_text_incredient(v_incredients[i]);
OutputDebugString(text_incredient.c_str());
surf_text_incredient = Csurface::onLoadText(text_incredient.c_str(), font, text_color);
Csurface::OnDraw(surf_button_buy, surf_text_incredient, 0,0);
y+= SCREEN_HEIGHT / 100 * 5;
}
}
在文本表面中,所有单个成分似乎都在彼此之上呈现。
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;
}
我发现了我的错误,我在每个按钮字段的每个循环中绘制文本。我现在将文本渲染到背景中,这很有效。
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;
我想使用 ttf 显示矢量中的文本。到目前为止我的代码:
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) {
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 *25;
int i_max = v_incredients.size();
for (int i = 0; i < i_max; i++) {
Csurface::OnDraw(surf_list_incredients, surf_button_buy, SCREEN_WIDTH / 100 * 5, y);
std::string text_incredient = i_game_logic.get_text_incredient(v_incredients[i]);
OutputDebugString(text_incredient.c_str());
surf_text_incredient = Csurface::onLoadText(text_incredient.c_str(), font, text_color);
Csurface::OnDraw(surf_button_buy, surf_text_incredient, 0,0);
y+= SCREEN_HEIGHT / 100 * 5;
}
}
在文本表面中,所有单个成分似乎都在彼此之上呈现。
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;
}
我发现了我的错误,我在每个按钮字段的每个循环中绘制文本。我现在将文本渲染到背景中,这很有效。
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;