Parallel Programming - Commit before I break things
[matches/honours.git] / course / semester2 / pprog / assignment1 / single-thread / graphics.c
index 1b4fcb3..864ff91 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,40 +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())
-       {
-               glutLeaveMainLoop();
-               return;
-       }
-
-
-       #ifdef SINGLE_THREADED
-               if (options.verbosity != 0 && universe.steps % options.verbosity == 1)
-                       DisplayStatistics();
-               System_Compute(&universe);
-       #endif
-
-
 
-       //Check whether the runstate has been set to quit the program
-       switch (runstate)
+       if (options.draw_graphics == false)
        {
-               case RUN:
-                       break;
-               case QUIT:
-                       glutLeaveMainLoop();
-                       return;
-                       break;
-               case QUIT_ERROR:
-                       glutLeaveMainLoop();
-                       return;
-                       break;
+               // 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);
        }
 
-       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);
@@ -148,6 +127,11 @@ 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;
@@ -158,6 +142,11 @@ void Graphics_Display()
                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
+
 }
 
 /**
@@ -170,7 +159,7 @@ void Graphics_Keyboard(unsigned char theKey, int mouseX, int mouseY)
 {
        if (theKey == 'x' || theKey == 'X') 
        {
-               glutLeaveMainLoop();
+               QuitProgram(false);
                return;
        }
 

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