如何在 C++ 中使用 glutBitmapString() 将文本绘制到屏幕上?

2023-09-27C/C++开发问题
3

本文介绍了如何在 C++ 中使用 glutBitmapString() 将文本绘制到屏幕上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试在 2d 中使用 GLUT 在屏幕上绘制文本.

I'm attempting to draw text to the screen using GLUT in 2d.

我想使用 glutBitmapString(),有人可以向我展示一个简单的示例,说明在 C++ 中设置和正确使用此方法必须执行的操作,以便我可以在 (X,Y) 位置绘制任意字符串?

I want to use glutBitmapString(), can someone show me a simple example of what you have to do to setup and properly use this method in C++ so I can draw an arbitrary string at an (X,Y) position?

glutBitmapString(void *font, const unsigned char *string); 

我使用的是 linux,我知道我需要创建一个 Font 对象,尽管我不确定如何以及我可以将字符串作为第二个参数提供给它.但是,我该如何指定 x/y 位置?

I'm using linux, and I know I need to create a Font object, although I'm not sure exactly how and I can supply it with the string as the second arguement. However, how do I also specify the x/y position?

一个简单的例子会对我有很大帮助.如果你能告诉我从创建字体到调用最好的方法.

A quick example of this would help me greatly. If you can show me from creating the font, to calling the method that would be best.

推荐答案

你必须使用 glRasterPos 在调用 glutBitmapString() 之前设置光栅位置.请注意,每次调用 glutBitmapString() 都会推进光栅位置,因此多次连续调用将一个接一个地打印出字符串.您还可以使用 glColor() 设置文本颜色.此处列出了可用字体集.>

You have to use glRasterPos to set the raster position before calling glutBitmapString(). Note that each call to glutBitmapString() advances the raster position, so several consecutive calls will print out the strings one after another. You can also set the text color by using glColor(). The set of available fonts are listed here.

// Draw blue text at screen coordinates (100, 120), where (0, 0) is the top-left of the
// screen in an 18-point Helvetica font
glRasterPos2i(100, 120);
glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, "text to render");

这篇关于如何在 C++ 中使用 glutBitmapString() 将文本绘制到屏幕上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6