0

My issue is that my function does the job so quickly that i don't see the progress in the screen, here is my display function:

 void showMaze(const Maze::Maze &m){

 glPushMatrix();
 glTranslatef(-1,1, 0);

 for(int i=0;i<m.num_Y;i++) 
   {
 for(int j=0;j<m.num_X;j++)  
     {

char c = m.maze[i][j]; 

if(c=='1'){   glColor3f(255,255,0);
            glRectf(0.05, -0.05, -0.05,0.05);
        }
if(c=='0'){   glColor3f(20,60,60);
            glRectf(0.05, -0.05, -0.05,0.05);
        } 
 glTranslatef(0.1,0, 0);
  }
     glTranslatef(-(m.num_X*0.1),-0.1, 0);
  }
  glPopMatrix();
  } 

The recursive function:

    bool findPath(Maze* M, char m){  

    showMaze(*M);
    glutPostRedisplay();

        if(M->isFinish())   
            return true;

        if (m!='s' && M->north()){
            update(M, 'n');
            if(isNew(M) && findPath(M, 'n') && M->isFinish()){  
                return true;
            }
            else{       
                M->south();
            }
        }
        // ..... 
            else{
            return false;
        }
    }
  void render()
 {
 glClear( GL_COLOR_BUFFER_BIT );
      findPath(N,'z');
 glutSwapBuffers();
 }

In main:

   glutDisplayFunc( render );

so my question is how do i get to wait few seconds ( so that i can see the progress ) whenever findpath is called, i've tried glutimeelapsed, sleep,gettickcount but none of do the job correctly.

Edit1: when i put something like sleep() right after calling showMaze(), nothing is displayed for few seconds, then the final screen shows, am not an expert in c++, but i suppose showMaze() should be executed first, then the sleep() function, or c++ wait to execute the whole function to display results? Edit2: i found a solution for the problem, i took X and Y o the maze whenever they change, and stored the in two vectors, and in my drawing function i display fisrt the empty maze, then i slowly add the changed X and Y.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Hamza Tahiri
  • 488
  • 3
  • 13
  • Since you're using glut, have you considered using [glutTimerFunc()](https://www.opengl.org/resources/libraries/glut/spec3/node64.html)? – Ken Slade Dec 03 '15 at 05:04
  • i thought about that, but since its a recursive function, once its called it stuck inside the recursive loop until final result of the function – Hamza Tahiri Dec 03 '15 at 05:31
  • Create a class that has an independent thread. Inside this class, create `draw()`. In the main, call `object.draw()` inside `render()`. Don't disturb `MainLoop()` with some loops. This will freeze your screen. You can trigger `findPath` from anywhere in the main function. – CroCo Dec 03 '15 at 06:49
  • for a simple issue its kinda hard to change the whole structure, i toughts also about timed callbacks, but if i cant find a solution multi threading is what i would do – Hamza Tahiri Dec 03 '15 at 07:00

1 Answers1

1

Nothing will be visible on the screen unless you show what you are rendering by swapping buffers (unless you're rendering to the front buffer, but that's iffy). So you can sleep for however long you like in the middle of your recursive function, but you're not going to see anything until you exit that callstack. And by then, you've drawn over everything.

So instead of merely sleeping, you need to use glutSwapBuffers in your recursive call stack when you want something to be visible.

However, this is a bad idea. The render call should not do things like sleep. The system needs your render to actually render, because the screen may need to be updated for a variety of reasons (another window revealing more of your window, etc). You should not have your render loop suspend itself like this.

Instead, what you ought to be doing is executing one segment per render loop execution, relying on glutPostRedisplay to make sure the loop keeps getting called. And you should either base your animation on the time delta between loop executions, or you should use a sleep timer to make sure that at least X time always passes between cycles.

So you need to unroll your recursive call into an iterative process.

Alternatively, if you have access to Boost.Context or Boost.Coroutine, you could use that to handle things. That way, you could keep your recursive structure. When you have rendered everything you want to display, you simply suspending your coroutine back to render, who will swap buffers and return.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • as you said messing with render was a bad idea, so i separated the recursive function from the rendring, by putting the function results in order inside a vector i get to display my results as i want – Hamza Tahiri Dec 04 '15 at 16:49