made a ball move
[physics.git] / src / graphics / graphics.cpp
CommitLineData
ad9f1fb6
PG
1#include "graphics.h"
2
3#include <GL/gl.h>
4#include <GL/glu.h>
5#include <SDL/SDL.h>
6#include <cmath>
7
44b079f8 8#include "../debug.h"
ad9f1fb6
PG
9
10static const float PI = 3.1415926535897;
11
12/// ***** Private Method Headers *****
f7b3b2eb 13void drawCircle(int);
ad9f1fb6
PG
14void sdlInit();
15void glInit();
16
17/// ***** Public Methods *****
18
19void graphicsInit()
20{
21 sdlInit();
22 glInit();
23}
24
25void graphicsCleanUp()
26{
27
28}
29
89ca62b2 30void glDrawCircle(float radius, const Vector2& vec, const float* color)
ad9f1fb6 31{
f7b3b2eb
PG
32 glMatrixMode(GL_MODELVIEW);
33 glLoadIdentity();
89ca62b2 34 glTranslatef(vec.x, vec.y, -1);
f7b3b2eb
PG
35 glScalef(radius, radius, radius);
36
37 if(color != NULL)
38 glColor3fv(color);
39
40 drawCircle(32);
41}
42
43/// ***** Private Methods *****
ad9f1fb6 44
f7b3b2eb
PG
45void drawCircle(int pieces)
46{
ad9f1fb6 47 glBegin(GL_POLYGON);
f7b3b2eb 48 for(int n = 0; n < pieces; n++)
ad9f1fb6 49 {
f7b3b2eb
PG
50 float angle = 2 * PI * n / pieces;
51 float ix = cos(angle);
52 float iy = sin(angle);
ad9f1fb6 53
f7b3b2eb 54 glVertex3f(ix, iy, 0);
ad9f1fb6
PG
55 }
56 glEnd();
57}
58
ad9f1fb6
PG
59void sdlInit()
60{
61 if(SDL_Init(SDL_INIT_VIDEO) < 0)
62 {
63 cerr << "SDL_Init failed: " << SDL_GetError() << endl;
64 exit(1);
65 }
66
67 // In order to use SDL_OPENGLBLIT we have to
68 // set GL attributes first
69 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8);
70 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
71
72 if(SDL_SetVideoMode(800, 600, 16, SDL_OPENGL) < 0)
73 {
74 cerr << "SDL_SetVideoMode failed: " << SDL_GetError() << endl;
75 exit(1);
76 }
77}
78
79void glInit()
80{
81 glClearColor(0.0, 0.0, 0.0, 0.0);
82
83 glMatrixMode(GL_PROJECTION);
84 glLoadIdentity();
85
f7b3b2eb 86 glOrtho(0, 800.0, 600.0, 0.0, -0.01, 1.01);
ad9f1fb6
PG
87
88 glMatrixMode(GL_MODELVIEW);
89 glLoadIdentity();
90
91 glEnable(GL_DEPTH_TEST);
92}