SDL_ttf 和数字 (int)

发布于 2024-09-06 02:52:01 字数 438 浏览 6 评论 0原文

int score = 0;
char* fixedscore=(char*)score;
.
.
.
imgTxt = TTF_RenderText_Solid( font, fixedscore, fColor );

^^ 这不起作用 - 看起来固定分数为空或不存在。

int score = 0;
char* fixedscore=(char*)score;
.
.
.
imgTxt = TTF_RenderText_Solid( font, "Works fine", fColor );

^^ 工作正常,但是...

我猜将 int 转换为 char* 不会真的不起作用。那么如何在 SDL 中打印乐谱呢? 哦,还有一件事:为什么文字这么难看?

int score = 0;
char* fixedscore=(char*)score;
.
.
.
imgTxt = TTF_RenderText_Solid( font, fixedscore, fColor );

^^ This doesn't work - looks like fixedscore is empty or doesn't exists.

int score = 0;
char* fixedscore=(char*)score;
.
.
.
imgTxt = TTF_RenderText_Solid( font, "Works fine", fColor );

^^ Works fine, but...

I guess converting int to char* doesn't really work. So how do you print scores in SDL?
Oh and one more thing: why is the text so ugly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

血之狂魔 2024-09-13 02:52:05

我遇到了类似的问题,
我有

using namespace std;
int PlayerScore=0,AIscore=0;
stringstream Pscore,Ascore;
Pscore<<PlayerScore;
Ascore<<AIscore;

score1=TTF_RenderText_Solid( font, Pscore.str().c_str(), fontColor );
//Where score 1 is player1 surface on the screen
score2=TTF_RenderText_Solid( font, Ascore.str().c_str(), fontColor );
//Where score 2 is AI surface on the screen

I faced a similar problem,
I had

using namespace std;
int PlayerScore=0,AIscore=0;
stringstream Pscore,Ascore;
Pscore<<PlayerScore;
Ascore<<AIscore;

score1=TTF_RenderText_Solid( font, Pscore.str().c_str(), fontColor );
//Where score 1 is player1 surface on the screen
score2=TTF_RenderText_Solid( font, Ascore.str().c_str(), fontColor );
//Where score 2 is AI surface on the screen
云柯 2024-09-13 02:52:04

选角不是你想要的。此代码:

int score = 0;
char* fixedscore=(char*)score;

相当于执行以下操作:

char* fixedscore = NULL;

我假设您正在尝试让 fixedscore 来保存分数中数字的文本值。仅使用标准 C++ 的最简单方法是通过 stringstream:

std::stringstream strm;
strm << score;

...

imgTxt = TTF_RenderText_Solid( font, strm.str().c_str(), fColor );

Casting is not what you want. This code:

int score = 0;
char* fixedscore=(char*)score;

is the equivalent of doing:

char* fixedscore = NULL;

I assume you are trying to get fixedscore to hold the textual value of the number in score. The easiest way using just standard C++ is via stringstream:

std::stringstream strm;
strm << score;

...

imgTxt = TTF_RenderText_Solid( font, strm.str().c_str(), fColor );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文