c++ /SDL渲染文本
C++/SDL render text
我有一个使用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.x
和event.motion.y
获取x和y坐标。这是正确的吗?
我假设它们首先需要被强制转换为字符串
不,不是强制转换而是转换。最简单的方法是使用流,比如:
#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);
std::stringstream tmp;
tmp << "X: " << event.motion.x << " Y: " << event.motion.y;
mouse_move = TTF_RenderText_Solid( font, tmp.str().c_str(), textColor );
通常我使用boost::lexical_cast
或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 );
我倾向于避免std::stringstream
,除非它是迭代的。您必须检查.good()
或类似的检查是否失败,并且它并不像您希望的那样常见。
相关文章:
- 文本文件中的单词链表
- 从命令行c++发送文本文件名
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 用SDL TTF显示带有SDL2和OpenGL的文本
- 我似乎无法让 SDL 处理崇高的文本
- 是否可以使用 select(2) 来监视 SDL 文本输入事件
- SDL:制作静态文本变量
- 使用 SDL TTF for SDL 2 无法查看我的文本
- 编辑文本框的SDL
- 在Mac OSX下的SDL中不显示图像和文本
- c++ /SDL渲染文本
- 使用sdl/ttf重绘文本
- 使用SDL在OpenGL表面上绘制文本