C++/SDL 渲染文本

发布于 2024-12-04 16:40:08 字数 449 浏览 0 评论 0原文

我有一个使用 SDL_ttf 来显示文本的小应用程序。这通过以下方式工作得很好: TTF_RenderText_Solid( font, "text here", textColor ); 但是,我想知道如何渲染整数。我假设它们需要首先转换为字符串,但我遇到了一个问题。特别是当我想像这样显示鼠标的 x 和 y 位置时:

if( event.type == SDL_MOUSEMOTION )
{           
    mouse_move = TTF_RenderText_Solid( font, "need mouse x and y here", textColor );
}

我相信我可以通过 event.motion.xevent.motion.y< 获取 x 和 y 坐标/代码>。这是正确的吗?

I have a little application which uses SDL_ttf to display text. This works just fine via: TTF_RenderText_Solid( font, "text here", textColor ); However, I was wondering how I would go about rendering integers. I am assuming they would need to be casted to strings first, but I am running into a problem with this. Specifically when I want to display the x and y position of the mouse like such:

if( event.type == SDL_MOUSEMOTION )
{           
    mouse_move = TTF_RenderText_Solid( font, "need mouse x and y here", textColor );
}

I believe I can grab the x and y coordinates via event.motion.x and event.motion.y. Is this correct?

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

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

发布评论

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

评论(3

裸钻 2024-12-11 16:40:08

我假设它们需要首先转换为字符串

不,不是转换而是转换。最简单的方法是使用流,例如:

#include <sstream>

// ...

std::stringstream text;
// format
text << "mouse coords: " << event.motion.x << " " << event.motion.y;
// and display
TTF_RenderText_Solid(font, text.c_str(), textColor);

I am assuming they would need to be casted to strings first

Nope, not casted but converted. The simplest way is using streams, something like:

#include <sstream>

// ...

std::stringstream text;
// format
text << "mouse coords: " << event.motion.x << " " << event.motion.y;
// and display
TTF_RenderText_Solid(font, text.c_str(), textColor);
老旧海报 2024-12-11 16:40:08
std::stringstream tmp;
tmp << "X: " << event.motion.x << " Y: " << event.motion.y;
mouse_move = TTF_RenderText_Solid( font, tmp.str().c_str(), textColor );
std::stringstream tmp;
tmp << "X: " << event.motion.x << " Y: " << event.motion.y;
mouse_move = TTF_RenderText_Solid( font, tmp.str().c_str(), textColor );
剑心龙吟 2024-12-11 16:40:08

通常我使用诸如 boost::lexical_castboost::format

int r = 5;
std::string r_str = boost::lexical_cast<std::string>(r);

int x = 10, 7 = 4;
std::string f_str = boost::str( boost::format("Need %1% and %2% here") % x % y );

我倾向于避免 std::stringstream 除非它是迭代的。您必须检查 .good() 或类似的内容来查看它是否失败,而且它并不像您希望的那么常见。

Usually I use something like boost::lexical_cast or boost::format

int r = 5;
std::string r_str = boost::lexical_cast<std::string>(r);

int x = 10, 7 = 4;
std::string f_str = boost::str( boost::format("Need %1% and %2% here") % x % y );

I tend to avoid std::stringstream unless its iterative. You have to check .good() or the like to see if it failed, and it's not as common as you'd hope.

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