0
我正在嘗試使用此代碼在我的屏幕上打印文字。C++在屏幕上未顯示文字
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitWindowSize(640, 480);
int WindowHandle = glutCreateWindow("Maze");
if (WindowHandle < 1) {
fprintf(stderr, "ERROR: Could not create a new rendering window.\n");
exit(EXIT_FAILURE);
}
//Initialize OpenGL
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//Initialize the viewport
glViewport(0, 0, 640, 480);
//Initialize OpenGL projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, 640.0f, 480.0f, 0.0f, -2.0f, 2.0f);
//Clear back buffer
glClear(GL_COLOR_BUFFER_BIT);
//Create font
GLuint textureName;
glGenTextures(1, &textureName);
PixelPerfectGLFont font;
try {
font.Create("fonts\\quad20.glf", textureName);
}
catch(GLFontError::InvalidFile) {
cerr << "Cannot load font\n";
abort();
}
//Draw some stuff
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glColor3f(1.0f, 0.0f, 0.0f);
try {
font.Begin();
font.TextOut("hello world", 50, 50, 0);
}
catch(GLFontError::InvalidFont) {
cerr << "Trying to draw with an uninitialized font\n";
abort();
}
glDisable(GL_TEXTURE_2D);
glLoadIdentity();
glutMainLoop();
return 0;
}
將此代碼放在一個main.cpp的文件,它會運行(假設你有接頭右)
上的代碼的頂部我有
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glew.h>
#include <GL/glut.h>
#endif
#include "glfont.h"
使用glfont.h你在上面添加using namespace std;
(否則它會拋出編譯錯誤)
所以......當我運行只顯示一個白色的屏幕,沒有文字顯示的應用程序。
有人可以告訴我什麼我做錯了嗎?
p.s. glfont可以在這裏找到http://students.cs.byu.edu/~bfish/glfontdl.php