Even though graphics are not required... wasting time...
$(BIN) : $(OBJ)
$(CXX) -o $(BIN) $(OBJ)
-%.o : %.c %.h
+%.o : %.c
$(CXX) -c $<
clean :
#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
glDisable(GL_DEPTH_TEST);
SDL_WM_SetCaption(caption, NULL);
+
+ screen_width = w; screen_height = h;
+
atexit(Graphics_Destroy);
}
//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);
+}
+
#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
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];
}
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
}
+ /* 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];
+ */
}
}
}
-/**
- * @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
#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