dungeon_crawler

view prototype/src/level.cc @ 50:c40efa9cf844

torches sound
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 18 Sep 2012 04:45:46 +0300
parents 303743485aba
children d57df51f6b50
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 }
72 if(isalpha(buf[i]) || buf[i] == ' ') {
73 GridCell *cell = new GridCell(deftile);
74 *grid_row = cell;
75 cell_list.push_back(*grid_row);
76 }
77 grid_row++;
78 }
80 if(++y >= ysz) {
81 break;
82 }
83 }
84 fclose(fp);
86 for(int i=0; i<ysz; i++) {
87 for(int j=0; j<xsz; j++) {
88 GridCell *cell = get_cell(j, i);
89 if(!cell) {
90 continue;
91 }
93 // find the adjacency mask of this cell and store it
94 unsigned int adjmask = get_cell_dirmask(j, i);
95 cell->set_adj_mask(adjmask);
97 // add any audio sources in this grid-cell to the level static audio manager
98 std::list<Tile::AudioSourceDesc> asrc = cell->get_audio_sources();
99 for(auto sdesc : asrc) {
100 if(sdesc.dirmask & adjmask) {
101 AudioSource *s = new AudioSource;
102 s->set_sample(sdesc.sample);
103 s->set_position(sdesc.pos + get_cell_pos(i, y));
104 austatic.add_source(s);
105 }
106 }
107 }
108 }
110 return true;
111 }
113 bool Level::save(const char *fname) const
114 {
115 return false;
116 }
118 GridCell *Level::get_cell(int x, int y)
119 {
120 if(x < 0 || x >= xsz || y < 0 || y >= ysz) {
121 return 0;
122 }
123 return cells[y * xsz + x];
124 }
126 const GridCell *Level::get_cell(int x, int y) const
127 {
128 if(x < 0 || x >= xsz || y < 0 || y >= ysz) {
129 return 0;
130 }
131 return cells[y * xsz + x];
132 }
134 Vector3 Level::get_cell_pos(int x, int y) const
135 {
136 float posx = (float)x * cell_size;
137 float posy = (float)y * cell_size;
138 posx -= cell_size * (float)xsz / 2.0f;
139 posy -= cell_size * (float)ysz / 2.0f;
140 return Vector3(posx, 0, posy);
141 }
143 void Level::get_cell_coords_at(const Vector3 &pos, int *xptr, int *yptr) const
144 {
145 float posx = pos.x + cell_size * (float)xsz / 2.0f;
146 float posy = pos.z + cell_size * (float)ysz / 2.0f;
147 *xptr = (int)round(posx / cell_size);
148 *yptr = (int)round(posy / cell_size);
149 }
151 unsigned int Level::get_cell_dirmask(int x, int y) const
152 {
153 unsigned int dmask = TILE_ALL;
154 if(y > 0 && get_cell(x, y - 1)) {
155 dmask &= ~TILE_NORTH;
156 }
157 if(y < ysz - 1 && get_cell(x, y + 1)) {
158 dmask &= ~TILE_SOUTH;
159 }
160 if(x > 0 && get_cell(x - 1, y)) {
161 dmask &= ~TILE_WEST;
162 }
163 if(x < xsz - 1 && get_cell(x + 1, y)) {
164 dmask &= ~TILE_EAST;
165 }
166 return dmask;
167 }
169 void Level::set_player_position(const Vector3 &ppos)
170 {
171 player_pos = ppos;
172 }
174 void Level::update(unsigned long msec, float dt)
175 {
176 for(auto cell : cell_list) {
177 cell->update(msec, dt);
178 }
180 // activate the closest audio sources
181 if(cfg.sound) {
182 austatic.active_range(player_pos, 2.0);
183 }
184 }
186 void Level::draw() const
187 {
188 glMatrixMode(GL_MODELVIEW);
190 for(int i=0; i<ysz; i++) {
191 for(int j=0; j<xsz; j++) {
192 const GridCell *cell = get_cell(j, i);
193 if(cell) {
194 Vector3 pos = get_cell_pos(j, i);
195 glPushMatrix();
196 glTranslatef(pos.x, pos.y, pos.z);
197 glScalef(cell_size, cell_size, cell_size);
199 unsigned int dmask = get_cell_dirmask(j, i);
201 cell->draw(dmask);
202 glPopMatrix();
203 }
204 }
205 }
206 }
208 void Level::draw_lights() const
209 {
210 glMatrixMode(GL_MODELVIEW);
212 for(int i=0; i<ysz; i++) {
213 for(int j=0; j<xsz; j++) {
214 const GridCell *cell = get_cell(j, i);
215 if(cell) {
216 Vector3 pos = get_cell_pos(j, i);
218 glPushMatrix();
219 glTranslatef(pos.x, pos.y, pos.z);
220 glScalef(cell_size, cell_size, cell_size);
222 unsigned int dmask = get_cell_dirmask(j, i);
223 cell->draw_lights(dmask);
225 glPopMatrix();
226 }
227 }
228 }
229 }
231 void Level::draw_post() const
232 {
233 glMatrixMode(GL_MODELVIEW);
235 for(int i=0; i<ysz; i++) {
236 for(int j=0; j<xsz; j++) {
237 const GridCell *cell = get_cell(j, i);
238 if(cell) {
239 Vector3 pos = get_cell_pos(j, i);
240 glPushMatrix();
241 glTranslatef(pos.x, pos.y, pos.z);
242 glScalef(cell_size, cell_size, cell_size);
244 unsigned int dmask = get_cell_dirmask(j, i);
246 cell->draw_post(dmask);
247 glPopMatrix();
248 }
249 }
250 }
251 }
253 void Level::draw_grid() const
254 {
255 float xlen = xsz * cell_size;
256 float ylen = ysz * cell_size;
258 glPushAttrib(GL_ENABLE_BIT);
259 glDisable(GL_LIGHTING);
261 glBegin(GL_LINES);
262 glColor3f(0.4, 0.4, 0.4);
264 float y = -ylen / 2.0 - cell_size / 2.0;
265 for(int i=0; i<ysz; i++) {
266 glVertex3f(-xlen / 2.0, 0, y);
267 glVertex3f(xlen / 2.0, 0, y);
268 y += cell_size;
269 }
271 float x = -xlen / 2.0 - cell_size / 2.0;
272 for(int i=0; i<xsz; i++) {
273 glVertex3f(x, 0, -ylen / 2.0);
274 glVertex3f(x, 0, ylen / 2.0);
275 x += cell_size;
276 }
277 glEnd();
279 glPopAttrib();
280 }
283 AudioSample *Level::get_sample(int x, int y, int which) const
284 {
285 const GridCell *cell = get_cell(x, y);
286 if(!cell) {
287 return 0;
288 }
289 return cell->get_sample(which);
290 }
293 GridCell::GridCell(Tile *tile)
294 {
295 if(tile) {
296 add_tile(tile);
297 }
299 adjmask = TILE_ALL;
300 }
302 void GridCell::set_adj_mask(unsigned int mask)
303 {
304 adjmask = mask;
305 }
307 unsigned int GridCell::get_adj_mask() const
308 {
309 return adjmask;
310 }
312 void GridCell::add_tile(Tile *tile)
313 {
314 if(!tile) {
315 return;
316 }
318 tiles.push_back(tile);
320 /* instanciate any particle systems */
321 int num_psattr = tile->get_unique_psys_count();
323 for(int i=0; i<num_psattr; i++) {
324 const struct psys_attributes *psattr = tile->get_unique_psys(i);
325 struct psys_emitter *emitter = psys_create();
326 emitter->attr = *psattr;
327 psys.push_back(emitter);
328 }
329 }
331 void GridCell::update(unsigned long msec, float dt)
332 {
333 for(auto ps : psys) {
334 psys_update(ps, (float)msec / 1000.0f);
335 }
336 }
338 void GridCell::draw(unsigned int draw_mask) const
339 {
340 for(auto tile : tiles) {
341 tile->draw(draw_mask);
342 }
343 }
345 void GridCell::draw_lights(unsigned int draw_mask) const
346 {
347 for(auto tile : tiles) {
348 tile->draw_lights(draw_mask);
349 }
350 }
352 void GridCell::draw_post(unsigned int draw_mask) const
353 {
354 for(auto tile : tiles) {
355 tile->draw_post(draw_mask);
356 }
357 for(auto ps : psys) {
358 psys_draw(ps);
359 }
360 }
362 AudioSample *GridCell::get_sample(int which) const
363 {
364 for(auto tile : tiles) {
365 AudioSample *s = tile->get_sample(which);
366 if(s) {
367 return s;
368 }
369 }
370 return 0;
371 }
373 std::list<Tile::AudioSourceDesc> GridCell::get_audio_sources() const
374 {
375 std::list<Tile::AudioSourceDesc> srclist;
377 for(auto tile : tiles) {
378 int num_src = tile->get_audio_source_count();
379 for(int i=0; i<num_src; i++) {
380 srclist.push_back(tile->get_audio_source(i));
381 }
382 }
383 return srclist;
384 }