Commit | Line | Data |
---|---|---|
e68f847b PG |
1 | /* |
2 | * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com. | |
3 | * | |
4 | * This program is free software: you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation, either version 3 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
ad9f1fb6 | 18 | #include "graphics.h" |
3d1f0813 | 19 | #include "debug.h" |
ad9f1fb6 PG |
20 | |
21 | #include <GL/gl.h> | |
22 | #include <GL/glu.h> | |
23 | #include <SDL/SDL.h> | |
24 | #include <cmath> | |
25 | ||
3d1f0813 | 26 | #include "mathw.h" |
ad9f1fb6 | 27 | |
ca2d526e PG |
28 | #include <iostream> |
29 | using std::cerr; | |
30 | using std::cout; | |
31 | using std::endl; | |
32 | ||
68b2316d | 33 | |
ad9f1fb6 | 34 | /// ***** Private Method Headers ***** |
617dcc71 | 35 | |
68b2316d | 36 | void glDrawCircle(int); |
ad9f1fb6 PG |
37 | void sdlInit(); |
38 | void glInit(); | |
39 | ||
617dcc71 | 40 | /// ***** Initializers/Cleaners ***** |
ad9f1fb6 | 41 | |
68b2316d | 42 | void graphics::init() |
ad9f1fb6 PG |
43 | { |
44 | sdlInit(); | |
45 | glInit(); | |
46 | } | |
47 | ||
68b2316d | 48 | void graphics::clean() |
ad9f1fb6 PG |
49 | { |
50 | ||
51 | } | |
52 | ||
617dcc71 PG |
53 | /// ***** Public Methods ***** |
54 | ||
58ac440e | 55 | void graphics::drawCircle(float radius, const Vector2& pos, const float* color) |
ad9f1fb6 | 56 | { |
f7b3b2eb PG |
57 | glMatrixMode(GL_MODELVIEW); |
58 | glLoadIdentity(); | |
58ac440e | 59 | glTranslatef(pos.x, pos.y, -1); |
f7b3b2eb PG |
60 | glScalef(radius, radius, radius); |
61 | ||
62 | if(color != NULL) | |
63 | glColor3fv(color); | |
64 | ||
68b2316d | 65 | glDrawCircle(32); |
f7b3b2eb PG |
66 | } |
67 | ||
68 | /// ***** Private Methods ***** | |
ad9f1fb6 | 69 | |
68b2316d | 70 | void glDrawCircle(int pieces) |
f7b3b2eb | 71 | { |
ad9f1fb6 | 72 | glBegin(GL_POLYGON); |
f7b3b2eb | 73 | for(int n = 0; n < pieces; n++) |
ad9f1fb6 | 74 | { |
f7b3b2eb PG |
75 | float angle = 2 * PI * n / pieces; |
76 | float ix = cos(angle); | |
77 | float iy = sin(angle); | |
ad9f1fb6 | 78 | |
f7b3b2eb | 79 | glVertex3f(ix, iy, 0); |
ad9f1fb6 PG |
80 | } |
81 | glEnd(); | |
82 | } | |
83 | ||
ad9f1fb6 PG |
84 | void sdlInit() |
85 | { | |
86 | if(SDL_Init(SDL_INIT_VIDEO) < 0) | |
87 | { | |
88 | cerr << "SDL_Init failed: " << SDL_GetError() << endl; | |
89 | exit(1); | |
90 | } | |
91 | ||
92 | // In order to use SDL_OPENGLBLIT we have to | |
93 | // set GL attributes first | |
94 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8); | |
95 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
96 | ||
97 | if(SDL_SetVideoMode(800, 600, 16, SDL_OPENGL) < 0) | |
98 | { | |
99 | cerr << "SDL_SetVideoMode failed: " << SDL_GetError() << endl; | |
100 | exit(1); | |
101 | } | |
102 | } | |
103 | ||
104 | void glInit() | |
105 | { | |
106 | glClearColor(0.0, 0.0, 0.0, 0.0); | |
107 | ||
108 | glMatrixMode(GL_PROJECTION); | |
109 | glLoadIdentity(); | |
110 | ||
f7b3b2eb | 111 | glOrtho(0, 800.0, 600.0, 0.0, -0.01, 1.01); |
ad9f1fb6 PG |
112 | |
113 | glMatrixMode(GL_MODELVIEW); | |
114 | glLoadIdentity(); | |
115 | ||
116 | glEnable(GL_DEPTH_TEST); | |
b625a3cb PG |
117 | |
118 | // anti aliasing? | |
119 | /* | |
120 | glEnable(GL_BLEND); | |
121 | glEnable(GL_POLYGON_SMOOTH_HINT); | |
122 | ||
123 | glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE); | |
124 | ||
125 | glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE); | |
126 | */ | |
ad9f1fb6 | 127 | } |