dungeon_crawler

diff prototype/src/main.cc @ 1:96de911d05d4

started a rough prototype
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 28 Jun 2012 06:05:50 +0300
parents
children 252a00508411
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/prototype/src/main.cc	Thu Jun 28 06:05:50 2012 +0300
     1.3 @@ -0,0 +1,105 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <assert.h>
     1.7 +#include "opengl.h"
     1.8 +#include "level.h"
     1.9 +#include "camera.h"
    1.10 +
    1.11 +void disp();
    1.12 +void reshape(int x, int y);
    1.13 +void keyb(unsigned char key, int x, int y);
    1.14 +void mouse(int bn, int state, int x, int y);
    1.15 +void motion(int x, int y);
    1.16 +
    1.17 +static Level *level;
    1.18 +static OrbitCamera cam;
    1.19 +
    1.20 +int main(int argc, char **argv)
    1.21 +{
    1.22 +	glutInit(&argc, argv);
    1.23 +	glutInitWindowSize(800, 600);
    1.24 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
    1.25 +	glutCreateWindow("prototype");
    1.26 +
    1.27 +	glutDisplayFunc(disp);
    1.28 +	glutReshapeFunc(reshape);
    1.29 +	glutKeyboardFunc(keyb);
    1.30 +	glutMouseFunc(mouse);
    1.31 +	glutMotionFunc(motion);
    1.32 +
    1.33 +	glewInit();
    1.34 +
    1.35 +	glEnable(GL_LIGHTING);
    1.36 +	glEnable(GL_LIGHT0);
    1.37 +	float ldir[] = {-1, 1, 2, 0};
    1.38 +	glLightfv(GL_LIGHT0, GL_POSITION, ldir);
    1.39 +	glEnable(GL_NORMALIZE);
    1.40 +
    1.41 +	glEnable(GL_DEPTH_TEST);
    1.42 +	glEnable(GL_CULL_FACE);
    1.43 +	glEnable(GL_MULTISAMPLE);
    1.44 +
    1.45 +	level = new Level;
    1.46 +	if(!level->load("foobar")) {
    1.47 +		return 1;
    1.48 +	}
    1.49 +
    1.50 +	glutMainLoop();
    1.51 +}
    1.52 +
    1.53 +void disp()
    1.54 +{
    1.55 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.56 +
    1.57 +	glMatrixMode(GL_MODELVIEW);
    1.58 +	glLoadIdentity();
    1.59 +	cam.use();
    1.60 +
    1.61 +	level->draw();
    1.62 +
    1.63 +	glutSwapBuffers();
    1.64 +	assert(glGetError() == GL_NO_ERROR);
    1.65 +}
    1.66 +
    1.67 +void reshape(int x, int y)
    1.68 +{
    1.69 +	glViewport(0, 0, x, y);
    1.70 +	glMatrixMode(GL_PROJECTION);
    1.71 +	glLoadIdentity();
    1.72 +	gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
    1.73 +}
    1.74 +
    1.75 +void keyb(unsigned char key, int x, int y)
    1.76 +{
    1.77 +	switch(key) {
    1.78 +	case 27:
    1.79 +		exit(0);
    1.80 +	}
    1.81 +}
    1.82 +
    1.83 +static int prev_x, prev_y;
    1.84 +static bool bnstate[32];
    1.85 +
    1.86 +void mouse(int bn, int state, int x, int y)
    1.87 +{
    1.88 +	prev_x = x;
    1.89 +	prev_y = y;
    1.90 +	bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
    1.91 +}
    1.92 +
    1.93 +void motion(int x, int y)
    1.94 +{
    1.95 +	int dx = x - prev_x;
    1.96 +	int dy = y - prev_y;
    1.97 +	prev_x = x;
    1.98 +	prev_y = y;
    1.99 +
   1.100 +	if(bnstate[0]) {
   1.101 +		cam.input_rotate(dx * 0.01, dy * 0.01, 0);
   1.102 +		glutPostRedisplay();
   1.103 +	}
   1.104 +	if(bnstate[2]) {
   1.105 +		cam.input_zoom(dy * 0.1);
   1.106 +		glutPostRedisplay();
   1.107 +	}
   1.108 +}