# HG changeset patch # User John Tsiombikas # Date 1408993328 -10800 # Node ID c814f77d177e09db58db9b2f11ce41a89784e681 # Parent 7a2041ddb7e74224138fd1dceddc2de5eb2a85c0 moved to SDL2 diff -r 7a2041ddb7e7 -r c814f77d177e Makefile --- a/Makefile Sun Aug 24 18:42:40 2014 +0300 +++ b/Makefile Mon Aug 25 22:02:08 2014 +0300 @@ -11,13 +11,13 @@ ohmd_cflags = -DUSE_OPENHMD ohmd_libs = -lopenhmd -CFLAGS = -pedantic -Wall -g $(ovr_cflags) +CFLAGS = -pedantic -Wall -g $(ovr_cflags) `pkg-config --cflags sdl2` CXXFLAGS = -std=c++11 $(CFLAGS) -LDFLAGS = $(libgl_$(sys)) -lm -lvmath -limago -lanim $(ovr_libs) +LDFLAGS = $(libgl_$(sys)) -lm -lvmath -limago -lanim $(ovr_libs) `pkg-config --libs sdl2` -libgl_unix = -lGL -lGLU -lglut -lGLEW -libgl_mac = -framework OpenGL -framework GLUT -lGLEW -libgl_win = -lopengl32 -lglu32 -lglut32 -lglew32 +libgl_unix = -lGL -lGLU -lGLEW +libgl_mac = -framework OpenGL -lGLEW +libgl_win = -lopengl32 -lglu32 -lglew32 $(bin): $(obj) $(CXX) -o $@ $(obj) $(LDFLAGS) diff -r 7a2041ddb7e7 -r c814f77d177e src/game.cc --- a/src/game.cc Sun Aug 24 18:42:40 2014 +0300 +++ b/src/game.cc Mon Aug 25 22:02:08 2014 +0300 @@ -178,7 +178,7 @@ setup_rtarg(rtwidth, rtheight); } -void game_keyboard(int key, bool pressed, int x, int y) +void game_keyboard(int key, bool pressed) { if(pressed) { switch(key) { diff -r 7a2041ddb7e7 -r c814f77d177e src/game.h --- a/src/game.h Sun Aug 24 18:42:40 2014 +0300 +++ b/src/game.h Mon Aug 25 22:02:08 2014 +0300 @@ -8,7 +8,7 @@ void game_render(); void game_reshape(int x, int y); -void game_keyboard(int key, bool pressed, int x, int y); +void game_keyboard(int key, bool pressed); void game_mouse(int bn, bool pressed, int x, int y); void game_motion(int x, int y); void game_mwheel(int dir); diff -r 7a2041ddb7e7 -r c814f77d177e src/main.cc --- a/src/main.cc Sun Aug 24 18:42:40 2014 +0300 +++ b/src/main.cc Mon Aug 25 22:02:08 2014 +0300 @@ -1,53 +1,64 @@ #include #include +#include #include "opengl.h" #include "game.h" #include "gameopt.h" #include "vr/vr.h" +#define ANYPOS SDL_WINDOWPOS_UNDEFINED + static bool init(); static void cleanup(); +static void handle_event(SDL_Event *ev); -static void display(); -static void idle(); -static void reshape(int x, int y); -static void keyb(unsigned char key, int x, int y); -static void keyb_up(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 sball_motion(int x, int y, int z); -static void sball_rotate(int x, int y, int z); - -static bool fullscreen_pending; +static SDL_Window *win; +static SDL_GLContext ctx; int main(int argc, char **argv) { - glutInitWindowSize(1024, 600); - glutInit(&argc, argv); - if(!parse_args(argc, argv)) { return 1; } - glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (opt.stereo ? GLUT_STEREO : 0)); - glutCreateWindow("LD48 #30 - connected worlds"); + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); - glutDisplayFunc(display); - glutIdleFunc(idle); - glutReshapeFunc(reshape); - glutKeyboardFunc(keyb); - glutKeyboardUpFunc(keyb_up); - glutMouseFunc(mouse); - glutMotionFunc(motion); - glutSpaceballMotionFunc(sball_motion); - glutSpaceballRotateFunc(sball_rotate); + unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; + if(!(win = SDL_CreateWindow("oculus test", ANYPOS, ANYPOS, 1024, 600, flags))) { + fprintf(stderr, "failed to create window\n"); + return 1; + } + + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); + + if(!(ctx = SDL_GL_CreateContext(win))) { + fprintf(stderr, "failed to create OpenGL context\n"); + return 1; + } if(!init()) { return 1; } atexit(cleanup); - glutMainLoop(); + int xsz, ysz; + SDL_GetWindowSize(win, &xsz, &ysz); + game_reshape(xsz, ysz); + + for(;;) { + SDL_Event ev; + + while(SDL_PollEvent(&ev)) { + handle_event(&ev); + } + + unsigned int msec = SDL_GetTicks(); + + game_update(msec); + game_render(); + } + return 0; } @@ -63,7 +74,7 @@ int win_xsz = vr_get_opti(VR_OPT_DISPLAY_WIDTH); int win_ysz = vr_get_opti(VR_OPT_DISPLAY_HEIGHT); if(win_xsz && win_ysz) { - glutReshapeWindow(win_xsz, win_ysz); + SDL_SetWindowSize(win, win_xsz, win_ysz); } } return true; @@ -74,101 +85,66 @@ game_cleanup(); } -static void display() -{ - unsigned int msec = glutGet(GLUT_ELAPSED_TIME); - - game_update(msec); - game_render(); -} - -static void idle() -{ - if(fullscreen_pending) { - glutFullScreen(); - } - - glutPostRedisplay(); -} - -static void reshape(int x, int y) -{ - game_reshape(x, y); -} - -static void keyb(unsigned char key, int x, int y) +static void handle_event(SDL_Event *ev) { static bool fullscr; static int prev_xpos, prev_ypos; static int prev_xsz, prev_ysz; - switch(key) { - case 'f': - fullscr = !fullscr; - if(fullscr) { - int xoffs, yoffs; + switch(ev->type) { + case SDL_KEYDOWN: + case SDL_KEYUP: + if(ev->key.keysym.sym == 'f' && ev->key.state == SDL_PRESSED) { + fullscr = !fullscr; + if(fullscr) { + int xoffs, yoffs; - prev_xpos = glutGet(GLUT_WINDOW_X); - prev_ypos = glutGet(GLUT_WINDOW_Y); - prev_xsz = glutGet(GLUT_WINDOW_WIDTH); - prev_ysz = glutGet(GLUT_WINDOW_HEIGHT); + SDL_GetWindowPosition(win, &prev_xpos, &prev_ypos); + SDL_GetWindowSize(win, &prev_xsz, &prev_ysz); - xoffs = vr_get_opti(VR_OPT_WIN_XOFFS); - yoffs = vr_get_opti(VR_OPT_WIN_YOFFS); - if(xoffs || yoffs) { - printf("repositioning: %d,%d\n", xoffs, yoffs); - glutPositionWindow(xoffs, yoffs); + xoffs = vr_get_opti(VR_OPT_WIN_XOFFS); + yoffs = vr_get_opti(VR_OPT_WIN_YOFFS); + if(xoffs || yoffs) { + printf("repositioning: %d,%d\n", xoffs, yoffs); + SDL_SetWindowPosition(win, xoffs, yoffs); + } + SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP); + } else { + SDL_SetWindowFullscreen(win, 0); + /*SDL_SetWindowPosition(win, prev_xpos, prev_ypos); + SDL_SetWindowSize(win, prev_xsz, prev_ysz);*/ } - fullscreen_pending = true; - } else { - fullscreen_pending = false; - glutPositionWindow(prev_xpos, prev_ypos); - glutReshapeWindow(prev_xsz, prev_ysz); + break; } + + game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED); + break; + + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + game_mouse(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED, + ev->button.x, ev->button.y); + break; + + case SDL_MOUSEWHEEL: + game_mwheel(ev->wheel.y); + break; + + case SDL_MOUSEMOTION: + game_motion(ev->motion.x, ev->motion.y); + break; + + case SDL_WINDOWEVENT: + switch(ev->window.event) { + case SDL_WINDOWEVENT_RESIZED: + game_reshape(ev->window.data1, ev->window.data2); + break; + + default: + break; + } + + default: break; } - game_keyboard(key, true, x, y); } - -static void keyb_up(unsigned char key, int x, int y) -{ - game_keyboard(key, false, x, y); -} - -static void mouse(int bn, int st, int x, int y) -{ - switch(bn) { - case GLUT_RIGHT_BUTTON + 1: - if(st == GLUT_DOWN) { - game_mwheel(1); - } - break; - - case GLUT_RIGHT_BUTTON + 2: - if(st == GLUT_DOWN) { - game_mwheel(-1); - } - break; - - default: - game_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y); - } -} - -static void motion(int x, int y) -{ - game_motion(x, y); -} - -#define SBALL_MOVE_SCALE 0.00025 -#define SBALL_ROT_SCALE 0.01 - -static void sball_motion(int x, int y, int z) -{ - game_6dof_move(x * SBALL_MOVE_SCALE, y * SBALL_MOVE_SCALE, z * SBALL_MOVE_SCALE); -} - -static void sball_rotate(int x, int y, int z) -{ - game_6dof_rotate(x * SBALL_ROT_SCALE, y * SBALL_ROT_SCALE, z * SBALL_ROT_SCALE); -} diff -r 7a2041ddb7e7 -r c814f77d177e src/opengl.h --- a/src/opengl.h Sun Aug 24 18:42:40 2014 +0300 +++ b/src/opengl.h Mon Aug 25 22:02:08 2014 +0300 @@ -4,9 +4,14 @@ #include #ifndef __APPLE__ -#include + +#ifdef WIN32 +#include +#endif + +#include #else -#include +#include #endif #define CHECKGLERR \