dungeon_crawler

view prototype/src/level.cc @ 53:1ea56011c1ff

stuff + streaming start
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 19 Sep 2012 01:08:41 +0300
parents c40efa9cf844
children 0d4061b1e6a1
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 Vector3 pos = sdesc.pos + get_cell_pos(j, i);
103 AudioSource *s = new AudioSource;
104 s->set_sample(sdesc.sample);
105 s->set_position(pos);
106 s->set_volume(sdesc.volume);
107 s->set_reference_dist(sdesc.ref_dist);
108 //s->set_rolloff(1.0);
109 austatic.add_source(s);
110 }
111 }
112 }
113 }
115 return true;
116 }
118 bool Level::save(const char *fname) const
119 {
120 return false;
121 }
123 GridCell *Level::get_cell(int x, int y)
124 {
125 if(x < 0 || x >= xsz || y < 0 || y >= ysz) {
126 return 0;
127 }
128 return cells[y * xsz + x];
129 }
131 const GridCell *Level::get_cell(int x, int y) const
132 {
133 if(x < 0 || x >= xsz || y < 0 || y >= ysz) {
134 return 0;
135 }
136 return cells[y * xsz + x];
137 }
139 Vector3 Level::get_cell_pos(int x, int y) const
140 {
141 float posx = (float)x * cell_size;
142 float posy = (float)y * cell_size;
143 posx -= cell_size * (float)xsz / 2.0f;
144 posy -= cell_size * (float)ysz / 2.0f;
145 return Vector3(posx, 0, posy);
146 }
148 void Level::get_cell_coords_at(const Vector3 &pos, int *xptr, int *yptr) const
149 {
150 float posx = pos.x + cell_size * (float)xsz / 2.0f;
151 float posy = pos.z + cell_size * (float)ysz / 2.0f;
152 *xptr = (int)round(posx / cell_size);
153 *yptr = (int)round(posy / cell_size);
154 }
156 unsigned int Level::get_cell_dirmask(int x, int y) const
157 {
158 unsigned int dmask = TILE_ALL;
159 if(y > 0 && get_cell(x, y - 1)) {
160 dmask &= ~TILE_NORTH;
161 }
162 if(y < ysz - 1 && get_cell(x, y + 1)) {
163 dmask &= ~TILE_SOUTH;
164 }
165 if(x > 0 && get_cell(x - 1, y)) {
166 dmask &= ~TILE_WEST;
167 }
168 if(x < xsz - 1 && get_cell(x + 1, y)) {
169 dmask &= ~TILE_EAST;
170 }
171 return dmask;
172 }
174 void Level::set_player_position(const Vector3 &ppos)
175 {
176 player_pos = ppos;
177 }
179 void Level::update(unsigned long msec, float dt)
180 {
181 for(auto cell : cell_list) {
182 cell->update(msec, dt);
183 }
185 // activate the closest audio sources
186 if(cfg.sound) {
187 austatic.active_range(player_pos, 2.0);
188 }
189 }
191 void Level::draw() const
192 {
193 glMatrixMode(GL_MODELVIEW);
195 for(int i=0; i<ysz; i++) {
196 for(int j=0; j<xsz; j++) {
197 const GridCell *cell = get_cell(j, i);
198 if(cell) {
199 Vector3 pos = get_cell_pos(j, i);
200 glPushMatrix();
201 glTranslatef(pos.x, pos.y, pos.z);
202 glScalef(cell_size, cell_size, cell_size);
204 unsigned int dmask = get_cell_dirmask(j, i);
206 cell->draw(dmask);
207 glPopMatrix();
208 }
209 }
210 }
211 }
213 void Level::draw_lights() const
214 {
215 glMatrixMode(GL_MODELVIEW);
217 for(int i=0; i<ysz; i++) {
218 for(int j=0; j<xsz; j++) {
219 const GridCell *cell = get_cell(j, i);
220 if(cell) {
221 Vector3 pos = get_cell_pos(j, i);
223 glPushMatrix();
224 glTranslatef(pos.x, pos.y, pos.z);
225 glScalef(cell_size, cell_size, cell_size);
227 unsigned int dmask = get_cell_dirmask(j, i);
228 cell->draw_lights(dmask);
230 glPopMatrix();
231 }
232 }
233 }
234 }
236 void Level::draw_post() const
237 {
238 glMatrixMode(GL_MODELVIEW);
240 for(int i=0; i<ysz; i++) {
241 for(int j=0; j<xsz; j++) {
242 const GridCell *cell = get_cell(j, i);
243 if(cell) {
244 Vector3 pos = get_cell_pos(j, i);
245 glPushMatrix();
246 glTranslatef(pos.x, pos.y, pos.z);
247 glScalef(cell_size, cell_size, cell_size);
249 unsigned int dmask = get_cell_dirmask(j, i);
251 cell->draw_post(dmask);
252 glPopMatrix();
253 }
254 }
255 }
256 }
258 void Level::draw_grid() const
259 {
260 float xlen = xsz * cell_size;
261 float ylen = ysz * cell_size;
263 glPushAttrib(GL_ENABLE_BIT);
264 glDisable(GL_LIGHTING);
266 glBegin(GL_LINES);
267 glColor3f(0.4, 0.4, 0.4);
269 float y = -ylen / 2.0 - cell_size / 2.0;
270 for(int i=0; i<ysz; i++) {
271 glVertex3f(-xlen / 2.0, 0, y);
272 glVertex3f(xlen / 2.0, 0, y);
273 y += cell_size;
274 }
276 float x = -xlen / 2.0 - cell_size / 2.0;
277 for(int i=0; i<xsz; i++) {
278 glVertex3f(x, 0, -ylen / 2.0);
279 glVertex3f(x, 0, ylen / 2.0);
280 x += cell_size;
281 }
282 glEnd();
284 glPopAttrib();
285 }
288 AudioSample *Level::get_sample(int x, int y, int which) const
289 {
290 const GridCell *cell = get_cell(x, y);
291 if(!cell) {
292 return 0;
293 }
294 return cell->get_sample(which);
295 }
298 GridCell::GridCell(Tile *tile)
299 {
300 if(tile) {
301 add_tile(tile);
302 }
304 adjmask = TILE_ALL;
305 }
307 void GridCell::set_adj_mask(unsigned int mask)
308 {
309 adjmask = mask;
310 }
312 unsigned int GridCell::get_adj_mask() const
313 {
314 return adjmask;
315 }
317 void GridCell::add_tile(Tile *tile)
318 {
319 if(!tile) {
320 return;
321 }
323 tiles.push_back(tile);
325 /* instanciate any particle systems */
326 int num_psattr = tile->get_unique_psys_count();
328 for(int i=0; i<num_psattr; i++) {
329 const struct psys_attributes *psattr = tile->get_unique_psys(i);
330 struct psys_emitter *emitter = psys_create();
331 emitter->attr = *psattr;
332 psys.push_back(emitter);
333 }
334 }
336 void GridCell::update(unsigned long msec, float dt)
337 {
338 for(auto ps : psys) {
339 psys_update(ps, (float)msec / 1000.0f);
340 }
341 }
343 void GridCell::draw(unsigned int draw_mask) const
344 {
345 for(auto tile : tiles) {
346 tile->draw(draw_mask);
347 }
348 }
350 void GridCell::draw_lights(unsigned int draw_mask) const
351 {
352 for(auto tile : tiles) {
353 tile->draw_lights(draw_mask);
354 }
355 }
357 void GridCell::draw_post(unsigned int draw_mask) const
358 {
359 for(auto tile : tiles) {
360 tile->draw_post(draw_mask);
361 }
362 for(auto ps : psys) {
363 psys_draw(ps);
364 }
365 }
367 AudioSample *GridCell::get_sample(int which) const
368 {
369 for(auto tile : tiles) {
370 AudioSample *s = tile->get_sample(which);
371 if(s) {
372 return s;
373 }
374 }
375 return 0;
376 }
378 std::list<Tile::AudioSourceDesc> GridCell::get_audio_sources() const
379 {
380 std::list<Tile::AudioSourceDesc> srclist;
382 for(auto tile : tiles) {
383 int num_src = tile->get_audio_source_count();
384 for(int i=0; i<num_src; i++) {
385 srclist.push_back(tile->get_audio_source(i));
386 }
387 }
388 return srclist;
389 }