dungeon_crawler

view prototype/src/level.cc @ 1:96de911d05d4

started a rough prototype
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 28 Jun 2012 06:05:50 +0300
parents
children 252a00508411
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include "opengl.h"
4 #include "level.h"
5 #include "tile.h"
7 Level::Level()
8 {
9 cell_size = 1.0;
11 cells = 0;
12 xsz = ysz = 0;
13 }
15 Level::~Level()
16 {
17 delete [] cells;
18 }
20 bool Level::load(const char *fname)
21 {
22 xsz = ysz = 64;
24 cells = new GridCell*[xsz * ysz];
25 memset(cells, 0, xsz * ysz * sizeof *cells);
27 GridCell *g = new GridCell;
28 g->add_tile(new Tile);
29 cells[ysz / 2 * xsz + xsz / 2] = g;
31 return true;
32 }
34 bool Level::save(const char *fname) const
35 {
36 return false;
37 }
39 const GridCell *Level::get_cell(int x, int y) const
40 {
41 if(x < 0 || x >= xsz || y < 0 || y >= ysz) {
42 return 0;
43 }
44 return cells[y * xsz + x];
45 }
47 Vector3 Level::get_cell_pos(int x, int y) const
48 {
49 float posx = (x - xsz / 2) * cell_size;
50 float posy = (y - ysz / 2) * cell_size;
51 return Vector3(posx, 0, posy);
52 }
54 void Level::draw() const
55 {
56 glMatrixMode(GL_MODELVIEW);
58 draw_grid();
60 for(int i=0; i<ysz; i++) {
61 for(int j=0; j<xsz; j++) {
62 const GridCell *cell = get_cell(j, i);
63 if(cell) {
64 Vector3 pos = get_cell_pos(j, i);
65 glPushMatrix();
66 glTranslatef(pos.x, pos.y, pos.z);
67 glScalef(cell_size, cell_size, cell_size);
68 cell->draw();
69 glPopMatrix();
70 }
71 }
72 }
73 }
75 void Level::draw_grid() const
76 {
77 float xlen = xsz * cell_size;
78 float ylen = ysz * cell_size;
80 glPushAttrib(GL_ENABLE_BIT);
81 glDisable(GL_LIGHTING);
83 glBegin(GL_LINES);
84 glColor3f(0.4, 0.4, 0.4);
86 float y = -ylen / 2.0 - cell_size / 2.0;
87 for(int i=0; i<ysz; i++) {
88 glVertex3f(-xlen / 2.0, 0, y);
89 glVertex3f(xlen / 2.0, 0, y);
90 y += cell_size;
91 }
93 float x = -xlen / 2.0 - cell_size / 2.0;
94 for(int i=0; i<xsz; i++) {
95 glVertex3f(x, 0, -ylen / 2.0);
96 glVertex3f(x, 0, ylen / 2.0);
97 x += cell_size;
98 }
99 glEnd();
101 glPopAttrib();
102 }
104 void GridCell::add_tile(const Tile *tile)
105 {
106 tiles.push_back(tile);
107 }
109 void GridCell::draw() const
110 {
111 for(size_t i=0; i<tiles.size(); i++) {
112 tiles[i]->draw();
113 }
114 }