-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
242 lines (207 loc) · 5.98 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "Game.h"
#include<chrono>
#include<thread>
using namespace std;
double gravity = 0;
int Game::frameCount, Game::frameRate;
void MySleep( int milisecond )
{
std::this_thread::sleep_for(std::chrono::milliseconds(milisecond));
}
void Game::FrameRateThreadFun(void *arg) {
frameCount = 0;
while (1) {
MySleep(1000);
settings.frameRate = frameCount;
frameCount = 0;
}
}
bool Game::MouseButtonEvent(int button, int action) {
if (designer.MouseButtonEvent(button, action)) return true;
if (bodyLinker.MouseButtonEvent(button, action)) return true;
if (button == GLFW_MOUSE_BUTTON_1 && action == GLFW_PRESS) {
Object *object = ShapeFactory::Generate();
object->SetPosition(input.mouse.position);
physics.AddObject(object);
return true;
}
return false;
}
bool Game::KeyEvent(int key, int action) {
if (designer.KeyEvent(key, action)) return true;
if (bodyLinker.KeyEvent(key, action)) return true;
if (ShapeFactory::KeyEvent(key, action)) return true;
if (action == GLFW_RELEASE) return false;
if (key == GLFW_KEY_PAUSE) return settings.pause ^= 1, true;
if (key == GLFW_KEY_F1) return Test1(), true;
if (key == GLFW_KEY_F2) return Test2(), true;
if (key == GLFW_KEY_F3) return Test3(), true;
if (key == GLFW_KEY_F4) return Test4(), true;
if (key == GLFW_KEY_F5) return Test5(), true;
if (key == GLFW_KEY_F6) return Test6(), true;
if (key == GLFW_KEY_F7) return Test7(), true;
return false;
}
bool Game::MouseWheelEvent(int delta)
{
return scoper.MouseWheelEvent(delta);
}
void WindowSizeCallback( GLFWwindow * window, int width, int height );
void processInput( GLFWwindow* window );
void processInput( GLFWwindow* window )
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
void WindowSizeCallback( GLFWwindow * window, int width, int height )
{
glViewport(0, 0, width, height);// Draw into entire window
}
void Game::Initialize()
{
srand((unsigned int)time(0));
glfwInit();
window = glfwCreateWindow( screenWidth, screenHeight, "Physics Engine", NULL, NULL );
if ( window == NULL )
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return;
}
glfwMakeContextCurrent( window );
glfwSetFramebufferSizeCallback( window, WindowSizeCallback );
if ( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) )
{
std::cout << "Failed to initialize GLAD" << std::endl;
return;
}
TwInit( TW_OPENGL, NULL );
TwWindowSize( screenWidth, screenHeight );
glClearColor(0.0f , 0.0f , 0.0f , 0.0f );
glColor3f(1.0f , 1.0f , 1.0f );
glShadeModel(GL_FLAT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, screenWidth, 0, screenHeight, -1.0f, 1.0f);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); // Make round points, not square points
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); // Make round points, not square points
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); // Antialias the lines
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glfwSetWindowSizeCallback(window, WindowSizeCB);
glfwSetCharCallback( window, (GLFWcharfun)TwEventCharGLFW );
glfwSetCursorPosCallback( window, MousePosCallback );
glfwSetMouseButtonCallback( window, MouseButtonCallback );
glfwSetKeyCallback( window, KeyEventCallback );
glfwSetScrollCallback( window, MouseWheelCallback );
frameRate = 0;
frameCount = 0;
ShapeFactory::Init();
settings.Init();
designer.SetGame( this );
bodyLinker.SetGame( this );
scoper.SetGame( this );
}
void Game::PrepareRendering( Object *& target, Vector2D & shapeR )
{
input.Update();
{
if ( input.keyboard.NeedProcess((int)'X'))
{
if ( physics.objects.size() > 1 )
while (physics.objects.size() > 1) physics.DeleteLastObject();
else if (physics.objects.size() == 1)
physics.DeleteLastObject();
}
}
if ( input.mouse.NeedProcess(GLFW_MOUSE_BUTTON_2) )
{
physics.SelectObject(input.mouse.position, target, shapeR);
}
if ( target != NULL )
{
if ( input.mouse.IsPressed(GLFW_MOUSE_BUTTON_2) )
{
physics.MouseAdjust(target, shapeR, input.mouse.position, &scoper);
if (input.keyboard.NeedProcess('S'))
{
target->SetFixed( !target->GetFixed() );
}
}
else
{
target = NULL;
}
}
if ( target != NULL )
{
if ( input.keyboard.NeedProcess(GLFW_KEY_DELETE) && target != physics.GetWorldBox() )
{
physics.DeleteObject(target);
target = NULL;
}
}
if ( ! settings.pause )
{
physics.Proceed( timeInterval );
}
else
{
MySleep( 20 );
}
}
void Game::Rendering( Object * target, Vector2D & shapeR )
{
graphics.BeginRendering();
physics.Redraw();
if ( target != NULL && input.mouse.IsPressed(GLFW_MOUSE_BUTTON_2) )
{
Vector2D p = target->GetTransformToWorld()(shapeR + Vector2D::Origin);
graphics.DrawLine(p.x, p.y, input.mouse.position.x, input.mouse.position.y, HSB3f(0.0f, 0.8f, 0.8f), 5.0);
}
designer.Redraw();
bodyLinker.Redraw();
graphics.EndRendering();
}
void Game::Run()
{
Initialize();
Test1();
Object *target = NULL;
Vector2D shapeR;
while ( !glfwWindowShouldClose( window ) )
{
this->PrepareRendering( target, shapeR );
this->Rendering( target, shapeR );
}
glfwMakeContextCurrent( window );
return;
}
Game game;
void KeyEventCallback( GLFWwindow* window, int key, int action, int my_none1, int my_none2 )
{
if (TwEventKeyGLFW(key, action)) return;
if (game.KeyEvent(key, action)) return;
}
void MouseButtonCallback( GLFWwindow* window, int botton, int action, int my_none )
{
if (TwEventMouseButtonGLFW(botton, action)) return;
if (game.MouseButtonEvent(botton, action)) return;
}
void MousePosCallback( GLFWwindow* window, double x, double y )
{
TwEventMousePosGLFW(x, y);
}
void MouseWheelCallback( GLFWwindow* window, double pos, double my_none )
{
static int lastPos;
int delta = pos - lastPos;
lastPos = pos;
TwEventMouseWheelGLFW(pos);
game.MouseWheelEvent(delta);
}