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