dungeon_crawler
view prototype/src/main.cc @ 9:b10ba85f75e0
lalala
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 17 Aug 2012 17:25:42 +0300 |
parents | 8fb37db44fd8 |
children | 3a3236a4833c |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <unistd.h>
5 #include "opengl.h"
6 #include "level.h"
7 #include "camera.h"
8 #include "datapath.h"
9 #include "tileset.h"
11 bool init();
12 void cleanup();
13 void idle();
14 void disp();
15 void update(unsigned long msec);
16 void reshape(int x, int y);
17 void keyb(unsigned char key, int x, int y);
18 void key_release(unsigned char key, int x, int y);
19 void mouse(int bn, int state, int x, int y);
20 void motion(int x, int y);
22 static TileSet *tileset;
23 static Level *level;
25 static FpsCamera cam;
26 static bool keystate[256];
28 static const char *level_file = "0.level";
30 int main(int argc, char **argv)
31 {
32 glutInit(&argc, argv);
34 if(argc > 1) {
35 level_file = argv[1];
36 }
38 glutInitWindowSize(800, 600);
39 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
40 glutCreateWindow("prototype");
42 glutIdleFunc(idle);
43 glutDisplayFunc(disp);
44 glutReshapeFunc(reshape);
45 glutKeyboardFunc(keyb);
46 glutKeyboardUpFunc(key_release);
47 glutMouseFunc(mouse);
48 glutMotionFunc(motion);
50 glewInit();
52 if(!init()) {
53 return 1;
54 }
56 glutMainLoop();
57 }
59 bool init()
60 {
61 glEnable(GL_LIGHTING);
62 glEnable(GL_LIGHT0);
63 float ldir[] = {-1, 1, 2, 0};
64 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
65 glEnable(GL_NORMALIZE);
67 glEnable(GL_DEPTH_TEST);
68 glEnable(GL_CULL_FACE);
69 glEnable(GL_MULTISAMPLE);
71 add_data_path("data");
73 // load a tileset
74 tileset = new TileSet;
75 if(!tileset->load(datafile_path("default.tileset"))) {
76 return false;
77 }
78 set_active_tileset(tileset);
80 level = new Level;
81 printf("loading level: %s\n", level_file);
82 if(!level->load(datafile_path(level_file))) {
83 return false;
84 }
86 cam.input_move(0, 0.5, 0);
88 return true;
89 }
91 void idle()
92 {
93 glutPostRedisplay();
94 }
96 void disp()
97 {
98 update(glutGet(GLUT_ELAPSED_TIME));
100 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
102 glMatrixMode(GL_MODELVIEW);
103 glLoadIdentity();
104 cam.use_inverse();
106 level->draw();
108 glutSwapBuffers();
109 assert(glGetError() == GL_NO_ERROR);
111 usleep(10000);
112 }
114 void update(unsigned long msec)
115 {
116 static unsigned long last_upd;
118 if(last_upd == 0) {
119 last_upd = msec;
120 }
121 float dt = (float)(msec - last_upd) / 1000.0;
123 float offs = 2.5 * dt;
124 float dx = 0, dy = 0;
126 // handle key input
127 if(keystate['w'] || keystate['W']) {
128 dy -= offs;
129 }
130 if(keystate['s'] || keystate['S']) {
131 dy += offs;
132 }
133 if(keystate['d'] || keystate['D']) {
134 dx += offs;
135 }
136 if(keystate['a'] || keystate['A']) {
137 dx -= offs;
138 }
140 cam.input_move(dx, 0, dy);
142 last_upd = msec;
143 }
145 void reshape(int x, int y)
146 {
147 glViewport(0, 0, x, y);
148 glMatrixMode(GL_PROJECTION);
149 glLoadIdentity();
150 gluPerspective(45.0, (float)x / (float)y, 0.25, 100.0);
151 }
153 void keyb(unsigned char key, int x, int y)
154 {
155 switch(key) {
156 case 27:
157 exit(0);
158 }
160 keystate[key] = true;
161 }
163 void key_release(unsigned char key, int x, int y)
164 {
165 keystate[key] = false;
166 }
168 static int prev_x, prev_y;
169 static bool bnstate[32];
171 void mouse(int bn, int state, int x, int y)
172 {
173 prev_x = x;
174 prev_y = y;
175 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
176 }
178 void motion(int x, int y)
179 {
180 int dx = x - prev_x;
181 int dy = y - prev_y;
182 prev_x = x;
183 prev_y = y;
185 if(bnstate[0]) {
186 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
187 glutPostRedisplay();
188 }
189 if(bnstate[2]) {
190 cam.input_zoom(dy * 0.1);
191 glutPostRedisplay();
192 }
193 }