added GPL copyrights
[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"
617dcc71 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
617dcc71 26#include "../mathw.h"
ad9f1fb6 27
68b2316d 28
ad9f1fb6 29/// ***** Private Method Headers *****
617dcc71 30
68b2316d 31void glDrawCircle(int);
ad9f1fb6
PG
32void sdlInit();
33void glInit();
34
617dcc71 35/// ***** Initializers/Cleaners *****
ad9f1fb6 36
68b2316d 37void graphics::init()
ad9f1fb6
PG
38{
39 sdlInit();
40 glInit();
41}
42
68b2316d 43void graphics::clean()
ad9f1fb6
PG
44{
45
46}
47
617dcc71
PG
48/// ***** Public Methods *****
49
68b2316d 50void graphics::drawCircle(float radius, const Vector2& vec, const float* color)
ad9f1fb6 51{
f7b3b2eb
PG
52 glMatrixMode(GL_MODELVIEW);
53 glLoadIdentity();
89ca62b2 54 glTranslatef(vec.x, vec.y, -1);
f7b3b2eb
PG
55 glScalef(radius, radius, radius);
56
57 if(color != NULL)
58 glColor3fv(color);
59
68b2316d 60 glDrawCircle(32);
f7b3b2eb
PG
61}
62
63/// ***** Private Methods *****
ad9f1fb6 64
68b2316d 65void glDrawCircle(int pieces)
f7b3b2eb 66{
ad9f1fb6 67 glBegin(GL_POLYGON);
f7b3b2eb 68 for(int n = 0; n < pieces; n++)
ad9f1fb6 69 {
f7b3b2eb
PG
70 float angle = 2 * PI * n / pieces;
71 float ix = cos(angle);
72 float iy = sin(angle);
ad9f1fb6 73
f7b3b2eb 74 glVertex3f(ix, iy, 0);
ad9f1fb6
PG
75 }
76 glEnd();
77}
78
ad9f1fb6
PG
79void sdlInit()
80{
81 if(SDL_Init(SDL_INIT_VIDEO) < 0)
82 {
83 cerr << "SDL_Init failed: " << SDL_GetError() << endl;
84 exit(1);
85 }
86
87 // In order to use SDL_OPENGLBLIT we have to
88 // set GL attributes first
89 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8);
90 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
91
92 if(SDL_SetVideoMode(800, 600, 16, SDL_OPENGL) < 0)
93 {
94 cerr << "SDL_SetVideoMode failed: " << SDL_GetError() << endl;
95 exit(1);
96 }
97}
98
99void glInit()
100{
101 glClearColor(0.0, 0.0, 0.0, 0.0);
102
103 glMatrixMode(GL_PROJECTION);
104 glLoadIdentity();
105
f7b3b2eb 106 glOrtho(0, 800.0, 600.0, 0.0, -0.01, 1.01);
ad9f1fb6
PG
107
108 glMatrixMode(GL_MODELVIEW);
109 glLoadIdentity();
110
111 glEnable(GL_DEPTH_TEST);
112}