dungeon_crawler

view prototype/src/main.cc @ 16:91180ee7b7d9

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 20 Aug 2012 03:40:51 +0300
parents 3a3236a4833c
children d98240a13793
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"
10 #include "renderer.h"
12 bool init();
13 void cleanup();
14 void idle();
15 void disp();
16 void draw();
17 void update(unsigned long msec);
18 void reshape(int x, int y);
19 void keyb(unsigned char key, int x, int y);
20 void key_release(unsigned char key, int x, int y);
21 void mouse(int bn, int state, int x, int y);
22 void motion(int x, int y);
24 static TileSet *tileset;
25 static Level *level;
27 static FpsCamera cam;
28 static bool keystate[256];
30 static const char *level_file = "0.level";
32 int main(int argc, char **argv)
33 {
34 glutInit(&argc, argv);
36 if(argc > 1) {
37 level_file = argv[1];
38 }
40 glutInitWindowSize(800, 600);
41 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
42 glutCreateWindow("prototype");
44 glutIdleFunc(idle);
45 glutDisplayFunc(disp);
46 glutReshapeFunc(reshape);
47 glutKeyboardFunc(keyb);
48 glutKeyboardUpFunc(key_release);
49 glutMouseFunc(mouse);
50 glutMotionFunc(motion);
52 glewInit();
54 if(!init()) {
55 return 1;
56 }
58 glutMainLoop();
59 }
61 bool init()
62 {
63 glEnable(GL_LIGHTING);
64 glEnable(GL_LIGHT0);
65 float ldir[] = {-1, 1, 2, 0};
66 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
67 glEnable(GL_NORMALIZE);
69 glEnable(GL_DEPTH_TEST);
70 glEnable(GL_CULL_FACE);
71 glEnable(GL_MULTISAMPLE);
73 add_data_path("data");
74 add_data_path("sdr");
76 if(!init_renderer()) {
77 return false;
78 }
80 // load a tileset
81 tileset = new TileSet;
82 if(!tileset->load(datafile_path("default.tileset"))) {
83 return false;
84 }
85 set_active_tileset(tileset);
87 level = new Level;
88 printf("loading level: %s\n", level_file);
89 if(!level->load(datafile_path(level_file))) {
90 return false;
91 }
93 cam.input_move(0, 0.5, 0);
95 return true;
96 }
98 void cleanup()
99 {
100 delete level;
101 delete tileset;
103 destroy_renderer();
104 }
106 void idle()
107 {
108 glutPostRedisplay();
109 }
111 void disp()
112 {
113 update(glutGet(GLUT_ELAPSED_TIME));
115 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
117 glMatrixMode(GL_MODELVIEW);
118 glLoadIdentity();
119 cam.use_inverse();
121 render_deferred(draw);
123 glutSwapBuffers();
124 assert(glGetError() == GL_NO_ERROR);
126 usleep(10000);
127 }
129 void draw()
130 {
131 level->draw();
132 }
134 void update(unsigned long msec)
135 {
136 static unsigned long last_upd;
138 if(last_upd == 0) {
139 last_upd = msec;
140 }
141 float dt = (float)(msec - last_upd) / 1000.0;
143 float offs = 2.5 * dt;
144 float dx = 0, dy = 0;
146 // handle key input
147 if(keystate['w'] || keystate['W']) {
148 dy -= offs;
149 }
150 if(keystate['s'] || keystate['S']) {
151 dy += offs;
152 }
153 if(keystate['d'] || keystate['D']) {
154 dx += offs;
155 }
156 if(keystate['a'] || keystate['A']) {
157 dx -= offs;
158 }
160 cam.input_move(dx, 0, dy);
162 last_upd = msec;
163 }
165 void reshape(int x, int y)
166 {
167 glViewport(0, 0, x, y);
168 glMatrixMode(GL_PROJECTION);
169 glLoadIdentity();
170 gluPerspective(45.0, (float)x / (float)y, 0.25, 100.0);
171 }
173 void keyb(unsigned char key, int x, int y)
174 {
175 switch(key) {
176 case 27:
177 exit(0);
178 }
180 keystate[key] = true;
181 }
183 void key_release(unsigned char key, int x, int y)
184 {
185 keystate[key] = false;
186 }
188 static int prev_x, prev_y;
189 static bool bnstate[32];
191 void mouse(int bn, int state, int x, int y)
192 {
193 prev_x = x;
194 prev_y = y;
195 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
196 }
198 void motion(int x, int y)
199 {
200 int dx = x - prev_x;
201 int dy = y - prev_y;
202 prev_x = x;
203 prev_y = y;
205 if(bnstate[0]) {
206 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
207 glutPostRedisplay();
208 }
209 if(bnstate[2]) {
210 cam.input_zoom(dy * 0.1);
211 glutPostRedisplay();
212 }
213 }