dungeon_crawler

view 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 source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include "opengl.h"
5 #include "level.h"
6 #include "camera.h"
8 void disp();
9 void reshape(int x, int y);
10 void keyb(unsigned char key, int x, int y);
11 void mouse(int bn, int state, int x, int y);
12 void motion(int x, int y);
14 static Level *level;
15 static OrbitCamera cam;
17 int main(int argc, char **argv)
18 {
19 glutInit(&argc, argv);
20 glutInitWindowSize(800, 600);
21 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
22 glutCreateWindow("prototype");
24 glutDisplayFunc(disp);
25 glutReshapeFunc(reshape);
26 glutKeyboardFunc(keyb);
27 glutMouseFunc(mouse);
28 glutMotionFunc(motion);
30 glewInit();
32 glEnable(GL_LIGHTING);
33 glEnable(GL_LIGHT0);
34 float ldir[] = {-1, 1, 2, 0};
35 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
36 glEnable(GL_NORMALIZE);
38 glEnable(GL_DEPTH_TEST);
39 glEnable(GL_CULL_FACE);
40 glEnable(GL_MULTISAMPLE);
42 level = new Level;
43 if(!level->load("foobar")) {
44 return 1;
45 }
47 glutMainLoop();
48 }
50 void disp()
51 {
52 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
54 glMatrixMode(GL_MODELVIEW);
55 glLoadIdentity();
56 cam.use();
58 level->draw();
60 glutSwapBuffers();
61 assert(glGetError() == GL_NO_ERROR);
62 }
64 void reshape(int x, int y)
65 {
66 glViewport(0, 0, x, y);
67 glMatrixMode(GL_PROJECTION);
68 glLoadIdentity();
69 gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0);
70 }
72 void keyb(unsigned char key, int x, int y)
73 {
74 switch(key) {
75 case 27:
76 exit(0);
77 }
78 }
80 static int prev_x, prev_y;
81 static bool bnstate[32];
83 void mouse(int bn, int state, int x, int y)
84 {
85 prev_x = x;
86 prev_y = y;
87 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
88 }
90 void motion(int x, int y)
91 {
92 int dx = x - prev_x;
93 int dy = y - prev_y;
94 prev_x = x;
95 prev_y = y;
97 if(bnstate[0]) {
98 cam.input_rotate(dx * 0.01, dy * 0.01, 0);
99 glutPostRedisplay();
100 }
101 if(bnstate[2]) {
102 cam.input_zoom(dy * 0.1);
103 glutPostRedisplay();
104 }
105 }