pprog assigment0 - messing with graphics
authorSam Moore <[email protected]>
Tue, 7 Aug 2012 15:30:06 +0000 (23:30 +0800)
committerSam Moore <[email protected]>
Tue, 7 Aug 2012 15:30:06 +0000 (23:30 +0800)
Even though graphics are not required... wasting time...

course/semester2/pprog/assignment0/Makefile
course/semester2/pprog/assignment0/graphics.c
course/semester2/pprog/assignment0/graphics.h
course/semester2/pprog/assignment0/main.c
course/semester2/pprog/assignment0/nbody.c
course/semester2/pprog/assignment0/nbody.h

index 538a0fa..aa949a2 100644 (file)
@@ -10,7 +10,7 @@ BIN = nbody
 $(BIN) : $(OBJ) 
        $(CXX) -o $(BIN) $(OBJ)
 
-%.o : %.c %.h
+%.o : %.c
        $(CXX) -c $<
 
 clean :
index 747e399..cf2bf12 100644 (file)
@@ -11,6 +11,9 @@
 
 #include <math.h> //For trig functions
 
+static int screen_width = 0; 
+static int screen_height = 0;
+
 /**
  * @function Graphics_Init
  * @purpose Initialise the SDL/OpenGL graphics system
@@ -47,6 +50,9 @@ void Graphics_Init(const char * caption, int w, int h)
        glDisable(GL_DEPTH_TEST);
        SDL_WM_SetCaption(caption, NULL);
 
+
+       screen_width = w; screen_height = h;
+
        atexit(Graphics_Destroy);
 }
 
@@ -168,3 +174,100 @@ void Graphics_Update()
        //SDL_Flip(screen);
 }
 
+/**
+ * @function Process_Events
+ * @purpose Handle any SDL events recieved.
+ */
+void Process_Events()
+{
+       static float view_v[3] = {0,0,0};
+       static float view_speed = 5.0;
+       static float view_scale = 1;
+       SDL_Event event;
+       
+       
+       
+       
+       while (SDL_PollEvent(&event)) 
+       {
+               switch (event.type)
+               {
+                       case SDL_KEYDOWN:
+                               switch (event.key.keysym.sym)
+                               {
+                                       case SDLK_LEFT:
+                                               view_v[0] += view_speed;
+                                               break;
+                                       case SDLK_RIGHT:
+                                               view_v[0] -= view_speed;
+                                               break;
+                                       case SDLK_UP:
+                                               view_v[1] += view_speed;
+                                               break;
+                                       case SDLK_DOWN:
+                                               view_v[1] -= view_speed;
+                                               break;
+                                       case SDLK_i:
+                                               view_v[0] = 0; view_v[1] = 0; view_v[2] = 0;
+                                               view_scale = 1;
+                                               glLoadIdentity();
+                                               break;
+                                       default:
+                                               break;
+                               }
+                               break;
+                       case SDL_KEYUP:
+                               switch (event.key.keysym.sym)
+                               {
+                                       case SDLK_LEFT:
+                                       case SDLK_RIGHT:
+                                               view_v[0] = 0;
+                                               break;
+                                       case SDLK_UP:
+                                       case SDLK_DOWN:
+                                               view_v[1] = 0;
+                                       default:
+                                               break;
+                               }
+                               break;
+
+                       case SDL_MOUSEBUTTONDOWN:
+                       {
+                               int mouse[2];
+                               SDL_GetMouseState(mouse, mouse+1);
+
+                               switch (event.button.button)
+                               {
+                                       case SDL_BUTTON_WHEELUP:
+                                               
+                                               view_scale = 1.05;
+                                               glTranslatef(screen_width/2.0, screen_height/2.0,0.0);
+                                               glScaled(view_scale, view_scale, 1);
+                                               glTranslatef(-screen_width/2.0, -screen_height/2.0, 0.0);
+                                               break;
+                                       case SDL_BUTTON_WHEELDOWN:
+                                               view_scale = 0.95;
+                                               glTranslatef(screen_width/2.0, screen_height/2.0,0.0);
+                                               glScaled(view_scale, view_scale, 1);
+                                               glTranslatef(-screen_width/2.0, -screen_height/2.0, 0.0);
+                                               break;
+                                       default:
+                                               break;
+
+                               }       
+                               
+                               break;
+                       }
+                       case SDL_MOUSEBUTTONUP:
+                               break;
+                       case SDL_QUIT:
+                               exit(EXIT_SUCCESS);
+                               break;
+               }
+       }
+
+       
+       glTranslatef(view_v[0], view_v[1], view_v[2]);
+       //SDL_Delay(1);
+}
+
index f977bd9..cbd53d7 100644 (file)
 #endif //DIMENSIONS
 
 
-void Graphics_Init(const char * caption, int w, int h); //Initialise graphics
-void Graphics_Destroy(); //Destroy graphics
-void Graphics_Update(); //Update view
-void Graphics_Clear(float r, float g, float b); //Clear screen
-void Graphics_Pixel(int x[DIMENSIONS], float r, float g, float b); //Draw single pixel
-void Graphics_Line(int x1[DIMENSIONS], int x2[DIMENSIONS], float r, float g, float b); //Draw straight line
-void Graphics_Circle(int x[2], float radius, float r, float g, float b); //Draw circle
+extern void Graphics_Init(const char * caption, int w, int h); //Initialise graphics
+extern void Graphics_Destroy(); //Destroy graphics
+extern void Graphics_Update(); //Update view
+extern void Graphics_Clear(float r, float g, float b); //Clear screen
+extern void Graphics_Pixel(int x[DIMENSIONS], float r, float g, float b); //Draw single pixel
+extern void Graphics_Line(int x1[DIMENSIONS], int x2[DIMENSIONS], float r, float g, float b); //Draw straight line
+extern void Graphics_Circle(int x[2], float radius, float r, float g, float b); //Draw circle
 
+extern void Process_Events(); //Handle any events from the SDL system
 
-
-void Graphics_DrawPixel(int x[DIMENSIONS], float r, float g, float b);
+extern void Graphics_DrawPixel(int x[DIMENSIONS], float r, float g, float b);
 
 
 #endif //_GRAPHICS_H
index d355687..a0d4671 100644 (file)
@@ -22,7 +22,7 @@ int main(int argc, char ** argv)
                Graphics_Init("N-Body", 640, 480);
        #endif //_GRAPHICS_H
 
-       for (unsigned a = 0; a < 100; ++a)
+       for (unsigned a = 0; a < 2; ++a)
        {
                float m = ((float)(rand() % 100))/1000.0;
                float x[DIMENSIONS];
@@ -34,13 +34,13 @@ int main(int argc, char ** argv)
                }
                System_AddBody(&system, m, x, v);
        }
-       System_AddBody(&system, 1.1, (float[2]){0,0}, (float[2]){0,0});
+       System_AddBody(&system, 1.0, (float[2]){0,0}, (float[2]){0,0});
 
        while (true)
        {
 
                System_Step(&system);
-               //System_WriteData(&system, stdout);
+               System_WriteData(&system, stdout);
                #ifdef _GRAPHICS_H
                        Graphics_Clear(1, 1, 1); //Clear screen
                        System_Draw(&system); //Draw system
index 76c9067..d23b134 100644 (file)
@@ -351,10 +351,12 @@ void System_Step(System * system)
                                
                }
 
+               /* Reflections
                if ((system->bodies+a)->x[0] + system->x[0] < 0 || (system->bodies+a)->x[0] + system->x[0] > 640)
                        (system->bodies+a)->v[0] = -(system->bodies+a)->v[0];
                if ((system->bodies+a)->x[1] + system->x[1] < 0 || (system->bodies+a)->x[1] + system->x[1] > 480)
                        (system->bodies+a)->v[1] = -(system->bodies+a)->v[1];
+               */
 
        }
 
@@ -476,25 +478,6 @@ void System_Draw(System * system)
        }
 }
 
-/**
- * @function Process_Events
- * @purpose Handle any SDL events recieved.
- */
-void Process_Events()
-{
-       SDL_Event event;
-       while (SDL_PollEvent(&event)) 
-       {
-               switch(event.type)
-               {
-                       case SDL_QUIT:
-                               exit(EXIT_SUCCESS);
-                               break;
-               }
-       }
-       //SDL_Delay(1);
-}
-
 #endif //_GRAPHICS_H
 
 //EOF
index efd713e..6064584 100644 (file)
@@ -91,8 +91,6 @@ extern void System_WriteData(System * system, FILE * file); //Write System info
 #ifdef _GRAPHICS_H
        extern void System_Draw(System * system); //Draw a System of Bodies
        extern void Body_Draw(Body * body, System * system); //Draw an individual Body
-
-       extern void Process_Events(); //Handle any events from the SDL system
 #endif //_GRAPHICS_H
 
 

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