- only enable sRGB if we got an sRGB framebuffer.
[laserbrain_demo] / src / main.cc
index 0447748..d24428c 100644 (file)
 #include <stdlib.h>
 #include <assert.h>
 #include <GL/glew.h>
-#ifdef __APPLE__
-#include <GLUT/glut.h>
-#else
-#include <GL/glut.h>
-#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 <SDL2/SDL.h>
+#include "app.h"
 
-int main(int argc, char **argv)
-{
-       glutInitWindowSize(1024, 768);
-       glutInit(&argc, argv);
-       glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
-       glutCreateWindow("Antikythera");
-
-       glutDisplayFunc(display);
-       glutIdleFunc(idle);
-       glutReshapeFunc(reshape);
-       glutKeyboardFunc(keyb);
-       glutMouseFunc(mouse);
-       glutMotionFunc(motion);
-
-       if(!init()) {
-               return 1;
-       }
-       atexit(cleanup);
+static bool init(int argc, char **argv);
+static void process_event(SDL_Event *ev);
+static void proc_modkeys();
 
-       glutMainLoop();
-       return 0;
-}
+static SDL_Window *win;
+static SDL_GLContext ctx;
+static bool fullscreen, mouse_grabbed;
+static bool quit;
 
-bool init()
-{
-       glewInit();
+static unsigned int start_time;
+static unsigned int modkeys;
 
-       glEnable(GL_DEPTH_TEST);
-       glEnable(GL_CULL_FACE);
-       glEnable(GL_LIGHTING);
-       glEnable(GL_LIGHT0);
-       glEnable(GL_NORMALIZE);
+SDL_GameController *gamepad;
 
-       Mesh::use_custom_sdr_attr = false;
+static int scale_factor = 1;
 
-       machine = new Machine;
+int main(int argc, char **argv)
+{
+       if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) {
+               fprintf(stderr, "failed to initialize SDL\n");
+               return 1;
+       }
 
-       const float pitch = 10.0f;
+       SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
+       SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
+       SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
+
+       int defpos = SDL_WINDOWPOS_UNDEFINED;
+       unsigned int sdlflags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
+
+       if(!(win = SDL_CreateWindow("demo", defpos, defpos, 1024, 768, sdlflags))) {
+               // try again without sRGB capability
+               SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 0);
+               if(!(win = SDL_CreateWindow("demo", defpos, defpos, 1024, 768, sdlflags))) {
+                       fprintf(stderr, "failed to create window\n");
+                       SDL_Quit();
+                       return 1;
+               }
+               fprintf(stderr, "failed to get an sRGB framebuffer.\n");
+       }
+       int val;
+       SDL_GL_GetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, &val);
+       printf("SDL says we %s an sRGB framebuffer\n", val ? "got" : "didn't get");
+       fb_srgb = val;
+
+       if(!(ctx = SDL_GL_CreateContext(win))) {
+               fprintf(stderr, "failed to create OpenGL context\n");
+               SDL_Quit();
+               return 1;
+       }
+       SDL_GL_GetDrawableSize(win, &win_width, &win_height);
+       win_aspect = (float)win_width / (float)win_height;
+
+       printf("detected %d joysticks\n", SDL_NumJoysticks());
+       for(int i=0; i<SDL_NumJoysticks(); i++) {
+               if(SDL_IsGameController(i)) {
+                       if(!(gamepad = SDL_GameControllerOpen(i))) {
+                               fprintf(stderr, "failed to open game controller %i: %s\n", i, SDL_GetError());
+                               continue;
+                       }
+                       printf("Using gamepad: %s\n", SDL_GameControllerNameForIndex(i));
+               }
+       }
 
-       Gear *gear1 = new Gear;
-       gear1->pos = Vec3(-50, 0, 0);
-       gear1->set_teeth(16, pitch);
-       gear1->gen_mesh();
-       machine->add_gear(gear1);
+       if(!init(argc, argv)) {
+               SDL_Quit();
+               return 1;
+       }
+       app_reshape(win_width, win_height);
 
-       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);
+       while(!quit) {
+               SDL_Event ev;
 
-       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);
+               time_msec = SDL_GetTicks() - start_time;
+               while(SDL_PollEvent(&ev)) {
+                       process_event(&ev);
+                       if(quit) goto break_evloop;
+               }
 
-       machine->add_motor(0, 1.0);
-       machine->calc_meshing();
+               app_display();
+       }
+break_evloop:
 
-       start_time = glutGet(GLUT_ELAPSED_TIME);
-       return true;
+       app_cleanup();
+       SDL_Quit();
+       return 0;
 }
 
-void cleanup()
+void app_swap_buffers()
 {
-       delete machine;
+       SDL_GL_SwapWindow(win);
 }
 
-void display()
+void app_quit()
 {
-       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);
-
-       glMatrixMode(GL_MODELVIEW);
-       glLoadIdentity();
-       glTranslatef(0, 0, -cam_dist);
-       glRotatef(cam_phi, 1, 0, 0);
-       glRotatef(cam_theta, 0, 1, 0);
-
-       draw_gears();
-
-       glutSwapBuffers();
-       assert(glGetError() == GL_NO_ERROR);
+       quit = true;
 }
 
-void idle()
+unsigned int app_get_modifiers()
 {
-       glutPostRedisplay();
+       return modkeys;
 }
 
-void draw_gears()
+void app_resize(int x, int y)
 {
-       /* world scale is in meters, gears are in millimeters, sclae by 1/1000 */
-       glPushMatrix();
-       glScalef(0.001, 0.001, 0.001);
-
-       machine->draw();
+       SDL_SetWindowSize(win, x, y);
+}
 
-       glPopMatrix();
+void app_fullscreen(bool fs)
+{
+       SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
+       fullscreen = fs;
 }
 
-void reshape(int x, int y)
+void app_toggle_fullscreen()
 {
-       glViewport(0, 0, x, y);
+       app_fullscreen(!fullscreen);
+}
 
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
-       gluPerspective(50.0, (float)x / (float)y, 0.05, 100.0);
+bool app_is_fullscreen()
+{
+       return fullscreen;
 }
 
-void keyb(unsigned char key, int x, int y)
+void app_grab_mouse(bool grab)
 {
-       switch(key) {
-       case 27:
-               exit(0);
+       if(grab) {
+               SDL_WarpMouseInWindow(win, win_width / 2, win_height / 2);
        }
+       //SDL_SetWindowGrab(win, grab ? SDL_TRUE : SDL_FALSE);
+       //SDL_ShowCursor(grab ? 1 : 0);
+       SDL_SetRelativeMouseMode(grab ? SDL_TRUE : SDL_FALSE);
+       mouse_grabbed = grab;
+}
+
+void app_toggle_grab_mouse()
+{
+       app_grab_mouse(!mouse_grabbed);
 }
 
-void mouse(int bn, int st, int x, int y)
+bool app_is_mouse_grabbed()
 {
-       prev_mx = x;
-       prev_my = y;
-       bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
+       return mouse_grabbed;
 }
 
-void motion(int x, int y)
+
+static bool init(int argc, char **argv)
 {
-       int dx = x - prev_mx;
-       int dy = y - prev_my;
-       prev_mx = x;
-       prev_my = y;
+       glewInit();
 
-       if(!dx && !dy) return;
+       if(!app_init(argc, argv)) {
+               return false;
+       }
 
-       if(bnstate[0]) {
-               cam_theta += dx * 0.5;
-               cam_phi += dy * 0.5;
+       start_time = SDL_GetTicks();
+       return true;
+}
 
-               if(cam_phi < -90) cam_phi = -90;
-               if(cam_phi > 90) cam_phi = 90;
-               glutPostRedisplay();
+static void process_event(SDL_Event *ev)
+{
+       switch(ev->type) {
+       case SDL_QUIT:
+               quit = true;
+               break;
+
+       case SDL_KEYDOWN:
+       case SDL_KEYUP:
+               proc_modkeys();
+               app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
+               break;
+
+       case SDL_MOUSEBUTTONDOWN:
+       case SDL_MOUSEBUTTONUP:
+               proc_modkeys();
+               app_mouse_button(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
+                               ev->button.x * scale_factor, ev->button.y * scale_factor);
+               break;
+
+       case SDL_MOUSEMOTION:
+               if(mouse_grabbed) {
+                       app_mouse_delta(ev->motion.xrel, ev->motion.yrel);
+               } else {
+                       app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor);
+               }
+               break;
+
+       case SDL_WINDOWEVENT:
+               if(ev->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
+                       SDL_GL_GetDrawableSize(win, &win_width, &win_height);
+                       win_aspect = (float)win_width / (float)win_height;
+                       scale_factor = win_width / ev->window.data1;
+                       app_reshape(win_width, win_height);
+               }
+               break;
+
+       case SDL_CONTROLLERAXISMOTION:
+               app_gamepad_axis(ev->caxis.axis, ev->caxis.value / 32768.0f);
+               break;
+
+       case SDL_CONTROLLERBUTTONDOWN:
+       case SDL_CONTROLLERBUTTONUP:
+               app_gamepad_button(ev->cbutton.button, ev->type == SDL_CONTROLLERBUTTONDOWN);
+               break;
+       }
+}
+
+static void proc_modkeys()
+{
+       modkeys = 0;
+       SDL_Keymod sdlmod = SDL_GetModState();
+       if(sdlmod & KMOD_SHIFT) {
+               modkeys |= MOD_SHIFT;
+       }
+       if(sdlmod & KMOD_ALT) {
+               modkeys |= MOD_ALT;
        }
-       if(bnstate[2]) {
-               cam_dist += dy * 0.05;
-               if(cam_dist < 0.0) cam_dist = 0.0;
-               glutPostRedisplay();
+       if(sdlmod & KMOD_CTRL) {
+               modkeys |= MOD_CTRL;
        }
 }