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" |
f32a9b7c PG |
19 | |
20 | #include <pg/debug.h> | |
21 | #include <pg/mathw.h> | |
b85b89ba | 22 | using namespace pg; |
ad9f1fb6 PG |
23 | |
24 | #include <GL/gl.h> | |
25 | #include <GL/glu.h> | |
26 | #include <SDL/SDL.h> | |
27 | #include <cmath> | |
28 | ||
ca2d526e PG |
29 | #include <iostream> |
30 | using std::cerr; | |
31 | using std::cout; | |
32 | using std::endl; | |
33 | ||
68b2316d | 34 | |
ad9f1fb6 | 35 | /// ***** Private Method Headers ***** |
617dcc71 | 36 | |
68b2316d | 37 | void glDrawCircle(int); |
4a76d2e2 | 38 | void glDrawPolygon( const std::vector<Vector2>& points ); |
ad9f1fb6 PG |
39 | void sdlInit(); |
40 | void glInit(); | |
41 | ||
617dcc71 | 42 | /// ***** Initializers/Cleaners ***** |
ad9f1fb6 | 43 | |
68b2316d | 44 | void graphics::init() |
ad9f1fb6 PG |
45 | { |
46 | sdlInit(); | |
47 | glInit(); | |
48 | } | |
49 | ||
68b2316d | 50 | void graphics::clean() |
ad9f1fb6 PG |
51 | { |
52 | ||
53 | } | |
54 | ||
617dcc71 PG |
55 | /// ***** Public Methods ***** |
56 | ||
58ac440e | 57 | void graphics::drawCircle(float radius, const Vector2& pos, const float* color) |
ad9f1fb6 | 58 | { |
f7b3b2eb PG |
59 | glMatrixMode(GL_MODELVIEW); |
60 | glLoadIdentity(); | |
9d6dea5f | 61 | glTranslatef(pos.m_fX, pos.m_fY, -1); |
f7b3b2eb PG |
62 | glScalef(radius, radius, radius); |
63 | ||
64 | if(color != NULL) | |
65 | glColor3fv(color); | |
66 | ||
68b2316d | 67 | glDrawCircle(32); |
f7b3b2eb PG |
68 | } |
69 | ||
4a76d2e2 PG |
70 | void graphics::drawPolygon |
71 | ( | |
72 | const std::vector<Vector2>& points, | |
73 | const float* color | |
74 | ) | |
75 | { | |
76 | glMatrixMode(GL_MODELVIEW); | |
77 | glLoadIdentity(); | |
78 | ||
79 | if(color != NULL) | |
80 | glColor3fv(color); | |
81 | ||
82 | glDrawPolygon(points); | |
83 | } | |
84 | ||
f7b3b2eb | 85 | /// ***** Private Methods ***** |
ad9f1fb6 | 86 | |
68b2316d | 87 | void glDrawCircle(int pieces) |
f7b3b2eb | 88 | { |
ad9f1fb6 | 89 | glBegin(GL_POLYGON); |
f7b3b2eb | 90 | for(int n = 0; n < pieces; n++) |
ad9f1fb6 | 91 | { |
f7b3b2eb PG |
92 | float angle = 2 * PI * n / pieces; |
93 | float ix = cos(angle); | |
94 | float iy = sin(angle); | |
ad9f1fb6 | 95 | |
f7b3b2eb | 96 | glVertex3f(ix, iy, 0); |
ad9f1fb6 PG |
97 | } |
98 | glEnd(); | |
99 | } | |
100 | ||
4a76d2e2 PG |
101 | void glDrawPolygon( const std::vector<Vector2>& points ) |
102 | { | |
103 | glBegin(GL_POLYGON); | |
104 | for(unsigned int n = 0; n < points.size(); n++) | |
105 | { | |
106 | const Vector2& vec = points.at(n); | |
107 | ||
9d6dea5f | 108 | glVertex3f(vec.m_fX, vec.m_fY, 0); |
4a76d2e2 PG |
109 | } |
110 | glEnd(); | |
111 | } | |
112 | ||
ad9f1fb6 PG |
113 | void sdlInit() |
114 | { | |
115 | if(SDL_Init(SDL_INIT_VIDEO) < 0) | |
116 | { | |
117 | cerr << "SDL_Init failed: " << SDL_GetError() << endl; | |
118 | exit(1); | |
119 | } | |
120 | ||
121 | // In order to use SDL_OPENGLBLIT we have to | |
122 | // set GL attributes first | |
123 | SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8); | |
124 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
125 | ||
126 | if(SDL_SetVideoMode(800, 600, 16, SDL_OPENGL) < 0) | |
127 | { | |
128 | cerr << "SDL_SetVideoMode failed: " << SDL_GetError() << endl; | |
129 | exit(1); | |
130 | } | |
131 | } | |
132 | ||
133 | void glInit() | |
134 | { | |
135 | glClearColor(0.0, 0.0, 0.0, 0.0); | |
136 | ||
137 | glMatrixMode(GL_PROJECTION); | |
138 | glLoadIdentity(); | |
139 | ||
f7b3b2eb | 140 | glOrtho(0, 800.0, 600.0, 0.0, -0.01, 1.01); |
ad9f1fb6 PG |
141 | |
142 | glMatrixMode(GL_MODELVIEW); | |
143 | glLoadIdentity(); | |
144 | ||
145 | glEnable(GL_DEPTH_TEST); | |
b625a3cb PG |
146 | |
147 | // anti aliasing? | |
148 | /* | |
149 | glEnable(GL_BLEND); | |
150 | glEnable(GL_POLYGON_SMOOTH_HINT); | |
151 | ||
152 | glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE); | |
153 | ||
154 | glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE); | |
155 | */ | |
ad9f1fb6 | 156 | } |