conworlds
changeset 17:c814f77d177e
moved to SDL2
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 25 Aug 2014 22:02:08 +0300 |
parents | 7a2041ddb7e7 |
children | 782ff06817fb |
files | Makefile src/game.cc src/game.h src/main.cc src/opengl.h |
diffstat | 5 files changed, 103 insertions(+), 122 deletions(-) [+] |
line diff
1.1 --- a/Makefile Sun Aug 24 18:42:40 2014 +0300 1.2 +++ b/Makefile Mon Aug 25 22:02:08 2014 +0300 1.3 @@ -11,13 +11,13 @@ 1.4 ohmd_cflags = -DUSE_OPENHMD 1.5 ohmd_libs = -lopenhmd 1.6 1.7 -CFLAGS = -pedantic -Wall -g $(ovr_cflags) 1.8 +CFLAGS = -pedantic -Wall -g $(ovr_cflags) `pkg-config --cflags sdl2` 1.9 CXXFLAGS = -std=c++11 $(CFLAGS) 1.10 -LDFLAGS = $(libgl_$(sys)) -lm -lvmath -limago -lanim $(ovr_libs) 1.11 +LDFLAGS = $(libgl_$(sys)) -lm -lvmath -limago -lanim $(ovr_libs) `pkg-config --libs sdl2` 1.12 1.13 -libgl_unix = -lGL -lGLU -lglut -lGLEW 1.14 -libgl_mac = -framework OpenGL -framework GLUT -lGLEW 1.15 -libgl_win = -lopengl32 -lglu32 -lglut32 -lglew32 1.16 +libgl_unix = -lGL -lGLU -lGLEW 1.17 +libgl_mac = -framework OpenGL -lGLEW 1.18 +libgl_win = -lopengl32 -lglu32 -lglew32 1.19 1.20 $(bin): $(obj) 1.21 $(CXX) -o $@ $(obj) $(LDFLAGS)
2.1 --- a/src/game.cc Sun Aug 24 18:42:40 2014 +0300 2.2 +++ b/src/game.cc Mon Aug 25 22:02:08 2014 +0300 2.3 @@ -178,7 +178,7 @@ 2.4 setup_rtarg(rtwidth, rtheight); 2.5 } 2.6 2.7 -void game_keyboard(int key, bool pressed, int x, int y) 2.8 +void game_keyboard(int key, bool pressed) 2.9 { 2.10 if(pressed) { 2.11 switch(key) {
3.1 --- a/src/game.h Sun Aug 24 18:42:40 2014 +0300 3.2 +++ b/src/game.h Mon Aug 25 22:02:08 2014 +0300 3.3 @@ -8,7 +8,7 @@ 3.4 void game_render(); 3.5 3.6 void game_reshape(int x, int y); 3.7 -void game_keyboard(int key, bool pressed, int x, int y); 3.8 +void game_keyboard(int key, bool pressed); 3.9 void game_mouse(int bn, bool pressed, int x, int y); 3.10 void game_motion(int x, int y); 3.11 void game_mwheel(int dir);
4.1 --- a/src/main.cc Sun Aug 24 18:42:40 2014 +0300 4.2 +++ b/src/main.cc Mon Aug 25 22:02:08 2014 +0300 4.3 @@ -1,53 +1,64 @@ 4.4 #include <stdio.h> 4.5 #include <stdlib.h> 4.6 +#include <SDL2/SDL.h> 4.7 #include "opengl.h" 4.8 #include "game.h" 4.9 #include "gameopt.h" 4.10 #include "vr/vr.h" 4.11 4.12 +#define ANYPOS SDL_WINDOWPOS_UNDEFINED 4.13 + 4.14 static bool init(); 4.15 static void cleanup(); 4.16 +static void handle_event(SDL_Event *ev); 4.17 4.18 -static void display(); 4.19 -static void idle(); 4.20 -static void reshape(int x, int y); 4.21 -static void keyb(unsigned char key, int x, int y); 4.22 -static void keyb_up(unsigned char key, int x, int y); 4.23 -static void mouse(int bn, int st, int x, int y); 4.24 -static void motion(int x, int y); 4.25 -static void sball_motion(int x, int y, int z); 4.26 -static void sball_rotate(int x, int y, int z); 4.27 - 4.28 -static bool fullscreen_pending; 4.29 +static SDL_Window *win; 4.30 +static SDL_GLContext ctx; 4.31 4.32 int main(int argc, char **argv) 4.33 { 4.34 - glutInitWindowSize(1024, 600); 4.35 - glutInit(&argc, argv); 4.36 - 4.37 if(!parse_args(argc, argv)) { 4.38 return 1; 4.39 } 4.40 4.41 - glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (opt.stereo ? GLUT_STEREO : 0)); 4.42 - glutCreateWindow("LD48 #30 - connected worlds"); 4.43 + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); 4.44 4.45 - glutDisplayFunc(display); 4.46 - glutIdleFunc(idle); 4.47 - glutReshapeFunc(reshape); 4.48 - glutKeyboardFunc(keyb); 4.49 - glutKeyboardUpFunc(keyb_up); 4.50 - glutMouseFunc(mouse); 4.51 - glutMotionFunc(motion); 4.52 - glutSpaceballMotionFunc(sball_motion); 4.53 - glutSpaceballRotateFunc(sball_rotate); 4.54 + unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; 4.55 + if(!(win = SDL_CreateWindow("oculus test", ANYPOS, ANYPOS, 1024, 600, flags))) { 4.56 + fprintf(stderr, "failed to create window\n"); 4.57 + return 1; 4.58 + } 4.59 + 4.60 + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); 4.61 + SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); 4.62 + 4.63 + if(!(ctx = SDL_GL_CreateContext(win))) { 4.64 + fprintf(stderr, "failed to create OpenGL context\n"); 4.65 + return 1; 4.66 + } 4.67 4.68 if(!init()) { 4.69 return 1; 4.70 } 4.71 atexit(cleanup); 4.72 4.73 - glutMainLoop(); 4.74 + int xsz, ysz; 4.75 + SDL_GetWindowSize(win, &xsz, &ysz); 4.76 + game_reshape(xsz, ysz); 4.77 + 4.78 + for(;;) { 4.79 + SDL_Event ev; 4.80 + 4.81 + while(SDL_PollEvent(&ev)) { 4.82 + handle_event(&ev); 4.83 + } 4.84 + 4.85 + unsigned int msec = SDL_GetTicks(); 4.86 + 4.87 + game_update(msec); 4.88 + game_render(); 4.89 + } 4.90 + 4.91 return 0; 4.92 } 4.93 4.94 @@ -63,7 +74,7 @@ 4.95 int win_xsz = vr_get_opti(VR_OPT_DISPLAY_WIDTH); 4.96 int win_ysz = vr_get_opti(VR_OPT_DISPLAY_HEIGHT); 4.97 if(win_xsz && win_ysz) { 4.98 - glutReshapeWindow(win_xsz, win_ysz); 4.99 + SDL_SetWindowSize(win, win_xsz, win_ysz); 4.100 } 4.101 } 4.102 return true; 4.103 @@ -74,101 +85,66 @@ 4.104 game_cleanup(); 4.105 } 4.106 4.107 -static void display() 4.108 -{ 4.109 - unsigned int msec = glutGet(GLUT_ELAPSED_TIME); 4.110 - 4.111 - game_update(msec); 4.112 - game_render(); 4.113 -} 4.114 - 4.115 -static void idle() 4.116 -{ 4.117 - if(fullscreen_pending) { 4.118 - glutFullScreen(); 4.119 - } 4.120 - 4.121 - glutPostRedisplay(); 4.122 -} 4.123 - 4.124 -static void reshape(int x, int y) 4.125 -{ 4.126 - game_reshape(x, y); 4.127 -} 4.128 - 4.129 -static void keyb(unsigned char key, int x, int y) 4.130 +static void handle_event(SDL_Event *ev) 4.131 { 4.132 static bool fullscr; 4.133 static int prev_xpos, prev_ypos; 4.134 static int prev_xsz, prev_ysz; 4.135 4.136 - switch(key) { 4.137 - case 'f': 4.138 - fullscr = !fullscr; 4.139 - if(fullscr) { 4.140 - int xoffs, yoffs; 4.141 + switch(ev->type) { 4.142 + case SDL_KEYDOWN: 4.143 + case SDL_KEYUP: 4.144 + if(ev->key.keysym.sym == 'f' && ev->key.state == SDL_PRESSED) { 4.145 + fullscr = !fullscr; 4.146 + if(fullscr) { 4.147 + int xoffs, yoffs; 4.148 4.149 - prev_xpos = glutGet(GLUT_WINDOW_X); 4.150 - prev_ypos = glutGet(GLUT_WINDOW_Y); 4.151 - prev_xsz = glutGet(GLUT_WINDOW_WIDTH); 4.152 - prev_ysz = glutGet(GLUT_WINDOW_HEIGHT); 4.153 + SDL_GetWindowPosition(win, &prev_xpos, &prev_ypos); 4.154 + SDL_GetWindowSize(win, &prev_xsz, &prev_ysz); 4.155 4.156 - xoffs = vr_get_opti(VR_OPT_WIN_XOFFS); 4.157 - yoffs = vr_get_opti(VR_OPT_WIN_YOFFS); 4.158 - if(xoffs || yoffs) { 4.159 - printf("repositioning: %d,%d\n", xoffs, yoffs); 4.160 - glutPositionWindow(xoffs, yoffs); 4.161 + xoffs = vr_get_opti(VR_OPT_WIN_XOFFS); 4.162 + yoffs = vr_get_opti(VR_OPT_WIN_YOFFS); 4.163 + if(xoffs || yoffs) { 4.164 + printf("repositioning: %d,%d\n", xoffs, yoffs); 4.165 + SDL_SetWindowPosition(win, xoffs, yoffs); 4.166 + } 4.167 + SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP); 4.168 + } else { 4.169 + SDL_SetWindowFullscreen(win, 0); 4.170 + /*SDL_SetWindowPosition(win, prev_xpos, prev_ypos); 4.171 + SDL_SetWindowSize(win, prev_xsz, prev_ysz);*/ 4.172 } 4.173 - fullscreen_pending = true; 4.174 - } else { 4.175 - fullscreen_pending = false; 4.176 - glutPositionWindow(prev_xpos, prev_ypos); 4.177 - glutReshapeWindow(prev_xsz, prev_ysz); 4.178 + break; 4.179 } 4.180 + 4.181 + game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED); 4.182 + break; 4.183 + 4.184 + case SDL_MOUSEBUTTONDOWN: 4.185 + case SDL_MOUSEBUTTONUP: 4.186 + game_mouse(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED, 4.187 + ev->button.x, ev->button.y); 4.188 + break; 4.189 + 4.190 + case SDL_MOUSEWHEEL: 4.191 + game_mwheel(ev->wheel.y); 4.192 + break; 4.193 + 4.194 + case SDL_MOUSEMOTION: 4.195 + game_motion(ev->motion.x, ev->motion.y); 4.196 + break; 4.197 + 4.198 + case SDL_WINDOWEVENT: 4.199 + switch(ev->window.event) { 4.200 + case SDL_WINDOWEVENT_RESIZED: 4.201 + game_reshape(ev->window.data1, ev->window.data2); 4.202 + break; 4.203 + 4.204 + default: 4.205 + break; 4.206 + } 4.207 + 4.208 + default: 4.209 break; 4.210 } 4.211 - game_keyboard(key, true, x, y); 4.212 } 4.213 - 4.214 -static void keyb_up(unsigned char key, int x, int y) 4.215 -{ 4.216 - game_keyboard(key, false, x, y); 4.217 -} 4.218 - 4.219 -static void mouse(int bn, int st, int x, int y) 4.220 -{ 4.221 - switch(bn) { 4.222 - case GLUT_RIGHT_BUTTON + 1: 4.223 - if(st == GLUT_DOWN) { 4.224 - game_mwheel(1); 4.225 - } 4.226 - break; 4.227 - 4.228 - case GLUT_RIGHT_BUTTON + 2: 4.229 - if(st == GLUT_DOWN) { 4.230 - game_mwheel(-1); 4.231 - } 4.232 - break; 4.233 - 4.234 - default: 4.235 - game_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y); 4.236 - } 4.237 -} 4.238 - 4.239 -static void motion(int x, int y) 4.240 -{ 4.241 - game_motion(x, y); 4.242 -} 4.243 - 4.244 -#define SBALL_MOVE_SCALE 0.00025 4.245 -#define SBALL_ROT_SCALE 0.01 4.246 - 4.247 -static void sball_motion(int x, int y, int z) 4.248 -{ 4.249 - game_6dof_move(x * SBALL_MOVE_SCALE, y * SBALL_MOVE_SCALE, z * SBALL_MOVE_SCALE); 4.250 -} 4.251 - 4.252 -static void sball_rotate(int x, int y, int z) 4.253 -{ 4.254 - game_6dof_rotate(x * SBALL_ROT_SCALE, y * SBALL_ROT_SCALE, z * SBALL_ROT_SCALE); 4.255 -}
5.1 --- a/src/opengl.h Sun Aug 24 18:42:40 2014 +0300 5.2 +++ b/src/opengl.h Mon Aug 25 22:02:08 2014 +0300 5.3 @@ -4,9 +4,14 @@ 5.4 #include <GL/glew.h> 5.5 5.6 #ifndef __APPLE__ 5.7 -#include <GL/glut.h> 5.8 + 5.9 +#ifdef WIN32 5.10 +#include <windows.h> 5.11 +#endif 5.12 + 5.13 +#include <GL/gl.h> 5.14 #else 5.15 -#include <GLUT/glut.h> 5.16 +#include <OpenGL/gl.h> 5.17 #endif 5.18 5.19 #define CHECKGLERR \