changed src so the libpg headers are now used
[physics.git] / src / graphics / graphics.cpp
CommitLineData
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>
ad9f1fb6
PG
22
23#include <GL/gl.h>
24#include <GL/glu.h>
25#include <SDL/SDL.h>
26#include <cmath>
27
ca2d526e
PG
28#include <iostream>
29using std::cerr;
30using std::cout;
31using std::endl;
32
68b2316d 33
ad9f1fb6 34/// ***** Private Method Headers *****
617dcc71 35
68b2316d 36void glDrawCircle(int);
4a76d2e2 37void glDrawPolygon( const std::vector<Vector2>& points );
ad9f1fb6
PG
38void sdlInit();
39void glInit();
40
617dcc71 41/// ***** Initializers/Cleaners *****
ad9f1fb6 42
68b2316d 43void graphics::init()
ad9f1fb6
PG
44{
45 sdlInit();
46 glInit();
47}
48
68b2316d 49void graphics::clean()
ad9f1fb6
PG
50{
51
52}
53
617dcc71
PG
54/// ***** Public Methods *****
55
58ac440e 56void graphics::drawCircle(float radius, const Vector2& pos, const float* color)
ad9f1fb6 57{
f7b3b2eb
PG
58 glMatrixMode(GL_MODELVIEW);
59 glLoadIdentity();
9d6dea5f 60 glTranslatef(pos.m_fX, pos.m_fY, -1);
f7b3b2eb
PG
61 glScalef(radius, radius, radius);
62
63 if(color != NULL)
64 glColor3fv(color);
65
68b2316d 66 glDrawCircle(32);
f7b3b2eb
PG
67}
68
4a76d2e2
PG
69void 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
f7b3b2eb 84/// ***** Private Methods *****
ad9f1fb6 85
68b2316d 86void glDrawCircle(int pieces)
f7b3b2eb 87{
ad9f1fb6 88 glBegin(GL_POLYGON);
f7b3b2eb 89 for(int n = 0; n < pieces; n++)
ad9f1fb6 90 {
f7b3b2eb
PG
91 float angle = 2 * PI * n / pieces;
92 float ix = cos(angle);
93 float iy = sin(angle);
ad9f1fb6 94
f7b3b2eb 95 glVertex3f(ix, iy, 0);
ad9f1fb6
PG
96 }
97 glEnd();
98}
99
4a76d2e2
PG
100void 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
9d6dea5f 107 glVertex3f(vec.m_fX, vec.m_fY, 0);
4a76d2e2
PG
108 }
109 glEnd();
110}
111
ad9f1fb6
PG
112void 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
132void glInit()
133{
134 glClearColor(0.0, 0.0, 0.0, 0.0);
135
136 glMatrixMode(GL_PROJECTION);
137 glLoadIdentity();
138
f7b3b2eb 139 glOrtho(0, 800.0, 600.0, 0.0, -0.01, 1.01);
ad9f1fb6
PG
140
141 glMatrixMode(GL_MODELVIEW);
142 glLoadIdentity();
143
144 glEnable(GL_DEPTH_TEST);
b625a3cb
PG
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 */
ad9f1fb6 155}