conworlds

diff 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 diff
     1.1 --- a/src/glut/main.cc	Tue Aug 26 18:40:23 2014 +0300
     1.2 +++ b/src/glut/main.cc	Tue Aug 26 18:42:53 2014 +0300
     1.3 @@ -1,53 +1,64 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6 +#include <SDL2/SDL.h>
     1.7  #include "opengl.h"
     1.8  #include "game.h"
     1.9  #include "gameopt.h"
    1.10  #include "vr/vr.h"
    1.11  
    1.12 +#define ANYPOS	SDL_WINDOWPOS_UNDEFINED
    1.13 +
    1.14  static bool init();
    1.15  static void cleanup();
    1.16 +static void handle_event(SDL_Event *ev);
    1.17  
    1.18 -static void display();
    1.19 -static void idle();
    1.20 -static void reshape(int x, int y);
    1.21 -static void keyb(unsigned char key, int x, int y);
    1.22 -static void keyb_up(unsigned char key, int x, int y);
    1.23 -static void mouse(int bn, int st, int x, int y);
    1.24 -static void motion(int x, int y);
    1.25 -static void sball_motion(int x, int y, int z);
    1.26 -static void sball_rotate(int x, int y, int z);
    1.27 -
    1.28 -static bool fullscreen_pending;
    1.29 +static SDL_Window *win;
    1.30 +static SDL_GLContext ctx;
    1.31  
    1.32  int main(int argc, char **argv)
    1.33  {
    1.34 -	glutInitWindowSize(1024, 600);
    1.35 -	glutInit(&argc, argv);
    1.36 -
    1.37  	if(!parse_args(argc, argv)) {
    1.38  		return 1;
    1.39  	}
    1.40  
    1.41 -	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (opt.stereo ? GLUT_STEREO : 0));
    1.42 -	glutCreateWindow("LD48 #30 - connected worlds");
    1.43 +	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
    1.44  
    1.45 -	glutDisplayFunc(display);
    1.46 -	glutIdleFunc(idle);
    1.47 -	glutReshapeFunc(reshape);
    1.48 -	glutKeyboardFunc(keyb);
    1.49 -	glutKeyboardUpFunc(keyb_up);
    1.50 -	glutMouseFunc(mouse);
    1.51 -	glutMotionFunc(motion);
    1.52 -	glutSpaceballMotionFunc(sball_motion);
    1.53 -	glutSpaceballRotateFunc(sball_rotate);
    1.54 +	unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
    1.55 +	if(!(win = SDL_CreateWindow("oculus test", ANYPOS, ANYPOS, 1024, 600, flags))) {
    1.56 +		fprintf(stderr, "failed to create window\n");
    1.57 +		return 1;
    1.58 +	}
    1.59 +
    1.60 +	SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
    1.61 +	SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
    1.62 +
    1.63 +	if(!(ctx = SDL_GL_CreateContext(win))) {
    1.64 +		fprintf(stderr, "failed to create OpenGL context\n");
    1.65 +		return 1;
    1.66 +	}
    1.67  
    1.68  	if(!init()) {
    1.69  		return 1;
    1.70  	}
    1.71  	atexit(cleanup);
    1.72  
    1.73 -	glutMainLoop();
    1.74 +	int xsz, ysz;
    1.75 +	SDL_GetWindowSize(win, &xsz, &ysz);
    1.76 +	game_reshape(xsz, ysz);
    1.77 +
    1.78 +	for(;;) {
    1.79 +		SDL_Event ev;
    1.80 +
    1.81 +		while(SDL_PollEvent(&ev)) {
    1.82 +			handle_event(&ev);
    1.83 +		}
    1.84 +
    1.85 +		unsigned int msec = SDL_GetTicks();
    1.86 +
    1.87 +		game_update(msec);
    1.88 +		game_render();
    1.89 +	}
    1.90 +
    1.91  	return 0;
    1.92  }
    1.93  
    1.94 @@ -63,7 +74,7 @@
    1.95  		int win_xsz = vr_get_opti(VR_OPT_DISPLAY_WIDTH);
    1.96  		int win_ysz = vr_get_opti(VR_OPT_DISPLAY_HEIGHT);
    1.97  		if(win_xsz && win_ysz) {
    1.98 -			glutReshapeWindow(win_xsz, win_ysz);
    1.99 +			SDL_SetWindowSize(win, win_xsz, win_ysz);
   1.100  		}
   1.101  	}
   1.102  	return true;
   1.103 @@ -74,101 +85,66 @@
   1.104  	game_cleanup();
   1.105  }
   1.106  
   1.107 -static void display()
   1.108 -{
   1.109 -	unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
   1.110 -
   1.111 -	game_update(msec);
   1.112 -	game_render();
   1.113 -}
   1.114 -
   1.115 -static void idle()
   1.116 -{
   1.117 -	if(fullscreen_pending) {
   1.118 -		glutFullScreen();
   1.119 -	}
   1.120 -
   1.121 -	glutPostRedisplay();
   1.122 -}
   1.123 -
   1.124 -static void reshape(int x, int y)
   1.125 -{
   1.126 -	game_reshape(x, y);
   1.127 -}
   1.128 -
   1.129 -static void keyb(unsigned char key, int x, int y)
   1.130 +static void handle_event(SDL_Event *ev)
   1.131  {
   1.132  	static bool fullscr;
   1.133  	static int prev_xpos, prev_ypos;
   1.134  	static int prev_xsz, prev_ysz;
   1.135  
   1.136 -	switch(key) {
   1.137 -	case 'f':
   1.138 -		fullscr = !fullscr;
   1.139 -		if(fullscr) {
   1.140 -			int xoffs, yoffs;
   1.141 +	switch(ev->type) {
   1.142 +	case SDL_KEYDOWN:
   1.143 +	case SDL_KEYUP:
   1.144 +		if(ev->key.keysym.sym == 'f' && ev->key.state == SDL_PRESSED) {
   1.145 +			fullscr = !fullscr;
   1.146 +			if(fullscr) {
   1.147 +				int xoffs, yoffs;
   1.148  
   1.149 -			prev_xpos = glutGet(GLUT_WINDOW_X);
   1.150 -			prev_ypos = glutGet(GLUT_WINDOW_Y);
   1.151 -			prev_xsz = glutGet(GLUT_WINDOW_WIDTH);
   1.152 -			prev_ysz = glutGet(GLUT_WINDOW_HEIGHT);
   1.153 +				SDL_GetWindowPosition(win, &prev_xpos, &prev_ypos);
   1.154 +				SDL_GetWindowSize(win, &prev_xsz, &prev_ysz);
   1.155  
   1.156 -			xoffs = vr_get_opti(VR_OPT_WIN_XOFFS);
   1.157 -			yoffs = vr_get_opti(VR_OPT_WIN_YOFFS);
   1.158 -			if(xoffs || yoffs) {
   1.159 -				printf("repositioning: %d,%d\n", xoffs, yoffs);
   1.160 -				glutPositionWindow(xoffs, yoffs);
   1.161 +				xoffs = vr_get_opti(VR_OPT_WIN_XOFFS);
   1.162 +				yoffs = vr_get_opti(VR_OPT_WIN_YOFFS);
   1.163 +				if(xoffs || yoffs) {
   1.164 +					printf("repositioning: %d,%d\n", xoffs, yoffs);
   1.165 +					SDL_SetWindowPosition(win, xoffs, yoffs);
   1.166 +				}
   1.167 +				SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
   1.168 +			} else {
   1.169 +				SDL_SetWindowFullscreen(win, 0);
   1.170 +				/*SDL_SetWindowPosition(win, prev_xpos, prev_ypos);
   1.171 +				SDL_SetWindowSize(win, prev_xsz, prev_ysz);*/
   1.172  			}
   1.173 -			fullscreen_pending = true;
   1.174 -		} else {
   1.175 -			fullscreen_pending = false;
   1.176 -			glutPositionWindow(prev_xpos, prev_ypos);
   1.177 -			glutReshapeWindow(prev_xsz, prev_ysz);
   1.178 +			break;
   1.179  		}
   1.180 +
   1.181 +		game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
   1.182 +		break;
   1.183 +
   1.184 +	case SDL_MOUSEBUTTONDOWN:
   1.185 +	case SDL_MOUSEBUTTONUP:
   1.186 +		game_mouse(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
   1.187 +				ev->button.x, ev->button.y);
   1.188 +		break;
   1.189 +
   1.190 +	case SDL_MOUSEWHEEL:
   1.191 +		game_mwheel(ev->wheel.y);
   1.192 +		break;
   1.193 +
   1.194 +	case SDL_MOUSEMOTION:
   1.195 +		game_motion(ev->motion.x, ev->motion.y);
   1.196 +		break;
   1.197 +
   1.198 +	case SDL_WINDOWEVENT:
   1.199 +		switch(ev->window.event) {
   1.200 +		case SDL_WINDOWEVENT_RESIZED:
   1.201 +			game_reshape(ev->window.data1, ev->window.data2);
   1.202 +			break;
   1.203 +
   1.204 +		default:
   1.205 +			break;
   1.206 +		}
   1.207 +
   1.208 +	default:
   1.209  		break;
   1.210  	}
   1.211 -	game_keyboard(key, true, x, y);
   1.212  }
   1.213 -
   1.214 -static void keyb_up(unsigned char key, int x, int y)
   1.215 -{
   1.216 -	game_keyboard(key, false, x, y);
   1.217 -}
   1.218 -
   1.219 -static void mouse(int bn, int st, int x, int y)
   1.220 -{
   1.221 -	switch(bn) {
   1.222 -	case GLUT_RIGHT_BUTTON + 1:
   1.223 -		if(st == GLUT_DOWN) {
   1.224 -			game_mwheel(1);
   1.225 -		}
   1.226 -		break;
   1.227 -
   1.228 -	case GLUT_RIGHT_BUTTON + 2:
   1.229 -		if(st == GLUT_DOWN) {
   1.230 -			game_mwheel(-1);
   1.231 -		}
   1.232 -		break;
   1.233 -
   1.234 -	default:
   1.235 -		game_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
   1.236 -	}
   1.237 -}
   1.238 -
   1.239 -static void motion(int x, int y)
   1.240 -{
   1.241 -	game_motion(x, y);
   1.242 -}
   1.243 -
   1.244 -#define SBALL_MOVE_SCALE	0.00025
   1.245 -#define SBALL_ROT_SCALE		0.01
   1.246 -
   1.247 -static void sball_motion(int x, int y, int z)
   1.248 -{
   1.249 -	game_6dof_move(x * SBALL_MOVE_SCALE, y * SBALL_MOVE_SCALE, z * SBALL_MOVE_SCALE);
   1.250 -}
   1.251 -
   1.252 -static void sball_rotate(int x, int y, int z)
   1.253 -{
   1.254 -	game_6dof_rotate(x * SBALL_ROT_SCALE, y * SBALL_ROT_SCALE, z * SBALL_ROT_SCALE);
   1.255 -}