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