#else
#include <GL/glut.h>
#endif
-#include "gear.h"
+#include "app.h"
-bool init();
-void cleanup();
-void display();
-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 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 float cam_dist = 2;
-static float cam_theta, cam_phi;
-static int prev_mx, prev_my;
-static bool bnstate[8];
-
-static Gear *test_gear;
+static unsigned int start_time;
+static unsigned int modkeys;
int main(int argc, char **argv)
{
- glutInit(&argc, argv);
glutInitWindowSize(1024, 768);
- glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
- glutCreateWindow("Antikythera");
+ glutInit(&argc, argv);
+ glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
+ 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_DEPTH_TEST);
- glEnable(GL_CULL_FACE);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_NORMALIZE);
-
- Mesh::use_custom_sdr_attr = false;
-
- test_gear = new Gear;
- test_gear->gen_mesh();
-
- return true;
+ glutSwapBuffers();
}
-void cleanup()
+void app_quit()
{
- delete test_gear;
+ exit(0);
}
-void display()
+unsigned int app_get_modifiers()
{
- 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 draw_gears()
+static void display()
{
- /* world scale is in meters, gears are in millimeters, sclae by 1/1000 */
- glPushMatrix();
- glScalef(0.001, 0.001, 0.001);
-
- test_gear->draw();
+ time_msec = glutGet(GLUT_ELAPSED_TIME) - start_time;
+ app_display();
+}
- glPopMatrix();
+static void idle()
+{
+ 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.05;
- if(cam_dist < 0.0) cam_dist = 0.0;
- glutPostRedisplay();
+ if(glutmod & GLUT_ACTIVE_ALT) {
+ modkeys |= MOD_ALT;
}
}