X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=laserbrain_demo;a=blobdiff_plain;f=src%2Fmain.cc;h=4fae92d39173be655e1373aa3fc1fd85724424da;hp=b17009500a3da81831977c0cea4ae980c0233cb1;hb=c48096383ed398a518e69070bfc9373537ab00bb;hpb=b7c92831285013b2a0783bccaf3d900545ebb5ba diff --git a/src/main.cc b/src/main.cc index b170095..4fae92d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -2,211 +2,274 @@ #include #include #include -#ifdef __APPLE__ -#include -#else -#include -#endif +#include #include "app.h" -#include "sdr.h" -#include "shadow.h" -#include "texture.h" -#include "mesh.h" -#include "meshgen.h" - -static bool init(); -static void cleanup(); -static void display(); -static void idle(); -static void draw_scene(); -static void reshape(int x, int y); -static void keyb(unsigned char key, int x, int y); -static void mouse(int bn, int st, int x, int y); -static void motion(int x, int y); -static void passive_motion(int x, int y); - -static int win_width, win_height; -static float cam_dist = 0.25; -static float cam_theta, cam_phi = 20; -static int prev_mx, prev_my; -static bool bnstate[8]; +static bool init(int argc, char **argv); +static void process_event(SDL_Event *ev); +static void proc_modkeys(); +static int translate_keysym(SDL_Keycode sym); -static Mat4 view_matrix; +static SDL_Window *win; +static SDL_GLContext ctx; +static bool fullscreen, mouse_grabbed; +static bool quit; -static unsigned int start_time, prev_msec; +static unsigned int start_time; +static unsigned int modkeys; -static TextureSet texman; +SDL_GameController *gamepad; +static int scale_factor = 1; int main(int argc, char **argv) { - glutInitWindowSize(1024, 768); - glutInit(&argc, argv); - glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE); - glutCreateWindow("demo"); - - glutDisplayFunc(display); - glutIdleFunc(idle); - glutReshapeFunc(reshape); - glutKeyboardFunc(keyb); - glutMouseFunc(mouse); - glutMotionFunc(motion); - glutPassiveMotionFunc(passive_motion); - - if(!init()) { + if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) { + fprintf(stderr, "failed to initialize SDL\n"); return 1; } - atexit(cleanup); - glutMainLoop(); - return 0; -} + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8); + SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1); +#ifndef NDEBUG + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); +#endif -static bool init() -{ - glewInit(); + 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; itype) { + case SDL_QUIT: + quit = true; + break; + + case SDL_KEYDOWN: + case SDL_KEYUP: + proc_modkeys(); + if((key = translate_keysym(ev->key.keysym.sym)) != -1) { + app_keyboard(key, 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_MOUSEWHEEL: + app_mouse_wheel(ev->wheel.y); + 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; + } +} - if(cam_phi < -90) cam_phi = -90; - if(cam_phi > 90) cam_phi = 90; - glutPostRedisplay(); +static void proc_modkeys() +{ + modkeys = 0; + SDL_Keymod sdlmod = SDL_GetModState(); + if(sdlmod & KMOD_SHIFT) { + modkeys |= MOD_SHIFT; } - if(bnstate[2]) { - cam_dist += dy * 0.01; - if(cam_dist < 0.0) cam_dist = 0.0; - glutPostRedisplay(); + if(sdlmod & KMOD_ALT) { + modkeys |= MOD_ALT; + } + if(sdlmod & KMOD_CTRL) { + modkeys |= MOD_CTRL; } } -static void passive_motion(int x, int y) +static int translate_keysym(SDL_Keycode sym) { - prev_mx = x; - prev_my = y; + switch(sym) { + case SDLK_RETURN: + return '\n'; + case SDLK_DELETE: + return KEY_DEL; + case SDLK_LEFT: + return KEY_LEFT; + case SDLK_RIGHT: + return KEY_RIGHT; + case SDLK_UP: + return KEY_UP; + case SDLK_DOWN: + return KEY_DOWN; + case SDLK_PAGEUP: + return KEY_PGUP; + case SDLK_PAGEDOWN: + return KEY_PGDOWN; + case SDLK_HOME: + return KEY_HOME; + case SDLK_END: + return KEY_END; + default: + break; + } + + if(sym < 127) { + return sym; + } + if(sym >= SDLK_F1 && sym <= SDLK_F12) { + return KEY_F1 + sym - SDLK_F1; + } + return -1; }