X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fmain.cc;h=f9c9f3a0d88dd0e87aab3f946af2403dd7f5a523;hb=dbcb9345c23c5c027d808915962843e7db2d14aa;hp=b5d430b80f996e80d9cdcb15618a6e13684a3bff;hpb=1716bebf166a5424e4a683897a78883b7733a636;p=laserbrain_demo diff --git a/src/main.cc b/src/main.cc index b5d430b..f9c9f3a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -7,179 +7,124 @@ #else #include #endif -#include "machine.h" - -bool init(); -void cleanup(); -void display(); -void idle(); -void draw_gears(); -void reshape(int x, int y); -void keyb(unsigned char key, int x, int y); -void mouse(int bn, int st, int x, int y); -void motion(int x, int y); - -static float cam_dist = 2; -static float cam_theta, cam_phi; -static int prev_mx, prev_my; -static bool bnstate[8]; - -static unsigned int start_time, prev_msec; -static Machine *machine; +#include "app.h" + +static bool init(); +static void display(); +static void idle(); +static void reshape(int x, int y); +static void key_press(unsigned char key, int x, int y); +static void key_release(unsigned char key, int x, int y); +static void mouse(int bn, int st, int x, int y); +static void proc_modkeys(); + +static unsigned int start_time; +static unsigned int modkeys; int main(int argc, char **argv) { glutInitWindowSize(1024, 768); glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE); - glutCreateWindow("Antikythera"); + glutCreateWindow("demo"); glutDisplayFunc(display); glutIdleFunc(idle); glutReshapeFunc(reshape); - glutKeyboardFunc(keyb); + glutKeyboardFunc(key_press); + glutKeyboardUpFunc(key_release); glutMouseFunc(mouse); - glutMotionFunc(motion); + glutMotionFunc(app_mouse_motion); + glutPassiveMotionFunc(app_mouse_motion); if(!init()) { return 1; } - atexit(cleanup); + atexit(app_cleanup); glutMainLoop(); return 0; } -bool init() +void app_swap_buffers() { - glewInit(); - - glEnable(GL_MULTISAMPLE); - glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - glEnable(GL_NORMALIZE); - - Mesh::use_custom_sdr_attr = false; - - machine = new Machine; - - const float pitch = 10.0f; - - Gear *gear1 = new Gear; - gear1->pos = Vec3(-50, 0, 0); - gear1->set_teeth(16, pitch); - gear1->gen_mesh(); - machine->add_gear(gear1); - - Gear *gear2 = new Gear; - gear2->set_teeth(32, pitch); - gear2->pos = gear1->pos + Vec3(gear1->radius + gear2->radius - gear1->teeth_length * 0.75, 0, 0); - gear2->thickness = 5; - gear2->gen_mesh(); - machine->add_gear(gear2); - - Gear *gear3 = new Gear; - gear3->set_teeth(8, pitch); - gear3->pos = gear2->pos + Vec3(0, gear2->radius + gear3->radius - gear2->teeth_length * 0.75, 0); - gear3->gen_mesh(); - machine->add_gear(gear3); - - machine->add_motor(0, 1.0); - machine->calc_meshing(); - - start_time = glutGet(GLUT_ELAPSED_TIME); - return true; + glutSwapBuffers(); } -void cleanup() +void app_quit() { - delete machine; + exit(0); } -void display() +unsigned int app_get_modifiers() { - unsigned int msec = glutGet(GLUT_ELAPSED_TIME) - start_time; - float dt = (float)(msec - prev_msec) / 1000.0f; - prev_msec = msec; - - machine->update(dt); - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + return modkeys; +} - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glTranslatef(0, 0, -cam_dist); - glRotatef(cam_phi, 1, 0, 0); - glRotatef(cam_theta, 0, 1, 0); +static bool init() +{ + glewInit(); - draw_gears(); + if(!app_init()) { + return false; + } - glutSwapBuffers(); - assert(glGetError() == GL_NO_ERROR); + start_time = glutGet(GLUT_ELAPSED_TIME); + return true; } -void idle() +static void display() { - glutPostRedisplay(); + time_msec = glutGet(GLUT_ELAPSED_TIME) - start_time; + app_display(); } -void draw_gears() +static void idle() { - /* world scale is in meters, gears are in millimeters, sclae by 1/1000 */ - glPushMatrix(); - glScalef(0.001, 0.001, 0.001); - - machine->draw(); - - glPopMatrix(); + glutPostRedisplay(); } -void reshape(int x, int y) +static void reshape(int x, int y) { - glViewport(0, 0, x, y); + win_width = x; + win_height = y; - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective(50.0, (float)x / (float)y, 0.05, 100.0); + app_reshape(x, y); } -void keyb(unsigned char key, int x, int y) +static void key_press(unsigned char key, int x, int y) { - switch(key) { - case 27: - exit(0); - } + proc_modkeys(); + app_keyboard(key, true); } -void mouse(int bn, int st, int x, int y) +static void key_release(unsigned char key, int x, int y) { - prev_mx = x; - prev_my = y; - bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN; + proc_modkeys(); + app_keyboard(key, false); } -void motion(int x, int y) +static void mouse(int bn, int st, int x, int y) { - int dx = x - prev_mx; - int dy = y - prev_my; - prev_mx = x; - prev_my = y; + int bidx = bn - GLUT_LEFT_BUTTON; + bool down = st == GLUT_DOWN; - if(!dx && !dy) return; + proc_modkeys(); + app_mouse_button(bidx, down, x, y); +} - if(bnstate[0]) { - cam_theta += dx * 0.5; - cam_phi += dy * 0.5; +static void proc_modkeys() +{ + int glutmod = glutGetModifiers(); - if(cam_phi < -90) cam_phi = -90; - if(cam_phi > 90) cam_phi = 90; - glutPostRedisplay(); + modkeys = 0; + if(glutmod & GLUT_ACTIVE_SHIFT) { + modkeys |= MOD_SHIFT; + } + if(glutmod & GLUT_ACTIVE_CTRL) { + modkeys |= MOD_CTRL; } - if(bnstate[2]) { - cam_dist += dy * 0.01; - if(cam_dist < 0.0) cam_dist = 0.0; - glutPostRedisplay(); + if(glutmod & GLUT_ACTIVE_ALT) { + modkeys |= MOD_ALT; } }