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
15 * @function Graphics_Init
16 * @purpose Initialise the SDL/OpenGL graphics system
17 * @param caption - The caption to give the view window
18 * @param w - The width of the view window
19 * @param h - The height of the view window
21 void Graphics_Init(const char * caption, int w, int h)
23 if (SDL_Init(SDL_INIT_VIDEO) != 0)
25 fprintf(stderr, "Graphics_Init - Couldn't initialise SDL! SDL_GetError() = %s\n",SDL_GetError());
29 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
31 SDL_Surface * screen = SDL_SetVideoMode(w,h, 32, SDL_OPENGL);
34 fprintf(stderr, "Graphics_Init - Couldn't set SDL VideoMode (%d x %d x 32). SDL_GetError() = %s\n", w, h, SDL_GetError());
38 glEnable(GL_TEXTURE_2D);
39 glClearColor(0,0,0,0);
40 glViewport(0,0,w,h); //DOES matter
41 glClear(GL_COLOR_BUFFER_BIT);
42 glMatrixMode(GL_PROJECTION);
44 glOrtho(0,w,h,0,-1,1);
45 glMatrixMode(GL_MODELVIEW);
47 glDisable(GL_DEPTH_TEST);
48 SDL_WM_SetCaption(caption, NULL);
50 atexit(Graphics_Destroy);
54 * @function Graphics_Destroy
55 * @purpose Destroy the Graphics system
56 * Note: Just wrap to SDL_Quit for now
58 void Graphics_Destroy()
64 * @function Graphics_DrawPixel
65 * @purpose Draw a pixel
66 * @param x[...] Position of pixel
67 * @param r - Red component (0-1)
68 * @param g - Green component (0-1)
69 * @param b - Blue component (0-1)
71 void Graphics_Pixel(int x[DIMENSIONS], float r, float g, float b)
76 glVertex2f(x[0], x[1]);
77 else if (DIMENSIONS == 3)
78 glVertex3f(x[0], x[1], x[2]);
81 fprintf(stderr, "Graphics_DrawPixel - DIMENSIONS = %d, expected 2 or 3\n", DIMENSIONS);
90 * @function Graphics_DrawLine
91 * @purpose Draw a straight line between two points
92 * @param x1[...] - First coordinate
93 * @param x2[...] - Second coordinate
94 * @param r - Red component (0-1)
95 * @param g - Green component (0-1)
96 * @param b - Blue component (0-1)
98 void Graphics_Line(int x1[DIMENSIONS], int x2[DIMENSIONS], float r, float g, float b)
105 glVertex2f(x1[0], x1[1]); // origin of the line
106 glVertex2f(x2[0], x2[1]); // ending point of the line
108 else if (DIMENSIONS == 3)
110 glVertex3f(x1[0], x1[1], x1[2]); // origin of the line
111 glVertex3f(x2[0], x2[1], x2[2]); // ending point of the line
115 fprintf(stderr, "Graphics_DrawLine - DIMENSIONS = %d, expected 2 or 3\n", DIMENSIONS);
125 * @function Graphics_Circle
126 * @purpose Draw a 2D Circle (filled)
127 * @param x[...] Position to draw at
128 * @param radius - Radius of circle
129 * @param r - Red component (0-1)
130 * @param g - Green component (0-1)
131 * @param b - Blue component (0-1)
134 void Graphics_Circle(int x[2], float radius, float r, float g, float b)
138 glBegin(GL_TRIANGLE_FAN);
139 glVertex2f(x[0], x[1]);
140 for (float angle = 0; angle < 360; angle+=1)
142 glVertex2f(x[0] + sin(angle) * radius, x[1] + cos(angle) * radius);
149 * @function Graphics_Clear
150 * @purpose Clear the screen
151 * @param r - Red component of clear colour
152 * @param g - Green component
153 * @param b - Blue component
155 void Graphics_Clear(float r, float g, float b)
157 glClearColor(r,g,b,1);
158 glClear(GL_COLOR_BUFFER_BIT);
162 * @function Graphics_Update
163 * @purpose Update the screen
165 void Graphics_Update()
167 SDL_GL_SwapBuffers();