conworlds

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