From c37245b967333688ad4c2bd842e7ab5ff4d0b8ec Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Tue, 7 Aug 2012 23:30:06 +0800 Subject: [PATCH] pprog assigment0 - messing with graphics Even though graphics are not required... wasting time... --- course/semester2/pprog/assignment0/Makefile | 2 +- course/semester2/pprog/assignment0/graphics.c | 103 ++++++++++++++++++ course/semester2/pprog/assignment0/graphics.h | 18 +-- course/semester2/pprog/assignment0/main.c | 6 +- course/semester2/pprog/assignment0/nbody.c | 21 +--- course/semester2/pprog/assignment0/nbody.h | 2 - 6 files changed, 118 insertions(+), 34 deletions(-) diff --git a/course/semester2/pprog/assignment0/Makefile b/course/semester2/pprog/assignment0/Makefile index 538a0fa0..aa949a24 100644 --- a/course/semester2/pprog/assignment0/Makefile +++ b/course/semester2/pprog/assignment0/Makefile @@ -10,7 +10,7 @@ BIN = nbody $(BIN) : $(OBJ) $(CXX) -o $(BIN) $(OBJ) -%.o : %.c %.h +%.o : %.c $(CXX) -c $< clean : diff --git a/course/semester2/pprog/assignment0/graphics.c b/course/semester2/pprog/assignment0/graphics.c index 747e3991..cf2bf12f 100644 --- a/course/semester2/pprog/assignment0/graphics.c +++ b/course/semester2/pprog/assignment0/graphics.c @@ -11,6 +11,9 @@ #include //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); +} + diff --git a/course/semester2/pprog/assignment0/graphics.h b/course/semester2/pprog/assignment0/graphics.h index f977bd9a..cbd53d70 100644 --- a/course/semester2/pprog/assignment0/graphics.h +++ b/course/semester2/pprog/assignment0/graphics.h @@ -17,17 +17,17 @@ #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 diff --git a/course/semester2/pprog/assignment0/main.c b/course/semester2/pprog/assignment0/main.c index d355687d..a0d46717 100644 --- a/course/semester2/pprog/assignment0/main.c +++ b/course/semester2/pprog/assignment0/main.c @@ -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 diff --git a/course/semester2/pprog/assignment0/nbody.c b/course/semester2/pprog/assignment0/nbody.c index 76c90675..d23b134a 100644 --- a/course/semester2/pprog/assignment0/nbody.c +++ b/course/semester2/pprog/assignment0/nbody.c @@ -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 diff --git a/course/semester2/pprog/assignment0/nbody.h b/course/semester2/pprog/assignment0/nbody.h index efd713ed..6064584f 100644 --- a/course/semester2/pprog/assignment0/nbody.h +++ b/course/semester2/pprog/assignment0/nbody.h @@ -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 -- 2.20.1