3 * @purpose Wrapper for SDL and OpenGL graphics libraries. Used for N-Body simulator. Implementations. Mostly copied from internet tutorials.
12 #include <math.h> //For trig functions
14 static int screen_width = 0;
15 static int screen_height = 0;
18 * @function Graphics_Init
19 * @purpose Initialise the SDL/OpenGL graphics system
20 * @param caption - The caption to give the view window
21 * @param w - The width of the view window
22 * @param h - The height of the view window
24 void Graphics_Init(const char * caption, int w, int h)
26 if (SDL_Init(SDL_INIT_VIDEO) != 0)
28 fprintf(stderr, "Graphics_Init - Couldn't initialise SDL! SDL_GetError() = %s\n",SDL_GetError());
32 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
34 SDL_Surface * screen = SDL_SetVideoMode(w,h, 32, SDL_OPENGL);
37 fprintf(stderr, "Graphics_Init - Couldn't set SDL VideoMode (%d x %d x 32). SDL_GetError() = %s\n", w, h, SDL_GetError());
41 glEnable(GL_TEXTURE_2D);
42 glClearColor(0,0,0,0);
43 glViewport(0,0,w,h); //DOES matter
44 glClear(GL_COLOR_BUFFER_BIT);
45 glMatrixMode(GL_PROJECTION);
47 glOrtho(0,w,h,0,-1,1);
48 glMatrixMode(GL_MODELVIEW);
50 glDisable(GL_DEPTH_TEST);
51 SDL_WM_SetCaption(caption, NULL);
54 screen_width = w; screen_height = h;
56 atexit(Graphics_Destroy);
60 * @function Graphics_Destroy
61 * @purpose Destroy the Graphics system
62 * Note: Just wrap to SDL_Quit for now
64 void Graphics_Destroy()
70 * @function Graphics_DrawPixel
71 * @purpose Draw a pixel
72 * @param x[...] Position of pixel
73 * @param r - Red component (0-1)
74 * @param g - Green component (0-1)
75 * @param b - Blue component (0-1)
77 void Graphics_Pixel(int x[DIMENSIONS], float r, float g, float b)
82 glVertex2f(x[0], x[1]);
83 else if (DIMENSIONS == 3)
84 glVertex3f(x[0], x[1], x[2]);
87 fprintf(stderr, "Graphics_DrawPixel - DIMENSIONS = %d, expected 2 or 3\n", DIMENSIONS);
96 * @function Graphics_DrawLine
97 * @purpose Draw a straight line between two points
98 * @param x1[...] - First coordinate
99 * @param x2[...] - Second coordinate
100 * @param r - Red component (0-1)
101 * @param g - Green component (0-1)
102 * @param b - Blue component (0-1)
104 void Graphics_Line(int x1[DIMENSIONS], int x2[DIMENSIONS], float r, float g, float b)
111 glVertex2f(x1[0], x1[1]); // origin of the line
112 glVertex2f(x2[0], x2[1]); // ending point of the line
114 else if (DIMENSIONS == 3)
116 glVertex3f(x1[0], x1[1], x1[2]); // origin of the line
117 glVertex3f(x2[0], x2[1], x2[2]); // ending point of the line
121 fprintf(stderr, "Graphics_DrawLine - DIMENSIONS = %d, expected 2 or 3\n", DIMENSIONS);
131 * @function Graphics_Circle
132 * @purpose Draw a 2D Circle (filled)
133 * @param x[...] Position to draw at
134 * @param radius - Radius of circle
135 * @param r - Red component (0-1)
136 * @param g - Green component (0-1)
137 * @param b - Blue component (0-1)
140 void Graphics_Circle(int x[2], float radius, float r, float g, float b)
144 glBegin(GL_TRIANGLE_FAN);
145 glVertex2f(x[0], x[1]);
146 for (float angle = 0; angle < 360; angle+=1)
148 glVertex2f(x[0] + sin(angle) * radius, x[1] + cos(angle) * radius);
155 * @function Graphics_Clear
156 * @purpose Clear the screen
157 * @param r - Red component of clear colour
158 * @param g - Green component
159 * @param b - Blue component
161 void Graphics_Clear(float r, float g, float b)
163 glClearColor(r,g,b,1);
164 glClear(GL_COLOR_BUFFER_BIT);
168 * @function Graphics_Update
169 * @purpose Update the screen
171 void Graphics_Update()
173 SDL_GL_SwapBuffers();
178 * @function Process_Events
179 * @purpose Handle any SDL events recieved.
181 void Process_Events()
183 static float view_v[3] = {0,0,0};
184 static float view_speed = 5.0;
185 static float view_scale = 1;
191 while (SDL_PollEvent(&event))
196 switch (event.key.keysym.sym)
199 view_v[0] += view_speed;
202 view_v[0] -= view_speed;
205 view_v[1] += view_speed;
208 view_v[1] -= view_speed;
211 view_v[0] = 0; view_v[1] = 0; view_v[2] = 0;
220 switch (event.key.keysym.sym)
234 case SDL_MOUSEBUTTONDOWN:
237 SDL_GetMouseState(mouse, mouse+1);
239 switch (event.button.button)
241 case SDL_BUTTON_WHEELUP:
244 glTranslatef(screen_width/2.0, screen_height/2.0,0.0);
245 glScaled(view_scale, view_scale, 1);
246 glTranslatef(-screen_width/2.0, -screen_height/2.0, 0.0);
248 case SDL_BUTTON_WHEELDOWN:
250 glTranslatef(screen_width/2.0, screen_height/2.0,0.0);
251 glScaled(view_scale, view_scale, 1);
252 glTranslatef(-screen_width/2.0, -screen_height/2.0, 0.0);
261 case SDL_MOUSEBUTTONUP:
270 glTranslatef(view_v[0], view_v[1], view_v[2]);