Parallel Programming - Tidied things up
[matches/honours.git] / course / semester2 / pprog / assignment1 / single-thread / graphics.c
index d3a8b3c..e3722e9 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
+
+// --- 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)
+       {
+               while (true)
+                       Graphics_Display();
+                       
+               return;
+       }       
+
+       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,14 +94,56 @@ 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.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)
+       {
+               case RUN:
+                       break;
+               case QUIT:
+                       exit(EXIT_SUCCESS);
+                       break;
+               case QUIT_ERROR:
+                       exit(EXIT_FAILURE);
+                       break;
+       }
+
+       if (options.draw_graphics == false)
+               return;
+       
+
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
@@ -85,7 +163,10 @@ void Graphics_Display()
 }
 
 /**
- * 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) 
 {
@@ -126,7 +207,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 +223,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