conworlds
diff src/sdl/main_sdl.cc @ 21:2da585428507
added both glut and sdl2 versions just for fun...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 27 Aug 2014 04:16:22 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/sdl/main_sdl.cc Wed Aug 27 04:16:22 2014 +0300 1.3 @@ -0,0 +1,154 @@ 1.4 +#ifdef USE_SDL 1.5 + 1.6 +#include <stdio.h> 1.7 +#include <stdlib.h> 1.8 +#include <SDL2/SDL.h> 1.9 +#include "opengl.h" 1.10 +#include "game.h" 1.11 +#include "gameopt.h" 1.12 +#include "vr/vr.h" 1.13 + 1.14 +#define ANYPOS SDL_WINDOWPOS_UNDEFINED 1.15 + 1.16 +static bool init(); 1.17 +static void cleanup(); 1.18 +static void handle_event(SDL_Event *ev); 1.19 + 1.20 +static SDL_Window *win; 1.21 +static SDL_GLContext ctx; 1.22 + 1.23 +int main(int argc, char **argv) 1.24 +{ 1.25 + if(!parse_args(argc, argv)) { 1.26 + return 1; 1.27 + } 1.28 + 1.29 + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); 1.30 + 1.31 + unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; 1.32 + if(!(win = SDL_CreateWindow("oculus test", ANYPOS, ANYPOS, 1024, 600, flags))) { 1.33 + fprintf(stderr, "failed to create window\n"); 1.34 + return 1; 1.35 + } 1.36 + 1.37 + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); 1.38 + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); 1.39 + 1.40 + if(!(ctx = SDL_GL_CreateContext(win))) { 1.41 + fprintf(stderr, "failed to create OpenGL context\n"); 1.42 + return 1; 1.43 + } 1.44 + 1.45 + if(!init()) { 1.46 + return 1; 1.47 + } 1.48 + atexit(cleanup); 1.49 + 1.50 + int xsz, ysz; 1.51 + SDL_GetWindowSize(win, &xsz, &ysz); 1.52 + game_reshape(xsz, ysz); 1.53 + 1.54 + for(;;) { 1.55 + SDL_Event ev; 1.56 + 1.57 + while(SDL_PollEvent(&ev)) { 1.58 + handle_event(&ev); 1.59 + } 1.60 + 1.61 + unsigned int msec = SDL_GetTicks(); 1.62 + 1.63 + game_update(msec); 1.64 + game_render(); 1.65 + } 1.66 + 1.67 + return 0; 1.68 +} 1.69 + 1.70 +static bool init() 1.71 +{ 1.72 + glewInit(); 1.73 + 1.74 + if(!game_init()) { 1.75 + return false; 1.76 + } 1.77 + 1.78 + if(opt.vr) { 1.79 + int win_xsz = vr_get_opti(VR_OPT_DISPLAY_WIDTH); 1.80 + int win_ysz = vr_get_opti(VR_OPT_DISPLAY_HEIGHT); 1.81 + if(win_xsz && win_ysz) { 1.82 + SDL_SetWindowSize(win, win_xsz, win_ysz); 1.83 + } 1.84 + } 1.85 + return true; 1.86 +} 1.87 + 1.88 +static void cleanup() 1.89 +{ 1.90 + game_cleanup(); 1.91 +} 1.92 + 1.93 +static void handle_event(SDL_Event *ev) 1.94 +{ 1.95 + static bool fullscr; 1.96 + static int prev_xpos, prev_ypos; 1.97 + static int prev_xsz, prev_ysz; 1.98 + 1.99 + switch(ev->type) { 1.100 + case SDL_KEYDOWN: 1.101 + case SDL_KEYUP: 1.102 + if(ev->key.keysym.sym == 'f' && ev->key.state == SDL_PRESSED) { 1.103 + fullscr = !fullscr; 1.104 + if(fullscr) { 1.105 + int xoffs, yoffs; 1.106 + 1.107 + SDL_GetWindowPosition(win, &prev_xpos, &prev_ypos); 1.108 + SDL_GetWindowSize(win, &prev_xsz, &prev_ysz); 1.109 + 1.110 + xoffs = vr_get_opti(VR_OPT_WIN_XOFFS); 1.111 + yoffs = vr_get_opti(VR_OPT_WIN_YOFFS); 1.112 + if(xoffs || yoffs) { 1.113 + printf("repositioning: %d,%d\n", xoffs, yoffs); 1.114 + SDL_SetWindowPosition(win, xoffs, yoffs); 1.115 + } 1.116 + SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP); 1.117 + } else { 1.118 + SDL_SetWindowFullscreen(win, 0); 1.119 + /*SDL_SetWindowPosition(win, prev_xpos, prev_ypos); 1.120 + SDL_SetWindowSize(win, prev_xsz, prev_ysz);*/ 1.121 + } 1.122 + break; 1.123 + } 1.124 + 1.125 + game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED); 1.126 + break; 1.127 + 1.128 + case SDL_MOUSEBUTTONDOWN: 1.129 + case SDL_MOUSEBUTTONUP: 1.130 + game_mouse(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED, 1.131 + ev->button.x, ev->button.y); 1.132 + break; 1.133 + 1.134 + case SDL_MOUSEWHEEL: 1.135 + game_mwheel(ev->wheel.y); 1.136 + break; 1.137 + 1.138 + case SDL_MOUSEMOTION: 1.139 + game_motion(ev->motion.x, ev->motion.y); 1.140 + break; 1.141 + 1.142 + case SDL_WINDOWEVENT: 1.143 + switch(ev->window.event) { 1.144 + case SDL_WINDOWEVENT_RESIZED: 1.145 + game_reshape(ev->window.data1, ev->window.data2); 1.146 + break; 1.147 + 1.148 + default: 1.149 + break; 1.150 + } 1.151 + 1.152 + default: 1.153 + break; 1.154 + } 1.155 +} 1.156 + 1.157 +#endif // USE_SDL