gpt4 book ai didi

c++ - 将 Int 转换为 char * 并显示在屏幕上

转载 作者:行者123 更新时间:2023-11-30 02:57:56 25 4
gpt4 key购买 nike

<分区>

当我在屏幕上绘图时,我想为游戏得分显示一个动态文本区域。唯一的问题是,当我重绘屏幕时,它并没有更新为新的分值,而且在零之后出现了乱码。我想做的是存储一个 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);

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com