Where in my code should I put function to constantly move a square (OpenGl, Glut, looping)

Greetings fellow programmers. I am making a game using opengl and glut. The game consists of a blue square (which you control) getting chased by a red square (the enemy). Currently, the way it gets chased around the window is every time you press a button to move the blue square, the red square moves closer to the blue square.

Below is the code of what I mentioned above:
1
2
3
4
5
6
7
8
9
 case GLUT_KEY_UP:
       posY_player+=move_unit;;
           
            if (posY_monster_1 < posY_player){
                posY_monster_1+=0.13;
            }
            if (posY_monster_1 > posY_player){
                posY_monster_1-=0.13;
            }

My goal is for the event that moves the red square closer to you (the blue square) to be based on a timer (loop).

I have made a void enemy_move() which I want to call every time the red square should move. I am just confused on when in the code I should call it. An example of when it could be called is GlutDisplayFunc but I have not been able to successfully implement this.

The code below shows what I have as the int main. Should I call it somewhere in here?
1
2
3
4
5
6
7
8
9
10
11
12
  int main(int argc, char** argv){
    //initialize mode and open a windows in upper left corner of screen
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(1000, 0);
    glutCreateWindow("Adventurer V.1 By Olivier");
    init();
    glutDisplayFunc(display);
    glutSpecialFunc(keyboardown);

    enemy_move();       

    cout << room_desc;
      
    
}


Is another possibility for me to call it once in int main and then it would redirect it back to int main?

Below is the void enemy_move():
1
2
3
4
5
6
7
8
9
10
void enemy_move(){
SDL_Delay(500);
    if (posY_monster_1 < posY_player){
posY_monster_1+=0.13;
    }
if (posY_monster_1 > posY_player){
    posY_monster_1-=0.13;
    
    
    glutMainLoop(); 
}


What I want to ask from you is how I can (maybe using a loop) move the red square closer to the blue square several times per second.

Thank you in advance,

a curious adventurer
Last edited on
Topic archived. No new replies allowed.