conworlds

view 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 source
1 #ifdef USE_SDL
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <SDL2/SDL.h>
6 #include "opengl.h"
7 #include "game.h"
8 #include "gameopt.h"
9 #include "vr/vr.h"
11 #define ANYPOS SDL_WINDOWPOS_UNDEFINED
13 static bool init();
14 static void cleanup();
15 static void handle_event(SDL_Event *ev);
17 static SDL_Window *win;
18 static SDL_GLContext ctx;
20 int main(int argc, char **argv)
21 {
22 if(!parse_args(argc, argv)) {
23 return 1;
24 }
26 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
28 unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
29 if(!(win = SDL_CreateWindow("oculus test", ANYPOS, ANYPOS, 1024, 600, flags))) {
30 fprintf(stderr, "failed to create window\n");
31 return 1;
32 }
34 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
35 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
37 if(!(ctx = SDL_GL_CreateContext(win))) {
38 fprintf(stderr, "failed to create OpenGL context\n");
39 return 1;
40 }
42 if(!init()) {
43 return 1;
44 }
45 atexit(cleanup);
47 int xsz, ysz;
48 SDL_GetWindowSize(win, &xsz, &ysz);
49 game_reshape(xsz, ysz);
51 for(;;) {
52 SDL_Event ev;
54 while(SDL_PollEvent(&ev)) {
55 handle_event(&ev);
56 }
58 unsigned int msec = SDL_GetTicks();
60 game_update(msec);
61 game_render();
62 }
64 return 0;
65 }
67 static bool init()
68 {
69 glewInit();
71 if(!game_init()) {
72 return false;
73 }
75 if(opt.vr) {
76 int win_xsz = vr_get_opti(VR_OPT_DISPLAY_WIDTH);
77 int win_ysz = vr_get_opti(VR_OPT_DISPLAY_HEIGHT);
78 if(win_xsz && win_ysz) {
79 SDL_SetWindowSize(win, win_xsz, win_ysz);
80 }
81 }
82 return true;
83 }
85 static void cleanup()
86 {
87 game_cleanup();
88 }
90 static void handle_event(SDL_Event *ev)
91 {
92 static bool fullscr;
93 static int prev_xpos, prev_ypos;
94 static int prev_xsz, prev_ysz;
96 switch(ev->type) {
97 case SDL_KEYDOWN:
98 case SDL_KEYUP:
99 if(ev->key.keysym.sym == 'f' && ev->key.state == SDL_PRESSED) {
100 fullscr = !fullscr;
101 if(fullscr) {
102 int xoffs, yoffs;
104 SDL_GetWindowPosition(win, &prev_xpos, &prev_ypos);
105 SDL_GetWindowSize(win, &prev_xsz, &prev_ysz);
107 xoffs = vr_get_opti(VR_OPT_WIN_XOFFS);
108 yoffs = vr_get_opti(VR_OPT_WIN_YOFFS);
109 if(xoffs || yoffs) {
110 printf("repositioning: %d,%d\n", xoffs, yoffs);
111 SDL_SetWindowPosition(win, xoffs, yoffs);
112 }
113 SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
114 } else {
115 SDL_SetWindowFullscreen(win, 0);
116 /*SDL_SetWindowPosition(win, prev_xpos, prev_ypos);
117 SDL_SetWindowSize(win, prev_xsz, prev_ysz);*/
118 }
119 break;
120 }
122 game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
123 break;
125 case SDL_MOUSEBUTTONDOWN:
126 case SDL_MOUSEBUTTONUP:
127 game_mouse(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
128 ev->button.x, ev->button.y);
129 break;
131 case SDL_MOUSEWHEEL:
132 game_mwheel(ev->wheel.y);
133 break;
135 case SDL_MOUSEMOTION:
136 game_motion(ev->motion.x, ev->motion.y);
137 break;
139 case SDL_WINDOWEVENT:
140 switch(ev->window.event) {
141 case SDL_WINDOWEVENT_RESIZED:
142 game_reshape(ev->window.data1, ev->window.data2);
143 break;
145 default:
146 break;
147 }
149 default:
150 break;
151 }
152 }
154 #endif // USE_SDL