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