Started Parallel Programming Self Assessment
[matches/honours.git] / course / semester2 / pprog / assignment0 / graphics.c
1 /**
2  * @file graphics.c
3  * @purpose Wrapper for SDL and OpenGL graphics libraries. Used for N-Body simulator. Implementations. Mostly copied from internet tutorials.
4  * @author Sam Moore
5  * @date August 2012
6  */
7
8
9
10 #include "graphics.h"
11
12 #include <math.h> //For trig functions
13
14 /**
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
20  */ 
21 void Graphics_Init(const char * caption, int w, int h)
22 {
23         if (SDL_Init(SDL_INIT_VIDEO) != 0)
24         {
25                 fprintf(stderr, "Graphics_Init - Couldn't initialise SDL! SDL_GetError() = %s\n",SDL_GetError());
26                 exit(EXIT_FAILURE);
27         }
28
29         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); 
30
31         SDL_Surface * screen = SDL_SetVideoMode(w,h, 32, SDL_OPENGL);
32         if ( screen == NULL )
33         {
34                 fprintf(stderr, "Graphics_Init - Couldn't set SDL VideoMode (%d x %d x 32). SDL_GetError() = %s\n", w, h, SDL_GetError());
35                 exit(EXIT_FAILURE);
36         } 
37
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);
43         glLoadIdentity();
44         glOrtho(0,w,h,0,-1,1);
45         glMatrixMode(GL_MODELVIEW);
46         glLoadIdentity();
47         glDisable(GL_DEPTH_TEST);
48         SDL_WM_SetCaption(caption, NULL);
49
50         atexit(Graphics_Destroy);
51 }
52
53 /**
54  * @function Graphics_Destroy
55  * @purpose Destroy the Graphics system
56  *              Note: Just wrap to SDL_Quit for now
57  */
58 void Graphics_Destroy()
59 {
60         SDL_Quit();
61 }
62
63 /**
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)
70  */
71 void Graphics_Pixel(int x[DIMENSIONS], float r, float g, float b)
72 {
73         glBegin(GL_POINTS);
74         glColor3f(r, g, b);
75         if (DIMENSIONS == 2)
76                 glVertex2f(x[0], x[1]); 
77         else if (DIMENSIONS == 3)
78                 glVertex3f(x[0], x[1], x[2]);
79         else
80         {
81                 fprintf(stderr, "Graphics_DrawPixel - DIMENSIONS = %d, expected 2 or 3\n", DIMENSIONS);
82                 exit(EXIT_FAILURE);
83         }
84
85         glColor3f(1,1,1);
86         glEnd();
87 }
88
89 /**
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)
97  */
98 void Graphics_Line(int x1[DIMENSIONS], int x2[DIMENSIONS], float r, float g, float b)
99 {               
100
101         glColor3f(r, g, b);
102         glBegin(GL_LINES);
103         if (DIMENSIONS == 2)
104         {
105                 glVertex2f(x1[0], x1[1]); // origin of the line
106                 glVertex2f(x2[0], x2[1]); // ending point of the line
107         }
108         else if (DIMENSIONS == 3)
109         {
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
112         }
113         else
114         {
115                 fprintf(stderr, "Graphics_DrawLine - DIMENSIONS = %d, expected 2 or 3\n", DIMENSIONS);
116                 exit(EXIT_FAILURE);             
117         }
118         glColor3f(1,1,1);
119         glEnd();
120
121         
122 }
123
124 /**
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)
132  */
133
134 void Graphics_Circle(int x[2], float radius, float r, float g, float b)
135 {
136         glColor3f(r, g, b);
137
138         glBegin(GL_TRIANGLE_FAN);
139         glVertex2f(x[0], x[1]);
140         for (float angle = 0; angle < 360; angle+=1)
141         {
142                 glVertex2f(x[0] + sin(angle) * radius, x[1] + cos(angle) * radius);
143         }
144         glColor3f(1, 1, 1);
145         glEnd();
146 }
147  
148 /**
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
154  */
155 void Graphics_Clear(float r, float g, float b)
156 {
157         glClearColor(r,g,b,1);
158         glClear(GL_COLOR_BUFFER_BIT);
159 }
160
161 /**
162  * @function Graphics_Update
163  * @purpose Update the screen
164  */
165 void Graphics_Update()
166 {
167         SDL_GL_SwapBuffers();
168         //SDL_Flip(screen);
169 }
170

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