Search code examples
openglcompiler-errorskeyboardmoverepaint

OpenGL: Error moving object using keyboard


I'm new to OpenGL and I'm trying to paint a stickman and then move it using the arrow-keys from the keyboard. My idea was to use global variables for the stickman and to change them when a certain key is pressed. Afterwards the draw-function (myDisplay()) is called again. Unfortunately i always get the following error-message: "Error 10 error C2371: 'myDisplay' : redefinition; different basic types " When I replace the myDisplay()-call in the keyboard-function with glutPostRedisplay() as suggested in some tutorials I read the error message disapears and the build is successful. But the stickman doesn't move when pressing the keys.

Here's my code:

#include <GL/glut.h>

GLint x; GLint y; GLint d;  //parameters for the stickman

void myKeyboard(unsigned char key, int mx, int my) {
    int x1 = mx;
    int y1 = 480 - my;

    switch(key){
    case GLUT_KEY_LEFT :
        x = x-50;
        myDisplay();
        break;
    case 'E' : 
        exit(-1);
        break;
    default: 
        break;
    }
}


void stickman () {
    glBegin(GL_LINES);          //body
        glVertex2i(x, y);
        glVertex2i(x, y-2*d);
        glVertex2i(x-d, y-d);
        glVertex2i(x+d, y-d);
        glVertex2i(x, y-2*d);
        glVertex2i(x-d, y-3*d);
        glVertex2i(x, y-2*d);
        glVertex2i(x+d, y-3*d);
    glEnd();

    glBegin(GL_LINE_LOOP);      //head
        glVertex2i(x,y);
        glVertex2i(x+0.5*d, y);
        glVertex2i(x+0.5*d, y+0.5*d);
        glVertex2i(x-0.5*d, y+0.5*d);
        glVertex2i(x-0.5*d, y);
    glEnd();
}


void myDisplay() {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0, 0.0, 0.0);

    stickman();

    glFlush();
}

void myInit() {
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}

void main(int argc, char** argv) {
    x = 320;
    y = 350;
    d = 100;

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(200, 50);
    glutCreateWindow("Stickman");
    glutDisplayFunc(myDisplay);
    glutKeyboardFunc(myKeyboard);
    glutPostRedisplay();
    myInit();
    glutMainLoop();
}

Solution

  • As I found out (GLUT Keyboard Tutorial) the problem wasn't the repaint of the picture, but rather that the left arrow key wasn't recognized. I needed to write another keyboard-function for my special keys such as the arrow keys.

    Now my code looks like this and it works:

    void processNormalKeys (unsigned char key, int mx, int my) {
        if (key == 'E')
            exit(-1);   
    }
    
    void processSpecialKeys (int key, int mx, int my) {
        switch(key){
        case GLUT_KEY_LEFT :
            x = x-5;
            glutPostRedisplay();
            break;
        case GLUT_KEY_RIGHT :
            x = x+5;
            glutPostRedisplay();
            break;
        case GLUT_KEY_UP :
            y = y+5;
            glutPostRedisplay();
            break;
        case GLUT_KEY_DOWN :
            y = y-5;
            glutPostRedisplay();
            break;
        default: 
            break;
        }
    }
    

    I also had to adjust the main function:

    glutKeyboardFunc(processNormalKeys);
    glutSpecialFunc(processSpecialKeys);
    

    But thanks for your help anyways!