conworlds

diff src/glut/main.cc @ 19:c5054e0cd564

moved main.cc -> glut/main.cc
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 26 Aug 2014 18:40:23 +0300
parents src/main.cc@283cdfa7dda2
children 782ff06817fb
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/glut/main.cc	Tue Aug 26 18:40:23 2014 +0300
     1.3 @@ -0,0 +1,174 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include "opengl.h"
     1.7 +#include "game.h"
     1.8 +#include "gameopt.h"
     1.9 +#include "vr/vr.h"
    1.10 +
    1.11 +static bool init();
    1.12 +static void cleanup();
    1.13 +
    1.14 +static void display();
    1.15 +static void idle();
    1.16 +static void reshape(int x, int y);
    1.17 +static void keyb(unsigned char key, int x, int y);
    1.18 +static void keyb_up(unsigned char key, int x, int y);
    1.19 +static void mouse(int bn, int st, int x, int y);
    1.20 +static void motion(int x, int y);
    1.21 +static void sball_motion(int x, int y, int z);
    1.22 +static void sball_rotate(int x, int y, int z);
    1.23 +
    1.24 +static bool fullscreen_pending;
    1.25 +
    1.26 +int main(int argc, char **argv)
    1.27 +{
    1.28 +	glutInitWindowSize(1024, 600);
    1.29 +	glutInit(&argc, argv);
    1.30 +
    1.31 +	if(!parse_args(argc, argv)) {
    1.32 +		return 1;
    1.33 +	}
    1.34 +
    1.35 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (opt.stereo ? GLUT_STEREO : 0));
    1.36 +	glutCreateWindow("LD48 #30 - connected worlds");
    1.37 +
    1.38 +	glutDisplayFunc(display);
    1.39 +	glutIdleFunc(idle);
    1.40 +	glutReshapeFunc(reshape);
    1.41 +	glutKeyboardFunc(keyb);
    1.42 +	glutKeyboardUpFunc(keyb_up);
    1.43 +	glutMouseFunc(mouse);
    1.44 +	glutMotionFunc(motion);
    1.45 +	glutSpaceballMotionFunc(sball_motion);
    1.46 +	glutSpaceballRotateFunc(sball_rotate);
    1.47 +
    1.48 +	if(!init()) {
    1.49 +		return 1;
    1.50 +	}
    1.51 +	atexit(cleanup);
    1.52 +
    1.53 +	glutMainLoop();
    1.54 +	return 0;
    1.55 +}
    1.56 +
    1.57 +static bool init()
    1.58 +{
    1.59 +	glewInit();
    1.60 +
    1.61 +	if(!game_init()) {
    1.62 +		return false;
    1.63 +	}
    1.64 +
    1.65 +	if(opt.vr) {
    1.66 +		int win_xsz = vr_get_opti(VR_OPT_DISPLAY_WIDTH);
    1.67 +		int win_ysz = vr_get_opti(VR_OPT_DISPLAY_HEIGHT);
    1.68 +		if(win_xsz && win_ysz) {
    1.69 +			glutReshapeWindow(win_xsz, win_ysz);
    1.70 +		}
    1.71 +	}
    1.72 +	return true;
    1.73 +}
    1.74 +
    1.75 +static void cleanup()
    1.76 +{
    1.77 +	game_cleanup();
    1.78 +}
    1.79 +
    1.80 +static void display()
    1.81 +{
    1.82 +	unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
    1.83 +
    1.84 +	game_update(msec);
    1.85 +	game_render();
    1.86 +}
    1.87 +
    1.88 +static void idle()
    1.89 +{
    1.90 +	if(fullscreen_pending) {
    1.91 +		glutFullScreen();
    1.92 +	}
    1.93 +
    1.94 +	glutPostRedisplay();
    1.95 +}
    1.96 +
    1.97 +static void reshape(int x, int y)
    1.98 +{
    1.99 +	game_reshape(x, y);
   1.100 +}
   1.101 +
   1.102 +static void keyb(unsigned char key, int x, int y)
   1.103 +{
   1.104 +	static bool fullscr;
   1.105 +	static int prev_xpos, prev_ypos;
   1.106 +	static int prev_xsz, prev_ysz;
   1.107 +
   1.108 +	switch(key) {
   1.109 +	case 'f':
   1.110 +		fullscr = !fullscr;
   1.111 +		if(fullscr) {
   1.112 +			int xoffs, yoffs;
   1.113 +
   1.114 +			prev_xpos = glutGet(GLUT_WINDOW_X);
   1.115 +			prev_ypos = glutGet(GLUT_WINDOW_Y);
   1.116 +			prev_xsz = glutGet(GLUT_WINDOW_WIDTH);
   1.117 +			prev_ysz = glutGet(GLUT_WINDOW_HEIGHT);
   1.118 +
   1.119 +			xoffs = vr_get_opti(VR_OPT_WIN_XOFFS);
   1.120 +			yoffs = vr_get_opti(VR_OPT_WIN_YOFFS);
   1.121 +			if(xoffs || yoffs) {
   1.122 +				printf("repositioning: %d,%d\n", xoffs, yoffs);
   1.123 +				glutPositionWindow(xoffs, yoffs);
   1.124 +			}
   1.125 +			fullscreen_pending = true;
   1.126 +		} else {
   1.127 +			fullscreen_pending = false;
   1.128 +			glutPositionWindow(prev_xpos, prev_ypos);
   1.129 +			glutReshapeWindow(prev_xsz, prev_ysz);
   1.130 +		}
   1.131 +		break;
   1.132 +	}
   1.133 +	game_keyboard(key, true, x, y);
   1.134 +}
   1.135 +
   1.136 +static void keyb_up(unsigned char key, int x, int y)
   1.137 +{
   1.138 +	game_keyboard(key, false, x, y);
   1.139 +}
   1.140 +
   1.141 +static void mouse(int bn, int st, int x, int y)
   1.142 +{
   1.143 +	switch(bn) {
   1.144 +	case GLUT_RIGHT_BUTTON + 1:
   1.145 +		if(st == GLUT_DOWN) {
   1.146 +			game_mwheel(1);
   1.147 +		}
   1.148 +		break;
   1.149 +
   1.150 +	case GLUT_RIGHT_BUTTON + 2:
   1.151 +		if(st == GLUT_DOWN) {
   1.152 +			game_mwheel(-1);
   1.153 +		}
   1.154 +		break;
   1.155 +
   1.156 +	default:
   1.157 +		game_mouse(bn - GLUT_LEFT_BUTTON, st == GLUT_DOWN, x, y);
   1.158 +	}
   1.159 +}
   1.160 +
   1.161 +static void motion(int x, int y)
   1.162 +{
   1.163 +	game_motion(x, y);
   1.164 +}
   1.165 +
   1.166 +#define SBALL_MOVE_SCALE	0.00025
   1.167 +#define SBALL_ROT_SCALE		0.01
   1.168 +
   1.169 +static void sball_motion(int x, int y, int z)
   1.170 +{
   1.171 +	game_6dof_move(x * SBALL_MOVE_SCALE, y * SBALL_MOVE_SCALE, z * SBALL_MOVE_SCALE);
   1.172 +}
   1.173 +
   1.174 +static void sball_rotate(int x, int y, int z)
   1.175 +{
   1.176 +	game_6dof_rotate(x * SBALL_ROT_SCALE, y * SBALL_ROT_SCALE, z * SBALL_ROT_SCALE);
   1.177 +}