-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
188 lines (156 loc) · 5.32 KB
/
game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#ifndef TEST_CPP
#define TEST_CPP
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "Defs.hpp"
#include "Ship.hpp"
#include "Rock.hpp"
#include "PhysicsWorld.hpp"
/////////////////////////////////////////////////////////
// Main stuff
static double currentTime (ClockGetTime());
static GLfloat scale (0.5);
static PhysicsWorld universe(300.0, 300.0);
static Ship triangle1(&universe, 150, 150, 0.0, 0.0, 0.0);
static Rock sphere1(&universe, 150, 50, 0.0, 10, 0.0, 20);
static Rock sphere2(&universe, 150, 150, 90.0, 20, 0.0, 10);
static Rock sphere3(&universe, 150, 150, -68.0, 30, 0.0, 15);
static Rock sphere4(&universe, 250, 250, 200.0, 35, 0.0, 5);
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
exit(0);
}
}
void initRendering() {
glClearColor (0.1, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glEnable(GL_POINT_SMOOTH); // make sure the drawn points are round!
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_BLEND);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
}
void handleResize(int w, int h) {
// choose what part of the window represents the OpenGL viewport (here the entire screen)
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
// Set the matrix for viewing (Projection matrix)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// use parallel projection matrix, the parameters denote the clipping box that is used around the scene.
// gluOrtho2D(-scale*(GLfloat)w/2.0, scale*(GLfloat)w/2.0, -scale*(GLfloat)h/2.0, scale*(GLfloat)h/2.0);
gluOrtho2D(0, scale*(GLfloat)w, 0, scale*(GLfloat)h);
// Change the size of the universe to match the new window size
universe.set_size(scale*(GLfloat)w, scale*(GLfloat)h);
// set the model transformation matrix (default)
glMatrixMode(GL_MODELVIEW);
}
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT);
// assume that the current matrix is the MODELVIEW matrix
glLoadIdentity();
glBlendFunc(GL_SRC_ALPHA_SATURATE, GL_ONE);
triangle1.draw();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
sphere1.draw();
sphere2.draw();
sphere3.draw();
sphere4.draw();
glutSwapBuffers(); // OpenGL commands issued after the swap will only be rendered after the swap took place
glFlush(); // Force the excecution of the commands (not sure this is needed when using SwapBuffers)
}
void main_loop(int i)
{
// sleep(1);
double newTime (ClockGetTime());
double dt (newTime-currentTime); // 'dt' is in seconds
currentTime = newTime;
// get the new state by integrating over time the old state
// For now the only state variable is the orientation of the triangle
triangle1.propagate(dt);
sphere1.propagate(dt);
sphere2.propagate(dt);
sphere3.propagate(dt);
sphere4.propagate(dt);
glutPostRedisplay();
glutTimerFunc(25, main_loop, 0);
}
void mouse(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
{
triangle1.input(Left); // The pressing of the buttons just modifies the state of the force
// std::cout << "left" << std::endl;
}
else
{
triangle1.input(Neutral);
// std::cout << "Neutral" << std::endl;
}
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
{
triangle1.input(Right);
// std::cout << "Right" << std::endl;
}
else
{
triangle1.input(Neutral);
// std::cout << "Neutral" << std::endl;
}
break;
case GLUT_MIDDLE_BUTTON:
if (state == GLUT_DOWN)
{
triangle1.input(Forward);
// std::cout << "Forward" << std::endl;
}
else
{
triangle1.input(Neutral);
// std::cout << "Neutral" << std::endl;
}
break;
default:
break;
}
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_ALPHA);
glutInitWindowSize(600, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Asteroids");
initRendering();
// get some OpenGL info (Note that we can only do it after initialization or smt)
GLfloat values[2];
glGetFloatv (GL_POINT_SIZE_RANGE, values);
std::cout << "GL_POINT_SIZE_RANGE: " << values[0] << ", " << values[1] << std::endl;
glGetFloatv (GL_LINE_WIDTH_RANGE, values);
std::cout << "GL_LINE_WIDTH_RANGE: " << values[0] << ", " << values[1] << std::endl;
glGetFloatv (GL_LINE_WIDTH_GRANULARITY, values);
std::cout << "GL_LINE_WIDTH_GRANULARITY: " << values[0] << std::endl;
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMouseFunc(mouse);
// glutIdleFunc(main_loop);
glutTimerFunc(25, main_loop, 0); // start the update sequence
// start handling events and such
glutMainLoop();
return 0;
}
#endif /* Test.cpp */