dungeon_crawler
view prototype/src/level.cc @ 45:dfd3a413ef9e
particle system 1
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 12 Sep 2012 06:04:20 +0300 |
parents | 862461b686f4 |
children | f3030df27110 |
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 = (x - xsz / 2) * cell_size;
104 float posy = (y - ysz / 2) * cell_size;
105 return Vector3(posx, 0, posy);
106 }
108 unsigned int Level::get_cell_dirmask(int x, int y) const
109 {
110 unsigned int dmask = TILE_ALL;
111 if(y > 0 && get_cell(x, y - 1)) {
112 dmask &= ~TILE_NORTH;
113 }
114 if(y < ysz - 1 && get_cell(x, y + 1)) {
115 dmask &= ~TILE_SOUTH;
116 }
117 if(x > 0 && get_cell(x - 1, y)) {
118 dmask &= ~TILE_WEST;
119 }
120 if(x < xsz - 1 && get_cell(x + 1, y)) {
121 dmask &= ~TILE_EAST;
122 }
123 return dmask;
124 }
126 void Level::update(unsigned long msec, float dt)
127 {
128 for(auto cell : cell_list) {
129 cell->update(msec, dt);
130 }
131 }
133 void Level::draw() const
134 {
135 glMatrixMode(GL_MODELVIEW);
137 for(int i=0; i<ysz; i++) {
138 for(int j=0; j<xsz; j++) {
139 const GridCell *cell = get_cell(j, i);
140 if(cell) {
141 Vector3 pos = get_cell_pos(j, i);
142 glPushMatrix();
143 glTranslatef(pos.x, pos.y, pos.z);
144 glScalef(cell_size, cell_size, cell_size);
146 unsigned int dmask = get_cell_dirmask(j, i);
148 cell->draw(dmask);
149 glPopMatrix();
150 }
151 }
152 }
153 }
155 void Level::draw_lights() const
156 {
157 glMatrixMode(GL_MODELVIEW);
159 for(int i=0; i<ysz; i++) {
160 for(int j=0; j<xsz; j++) {
161 const GridCell *cell = get_cell(j, i);
162 if(cell) {
163 Vector3 pos = get_cell_pos(j, i);
165 glPushMatrix();
166 glTranslatef(pos.x, pos.y, pos.z);
167 glScalef(cell_size, cell_size, cell_size);
169 unsigned int dmask = get_cell_dirmask(j, i);
170 cell->draw_lights(dmask);
172 glPopMatrix();
173 }
174 }
175 }
176 }
178 void Level::draw_grid() const
179 {
180 float xlen = xsz * cell_size;
181 float ylen = ysz * cell_size;
183 glPushAttrib(GL_ENABLE_BIT);
184 glDisable(GL_LIGHTING);
186 glBegin(GL_LINES);
187 glColor3f(0.4, 0.4, 0.4);
189 float y = -ylen / 2.0 - cell_size / 2.0;
190 for(int i=0; i<ysz; i++) {
191 glVertex3f(-xlen / 2.0, 0, y);
192 glVertex3f(xlen / 2.0, 0, y);
193 y += cell_size;
194 }
196 float x = -xlen / 2.0 - cell_size / 2.0;
197 for(int i=0; i<xsz; i++) {
198 glVertex3f(x, 0, -ylen / 2.0);
199 glVertex3f(x, 0, ylen / 2.0);
200 x += cell_size;
201 }
202 glEnd();
204 glPopAttrib();
205 }
208 GridCell::GridCell(Tile *tile)
209 {
210 if(tile) {
211 add_tile(tile);
212 }
213 }
215 void GridCell::add_tile(Tile *tile)
216 {
217 if(!tile) {
218 return;
219 }
221 tiles.push_back(tile);
223 /* instanciate any particle systems */
224 const struct psys_attributes **psattr = tile->get_unique_psys();
225 int num_psattr = tile->get_unique_psys_count();
227 for(int i=0; i<num_psattr; i++) {
228 struct psys_emitter *emitter = psys_create();
229 emitter->attr = *psattr[i];
230 psys.push_back(emitter);
231 }
232 }
234 void GridCell::update(unsigned long msec, float dt)
235 {
236 for(auto tile : tiles) {
237 tile->update(msec, dt);
238 }
239 for(auto ps : psys) {
240 psys_update(ps, (float)msec / 1000.0f);
241 }
242 }
244 void GridCell::draw(unsigned int draw_mask) const
245 {
246 for(auto tile : tiles) {
247 tile->draw(draw_mask);
248 }
249 }
251 void GridCell::draw_lights(unsigned int draw_mask) const
252 {
253 for(auto tile : tiles) {
254 tile->draw_lights(draw_mask);
255 }
256 }
258 void GridCell::draw_post(unsigned int draw_mask) const
259 {
260 for(auto tile : tiles) {
261 tile->draw_post(draw_mask);
262 }
263 for(auto ps : psys) {
264 psys_draw(ps);
265 }
266 }