dungeon_crawler

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