C++/SDL 渲染文本

标签 c++ sdl

我有一个使用 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 am assuming they would need to be casted to strings first

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

#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);

关于C++/SDL 渲染文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7417882/

相关文章:

c++ - OSX OpenGL 设置代码中的竞争条件

javascript - 如何将我的库打包成一个自执行函数? (emscripten)

C++ 二叉树堆栈溢出

c++ - 在 try-catch block 中包装循环会导致性能问题吗?

opengl - 第一次调用 opengl 时出现 sdl/opengl 段错误(macbook pro/macports)

c++ - Opengl + SDL 链接错误

c++ - 如何为模板化运算符指定模板参数

c++ - 通过属性搜索和提取XML节点

c++ - 从数组访问 SDL_Rect 成员时出现垃圾值?

c++ - SDL/TTF : TTF_RenderText_Blended_Wrapped returns NULL