dungeon_crawler

diff prototype/src/main.cc @ 7:8fb37db44fd8

first person motion
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 17 Aug 2012 14:29:37 +0300
parents 252a00508411
children b10ba85f75e0
line diff
     1.1 --- a/prototype/src/main.cc	Tue Aug 14 08:49:38 2012 +0300
     1.2 +++ b/prototype/src/main.cc	Fri Aug 17 14:29:37 2012 +0300
     1.3 @@ -1,6 +1,7 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6  #include <assert.h>
     1.7 +#include <unistd.h>
     1.8  #include "opengl.h"
     1.9  #include "level.h"
    1.10  #include "camera.h"
    1.11 @@ -9,15 +10,20 @@
    1.12  
    1.13  bool init();
    1.14  void cleanup();
    1.15 +void idle();
    1.16  void disp();
    1.17 +void update(unsigned long msec);
    1.18  void reshape(int x, int y);
    1.19  void keyb(unsigned char key, int x, int y);
    1.20 +void key_release(unsigned char key, int x, int y);
    1.21  void mouse(int bn, int state, int x, int y);
    1.22  void motion(int x, int y);
    1.23  
    1.24  static TileSet *tileset;
    1.25  static Level *level;
    1.26 -static OrbitCamera cam;
    1.27 +
    1.28 +static FpsCamera cam;
    1.29 +static bool keystate[256];
    1.30  
    1.31  static const char *level_file = "0.level";
    1.32  
    1.33 @@ -33,9 +39,11 @@
    1.34  	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
    1.35  	glutCreateWindow("prototype");
    1.36  
    1.37 +	glutIdleFunc(idle);
    1.38  	glutDisplayFunc(disp);
    1.39  	glutReshapeFunc(reshape);
    1.40  	glutKeyboardFunc(keyb);
    1.41 +	glutKeyboardUpFunc(key_release);
    1.42  	glutMouseFunc(mouse);
    1.43  	glutMotionFunc(motion);
    1.44  
    1.45 @@ -70,25 +78,68 @@
    1.46  	set_active_tileset(tileset);
    1.47  
    1.48  	level = new Level;
    1.49 +	printf("loading level: %s\n", level_file);
    1.50  	if(!level->load(datafile_path(level_file))) {
    1.51  		return false;
    1.52  	}
    1.53  
    1.54 +	cam.input_move(0, 0.5, 0);
    1.55 +
    1.56  	return true;
    1.57  }
    1.58  
    1.59 +void idle()
    1.60 +{
    1.61 +	glutPostRedisplay();
    1.62 +}
    1.63 +
    1.64  void disp()
    1.65  {
    1.66 +	update(glutGet(GLUT_ELAPSED_TIME));
    1.67 +
    1.68  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.69  
    1.70  	glMatrixMode(GL_MODELVIEW);
    1.71  	glLoadIdentity();
    1.72 -	cam.use();
    1.73 +	cam.use_inverse();
    1.74  
    1.75  	level->draw();
    1.76  
    1.77  	glutSwapBuffers();
    1.78  	assert(glGetError() == GL_NO_ERROR);
    1.79 +
    1.80 +	usleep(10000);
    1.81 +}
    1.82 +
    1.83 +void update(unsigned long msec)
    1.84 +{
    1.85 +	static unsigned long last_upd;
    1.86 +
    1.87 +	if(last_upd == 0) {
    1.88 +		last_upd = msec;
    1.89 +	}
    1.90 +	float dt = (float)(msec - last_upd) / 1000.0;
    1.91 +
    1.92 +	float offs = 2.0 * dt;
    1.93 +	float dx = 0, dy = 0;
    1.94 +
    1.95 +	// handle key input
    1.96 +	if(keystate['w'] || keystate['W']) {
    1.97 +		dy -= offs;
    1.98 +	}
    1.99 +	if(keystate['s'] || keystate['S']) {
   1.100 +		dy += offs;
   1.101 +	}
   1.102 +	if(keystate['d'] || keystate['D']) {
   1.103 +		dx += offs;
   1.104 +	}
   1.105 +	if(keystate['a'] || keystate['A']) {
   1.106 +		dx -= offs;
   1.107 +	}
   1.108 +
   1.109 +	cam.input_move(dx, 0, dy);
   1.110 +
   1.111 +	last_upd = msec;
   1.112  }
   1.113  
   1.114  void reshape(int x, int y)
   1.115 @@ -96,7 +147,7 @@
   1.116  	glViewport(0, 0, x, y);
   1.117  	glMatrixMode(GL_PROJECTION);
   1.118  	glLoadIdentity();
   1.119 -	gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
   1.120 +	gluPerspective(45.0, (float)x / (float)y, 0.25, 100.0);
   1.121  }
   1.122  
   1.123  void keyb(unsigned char key, int x, int y)
   1.124 @@ -105,6 +156,13 @@
   1.125  	case 27:
   1.126  		exit(0);
   1.127  	}
   1.128 +
   1.129 +	keystate[key] = true;
   1.130 +}
   1.131 +
   1.132 +void key_release(unsigned char key, int x, int y)
   1.133 +{
   1.134 +	keystate[key] = false;
   1.135  }
   1.136  
   1.137  static int prev_x, prev_y;
   1.138 @@ -125,7 +183,7 @@
   1.139  	prev_y = y;
   1.140  
   1.141  	if(bnstate[0]) {
   1.142 -		cam.input_rotate(dx * 0.01, dy * 0.01, 0);
   1.143 +		cam.input_rotate(dy * 0.01, dx * 0.01, 0);
   1.144  		glutPostRedisplay();
   1.145  	}
   1.146  	if(bnstate[2]) {