dungeon_crawler

view prototype/src/level.cc @ 26:21999ef6636b

lalala
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Aug 2012 04:07:51 +0300
parents 22562582c82d
children 862461b686f4
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;
20 }
22 bool Level::load(const char *fname)
23 {
24 if(!fname) {
25 return false;
26 }
28 TileSet *tileset = get_active_tileset();
29 if(!tileset) {
30 fprintf(stderr, "level loading failed: no active tileset\n");
31 return false;
32 }
33 Tile *deftile = tileset->get_tile("default");
34 if(!deftile) {
35 fprintf(stderr, "level loading failed: active tileset has no default tile\n");
36 return false;
37 }
39 FILE *fp = fopen(fname, "r");
40 if(!fp) {
41 fprintf(stderr, "failed to open level: %s: %s\n", fname, strerror(errno));
42 return false;
43 }
45 char buf[512];
46 fgets(buf, sizeof buf, fp);
47 if(sscanf(buf, "SIZE %dx%d", &xsz, &ysz) != 2) {
48 fprintf(stderr, "invalid or corrupt level file: %s\n", fname);
49 fclose(fp);
50 return false;
51 }
52 printf("level size: %dx%d\n", xsz, ysz);
54 cells = new GridCell*[xsz * ysz];
55 memset(cells, 0, xsz * ysz * sizeof *cells);
57 int y = 0;
58 GridCell **grid_row = cells;
60 while(fgets(buf, sizeof buf, fp)) {
61 for(int i=0; i<xsz; i++) {
62 if(!buf[i] || buf[i] == '\r' || buf[i] == '\n') {
63 grid_row += xsz - i;
64 break;
65 }
67 if(isalpha(buf[i]) || buf[i] == ' ') {
68 *grid_row = new GridCell(deftile);
69 }
70 grid_row++;
71 }
73 if(++y >= ysz) {
74 break;
75 }
76 }
77 fclose(fp);
79 return true;
80 }
82 bool Level::save(const char *fname) const
83 {
84 return false;
85 }
87 const GridCell *Level::get_cell(int x, int y) const
88 {
89 if(x < 0 || x >= xsz || y < 0 || y >= ysz) {
90 return 0;
91 }
92 return cells[y * xsz + x];
93 }
95 Vector3 Level::get_cell_pos(int x, int y) const
96 {
97 float posx = (x - xsz / 2) * cell_size;
98 float posy = (y - ysz / 2) * cell_size;
99 return Vector3(posx, 0, posy);
100 }
102 unsigned int Level::get_cell_dirmask(int x, int y) const
103 {
104 unsigned int dmask = TILE_ALL;
105 if(y > 0 && get_cell(x, y - 1)) {
106 dmask &= ~TILE_NORTH;
107 }
108 if(y < ysz - 1 && get_cell(x, y + 1)) {
109 dmask &= ~TILE_SOUTH;
110 }
111 if(x > 0 && get_cell(x - 1, y)) {
112 dmask &= ~TILE_WEST;
113 }
114 if(x < xsz - 1 && get_cell(x + 1, y)) {
115 dmask &= ~TILE_EAST;
116 }
117 return dmask;
118 }
120 void Level::draw() const
121 {
122 glMatrixMode(GL_MODELVIEW);
124 //draw_grid();
126 for(int i=0; i<ysz; i++) {
127 for(int j=0; j<xsz; j++) {
128 const GridCell *cell = get_cell(j, i);
129 if(cell) {
130 Vector3 pos = get_cell_pos(j, i);
131 glPushMatrix();
132 glTranslatef(pos.x, pos.y, pos.z);
133 glScalef(cell_size, cell_size, cell_size);
135 unsigned int dmask = get_cell_dirmask(j, i);
137 cell->draw(dmask);
138 glPopMatrix();
139 }
140 }
141 }
142 }
144 void Level::draw_lights() const
145 {
146 glMatrixMode(GL_MODELVIEW);
148 for(int i=0; i<ysz; i++) {
149 for(int j=0; j<xsz; j++) {
150 const GridCell *cell = get_cell(j, i);
151 if(cell) {
152 Vector3 pos = get_cell_pos(j, i);
154 glPushMatrix();
155 glTranslatef(pos.x, pos.y, pos.z);
156 glScalef(cell_size, cell_size, cell_size);
158 unsigned int dmask = get_cell_dirmask(j, i);
159 cell->draw_lights(dmask);
161 glPopMatrix();
162 }
163 }
164 }
165 }
167 void Level::draw_grid() const
168 {
169 float xlen = xsz * cell_size;
170 float ylen = ysz * cell_size;
172 glPushAttrib(GL_ENABLE_BIT);
173 glDisable(GL_LIGHTING);
175 glBegin(GL_LINES);
176 glColor3f(0.4, 0.4, 0.4);
178 float y = -ylen / 2.0 - cell_size / 2.0;
179 for(int i=0; i<ysz; i++) {
180 glVertex3f(-xlen / 2.0, 0, y);
181 glVertex3f(xlen / 2.0, 0, y);
182 y += cell_size;
183 }
185 float x = -xlen / 2.0 - cell_size / 2.0;
186 for(int i=0; i<xsz; i++) {
187 glVertex3f(x, 0, -ylen / 2.0);
188 glVertex3f(x, 0, ylen / 2.0);
189 x += cell_size;
190 }
191 glEnd();
193 glPopAttrib();
194 }
197 GridCell::GridCell(const Tile *tile)
198 {
199 if(tile) {
200 tiles.push_back(tile);
201 }
202 }
204 void GridCell::add_tile(const Tile *tile)
205 {
206 tiles.push_back(tile);
207 }
209 void GridCell::draw(unsigned int draw_mask) const
210 {
211 for(auto tile : tiles) {
212 tile->draw(draw_mask);
213 }
214 }
216 void GridCell::draw_lights(unsigned int draw_mask) const
217 {
218 for(auto tile : tiles) {
219 tile->draw_lights(draw_mask);
220 }
221 }