3

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

你能给我一个关于是什么导致问题的线索吗?

PS。我在Linux上工作,在glut ( 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;
    }
}
4

1 回答 1

1

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

void idle(void)
{
    // Force GLUT to render the scene
    glutPostRedisplay();
}
于 2012-07-13T13:54:34.770 回答