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