Rube Goldberg Machine 1.0
This is the base code for Rube Goldberg designed for the CS296 Software Systems Lab
|
#include <callbacks.hpp>
Static Public Member Functions | |
static b2Vec2 | convert_screen_to_world (int32 x, int32 y) |
Helper function for coordinates system conversion Note the function is a static function. Read about C++ static functions. | |
static void | resize_cb (int32 w, int32 h) |
GLUT resize callback. This is set via GLUI. Why is this so? This gets called when the window is resized. Passed as parameters are the current window width and height. | |
static void | keyboard_cb (unsigned char key, int x, int y) |
GLUT keyboard callback This gets called whenever a key is pressed. | |
static void | keyboard_special_cb (int key, int x, int y) |
GLUT keyboard callback for keys with special keycodes. | |
static void | keyboard_up_cb (unsigned char key, int x, int y) |
Another keyboard callback? Why is this necessary? Is this used? | |
static void | mouse_cb (int32 button, int32 state, int32 x, int32 y) |
GLUT mouse callback This is called when a mouse button is pressed. | |
static void | mouse_motion_cb (int32 x, int32 y) |
GLUT Mouse motion callback This is called when the mouse is moved/dragged. | |
static void | timer_cb (int) |
GLUT timer callback. This is used to control the frame rate - figure out how by reading the GLUT manual. | |
static void | display_cb (void) |
GLUT display callback This draws every time a draw event is posted. It also xecutes the main simulation loop by calling test->step(...) | |
static void | restart_cb (int) |
GLUI callback - Called when the restart button is pressed. | |
static void | pause_cb (int) |
GLUI callback - Called when the pause button is pressed. | |
static void | single_step_cb (int) |
GLUI callback - Called when the single-step button is pressed. | |
static void | exit_cb (int code) |
GLUI callback - Called when the exit button is pressed. |
Definition at line 29 of file callbacks.hpp.
b2Vec2 cs296::callbacks_t::convert_screen_to_world | ( | int32 | x, |
int32 | y | ||
) | [static] |
Helper function for coordinates system conversion Note the function is a static function. Read about C++ static functions.
Definition at line 39 of file callbacks.cpp.
{ 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 cs296::callbacks_t::display_cb | ( | void | ) | [static] |
GLUT display callback This draws every time a draw event is posted. It also xecutes the main simulation loop by calling test->step(...)
Definition at line 246 of file callbacks.cpp.
{ 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 = cs296::sim; test = entry->create_fcn(); view_zoom = 1.0f; settings.view_center.Set(0.0f, 20.0f); resize_cb(width, height); } }
void cs296::callbacks_t::exit_cb | ( | int | code | ) | [static] |
GLUI callback - Called when the exit button is pressed.
Definition at line 295 of file callbacks.cpp.
{ exit(code); }
void cs296::callbacks_t::keyboard_cb | ( | unsigned char | key, |
int | x, | ||
int | y | ||
) | [static] |
GLUT keyboard callback This gets called whenever a key is pressed.
What are these?
Press 'z' to zoom out.
Press 'x' to zoom in.
Press 'r' to reset.
Press 'p' to pause.
The default case. Why is this needed?
Definition at line 85 of file callbacks.cpp.
{ B2_NOT_USED(x); B2_NOT_USED(y); switch (key) { case 27: exit(0); break; case 'z': view_zoom = b2Min(1.1f * view_zoom, 20.0f); resize_cb(width, height); break; case 'x': view_zoom = b2Max(0.9f * view_zoom, 0.02f); resize_cb(width, height); break; case 'r': delete test; test = entry->create_fcn(); break; case 'p': settings.pause = !settings.pause; break; default: if (test) { test->keyboard(key); } } }
void cs296::callbacks_t::keyboard_special_cb | ( | int | key, |
int | x, | ||
int | y | ||
) | [static] |
GLUT keyboard callback for keys with special keycodes.
Press left to pan left.
Press right to pan right.
Press down to pan down.
Press up to pan up.
Press home to reset the view.
Definition at line 130 of file callbacks.cpp.
{ B2_NOT_USED(x); B2_NOT_USED(y); switch (key) { case GLUT_ACTIVE_SHIFT: case GLUT_KEY_LEFT: settings.view_center.x -= 0.5f; resize_cb(width, height); break; case GLUT_KEY_RIGHT: settings.view_center.x += 0.5f; resize_cb(width, height); break; case GLUT_KEY_DOWN: settings.view_center.y -= 0.5f; resize_cb(width, height); break; case GLUT_KEY_UP: settings.view_center.y += 0.5f; resize_cb(width, height); break; case GLUT_KEY_HOME: view_zoom = 1.0f; settings.view_center.Set(0.0f, 20.0f); callbacks_t::resize_cb(width, height); break; } }
void cs296::callbacks_t::keyboard_up_cb | ( | unsigned char | key, |
int | x, | ||
int | y | ||
) | [static] |
Another keyboard callback? Why is this necessary? Is this used?
Definition at line 172 of file callbacks.cpp.
{ B2_NOT_USED(x); B2_NOT_USED(y); if (test) { test->keyboard_up(key); } }
void cs296::callbacks_t::mouse_cb | ( | int32 | button, |
int32 | state, | ||
int32 | x, | ||
int32 | y | ||
) | [static] |
GLUT mouse callback This is called when a mouse button is pressed.
Use the mouse to move things around - figure out how this works?
Definition at line 183 of file callbacks.cpp.
{ 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 cs296::callbacks_t::mouse_motion_cb | ( | int32 | x, |
int32 | y | ||
) | [static] |
GLUT Mouse motion callback This is called when the mouse is moved/dragged.
Definition at line 224 of file callbacks.cpp.
{ 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 cs296::callbacks_t::pause_cb | ( | int | ) | [static] |
void cs296::callbacks_t::resize_cb | ( | int32 | w, |
int32 | h | ||
) | [static] |
GLUT resize callback. This is set via GLUI. Why is this so? This gets called when the window is resized. Passed as parameters are the current window width and height.
Notice the type casting Read about explicit/implicit type casting in C++
L/R/B/T extents of the view frustum Find where this function is defined
Definition at line 58 of file callbacks.cpp.
{ width = w; height = h; GLUI_Master.get_viewport_area(&tx, &ty, &tw, &th); glViewport(tx, ty, tw, th); glMatrixMode(GL_PROJECTION); glLoadIdentity(); 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; gluOrtho2D(lower.x, upper.x, lower.y, upper.y); }
void cs296::callbacks_t::restart_cb | ( | int | ) | [static] |
GLUI callback - Called when the restart button is pressed.
Definition at line 282 of file callbacks.cpp.
{ delete test; entry = cs296::sim; test = entry->create_fcn(); resize_cb(width, height); }
void cs296::callbacks_t::single_step_cb | ( | int | ) | [static] |
GLUI callback - Called when the single-step button is pressed.
Definition at line 300 of file callbacks.cpp.
{ settings.pause = 1; settings.single_step = 1; }
void cs296::callbacks_t::timer_cb | ( | int | ) | [static] |
GLUT timer callback. This is used to control the frame rate - figure out how by reading the GLUT manual.
Definition at line 239 of file callbacks.cpp.
{ glutSetWindow(main_window); glutPostRedisplay(); glutTimerFunc(frame_period, timer_cb, 0); }