dungeon_crawler
view prototype/src/level.cc @ 63:7f52d6310317
fixed design issue with datafile_path
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 02 Oct 2012 04:52:59 +0300 |
parents | d57df51f6b50 |
children |
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include <errno.h>
4 #include "opengl.h"
5 #include "level.h"
6 #include "tile.h"
7 #include "tileset.h"
8 #include "cfg.h"
10 Level::Level()
11 {
12 cell_size = 1.0;
14 cells = 0;
15 xsz = ysz = 0;
16 }
18 Level::~Level()
19 {
20 delete [] cells;
22 for(auto cell : cell_list) {
23 delete cell;
24 }
25 }
27 bool Level::load(const char *fname)
28 {
29 if(!fname) {
30 return false;
31 }
33 TileSet *tileset = get_active_tileset();
34 if(!tileset) {
35 fprintf(stderr, "level loading failed: no active tileset\n");
36 return false;
37 }
38 Tile *deftile = tileset->get_tile("default");
39 if(!deftile) {
40 fprintf(stderr, "level loading failed: active tileset has no default tile\n");
41 return false;
42 }
44 FILE *fp = fopen(fname, "r");
45 if(!fp) {
46 fprintf(stderr, "failed to open level: %s: %s\n", fname, strerror(errno));
47 return false;
48 }
50 char buf[512];
51 fgets(buf, sizeof buf, fp);
52 if(sscanf(buf, "SIZE %dx%d", &xsz, &ysz) != 2) {
53 fprintf(stderr, "invalid or corrupt level file: %s\n", fname);
54 fclose(fp);
55 return false;
56 }
57 printf("level size: %dx%d\n", xsz, ysz);
59 cells = new GridCell*[xsz * ysz];
60 memset(cells, 0, xsz * ysz * sizeof *cells);
62 int y = 0;
63 GridCell **grid_row = cells;
65 while(fgets(buf, sizeof buf, fp)) {
66 for(int i=0; i<xsz; i++) {
67 if(!buf[i] || buf[i] == '\r' || buf[i] == '\n') {
68 grid_row += xsz - i;
69 break;
70 }
73 if(buf[i] != '#') {
74 GridCell *cell = new GridCell(deftile);
75 *grid_row = cell;
76 cell_list.push_back(*grid_row);
78 Tile *tile;
80 switch(buf[i]) {
81 case '*':
82 if(!(tile = tileset->get_tile("light"))) {
83 fprintf(stderr, "failed to add light tile\n");
84 }
85 break;
87 default:
88 tile = 0;
89 break;
90 }
92 if(tile) {
93 cell->add_tile(tile);
94 }
95 }
96 grid_row++;
97 }
99 if(++y >= ysz) {
100 break;
101 }
102 }
103 fclose(fp);
105 for(int i=0; i<ysz; i++) {
106 for(int j=0; j<xsz; j++) {
107 GridCell *cell = get_cell(j, i);
108 if(!cell) {
109 continue;
110 }
112 // find the adjacency mask of this cell and store it
113 unsigned int adjmask = get_cell_dirmask(j, i);
114 cell->set_adj_mask(adjmask);
116 // add any audio sources in this grid-cell to the level static audio manager
117 std::list<Tile::AudioSourceDesc> asrc = cell->get_audio_sources();
118 for(auto sdesc : asrc) {
119 if(sdesc.dirmask & adjmask) {
120 Vector3 pos = sdesc.pos + get_cell_pos(j, i);
122 AudioSource *s = new AudioSource;
123 s->set_sample(sdesc.sample);
124 s->set_position(pos);
125 s->set_volume(sdesc.volume);
126 s->set_reference_dist(sdesc.ref_dist);
127 //s->set_rolloff(1.0);
128 austatic.add_source(s);
129 }
130 }
131 }
132 }
134 return true;
135 }
137 bool Level::save(const char *fname) const
138 {
139 return false;
140 }
142 GridCell *Level::get_cell(int x, int y)
143 {
144 if(x < 0 || x >= xsz || y < 0 || y >= ysz) {
145 return 0;
146 }
147 return cells[y * xsz + x];
148 }
150 const GridCell *Level::get_cell(int x, int y) const
151 {
152 if(x < 0 || x >= xsz || y < 0 || y >= ysz) {
153 return 0;
154 }
155 return cells[y * xsz + x];
156 }
158 Vector3 Level::get_cell_pos(int x, int y) const
159 {
160 float posx = (float)x * cell_size;
161 float posy = (float)y * cell_size;
162 posx -= cell_size * (float)xsz / 2.0f;
163 posy -= cell_size * (float)ysz / 2.0f;
164 return Vector3(posx, 0, posy);
165 }
167 void Level::get_cell_coords_at(const Vector3 &pos, int *xptr, int *yptr) const
168 {
169 float posx = pos.x + cell_size * (float)xsz / 2.0f;
170 float posy = pos.z + cell_size * (float)ysz / 2.0f;
171 *xptr = (int)round(posx / cell_size);
172 *yptr = (int)round(posy / cell_size);
173 }
175 unsigned int Level::get_cell_dirmask(int x, int y) const
176 {
177 unsigned int dmask = TILE_ALL;
178 if(y > 0 && get_cell(x, y - 1)) {
179 dmask &= ~TILE_NORTH;
180 }
181 if(y < ysz - 1 && get_cell(x, y + 1)) {
182 dmask &= ~TILE_SOUTH;
183 }
184 if(x > 0 && get_cell(x - 1, y)) {
185 dmask &= ~TILE_WEST;
186 }
187 if(x < xsz - 1 && get_cell(x + 1, y)) {
188 dmask &= ~TILE_EAST;
189 }
190 return dmask;
191 }
193 void Level::set_player_position(const Vector3 &ppos)
194 {
195 player_pos = ppos;
196 }
198 void Level::update(unsigned long msec, float dt)
199 {
200 for(auto cell : cell_list) {
201 cell->update(msec, dt);
202 }
204 // activate the closest audio sources
205 if(cfg.sound) {
206 austatic.active_range(player_pos, 2.0);
207 }
208 }
210 void Level::draw() const
211 {
212 glMatrixMode(GL_MODELVIEW);
214 for(int i=0; i<ysz; i++) {
215 for(int j=0; j<xsz; j++) {
216 const GridCell *cell = get_cell(j, i);
217 if(cell) {
218 Vector3 pos = get_cell_pos(j, i);
219 glPushMatrix();
220 glTranslatef(pos.x, pos.y, pos.z);
221 glScalef(cell_size, cell_size, cell_size);
223 unsigned int dmask = get_cell_dirmask(j, i);
225 cell->draw(dmask);
226 glPopMatrix();
227 }
228 }
229 }
230 }
232 void Level::draw_lights() const
233 {
234 glMatrixMode(GL_MODELVIEW);
236 for(int i=0; i<ysz; i++) {
237 for(int j=0; j<xsz; j++) {
238 const GridCell *cell = get_cell(j, i);
239 if(cell) {
240 Vector3 pos = get_cell_pos(j, i);
242 glPushMatrix();
243 glTranslatef(pos.x, pos.y, pos.z);
244 glScalef(cell_size, cell_size, cell_size);
246 unsigned int dmask = get_cell_dirmask(j, i);
247 cell->draw_lights(dmask);
249 glPopMatrix();
250 }
251 }
252 }
253 }
255 void Level::draw_post() const
256 {
257 glMatrixMode(GL_MODELVIEW);
259 for(int i=0; i<ysz; i++) {
260 for(int j=0; j<xsz; j++) {
261 const GridCell *cell = get_cell(j, i);
262 if(cell) {
263 Vector3 pos = get_cell_pos(j, i);
264 glPushMatrix();
265 glTranslatef(pos.x, pos.y, pos.z);
266 glScalef(cell_size, cell_size, cell_size);
268 unsigned int dmask = get_cell_dirmask(j, i);
270 cell->draw_post(dmask);
271 glPopMatrix();
272 }
273 }
274 }
275 }
277 void Level::draw_grid() const
278 {
279 float xlen = xsz * cell_size;
280 float ylen = ysz * cell_size;
282 glPushAttrib(GL_ENABLE_BIT);
283 glDisable(GL_LIGHTING);
285 glBegin(GL_LINES);
286 glColor3f(0.4, 0.4, 0.4);
288 float y = -ylen / 2.0 - cell_size / 2.0;
289 for(int i=0; i<ysz; i++) {
290 glVertex3f(-xlen / 2.0, 0, y);
291 glVertex3f(xlen / 2.0, 0, y);
292 y += cell_size;
293 }
295 float x = -xlen / 2.0 - cell_size / 2.0;
296 for(int i=0; i<xsz; i++) {
297 glVertex3f(x, 0, -ylen / 2.0);
298 glVertex3f(x, 0, ylen / 2.0);
299 x += cell_size;
300 }
301 glEnd();
303 glPopAttrib();
304 }
307 AudioSample *Level::get_sample(int x, int y, int which) const
308 {
309 const GridCell *cell = get_cell(x, y);
310 if(!cell) {
311 return 0;
312 }
313 return cell->get_sample(which);
314 }
317 GridCell::GridCell(Tile *tile)
318 {
319 if(tile) {
320 add_tile(tile);
321 }
323 adjmask = TILE_ALL;
324 }
326 void GridCell::set_adj_mask(unsigned int mask)
327 {
328 adjmask = mask;
329 }
331 unsigned int GridCell::get_adj_mask() const
332 {
333 return adjmask;
334 }
336 void GridCell::add_tile(Tile *tile)
337 {
338 if(!tile) {
339 return;
340 }
342 tiles.push_back(tile);
344 /* instanciate any particle systems */
345 int num_psattr = tile->get_unique_psys_count();
347 for(int i=0; i<num_psattr; i++) {
348 const struct psys_attributes *psattr = tile->get_unique_psys(i);
349 struct psys_emitter *emitter = psys_create();
350 emitter->attr = *psattr;
351 psys.push_back(emitter);
352 }
353 }
355 void GridCell::update(unsigned long msec, float dt)
356 {
357 for(auto ps : psys) {
358 psys_update(ps, (float)msec / 1000.0f);
359 }
360 }
362 void GridCell::draw(unsigned int draw_mask) const
363 {
364 for(auto tile : tiles) {
365 tile->draw(draw_mask);
366 }
367 }
369 void GridCell::draw_lights(unsigned int draw_mask) const
370 {
371 for(auto tile : tiles) {
372 tile->draw_lights(draw_mask);
373 }
374 }
376 void GridCell::draw_post(unsigned int draw_mask) const
377 {
378 for(auto tile : tiles) {
379 tile->draw_post(draw_mask);
380 }
381 for(auto ps : psys) {
382 psys_draw(ps);
383 }
384 }
386 AudioSample *GridCell::get_sample(int which) const
387 {
388 for(auto tile : tiles) {
389 AudioSample *s = tile->get_sample(which);
390 if(s) {
391 return s;
392 }
393 }
394 return 0;
395 }
397 std::list<Tile::AudioSourceDesc> GridCell::get_audio_sources() const
398 {
399 std::list<Tile::AudioSourceDesc> srclist;
401 for(auto tile : tiles) {
402 int num_src = tile->get_audio_source_count();
403 for(int i=0; i<num_src; i++) {
404 srclist.push_back(tile->get_audio_source(i));
405 }
406 }
407 return srclist;
408 }