# HG changeset patch # User John Tsiombikas # Date 1409102182 -10800 # Node ID 2da58542850796d905b510dae24cc75af61e5897 # Parent 782ff06817fb92ba7e1bb283c56f0d53eb64ba3f added both glut and sdl2 versions just for fun... diff -r 782ff06817fb -r 2da585428507 Makefile --- a/Makefile Tue Aug 26 18:42:53 2014 +0300 +++ b/Makefile Wed Aug 27 04:16:22 2014 +0300 @@ -1,5 +1,5 @@ csrc = $(wildcard src/*.c) $(wildcard src/vr/*.c) -ccsrc = $(wildcard src/*.cc) +ccsrc = $(wildcard src/*.cc) $(wildcard src/sdl/*.cc) obj = $(csrc:.c=.o) $(ccsrc:.cc=.o) dep = $(obj:.o=.d) bin = conworlds diff -r 782ff06817fb -r 2da585428507 conworlds.vcxproj --- a/conworlds.vcxproj Tue Aug 26 18:42:53 2014 +0300 +++ b/conworlds.vcxproj Wed Aug 27 04:16:22 2014 +0300 @@ -51,7 +51,7 @@ Level3 Disabled - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions);USE_LIBOVR + USE_GLUT;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions);USE_LIBOVR 4996;4244;4305 $(SolutionDir)\src @@ -69,7 +69,7 @@ MaxSpeed true true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions);USE_LIBOVR + USE_GLUT;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions);USE_LIBOVR 4996;4244;4305 $(SolutionDir)\src @@ -87,15 +87,16 @@ + - + diff -r 782ff06817fb -r 2da585428507 conworlds.vcxproj.filters --- a/conworlds.vcxproj.filters Tue Aug 26 18:42:53 2014 +0300 +++ b/conworlds.vcxproj.filters Wed Aug 27 04:16:22 2014 +0300 @@ -8,11 +8,14 @@ {e881ab02-1a45-43f6-a15d-ee7f77256a1e} + + {4b658839-b773-4d11-9744-27a21bb1312c} + + + {0350ffd6-db06-4220-9274-8e2eb4bb33d1} + - - src - src @@ -91,6 +94,12 @@ src + + src\glut + + + src\sdl + diff -r 782ff06817fb -r 2da585428507 src/glut/main.cc --- a/src/glut/main.cc Tue Aug 26 18:42:53 2014 +0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,150 +0,0 @@ -#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 SDL_Window *win; -static SDL_GLContext ctx; - -int main(int argc, char **argv) -{ - if(!parse_args(argc, argv)) { - return 1; - } - - SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); - - 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); - - 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; -} - -static bool init() -{ - glewInit(); - - if(!game_init()) { - return false; - } - - if(opt.vr) { - 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) { - SDL_SetWindowSize(win, win_xsz, win_ysz); - } - } - return true; -} - -static void cleanup() -{ - game_cleanup(); -} - -static void handle_event(SDL_Event *ev) -{ - static bool fullscr; - static int prev_xpos, prev_ypos; - static int prev_xsz, prev_ysz; - - 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; - - 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); - 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);*/ - } - 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; - } -} diff -r 782ff06817fb -r 2da585428507 src/glut/main_glut.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/glut/main_glut.cc Wed Aug 27 04:16:22 2014 +0300 @@ -0,0 +1,183 @@ +#ifdef USE_GLUT + +#include +#include +#include "opengl.h" +#ifdef __APPLE__ +#include +#else +#include +#endif +#include "game.h" +#include "gameopt.h" +#include "vr/vr.h" + +static bool init(); +static void cleanup(); + +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; + +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"); + + glutDisplayFunc(display); + glutIdleFunc(idle); + glutReshapeFunc(reshape); + glutKeyboardFunc(keyb); + glutKeyboardUpFunc(keyb_up); + glutMouseFunc(mouse); + glutMotionFunc(motion); + glutSpaceballMotionFunc(sball_motion); + glutSpaceballRotateFunc(sball_rotate); + + if(!init()) { + return 1; + } + atexit(cleanup); + + glutMainLoop(); + return 0; +} + +static bool init() +{ + glewInit(); + + if(!game_init()) { + return false; + } + + if(opt.vr) { + 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); + } + } + return true; +} + +static void cleanup() +{ + 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 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; + + prev_xpos = glutGet(GLUT_WINDOW_X); + prev_ypos = glutGet(GLUT_WINDOW_Y); + prev_xsz = glutGet(GLUT_WINDOW_WIDTH); + prev_ysz = glutGet(GLUT_WINDOW_HEIGHT); + + 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); + } + fullscreen_pending = true; + } else { + fullscreen_pending = false; + glutPositionWindow(prev_xpos, prev_ypos); + glutReshapeWindow(prev_xsz, prev_ysz); + } + break; + } + game_keyboard(key, true); +} + +static void keyb_up(unsigned char key, int x, int y) +{ + game_keyboard(key, false); +} + +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); +} + +#endif // USE_GLUT \ No newline at end of file diff -r 782ff06817fb -r 2da585428507 src/sdl/main_sdl.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/sdl/main_sdl.cc Wed Aug 27 04:16:22 2014 +0300 @@ -0,0 +1,154 @@ +#ifdef USE_SDL + +#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 SDL_Window *win; +static SDL_GLContext ctx; + +int main(int argc, char **argv) +{ + if(!parse_args(argc, argv)) { + return 1; + } + + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); + + 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); + + 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; +} + +static bool init() +{ + glewInit(); + + if(!game_init()) { + return false; + } + + if(opt.vr) { + 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) { + SDL_SetWindowSize(win, win_xsz, win_ysz); + } + } + return true; +} + +static void cleanup() +{ + game_cleanup(); +} + +static void handle_event(SDL_Event *ev) +{ + static bool fullscr; + static int prev_xpos, prev_ypos; + static int prev_xsz, prev_ysz; + + 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; + + 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); + 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);*/ + } + 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; + } +} + +#endif // USE_SDL