dungeon_crawler
view prototype/src/main.cc @ 15:3a3236a4833c
adding shaders and renderer abstraction
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 19 Aug 2012 23:09:30 +0300 |
parents | b10ba85f75e0 |
children | 91180ee7b7d9 |
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");
75 if(!init_renderer()) {
76 return false;
77 }
79 // load a tileset
80 tileset = new TileSet;
81 if(!tileset->load(datafile_path("default.tileset"))) {
82 return false;
83 }
84 set_active_tileset(tileset);
86 level = new Level;
87 printf("loading level: %s\n", level_file);
88 if(!level->load(datafile_path(level_file))) {
89 return false;
90 }
92 cam.input_move(0, 0.5, 0);
94 return true;
95 }
97 void cleanup()
98 {
99 delete level;
100 delete tileset;
102 destroy_renderer();
103 }
105 void idle()
106 {
107 glutPostRedisplay();
108 }
110 void disp()
111 {
112 update(glutGet(GLUT_ELAPSED_TIME));
114 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
116 glMatrixMode(GL_MODELVIEW);
117 glLoadIdentity();
118 cam.use_inverse();
120 render_deferred(draw);
122 glutSwapBuffers();
123 assert(glGetError() == GL_NO_ERROR);
125 usleep(10000);
126 }
128 void draw()
129 {
130 level->draw();
131 }
133 void update(unsigned long msec)
134 {
135 static unsigned long last_upd;
137 if(last_upd == 0) {
138 last_upd = msec;
139 }
140 float dt = (float)(msec - last_upd) / 1000.0;
142 float offs = 2.5 * dt;
143 float dx = 0, dy = 0;
145 // handle key input
146 if(keystate['w'] || keystate['W']) {
147 dy -= offs;
148 }
149 if(keystate['s'] || keystate['S']) {
150 dy += offs;
151 }
152 if(keystate['d'] || keystate['D']) {
153 dx += offs;
154 }
155 if(keystate['a'] || keystate['A']) {
156 dx -= offs;
157 }
159 cam.input_move(dx, 0, dy);
161 last_upd = msec;
162 }
164 void reshape(int x, int y)
165 {
166 glViewport(0, 0, x, y);
167 glMatrixMode(GL_PROJECTION);
168 glLoadIdentity();
169 gluPerspective(45.0, (float)x / (float)y, 0.25, 100.0);
170 }
172 void keyb(unsigned char key, int x, int y)
173 {
174 switch(key) {
175 case 27:
176 exit(0);
177 }
179 keystate[key] = true;
180 }
182 void key_release(unsigned char key, int x, int y)
183 {
184 keystate[key] = false;
185 }
187 static int prev_x, prev_y;
188 static bool bnstate[32];
190 void mouse(int bn, int state, int x, int y)
191 {
192 prev_x = x;
193 prev_y = y;
194 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
195 }
197 void motion(int x, int y)
198 {
199 int dx = x - prev_x;
200 int dy = y - prev_y;
201 prev_x = x;
202 prev_y = y;
204 if(bnstate[0]) {
205 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
206 glutPostRedisplay();
207 }
208 if(bnstate[2]) {
209 cam.input_zoom(dy * 0.1);
210 glutPostRedisplay();
211 }
212 }