conworlds
view src/glut/main.cc @ 20:782ff06817fb
merged ...
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 26 Aug 2014 18:42:53 +0300 |
parents | src/main.cc@c814f77d177e src/main.cc@c5054e0cd564 |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <SDL2/SDL.h>
4 #include "opengl.h"
5 #include "game.h"
6 #include "gameopt.h"
7 #include "vr/vr.h"
9 #define ANYPOS SDL_WINDOWPOS_UNDEFINED
11 static bool init();
12 static void cleanup();
13 static void handle_event(SDL_Event *ev);
15 static SDL_Window *win;
16 static SDL_GLContext ctx;
18 int main(int argc, char **argv)
19 {
20 if(!parse_args(argc, argv)) {
21 return 1;
22 }
24 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
26 unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
27 if(!(win = SDL_CreateWindow("oculus test", ANYPOS, ANYPOS, 1024, 600, flags))) {
28 fprintf(stderr, "failed to create window\n");
29 return 1;
30 }
32 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
33 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
35 if(!(ctx = SDL_GL_CreateContext(win))) {
36 fprintf(stderr, "failed to create OpenGL context\n");
37 return 1;
38 }
40 if(!init()) {
41 return 1;
42 }
43 atexit(cleanup);
45 int xsz, ysz;
46 SDL_GetWindowSize(win, &xsz, &ysz);
47 game_reshape(xsz, ysz);
49 for(;;) {
50 SDL_Event ev;
52 while(SDL_PollEvent(&ev)) {
53 handle_event(&ev);
54 }
56 unsigned int msec = SDL_GetTicks();
58 game_update(msec);
59 game_render();
60 }
62 return 0;
63 }
65 static bool init()
66 {
67 glewInit();
69 if(!game_init()) {
70 return false;
71 }
73 if(opt.vr) {
74 int win_xsz = vr_get_opti(VR_OPT_DISPLAY_WIDTH);
75 int win_ysz = vr_get_opti(VR_OPT_DISPLAY_HEIGHT);
76 if(win_xsz && win_ysz) {
77 SDL_SetWindowSize(win, win_xsz, win_ysz);
78 }
79 }
80 return true;
81 }
83 static void cleanup()
84 {
85 game_cleanup();
86 }
88 static void handle_event(SDL_Event *ev)
89 {
90 static bool fullscr;
91 static int prev_xpos, prev_ypos;
92 static int prev_xsz, prev_ysz;
94 switch(ev->type) {
95 case SDL_KEYDOWN:
96 case SDL_KEYUP:
97 if(ev->key.keysym.sym == 'f' && ev->key.state == SDL_PRESSED) {
98 fullscr = !fullscr;
99 if(fullscr) {
100 int xoffs, yoffs;
102 SDL_GetWindowPosition(win, &prev_xpos, &prev_ypos);
103 SDL_GetWindowSize(win, &prev_xsz, &prev_ysz);
105 xoffs = vr_get_opti(VR_OPT_WIN_XOFFS);
106 yoffs = vr_get_opti(VR_OPT_WIN_YOFFS);
107 if(xoffs || yoffs) {
108 printf("repositioning: %d,%d\n", xoffs, yoffs);
109 SDL_SetWindowPosition(win, xoffs, yoffs);
110 }
111 SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
112 } else {
113 SDL_SetWindowFullscreen(win, 0);
114 /*SDL_SetWindowPosition(win, prev_xpos, prev_ypos);
115 SDL_SetWindowSize(win, prev_xsz, prev_ysz);*/
116 }
117 break;
118 }
120 game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
121 break;
123 case SDL_MOUSEBUTTONDOWN:
124 case SDL_MOUSEBUTTONUP:
125 game_mouse(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
126 ev->button.x, ev->button.y);
127 break;
129 case SDL_MOUSEWHEEL:
130 game_mwheel(ev->wheel.y);
131 break;
133 case SDL_MOUSEMOTION:
134 game_motion(ev->motion.x, ev->motion.y);
135 break;
137 case SDL_WINDOWEVENT:
138 switch(ev->window.event) {
139 case SDL_WINDOWEVENT_RESIZED:
140 game_reshape(ev->window.data1, ev->window.data2);
141 break;
143 default:
144 break;
145 }
147 default:
148 break;
149 }
150 }