32811c32da8575baaaa180d43f8b01a8ce3562ff
[physics.git] / src / graphics / graphics.cpp
1 #include "graphics.h"
2 #include "../debug.h"
3
4 #include <GL/gl.h>
5 #include <GL/glu.h>
6 #include <SDL/SDL.h>
7 #include <cmath>
8
9 #include "../mathw.h"
10
11 /// ***** Private Method Headers *****
12
13 void drawCircle(int);
14 void sdlInit();
15 void glInit();
16
17 /// ***** Initializers/Cleaners *****
18
19 void graphicsInit()
20 {
21     sdlInit();
22     glInit();
23 }
24
25 void graphicsClean()
26 {
27
28 }
29
30 /// ***** Public Methods *****
31
32 void glDrawCircle(float radius, const Vector2& vec, const float* color)
33 {
34     glMatrixMode(GL_MODELVIEW);
35     glLoadIdentity();
36     glTranslatef(vec.x, vec.y, -1);
37     glScalef(radius, radius, radius);
38
39     if(color != NULL)
40         glColor3fv(color);
41
42     drawCircle(32);
43 }
44
45 /// ***** Private Methods *****
46
47 void drawCircle(int pieces)
48 {
49     glBegin(GL_POLYGON);
50         for(int n = 0; n < pieces; n++)
51         {
52             float angle = 2 * PI * n / pieces;
53             float ix = cos(angle);
54             float iy = sin(angle);
55
56             glVertex3f(ix, iy, 0);
57         }
58     glEnd();
59 }
60
61 void sdlInit()
62 {
63     if(SDL_Init(SDL_INIT_VIDEO) < 0)
64     {
65         cerr << "SDL_Init failed: " << SDL_GetError() << endl;
66         exit(1);
67     }
68
69     // In order to use SDL_OPENGLBLIT we have to
70     // set GL attributes first
71     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8);
72     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
73
74     if(SDL_SetVideoMode(800, 600, 16, SDL_OPENGL) < 0)
75     {
76         cerr << "SDL_SetVideoMode failed: " << SDL_GetError() << endl;
77         exit(1);
78     }
79 }
80
81 void glInit()
82 {
83     glClearColor(0.0, 0.0, 0.0, 0.0);
84
85     glMatrixMode(GL_PROJECTION);
86     glLoadIdentity();
87
88     glOrtho(0, 800.0, 600.0, 0.0, -0.01, 1.01);
89
90     glMatrixMode(GL_MODELVIEW);
91     glLoadIdentity();
92
93     glEnable(GL_DEPTH_TEST);
94 }