Commit | Line | Data |
---|---|---|
ad9f1fb6 | 1 | #include "graphics.h" |
617dcc71 | 2 | #include "../debug.h" |
ad9f1fb6 PG |
3 | |
4 | #include <GL/gl.h> | |
5 | #include <GL/glu.h> | |
6 | #include <SDL/SDL.h> | |
7 | #include <cmath> | |
8 | ||
617dcc71 | 9 | #include "../mathw.h" |
ad9f1fb6 | 10 | |
68b2316d | 11 | |
ad9f1fb6 | 12 | /// ***** Private Method Headers ***** |
617dcc71 | 13 | |
68b2316d | 14 | void glDrawCircle(int); |
ad9f1fb6 PG |
15 | void sdlInit(); |
16 | void glInit(); | |
17 | ||
617dcc71 | 18 | /// ***** Initializers/Cleaners ***** |
ad9f1fb6 | 19 | |
68b2316d | 20 | void graphics::init() |
ad9f1fb6 PG |
21 | { |
22 | sdlInit(); | |
23 | glInit(); | |
24 | } | |
25 | ||
68b2316d | 26 | void graphics::clean() |
ad9f1fb6 PG |
27 | { |
28 | ||
29 | } | |
30 | ||
617dcc71 PG |
31 | /// ***** Public Methods ***** |
32 | ||
68b2316d | 33 | void graphics::drawCircle(float radius, const Vector2& vec, const float* color) |
ad9f1fb6 | 34 | { |
f7b3b2eb PG |
35 | glMatrixMode(GL_MODELVIEW); |
36 | glLoadIdentity(); | |
89ca62b2 | 37 | glTranslatef(vec.x, vec.y, -1); |
f7b3b2eb PG |
38 | glScalef(radius, radius, radius); |
39 | ||
40 | if(color != NULL) | |
41 | glColor3fv(color); | |
42 | ||
68b2316d | 43 | glDrawCircle(32); |
f7b3b2eb PG |
44 | } |
45 | ||
46 | /// ***** Private Methods ***** | |
ad9f1fb6 | 47 | |
68b2316d | 48 | void glDrawCircle(int pieces) |
f7b3b2eb | 49 | { |
ad9f1fb6 | 50 | glBegin(GL_POLYGON); |
f7b3b2eb | 51 | for(int n = 0; n < pieces; n++) |
ad9f1fb6 | 52 | { |
f7b3b2eb PG |
53 | float angle = 2 * PI * n / pieces; |
54 | float ix = cos(angle); | |
55 | float iy = sin(angle); | |
ad9f1fb6 | 56 | |
f7b3b2eb | 57 | glVertex3f(ix, iy, 0); |
ad9f1fb6 PG |
58 | } |
59 | glEnd(); | |
60 | } | |
61 | ||
ad9f1fb6 PG |
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 | ||
f7b3b2eb | 89 | glOrtho(0, 800.0, 600.0, 0.0, -0.01, 1.01); |
ad9f1fb6 PG |
90 | |
91 | glMatrixMode(GL_MODELVIEW); | |
92 | glLoadIdentity(); | |
93 | ||
94 | glEnable(GL_DEPTH_TEST); | |
95 | } |