rev |
line source |
nuclear@1
|
1 #include <stdio.h>
|
nuclear@1
|
2 #include <string.h>
|
nuclear@5
|
3 #include <errno.h>
|
nuclear@1
|
4 #include "opengl.h"
|
nuclear@1
|
5 #include "level.h"
|
nuclear@1
|
6 #include "tile.h"
|
nuclear@5
|
7 #include "tileset.h"
|
nuclear@1
|
8
|
nuclear@1
|
9 Level::Level()
|
nuclear@1
|
10 {
|
nuclear@1
|
11 cell_size = 1.0;
|
nuclear@1
|
12
|
nuclear@1
|
13 cells = 0;
|
nuclear@1
|
14 xsz = ysz = 0;
|
nuclear@1
|
15 }
|
nuclear@1
|
16
|
nuclear@1
|
17 Level::~Level()
|
nuclear@1
|
18 {
|
nuclear@1
|
19 delete [] cells;
|
nuclear@1
|
20 }
|
nuclear@1
|
21
|
nuclear@1
|
22 bool Level::load(const char *fname)
|
nuclear@1
|
23 {
|
nuclear@5
|
24 if(!fname) {
|
nuclear@5
|
25 return false;
|
nuclear@5
|
26 }
|
nuclear@5
|
27
|
nuclear@5
|
28 TileSet *tileset = get_active_tileset();
|
nuclear@5
|
29 if(!tileset) {
|
nuclear@5
|
30 fprintf(stderr, "level loading failed: no active tileset\n");
|
nuclear@5
|
31 return false;
|
nuclear@5
|
32 }
|
nuclear@5
|
33 Tile *deftile = tileset->get_tile("default");
|
nuclear@5
|
34 if(!deftile) {
|
nuclear@5
|
35 fprintf(stderr, "level loading failed: active tileset has no default tile\n");
|
nuclear@5
|
36 return false;
|
nuclear@5
|
37 }
|
nuclear@5
|
38
|
nuclear@5
|
39 FILE *fp = fopen(fname, "r");
|
nuclear@5
|
40 if(!fp) {
|
nuclear@5
|
41 fprintf(stderr, "failed to open level: %s: %s\n", fname, strerror(errno));
|
nuclear@5
|
42 return false;
|
nuclear@5
|
43 }
|
nuclear@5
|
44
|
nuclear@5
|
45 char buf[512];
|
nuclear@5
|
46 fgets(buf, sizeof buf, fp);
|
nuclear@5
|
47 if(sscanf(buf, "SIZE %dx%d", &xsz, &ysz) != 2) {
|
nuclear@5
|
48 fprintf(stderr, "invalid or corrupt level file: %s\n", fname);
|
nuclear@5
|
49 fclose(fp);
|
nuclear@5
|
50 return false;
|
nuclear@5
|
51 }
|
nuclear@5
|
52 printf("level size: %dx%d\n", xsz, ysz);
|
nuclear@1
|
53
|
nuclear@1
|
54 cells = new GridCell*[xsz * ysz];
|
nuclear@1
|
55 memset(cells, 0, xsz * ysz * sizeof *cells);
|
nuclear@1
|
56
|
nuclear@5
|
57 int y = 0;
|
nuclear@5
|
58 GridCell **grid_row = cells;
|
nuclear@5
|
59
|
nuclear@5
|
60 while(fgets(buf, sizeof buf, fp)) {
|
nuclear@5
|
61 for(int i=0; i<xsz; i++) {
|
nuclear@5
|
62 if(!buf[i] || buf[i] == '\r' || buf[i] == '\n') {
|
nuclear@5
|
63 grid_row += xsz - i;
|
nuclear@5
|
64 break;
|
nuclear@5
|
65 }
|
nuclear@5
|
66
|
nuclear@5
|
67 if(isalpha(buf[i]) || buf[i] == ' ') {
|
nuclear@5
|
68 *grid_row = new GridCell(deftile);
|
nuclear@5
|
69 }
|
nuclear@5
|
70 grid_row++;
|
nuclear@5
|
71 }
|
nuclear@5
|
72
|
nuclear@5
|
73 if(++y >= ysz) {
|
nuclear@5
|
74 break;
|
nuclear@5
|
75 }
|
nuclear@5
|
76 }
|
nuclear@5
|
77 fclose(fp);
|
nuclear@1
|
78
|
nuclear@1
|
79 return true;
|
nuclear@1
|
80 }
|
nuclear@1
|
81
|
nuclear@1
|
82 bool Level::save(const char *fname) const
|
nuclear@1
|
83 {
|
nuclear@1
|
84 return false;
|
nuclear@1
|
85 }
|
nuclear@1
|
86
|
nuclear@1
|
87 const GridCell *Level::get_cell(int x, int y) const
|
nuclear@1
|
88 {
|
nuclear@1
|
89 if(x < 0 || x >= xsz || y < 0 || y >= ysz) {
|
nuclear@1
|
90 return 0;
|
nuclear@1
|
91 }
|
nuclear@1
|
92 return cells[y * xsz + x];
|
nuclear@1
|
93 }
|
nuclear@1
|
94
|
nuclear@1
|
95 Vector3 Level::get_cell_pos(int x, int y) const
|
nuclear@1
|
96 {
|
nuclear@1
|
97 float posx = (x - xsz / 2) * cell_size;
|
nuclear@1
|
98 float posy = (y - ysz / 2) * cell_size;
|
nuclear@1
|
99 return Vector3(posx, 0, posy);
|
nuclear@1
|
100 }
|
nuclear@1
|
101
|
nuclear@1
|
102 void Level::draw() const
|
nuclear@1
|
103 {
|
nuclear@1
|
104 glMatrixMode(GL_MODELVIEW);
|
nuclear@1
|
105
|
nuclear@10
|
106 //draw_grid();
|
nuclear@1
|
107
|
nuclear@1
|
108 for(int i=0; i<ysz; i++) {
|
nuclear@1
|
109 for(int j=0; j<xsz; j++) {
|
nuclear@1
|
110 const GridCell *cell = get_cell(j, i);
|
nuclear@1
|
111 if(cell) {
|
nuclear@1
|
112 Vector3 pos = get_cell_pos(j, i);
|
nuclear@1
|
113 glPushMatrix();
|
nuclear@1
|
114 glTranslatef(pos.x, pos.y, pos.z);
|
nuclear@1
|
115 glScalef(cell_size, cell_size, cell_size);
|
nuclear@5
|
116
|
nuclear@7
|
117 unsigned int dmask = TILE_ALL;
|
nuclear@7
|
118 if(i > 0 && get_cell(j, i - 1)) {
|
nuclear@7
|
119 dmask &= ~TILE_NORTH;
|
nuclear@5
|
120 }
|
nuclear@7
|
121 if(i < ysz - 1 && get_cell(j, i + 1)) {
|
nuclear@7
|
122 dmask &= ~TILE_SOUTH;
|
nuclear@5
|
123 }
|
nuclear@7
|
124 if(j > 0 && get_cell(j - 1, i)) {
|
nuclear@7
|
125 dmask &= ~TILE_WEST;
|
nuclear@5
|
126 }
|
nuclear@7
|
127 if(j < xsz - 1 && get_cell(j + 1, i)) {
|
nuclear@7
|
128 dmask &= ~TILE_EAST;
|
nuclear@5
|
129 }
|
nuclear@5
|
130
|
nuclear@5
|
131 cell->draw(dmask);
|
nuclear@1
|
132 glPopMatrix();
|
nuclear@1
|
133 }
|
nuclear@1
|
134 }
|
nuclear@1
|
135 }
|
nuclear@1
|
136 }
|
nuclear@1
|
137
|
nuclear@1
|
138 void Level::draw_grid() const
|
nuclear@1
|
139 {
|
nuclear@1
|
140 float xlen = xsz * cell_size;
|
nuclear@1
|
141 float ylen = ysz * cell_size;
|
nuclear@1
|
142
|
nuclear@1
|
143 glPushAttrib(GL_ENABLE_BIT);
|
nuclear@1
|
144 glDisable(GL_LIGHTING);
|
nuclear@1
|
145
|
nuclear@1
|
146 glBegin(GL_LINES);
|
nuclear@1
|
147 glColor3f(0.4, 0.4, 0.4);
|
nuclear@1
|
148
|
nuclear@1
|
149 float y = -ylen / 2.0 - cell_size / 2.0;
|
nuclear@1
|
150 for(int i=0; i<ysz; i++) {
|
nuclear@1
|
151 glVertex3f(-xlen / 2.0, 0, y);
|
nuclear@1
|
152 glVertex3f(xlen / 2.0, 0, y);
|
nuclear@1
|
153 y += cell_size;
|
nuclear@1
|
154 }
|
nuclear@1
|
155
|
nuclear@1
|
156 float x = -xlen / 2.0 - cell_size / 2.0;
|
nuclear@1
|
157 for(int i=0; i<xsz; i++) {
|
nuclear@1
|
158 glVertex3f(x, 0, -ylen / 2.0);
|
nuclear@1
|
159 glVertex3f(x, 0, ylen / 2.0);
|
nuclear@1
|
160 x += cell_size;
|
nuclear@1
|
161 }
|
nuclear@1
|
162 glEnd();
|
nuclear@1
|
163
|
nuclear@1
|
164 glPopAttrib();
|
nuclear@1
|
165 }
|
nuclear@1
|
166
|
nuclear@5
|
167
|
nuclear@5
|
168 GridCell::GridCell(const Tile *tile)
|
nuclear@5
|
169 {
|
nuclear@5
|
170 if(tile) {
|
nuclear@5
|
171 tiles.push_back(tile);
|
nuclear@5
|
172 }
|
nuclear@5
|
173 }
|
nuclear@5
|
174
|
nuclear@1
|
175 void GridCell::add_tile(const Tile *tile)
|
nuclear@1
|
176 {
|
nuclear@1
|
177 tiles.push_back(tile);
|
nuclear@1
|
178 }
|
nuclear@1
|
179
|
nuclear@5
|
180 void GridCell::draw(unsigned int draw_mask) const
|
nuclear@1
|
181 {
|
nuclear@1
|
182 for(size_t i=0; i<tiles.size(); i++) {
|
nuclear@5
|
183 tiles[i]->draw(draw_mask);
|
nuclear@1
|
184 }
|
nuclear@1
|
185 }
|