当我在屏幕上绘图时,我想显示游戏得分的动态文本区域。唯一的问题是,当我重绘屏幕时,它没有用新的分值更新,并且零后出现乱码。我要做的是存储一个 int 来保持玩家得分并增加该 int 并在屏幕上重新绘制新值。
void drawText(SDL_Surface* screen,
char* string,
int posX, int posY)
{
TTF_Font* font = TTF_OpenFont("ARIAL.TTF", 40);
SDL_Color foregroundColor = { 255, 255, 255 };
SDL_Color backgroundColor = { 0, 0, 0 };
SDL_Surface* textSurface = TTF_RenderText_Shaded(font, string,
foregroundColor, backgroundColor);
SDL_Rect textLocation = { posX, posY, 0, 0 };
SDL_BlitSurface(textSurface, NULL, screen, &textLocation);
SDL_FreeSurface(textSurface);
TTF_CloseFont(font);
}
char convertInt(int number)
{
stringstream ss;//create a stringstream
ss << number;//add number to the stream
std::string result = ss.str();//return a string with the contents of the stream
const char * c = result.c_str();
return *c;
}
score = score + 1;
char scoreString = convertInt(score);
drawText(screen, &scoreString, 580, 15);