c++ - 使用 ostream 使用 opengl 在 SDL 中打印乐谱?

标签 c++ sdl

我正在使用以下代码在 SDL 屏幕中打印:

SDL_GL_RenderText(  timer, font, color, &position);
sprintf(timer," Time : %d",time);

因为我在 C++ 上做项目,而且 sprintf 是 C 风格的函数,所以我不应该使用 sprintf,我正在尝试使用 ostringstream,但是我不确定如何使用它。

有人发布了等效的 c++ 样式(使用 ostringstream 或等效的)。

最佳答案

您可以在任何文档或示例中找到的代码示例..

#include <sstream>

std::ostringstream str;
str << "Time: " << time;

// get the std::string
str.str();
// get the C char*
str.str().c_str();

// reset the string stream
str.str("Initial value");

所以如果你想使用 C 字符数组

const char* text = str.str().c_str();
SDL_GL_RenderText(text, font, color, &position);

实现此目的的另一种方法是使用自定义 std::strbufstd::ostream 包装对 SDL_GL_RenderText 的调用,这将允许执行类似:

gl_out << font << color << position << "Timer :" << time << std::flush;

根据字体、颜色和位置具有真实类型,而不是类似 void* 或简单的 int。如果是这种情况,您可以用自定义类型包装它们。

这种方法有点复杂,需要对标准 C++ 库中的流管理有很好的理解,但 IMO 更优雅。虽然我从未在 OpenGL 上尝试过,但它适用于 syslog(具有日志级别规范)和其他文本输出媒体。

关于c++ - 使用 ostream 使用 opengl 在 SDL 中打印乐谱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15228642/

相关文章:

c++ - 在 mingw 中编译 SDL 1.3 时,SDL.dll 位于何处

c++ - 不可能 : this pointer as a default argument. 为什么?

c++ - 如何为 C++ 重载解析和 SFINAE 编写正/负测试?

c++ - 需要帮助解决错误 C2664

c++ - SDL 自身和其他窗口崩溃

c++ - SDL2 无法正确链接

c++ - 如何使 Windows 上的 Clang 链接到不同的运行时

c++ - new int[] 抛出 'Access Violation' 异常

OpenGL、SDL_ttf

c++ - 在 SDL2 中创建 TextureManager 的最有效方式