Parallel Programming - Commit before I break things
[matches/honours.git] / course / semester2 / pprog / assignment1 / single-thread / graphics.c
index d3a8b3c..864ff91 100644 (file)
@@ -1,28 +1,64 @@
 /**
  * @file graphics.c
- * @author Sam Moore (20503628) 2012 - adapted from template program provided by UWA
+ * @author Sam Moore (20503628) 2012 
+ *     - Adapted from template program provided by UWA
  * @purpose Definition of graphics related functions
+ * NOTE: This file is identical for both the single-threaded and multi-threaded versions of the program
  */
 
-#include "graphics.h" //Function declarations
+
+#include <stdlib.h>
+#include <stdio.h>
 #include <time.h> // Needed for clock
 #include <math.h> // Maths functions (sin, cos)
-/**
- * Variables
- */
+
+#include "graphics.h" //Function declarations
+#include "nbody.h" //The simulation
+#include <GL/freeglut.h>
+
+// --- Variable definitions --- //
 double previousTime, eyeTheta, eyePhi, eyeRho;
 float look[3];
 int windowWidth, windowHeight, upY;
-
 double scale = 1.0;
 
 
 /**
- * Initialise the graphics
+ * @function Graphics_Run
+ * @purpose Setup and start the graphics engine
+ * @param argc - Number of arguments to main function, passed to glutInit
+ * @param argv - Argument strings for main function, passed to glutInit
  */
-void Graphics_Init(void
+void Graphics_Run(int argc, char ** argv
 {
-    
+       if (options.draw_graphics == false)
+       {
+               // 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);  
+       glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
+       glutInitWindowSize(WIDTH, HEIGHT);
+       glutInitWindowPosition(POSITION_X, POSITION_Y);
+
+       //Set window title based on version of program
+       #ifdef SINGLE_THREADED
+               glutCreateWindow("N-Body : Single-Threaded");
+       #elif defined PTHREADED
+               glutCreateWindow("N-Body : Multi-Threaded (pthread)");
+       #elif defined OMP_THREADED
+               glutCreateWindow("N-Body : Multi-Threaded (OpenMP)");   
+       #else
+               glutCreateWindow("N-Body");
+       #endif 
+       glutDisplayFunc(Graphics_Display);
+       glutIdleFunc(Graphics_Display);
+       glutKeyboardFunc(Graphics_Keyboard);
+       glutReshapeFunc(Graphics_Reshape);
+        
+
        glClearColor(1.0,1.0,1.0,0.0);
        glColor3f(0.0f, 0.0f, 0.0f);
        glPointSize(POINT_SIZE);
@@ -58,20 +94,44 @@ void Graphics_Init(void)
        look[1] = 0;
        look[2] = 0;
        gluPerspective(VIEW_ANGLE, (double)WIDTH/(double)HEIGHT, WORLD_NEAR, WORLD_FAR);  
+
+       glutMainLoop();   
 }
 
 /**
- * This function redraws the screen after the positions of particles 
- * have been updated
+ * @function Graphics_Display
+ * @purpose This function redraws the screen after the positions of particles 
+ *             have been updated.
+ *     It also calls System_Compute, and checks for exit conditions, in the single-threaded version only
  */
 void Graphics_Display() 
 {
+
+       if (options.draw_graphics == false)
+       {
+               // 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);
+       }
+
+       //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);
        glLoadIdentity();
        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;
@@ -82,16 +142,25 @@ 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
+
 }
 
 /**
- * This function is to manipulate with the image
+ * @function Graphics_Keyboard
+ * @purpose This function is to manipulate with the image
+ * @param theKey key pressed
+ * @param mouseX, mouseY position of the mouse in the window
  */
 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') {
@@ -126,7 +195,9 @@ void Graphics_Keyboard(unsigned char theKey, int mouseX, int mouseY)
 }
 
 /**
- * Resize the view window
+ * @function Graphics_Reshape
+ * @purpose Resize the view window
+ * @param width, height - New size of the window
  */
 void Graphics_Reshape(int width, int height) 
 {
@@ -140,12 +211,4 @@ void Graphics_Reshape(int width, int height)
 }
 
 
-/**
- * This function is called repeatedly by graphics library. You can consider 
- * it as main loop in the program.
- */
-void Graphics_Animate(void) 
-{
-       System_Compute(&universe); //Compute and update new positions for the time step
-       Graphics_Display(); // Display needs to be called to redraw the screen
-}
+

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