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