-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.cpp
347 lines (283 loc) · 7.17 KB
/
callbacks.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Base code for CS 251 Software Systems Lab
* Department of Computer Science and Engineering, IIT Bombay
*
*/
//! These are user defined include files
//! Included in double quotes - the path to find these has to be given at compile time
#include "callbacks.hpp"
#ifndef __APPLE__
#include "GL/glui.h"
#else
#include "GL/glui.h"
#endif
//! The namespace protects the global variables and other names from
//! clashes in scope. Read about the use of named and unnamed
//! namespaces in C++ Figure out where all the datatypes used below
//! are defined
namespace cs251
{
int32 test_index = 0;
int32 test_selection = 0;
int32 test_count = 0;
cs251::sim_t* entry;
cs251::base_sim_t* test;
cs251::settings_t settings;
int32 width = 640;
int32 height = 480;
int32 frame_period = 16;
int32 main_window;
float settings_hz = 60.0;
float32 view_zoom = 1.0f;
int tx, ty, tw, th;
bool r_mouse_down;
b2Vec2 lastp;
b2Vec2 callbacks_t::convert_screen_to_world(int32 x, int32 y)
{
float32 u = x / static_cast<float32>(tw);
float32 v = (th - y) / float32(th);
float32 ratio = static_cast<float32>(tw) / static_cast<float32>(th);
b2Vec2 extents(ratio * 25.0f, 25.0f);
extents *= view_zoom;
b2Vec2 lower = settings.view_center - extents;
b2Vec2 upper = settings.view_center + extents;
b2Vec2 p;
p.x = (1.0f - u) * lower.x + u * upper.x;
p.y = (1.0f - v) * lower.y + v * upper.y;
return p;
}
void callbacks_t::resize_cb(int32 w, int32 h)
{
width = w;
height = h;
GLUI_Master.get_viewport_area(&tx, &ty, &tw, &th);
glViewport(tx, ty, tw, th);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//! Notice the type casting
//! Read about explicit/implicit type casting in C++
float32 ratio = static_cast<float32>(tw) / static_cast<float32>(th);
b2Vec2 extents(ratio * 25.0f, 25.0f);
extents *= view_zoom;
b2Vec2 lower = settings.view_center - extents;
b2Vec2 upper = settings.view_center + extents;
//! L/R/B/T extents of the view frustum
//! Find where this function is defined
gluOrtho2D(lower.x, upper.x, lower.y, upper.y);
}
void callbacks_t::keyboard_cb(unsigned char key, int x, int y)
{
//! What are these?
B2_NOT_USED(x);
B2_NOT_USED(y);
switch (key)
{
case 27:
exit(0);
break;
//! Press 'z' to zoom out.
case 'z':
view_zoom = b2Min(1.1f * view_zoom, 20.0f);
resize_cb(width, height);
break;
//! Press 'x' to zoom in.
case 'x':
//(sim->create_fcn())->launch();
view_zoom = b2Max(0.9f * view_zoom, 0.02f);
resize_cb(width, height);
break;
//! Press 'r' to reset.
case 'r':
delete test;
test = entry->create_fcn();
break;
//! Press 'p' to pause.
case 'p':
settings.pause = !settings.pause;
break;
case 'w':
(test)->launch();
break;
/*case 's':
(test)->delaunch();
break;*/
case '8':
(test)->launch2();
break;
/*case '2':
(test)->delaunch2();
break;*/
/*case 'n':
(test)->ball();
break;*/
/*case 'm':
(test)->createball();
break;*/
case 'a':
(test)->flipperleft();
break;
case 'd':
(test)->flipperright();
//(test)->flipperwheelright();
break;
case '4':
(test)->flipperwheelleft();
break;
case '6':
(test)->flipperwheelright();
break;
//! The default case. Why is this needed?
default:
if (test)
{
test->keyboard(key);
}
}
}
void callbacks_t::keyboard_special_cb(int key, int x, int y)
{
B2_NOT_USED(x);
B2_NOT_USED(y);
switch (key)
{
case GLUT_ACTIVE_SHIFT:
//! Press left to pan left.
case GLUT_KEY_LEFT:
settings.view_center.x -= 0.5f;
resize_cb(width, height);
break;
//! Press right to pan right.
case GLUT_KEY_RIGHT:
settings.view_center.x += 0.5f;
resize_cb(width, height);
break;
//! Press down to pan down.
case GLUT_KEY_DOWN:
settings.view_center.y -= 0.5f;
resize_cb(width, height);
break;
//! Press up to pan up.
case GLUT_KEY_UP:
settings.view_center.y += 0.5f;
resize_cb(width, height);
break;
//! Press home to reset the view.
case GLUT_KEY_HOME:
view_zoom = 1.0f;
settings.view_center.Set(0.0f, 20.0f);
callbacks_t::resize_cb(width, height);
break;
}
}
void callbacks_t::keyboard_up_cb(unsigned char key, int x, int y)
{
B2_NOT_USED(x);
B2_NOT_USED(y);
if (test)
{
test->keyboard_up(key);
}
}
void callbacks_t::mouse_cb(int32 button, int32 state, int32 x, int32 y)
{
//! Use the mouse to move things around - figure out how this works?
if (button == GLUT_LEFT_BUTTON)
{
int mod = glutGetModifiers();
b2Vec2 p = convert_screen_to_world(x, y);
if (state == GLUT_DOWN)
{
b2Vec2 p = convert_screen_to_world(x, y);
if (mod == GLUT_ACTIVE_SHIFT)
{
test->shift_mouse_down(p);
}
else
{
test->mouse_down(p);
}
}
if (state == GLUT_UP)
{
test->mouse_up(p);
}
}
else if (button == GLUT_RIGHT_BUTTON)
{
if (state == GLUT_DOWN)
{
lastp = convert_screen_to_world(x, y);
r_mouse_down = true;
}
if (state == GLUT_UP)
{
r_mouse_down = false;
}
}
}
void callbacks_t::mouse_motion_cb(int32 x, int32 y)
{
b2Vec2 p = convert_screen_to_world(x, y);
test->mouse_move(p);
if (r_mouse_down)
{
b2Vec2 diff = p - lastp;
settings.view_center.x -= diff.x;
settings.view_center.y -= diff.y;
resize_cb(width, height);
lastp = convert_screen_to_world(x, y);
}
}
void callbacks_t::timer_cb(int)
{
glutSetWindow(main_window);
glutPostRedisplay();
glutTimerFunc(frame_period, timer_cb, 0);
}
void callbacks_t::display_cb(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
test->set_text_line(30);
b2Vec2 old_center = settings.view_center;
settings.hz = settings_hz;
test->step(&settings);
if (old_center.x != settings.view_center.x || old_center.y != settings.view_center.y)
{
resize_cb(width, height);
}
test->draw_title(5, 15, entry->name);
glutSwapBuffers();
if (test_selection != test_index)
{
test_index = test_selection;
delete test;
entry = cs251::sim;
test = entry->create_fcn();
view_zoom = 1.0f;
settings.view_center.Set(0.0f, 20.0f);
resize_cb(width, height);
}
}
void callbacks_t::restart_cb(int)
{
delete test;
entry = cs251::sim;
test = entry->create_fcn();
resize_cb(width, height);
}
void callbacks_t::pause_cb(int)
{
settings.pause = !settings.pause;
}
void callbacks_t::exit_cb(int code)
{
exit(code);
}
void callbacks_t::single_step_cb(int)
{
settings.pause = 1;
settings.single_step = 1;
}
};