Parallel Programming - Commit before I break everything
[matches/honours.git] / course / semester2 / pprog / assignment1 / single-thread / graphics.c
index e3722e9..6fdb581 100644 (file)
@@ -14,6 +14,7 @@
 
 #include "graphics.h" //Function declarations
 #include "nbody.h" //The simulation
+#include <GL/freeglut.h>
 
 // --- Variable definitions --- //
 double previousTime, eyeTheta, eyePhi, eyeRho;
@@ -32,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,42 +107,19 @@ void Graphics_Run(int argc, char ** argv)
 void Graphics_Display() 
 {
 
-
-       if (options.verbosity != 0 && universe.steps % options.verbosity == 1)
-               DisplayStatistics();
-
-       #ifdef SINGLE_THREADED
-               System_Compute(&universe);
-
-               //Check whether the program should quit due to steps being computed, or a timeout
-               if (options.timeout > 0.0)
-               {
-                       if ((unsigned)(time(NULL) - options.start_time.tv_sec) >= options.timeout)
-                               exit(EXIT_SUCCESS);
-               }
-               if (options.num_steps > 0 && universe.steps > options.num_steps)
-                       exit(EXIT_SUCCESS);
-
-
-       #endif
-
-
-       //Check whether the runstate has been set to quit the program
-       switch (runstate)
+       if (options.draw_graphics == false)
        {
-               case RUN:
-                       break;
-               case QUIT:
-                       exit(EXIT_SUCCESS);
-                       break;
-               case QUIT_ERROR:
-                       exit(EXIT_FAILURE);
-                       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);
@@ -150,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
+
 }
 
 /**
@@ -172,7 +160,8 @@ void Graphics_Keyboard(unsigned char theKey, int mouseX, int mouseY)
 {
        if (theKey == 'x' || theKey == 'X') 
        {
-               exit(EXIT_SUCCESS);
+               QuitProgram(false);
+               return;
        }
 
                if (theKey == 'i' || theKey == 'I') {

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