dungeon_crawler

view prototype/src/level.cc @ 48:aa9e28670ae2

added sound playback, more to do
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 17 Sep 2012 08:40:59 +0300
parents f3030df27110
children 303743485aba
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"
9 Level::Level()
10 {
11 cell_size = 1.0;
13 cells = 0;
14 xsz = ysz = 0;
15 }
17 Level::~Level()
18 {
19 delete [] cells;
21 for(auto cell : cell_list) {
22 delete cell;
23 }
24 }
26 bool Level::load(const char *fname)
27 {
28 if(!fname) {
29 return false;
30 }
32 TileSet *tileset = get_active_tileset();
33 if(!tileset) {
34 fprintf(stderr, "level loading failed: no active tileset\n");
35 return false;
36 }
37 Tile *deftile = tileset->get_tile("default");
38 if(!deftile) {
39 fprintf(stderr, "level loading failed: active tileset has no default tile\n");
40 return false;
41 }
43 FILE *fp = fopen(fname, "r");
44 if(!fp) {
45 fprintf(stderr, "failed to open level: %s: %s\n", fname, strerror(errno));
46 return false;
47 }
49 char buf[512];
50 fgets(buf, sizeof buf, fp);
51 if(sscanf(buf, "SIZE %dx%d", &xsz, &ysz) != 2) {
52 fprintf(stderr, "invalid or corrupt level file: %s\n", fname);
53 fclose(fp);
54 return false;
55 }
56 printf("level size: %dx%d\n", xsz, ysz);
58 cells = new GridCell*[xsz * ysz];
59 memset(cells, 0, xsz * ysz * sizeof *cells);
61 int y = 0;
62 GridCell **grid_row = cells;
64 while(fgets(buf, sizeof buf, fp)) {
65 for(int i=0; i<xsz; i++) {
66 if(!buf[i] || buf[i] == '\r' || buf[i] == '\n') {
67 grid_row += xsz - i;
68 break;
69 }
71 if(isalpha(buf[i]) || buf[i] == ' ') {
72 GridCell *cell = new GridCell(deftile);
73 *grid_row = cell;
74 cell_list.push_back(*grid_row);
75 }
76 grid_row++;
77 }
79 if(++y >= ysz) {
80 break;
81 }
82 }
83 fclose(fp);
85 return true;
86 }
88 bool Level::save(const char *fname) const
89 {
90 return false;
91 }
93 const GridCell *Level::get_cell(int x, int y) const
94 {
95 if(x < 0 || x >= xsz || y < 0 || y >= ysz) {
96 return 0;
97 }
98 return cells[y * xsz + x];
99 }
101 Vector3 Level::get_cell_pos(int x, int y) const
102 {
103 float posx = (float)x * cell_size;
104 float posy = (float)y * cell_size;
105 posx -= cell_size * (float)xsz / 2.0f;
106 posy -= cell_size * (float)ysz / 2.0f;
107 return Vector3(posx, 0, posy);
108 }
110 void Level::get_cell_coords_at(const Vector3 &pos, int *xptr, int *yptr) const
111 {
112 float posx = pos.x + cell_size * (float)xsz / 2.0f;
113 float posy = pos.z + cell_size * (float)ysz / 2.0f;
114 *xptr = (int)round(posx / cell_size);
115 *yptr = (int)round(posy / cell_size);
116 }
118 unsigned int Level::get_cell_dirmask(int x, int y) const
119 {
120 unsigned int dmask = TILE_ALL;
121 if(y > 0 && get_cell(x, y - 1)) {
122 dmask &= ~TILE_NORTH;
123 }
124 if(y < ysz - 1 && get_cell(x, y + 1)) {
125 dmask &= ~TILE_SOUTH;
126 }
127 if(x > 0 && get_cell(x - 1, y)) {
128 dmask &= ~TILE_WEST;
129 }
130 if(x < xsz - 1 && get_cell(x + 1, y)) {
131 dmask &= ~TILE_EAST;
132 }
133 return dmask;
134 }
136 void Level::update(unsigned long msec, float dt)
137 {
138 for(auto cell : cell_list) {
139 cell->update(msec, dt);
140 }
141 }
143 void Level::draw() const
144 {
145 glMatrixMode(GL_MODELVIEW);
147 for(int i=0; i<ysz; i++) {
148 for(int j=0; j<xsz; j++) {
149 const GridCell *cell = get_cell(j, i);
150 if(cell) {
151 Vector3 pos = get_cell_pos(j, i);
152 glPushMatrix();
153 glTranslatef(pos.x, pos.y, pos.z);
154 glScalef(cell_size, cell_size, cell_size);
156 unsigned int dmask = get_cell_dirmask(j, i);
158 cell->draw(dmask);
159 glPopMatrix();
160 }
161 }
162 }
163 }
165 void Level::draw_lights() const
166 {
167 glMatrixMode(GL_MODELVIEW);
169 for(int i=0; i<ysz; i++) {
170 for(int j=0; j<xsz; j++) {
171 const GridCell *cell = get_cell(j, i);
172 if(cell) {
173 Vector3 pos = get_cell_pos(j, i);
175 glPushMatrix();
176 glTranslatef(pos.x, pos.y, pos.z);
177 glScalef(cell_size, cell_size, cell_size);
179 unsigned int dmask = get_cell_dirmask(j, i);
180 cell->draw_lights(dmask);
182 glPopMatrix();
183 }
184 }
185 }
186 }
188 void Level::draw_post() const
189 {
190 glMatrixMode(GL_MODELVIEW);
192 for(int i=0; i<ysz; i++) {
193 for(int j=0; j<xsz; j++) {
194 const GridCell *cell = get_cell(j, i);
195 if(cell) {
196 Vector3 pos = get_cell_pos(j, i);
197 glPushMatrix();
198 glTranslatef(pos.x, pos.y, pos.z);
199 glScalef(cell_size, cell_size, cell_size);
201 unsigned int dmask = get_cell_dirmask(j, i);
203 cell->draw_post(dmask);
204 glPopMatrix();
205 }
206 }
207 }
208 }
210 void Level::draw_grid() const
211 {
212 float xlen = xsz * cell_size;
213 float ylen = ysz * cell_size;
215 glPushAttrib(GL_ENABLE_BIT);
216 glDisable(GL_LIGHTING);
218 glBegin(GL_LINES);
219 glColor3f(0.4, 0.4, 0.4);
221 float y = -ylen / 2.0 - cell_size / 2.0;
222 for(int i=0; i<ysz; i++) {
223 glVertex3f(-xlen / 2.0, 0, y);
224 glVertex3f(xlen / 2.0, 0, y);
225 y += cell_size;
226 }
228 float x = -xlen / 2.0 - cell_size / 2.0;
229 for(int i=0; i<xsz; i++) {
230 glVertex3f(x, 0, -ylen / 2.0);
231 glVertex3f(x, 0, ylen / 2.0);
232 x += cell_size;
233 }
234 glEnd();
236 glPopAttrib();
237 }
240 AudioSample *Level::get_sample(int x, int y, int which) const
241 {
242 const GridCell *cell = get_cell(x, y);
243 if(!cell) {
244 return 0;
245 }
246 return cell->get_sample(which);
247 }
250 GridCell::GridCell(Tile *tile)
251 {
252 if(tile) {
253 add_tile(tile);
254 }
255 }
257 void GridCell::add_tile(Tile *tile)
258 {
259 if(!tile) {
260 return;
261 }
263 tiles.push_back(tile);
265 /* instanciate any particle systems */
266 const struct psys_attributes * const *psattr = tile->get_unique_psys();
267 int num_psattr = tile->get_unique_psys_count();
269 for(int i=0; i<num_psattr; i++) {
270 struct psys_emitter *emitter = psys_create();
271 emitter->attr = *psattr[i];
272 psys.push_back(emitter);
273 }
274 }
276 void GridCell::update(unsigned long msec, float dt)
277 {
278 for(auto ps : psys) {
279 psys_update(ps, (float)msec / 1000.0f);
280 }
281 }
283 void GridCell::draw(unsigned int draw_mask) const
284 {
285 for(auto tile : tiles) {
286 tile->draw(draw_mask);
287 }
288 }
290 void GridCell::draw_lights(unsigned int draw_mask) const
291 {
292 for(auto tile : tiles) {
293 tile->draw_lights(draw_mask);
294 }
295 }
297 void GridCell::draw_post(unsigned int draw_mask) const
298 {
299 for(auto tile : tiles) {
300 tile->draw_post(draw_mask);
301 }
302 for(auto ps : psys) {
303 psys_draw(ps);
304 }
305 }
307 AudioSample *GridCell::get_sample(int which) const
308 {
309 for(auto tile : tiles) {
310 AudioSample *s = tile->get_sample(which);
311 if(s) {
312 return s;
313 }
314 }
315 return 0;
316 }