如何在SDL中屏幕上显示分数? [英] How to blit Score on screen in SDL?

查看:312
本文介绍了如何在SDL中屏幕上显示分数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Pong克隆游戏。这几乎完成了,只是当我认为一切都是完美的。 SDL_ttf.h库似乎是一个痛苦。



我将给出一个概述,而不是整个代码,只是为了简单。
我使用了 -

  int PlayerScore = 0; 
int AIScore = 0;

以下是在SDL中呈现文本的语法。

  SDL_Surface * TTF_RenderText_Solid(TTF_Font * font,const char * text,SDL_Color fg); 

现在,看到 const char * text 这就是我需要给我的PlayerScore / AIScore那里。 PlayerScore / AIScore是整数类型,但他们应该是在const char *类型。所以经过几个小时的浏览和研究,我发现有这个 sstream 库我可以用来将整数转换为 const char *



我声明我的表面为imgTxt;

  SDL_Surface * imgTxt; 
std :: stringstream strm;
strm<< PlayerScore;

...

imgTxt = TTF_RenderText_Solid(font,strm.str()。c_str(),fColor);
SDL_BlitSurface(imgTxt,NULL,screen,null);

猜猜?转换成功。但我面临一个不同的问题,只是当我blit的表面。它将分数显示为 0000000000000000000000000000 ,它会继续,一段时间后字体消失,没有任何反应。
我不知道为什么会发生这种情况,也许字符串是附加在每个循环中的得分。这是屏幕截图。



有什么解决方案,为什么会发生这种情况?

解决方案

没有看到整个代码,我不能说。



一个替代方法是:在每个循环中创建sstream使用sprintf。

  char缓冲区[50]; 
sprintf(buffer,%d,PlayerScore);

及更高版本

 code> imgTxt = TTF_RenderText_Solid(font,buffer,fColor); 
SDL_BlitSurface(imgTxt,NULL,screen,null);


I have been working on a Pong clone game. It's almost done, just when I thought everything is perfect. The SDL_ttf.h Library seems to be a pain.

I am going to give a general overview and not the whole code just to make things simple. I have used-

int PlayerScore=0;
int AIScore=0;

Here's the syntax to render text in SDL.

SDL_Surface *TTF_RenderText_Solid(TTF_Font *font, const char *text, SDL_Color fg);

Now, see that const char* text? That's where I need to give my PlayerScore/AIScore there. PlayerScore/AIScore are of integer type but they are supposed to be in const char* type. So after hours of browsing and research I found that there is this sstream library I can use to convert integer to const char*

I declare my surface as imgTxt;

SDL_Surface* imgTxt;
std::stringstream strm;
strm << PlayerScore;

...

imgTxt = TTF_RenderText_Solid( font, strm.str().c_str(), fColor );
SDL_BlitSurface(imgTxt,NULL,screen,null);

Guess what? The conversion is successful. But I faced a different problem, just when I blit the surface. It displays the score as 0000000000000000000000000000 and it keeps on going, after a while the font disappears and nothing happens. I've no idea why this is happening, maybe the string is being appended with the score in every loop? This is the screenshot.

Is there any solution to this as to why this is happening?

解决方案

Without seeing the whole code, I cannot say. It certainly looks like it thought.

Are you creating the sstream every loop?

An alternative would be to use sprintf.

char buffer [50];
sprintf (buffer, "%d", PlayerScore);

and later

imgTxt = TTF_RenderText_Solid( font, buffer, fColor );
SDL_BlitSurface(imgTxt,NULL,screen,null);

这篇关于如何在SDL中屏幕上显示分数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆