如何在过剩的情况下逐字绘制文字?

时间:2014-11-06 14:14:13

标签: c++ opengl text draw glut

我无法使我的GLUT程序正常工作。我想逐字母地绘制文本(字符数组),间隔为几毫秒。我的节目:

#include <string.h>
#include <windows.h>
#include <glut.h>
#include<iostream>

int hei = glutGet(GLUT_SCREEN_HEIGHT)/2;
int wid = glutGet(GLUT_SCREEN_WIDTH)/2;
void *font = GLUT_BITMAP_TIMES_ROMAN_24;
void *fonts[] =
{
  GLUT_BITMAP_9_BY_15,
  GLUT_BITMAP_TIMES_ROMAN_10,
  GLUT_BITMAP_TIMES_ROMAN_24
};

void selectFont(int newfont)
{
  font = fonts[newfont];
  glutPostRedisplay();
}

void selectColor(float r, float g, float b)
{
glColor3f(r, g, b);
  glutPostRedisplay();
}




char *msg;

void tick(void)
{
  glutPostRedisplay();
}
void timer(int value) {

}
int element=0;
void output(int x, int y, char *string, bool center=true) {
  int len;
  len = strlen(string);
  if(center){
  glRasterPos2f((wid-len), y);
  }
  else {
  glRasterPos2f(x, y); 
  }
 for(int i=0;i<len;i++) {
    glutBitmapCharacter(font, string[i]);
    glPopMatrix();
    glutSwapBuffers();
    Sleep(500);
 }
}
void outputmsg(int x, int y, bool center=true) {
  int len;
  len = strlen(msg);
  if(center){
  glRasterPos2f((wid-len), y);
  }
  else {
  glRasterPos2f(x, y); 
  }
 for(int i=0;i<len;i++) {
     glutBitmapCharacter(font, msg[i]);
 }
}
void updatemsg() {
    msg="test msg letter by letter";
}
void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  msg="|";
  updatemsg();
  if(msg!="|")
    output(wid, hei, msg, true);
  else
    output(wid,hei,msg,true);
}
void reshape(int w, int h)
{
  glViewport(0, 0, w, h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0, w, h, 0);
  glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int x, int y) {
    switch(key){
    case 27:
        exit(1);
        break;
    }
}
int main(int argc, char **argv)
{
  int i, msg_submenu, color_submenu;

  glutInit(&argc, argv);
  for (i = 1; i < argc; i++) {
    if (!strcmp(argv[i], "-mono")) {
      font = GLUT_BITMAP_9_BY_15;
    }
  }
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowSize(wid*2, hei*2);
  glutCreateWindow("CHAT");
  glutFullScreen();
  glClearColor(0.0, 0.0, 0.0, 1.0);
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutKeyboardFunc(keyboard);
  glutIdleFunc(tick);
  selectFont(0);
  glutMainLoop();
  return 0;             /* ANSI C requires main to return int. */
}

它真的很奇怪的东西,比如擦除普通的一个字符然后打印然后打印下一个和擦除的字符。我在论坛上尝试了所有关于这个主题的内容。我该如何解决?

2 个答案:

答案 0 :(得分:2)

你应该做的是保持你想要显示的字符串的长度,并每隔几毫秒递增一次:

int len;
char *msg;

void incrementLength(int value){
   if(msg[len]==0)return; //end of string stop the callback
   len++;
   glutTimerFunc(100, incrementLength, 0);
   glutPostRedisplay();
}

void updatemsg() {
    msg="test msg letter by letter";
    len=0;
    glutTimerFunc(100, incrementLength, 0);
}

然后在outputmsg中(如果你实际上调用它)你只循环到len:

void outputmsg(int x, int y, bool center=true) {

  if(center){
  glRasterPos2f((wid-len), y);
  }
  else {
  glRasterPos2f(x, y); 
  }
 for(int i=0;i<len;i++) {
     glutBitmapCharacter(font, msg[i]);
 }
}

答案 1 :(得分:1)

此代码有几个问题:

 for(int i=0;i<len;i++) {
    glutBitmapCharacter(font, string[i]);
    glPopMatrix();
    glutSwapBuffers();
    Sleep(500);
 }
  1. 你正在弹出一个你从未在任何地方推过的矩阵,你正在做len多次。

  2. 您正在交换缓冲区而不清除其间的屏幕......这将导致未定义的结果(通常是前一帧中乱码乱码)。

  3. 你真的需要重新思考你是如何实现这一点的。

    我建议您在display (...)函数中实现一些经过时间检查,以确定要打印的字符,而不是在调用Sleep (...)的循环中执行此操作。