dungeon_crawler

view prototype/src/level.cc @ 46:f3030df27110

debugging the particles
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 13 Sep 2012 06:33:51 +0300
parents dfd3a413ef9e
children aa9e28670ae2
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 unsigned int Level::get_cell_dirmask(int x, int y) const
111 {
112 unsigned int dmask = TILE_ALL;
113 if(y > 0 && get_cell(x, y - 1)) {
114 dmask &= ~TILE_NORTH;
115 }
116 if(y < ysz - 1 && get_cell(x, y + 1)) {
117 dmask &= ~TILE_SOUTH;
118 }
119 if(x > 0 && get_cell(x - 1, y)) {
120 dmask &= ~TILE_WEST;
121 }
122 if(x < xsz - 1 && get_cell(x + 1, y)) {
123 dmask &= ~TILE_EAST;
124 }
125 return dmask;
126 }
128 void Level::update(unsigned long msec, float dt)
129 {
130 for(auto cell : cell_list) {
131 cell->update(msec, dt);
132 }
133 }
135 void Level::draw() const
136 {
137 glMatrixMode(GL_MODELVIEW);
139 for(int i=0; i<ysz; i++) {
140 for(int j=0; j<xsz; j++) {
141 const GridCell *cell = get_cell(j, i);
142 if(cell) {
143 Vector3 pos = get_cell_pos(j, i);
144 glPushMatrix();
145 glTranslatef(pos.x, pos.y, pos.z);
146 glScalef(cell_size, cell_size, cell_size);
148 unsigned int dmask = get_cell_dirmask(j, i);
150 cell->draw(dmask);
151 glPopMatrix();
152 }
153 }
154 }
155 }
157 void Level::draw_lights() const
158 {
159 glMatrixMode(GL_MODELVIEW);
161 for(int i=0; i<ysz; i++) {
162 for(int j=0; j<xsz; j++) {
163 const GridCell *cell = get_cell(j, i);
164 if(cell) {
165 Vector3 pos = get_cell_pos(j, i);
167 glPushMatrix();
168 glTranslatef(pos.x, pos.y, pos.z);
169 glScalef(cell_size, cell_size, cell_size);
171 unsigned int dmask = get_cell_dirmask(j, i);
172 cell->draw_lights(dmask);
174 glPopMatrix();
175 }
176 }
177 }
178 }
180 void Level::draw_post() const
181 {
182 glMatrixMode(GL_MODELVIEW);
184 for(int i=0; i<ysz; i++) {
185 for(int j=0; j<xsz; j++) {
186 const GridCell *cell = get_cell(j, i);
187 if(cell) {
188 Vector3 pos = get_cell_pos(j, i);
189 glPushMatrix();
190 glTranslatef(pos.x, pos.y, pos.z);
191 glScalef(cell_size, cell_size, cell_size);
193 unsigned int dmask = get_cell_dirmask(j, i);
195 cell->draw_post(dmask);
196 glPopMatrix();
197 }
198 }
199 }
200 }
202 void Level::draw_grid() const
203 {
204 float xlen = xsz * cell_size;
205 float ylen = ysz * cell_size;
207 glPushAttrib(GL_ENABLE_BIT);
208 glDisable(GL_LIGHTING);
210 glBegin(GL_LINES);
211 glColor3f(0.4, 0.4, 0.4);
213 float y = -ylen / 2.0 - cell_size / 2.0;
214 for(int i=0; i<ysz; i++) {
215 glVertex3f(-xlen / 2.0, 0, y);
216 glVertex3f(xlen / 2.0, 0, y);
217 y += cell_size;
218 }
220 float x = -xlen / 2.0 - cell_size / 2.0;
221 for(int i=0; i<xsz; i++) {
222 glVertex3f(x, 0, -ylen / 2.0);
223 glVertex3f(x, 0, ylen / 2.0);
224 x += cell_size;
225 }
226 glEnd();
228 glPopAttrib();
229 }
232 GridCell::GridCell(Tile *tile)
233 {
234 if(tile) {
235 add_tile(tile);
236 }
237 }
239 void GridCell::add_tile(Tile *tile)
240 {
241 if(!tile) {
242 return;
243 }
245 tiles.push_back(tile);
247 /* instanciate any particle systems */
248 const struct psys_attributes * const *psattr = tile->get_unique_psys();
249 int num_psattr = tile->get_unique_psys_count();
251 for(int i=0; i<num_psattr; i++) {
252 struct psys_emitter *emitter = psys_create();
253 emitter->attr = *psattr[i];
254 psys.push_back(emitter);
255 }
256 }
258 void GridCell::update(unsigned long msec, float dt)
259 {
260 for(auto ps : psys) {
261 psys_update(ps, (float)msec / 1000.0f);
262 }
263 }
265 void GridCell::draw(unsigned int draw_mask) const
266 {
267 for(auto tile : tiles) {
268 tile->draw(draw_mask);
269 }
270 }
272 void GridCell::draw_lights(unsigned int draw_mask) const
273 {
274 for(auto tile : tiles) {
275 tile->draw_lights(draw_mask);
276 }
277 }
279 void GridCell::draw_post(unsigned int draw_mask) const
280 {
281 for(auto tile : tiles) {
282 tile->draw_post(draw_mask);
283 }
284 for(auto ps : psys) {
285 psys_draw(ps);
286 }
287 }