dungeon_crawler
view prototype/src/level.cc @ 11:e5567ddbf2ef
- Texture set (texture manager)
- materials
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 19 Aug 2012 03:11:36 +0300 |
parents | 8fb37db44fd8 |
children | fa8f89d06f6f |
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 void Level::draw() const
103 {
104 glMatrixMode(GL_MODELVIEW);
106 //draw_grid();
108 for(int i=0; i<ysz; i++) {
109 for(int j=0; j<xsz; j++) {
110 const GridCell *cell = get_cell(j, i);
111 if(cell) {
112 Vector3 pos = get_cell_pos(j, i);
113 glPushMatrix();
114 glTranslatef(pos.x, pos.y, pos.z);
115 glScalef(cell_size, cell_size, cell_size);
117 unsigned int dmask = TILE_ALL;
118 if(i > 0 && get_cell(j, i - 1)) {
119 dmask &= ~TILE_NORTH;
120 }
121 if(i < ysz - 1 && get_cell(j, i + 1)) {
122 dmask &= ~TILE_SOUTH;
123 }
124 if(j > 0 && get_cell(j - 1, i)) {
125 dmask &= ~TILE_WEST;
126 }
127 if(j < xsz - 1 && get_cell(j + 1, i)) {
128 dmask &= ~TILE_EAST;
129 }
131 cell->draw(dmask);
132 glPopMatrix();
133 }
134 }
135 }
136 }
138 void Level::draw_grid() const
139 {
140 float xlen = xsz * cell_size;
141 float ylen = ysz * cell_size;
143 glPushAttrib(GL_ENABLE_BIT);
144 glDisable(GL_LIGHTING);
146 glBegin(GL_LINES);
147 glColor3f(0.4, 0.4, 0.4);
149 float y = -ylen / 2.0 - cell_size / 2.0;
150 for(int i=0; i<ysz; i++) {
151 glVertex3f(-xlen / 2.0, 0, y);
152 glVertex3f(xlen / 2.0, 0, y);
153 y += cell_size;
154 }
156 float x = -xlen / 2.0 - cell_size / 2.0;
157 for(int i=0; i<xsz; i++) {
158 glVertex3f(x, 0, -ylen / 2.0);
159 glVertex3f(x, 0, ylen / 2.0);
160 x += cell_size;
161 }
162 glEnd();
164 glPopAttrib();
165 }
168 GridCell::GridCell(const Tile *tile)
169 {
170 if(tile) {
171 tiles.push_back(tile);
172 }
173 }
175 void GridCell::add_tile(const Tile *tile)
176 {
177 tiles.push_back(tile);
178 }
180 void GridCell::draw(unsigned int draw_mask) const
181 {
182 for(size_t i=0; i<tiles.size(); i++) {
183 tiles[i]->draw(draw_mask);
184 }
185 }