Parallel Programming - Commit before I break everything
[matches/honours.git] / course / semester2 / pprog / assignment1 / single-thread / graphics.c
index 7a7d213..6fdb581 100644 (file)
@@ -33,10 +33,9 @@ void Graphics_Run(int argc, char ** argv)
 {
        if (options.draw_graphics == false)
        {
-               while (true)
-                       Graphics_Display();
-                       
-               return;
+               // This message is left here for when I inevitably accidentally call the function
+               fprintf(stderr, "Graphics_Run should not be called if graphics are disabled!\n");
+               exit(EXIT_FAILURE);
        }       
 
        glutInit(&argc, argv);  
@@ -107,23 +106,20 @@ void Graphics_Run(int argc, char ** argv)
  */
 void Graphics_Display() 
 {
-       //Check whether the program should quit due to steps being computed, or a timeout
-       if (ExitCondition())
+
+       if (options.draw_graphics == false)
        {
-               //printf("Leave graphics loop\n");
-               glutLeaveMainLoop();    
-               return;
+               // This message is left here for when I inevitably accidentally call the function
+               fprintf(stderr, "Graphics_Display should not be called if graphics are disabled!\n");
+               exit(EXIT_FAILURE);
        }
 
-       #ifdef SINGLE_THREADED
-               if (options.verbosity != 0 && universe.steps % options.verbosity == 1)
-                       DisplayStatistics();
-               System_Compute(&universe);
-       #endif
-
-       if (options.draw_graphics == false)
+       //Check whether the graphics thread should exit for any reason
+       if (ExitCondition())
+       {
+               glutLeaveMainLoop();    // original glut does not have this, which makes "nicely" exiting a bit annoying
                return;
-       
+       }
 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);
@@ -131,16 +127,27 @@ void Graphics_Display()
        gluLookAt(eyeRho * sin(eyePhi) * sin(eyeTheta), eyeRho * cos(eyePhi),
                eyeRho * sin(eyePhi) * cos(eyeTheta),look[0], look[1], look[2], 0, upY, 0);
 
+       BeforeDraw(); // Stuff to do before graphics is allowed to draw
+                       // Single-thread - perform a computation step, obviously! It's not done anywhere else
+                       // Multiple threads - ensure that all body positions are updated (ie: not halfway through step).
+                       //      (We don't care about the force and velocity variables though)
+
        for (int i = 0; i < universe.N; ++i) 
        {
                Body * b = universe.body+i;
                glColor3f(0.0f, b->mass/1e11*100, 0.0f);
+               //glColor3f(1.0f, 0.0f, 0.0f);
                glPushMatrix(); // to save the current matrix
                glTranslated(scale*b->x[0], scale*b->x[1], scale*b->x[2]);
                glutSolidSphere (BALL_SIZE, 10, 10);
                glPopMatrix(); // restore the previous matrix
        }
        glFlush();
+
+       AfterDraw(); // Stuff to do after graphics is done drawing
+                       // Single-thread - Nothing at all
+                       // Multiple threads - signal threads it is safe to change position variables
+
 }
 
 /**

UCC git Repository :: git.ucc.asn.au