使用OpenGL 实现 代码弹幕” 200312225 OpenGL”从上到下,循环反复
时间: 2024-05-02 13:17:54 浏览: 12
以下是使用OpenGL实现代码弹幕的示例代码:
```c++
#include <GL/glut.h>
#include <string.h>
char* message = "200312225 OpenGL";
int messageLength = strlen(message);
int screenWidth = 640;
int screenHeight = 480;
int messageX = 0;
int messageY = 0;
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRasterPos2i(messageX, messageY);
glColor3f(1.0, 1.0, 1.0);
for (int i = 0; i < messageLength; i++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, message[i]);
}
glutSwapBuffers();
}
void timer(int value) {
messageY -= 5;
if (messageY < -18) {
messageY = screenHeight;
}
glutPostRedisplay();
glutTimerFunc(1000 / 60, timer, 0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(screenWidth, screenHeight);
glutCreateWindow("Code Danmu");
glClearColor(0.0, 0.0, 0.0, 1.0);
glutDisplayFunc(display);
glutTimerFunc(0, timer, 0);
glutMainLoop();
return 0;
}
```
解释:
在`display`函数中,我们使用`glRasterPos2i`函数设置弹幕的起始位置,并使用`glColor3f`函数设置颜色。然后使用`glutBitmapCharacter`函数逐个绘制字符。
在`timer`函数中,我们每次将弹幕的Y坐标减少5个像素,当弹幕超出屏幕范围时,将其重新设为屏幕高度。然后使用`glutPostRedisplay`函数刷新窗口。
在`main`函数中,我们设置窗口大小、背景色,并注册`display`和`timer`函数。最后进入GLUT的事件处理循环。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)