场景刷新冻结

时间:2012-07-11 18:33:45

标签: c++ linux opengl glut

我渲染2D(基于图块的)场景并注册按键。按下箭头键时,我移动相机。虽然按键注册得很好,但我的场景不会更新,直到我换到另一个窗口并返回

你能告诉我一个导致问题的原因吗?

PS。我正在 Linux 上工作,在过剩 freeglut )的帮助下开发 OpenGL 应用程序。

我的渲染功能:

void render() {
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();

    if (s_map)
    for (int i = s_iCameraX; i < sc_iTilesY + s_iCameraY && i < s_map->usiHeight; i++)
    {
        for (int j = s_iCameraY; j < sc_iTilesX + s_iCameraX && j < s_map->usiWidth; j++)
        {
            short val = s_map->data[i][j];              
            if (val == 0x0010) glBindTexture(GL_TEXTURE_2D, texture[0]);
            else if (val == 0x0020) glBindTexture(GL_TEXTURE_2D, texture[1]);

            glBegin(GL_QUADS);
                glTexCoord2f(0.0f, 0.0f);   glVertex2f(j-s_iCameraX, i-s_iCameraY);
                glTexCoord2f(1.0f, 0.0f);   glVertex2f(j-s_iCameraX+1.0f, i-s_iCameraY);
                glTexCoord2f(1.0f, 1.0f);   glVertex2f(j-s_iCameraX+1.0f, i-s_iCameraY+1.0f);
                glTexCoord2f(0.0f, 1.0f);   glVertex2f(j-s_iCameraX, i-s_iCameraY+1.0f);
            glEnd();
        }
    }

glutSwapBuffers();
} 

我的特殊密钥处理功能:

void keyproc(int key, int x, int y) {
    switch (key)
    {
    case GLUT_KEY_UP:
        printf("UP: %d %d\n", s_iCameraX, s_iCameraY);
        if (s_iCameraY - 1 >= 0) s_iCameraY--;
        break;

    case GLUT_KEY_DOWN:
        printf("DOWN: %d %d\n", s_iCameraX, s_iCameraY);
        if (s_map && s_iCameraY + 1 < s_map->usiHeight) s_iCameraY++;
        break;

    case GLUT_KEY_RIGHT:
        printf("RIGHT: %d %d\n", s_iCameraX, s_iCameraY);
        if (s_map && s_iCameraX + 1 < s_map->usiWidth) s_iCameraX++;
        break;

    case GLUT_KEY_LEFT:
        printf("LEFT: %d %d\n", s_iCameraX, s_iCameraY);
        if (s_iCameraX - 1 >= 0) s_iCameraX--;
        break;

    default: break;
    }
}

1 个答案:

答案 0 :(得分:1)

以下可能值得一试:添加空闲回调:

void idle(void)
{
    // Force GLUT to render the scene
    glutPostRedisplay();
}