dungeon_crawler

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/prototype/src/level.cc	Thu Jun 28 06:05:50 2012 +0300
     1.3 @@ -0,0 +1,114 @@
     1.4 +#include <stdio.h>
     1.5 +#include <string.h>
     1.6 +#include "opengl.h"
     1.7 +#include "level.h"
     1.8 +#include "tile.h"
     1.9 +
    1.10 +Level::Level()
    1.11 +{
    1.12 +	cell_size = 1.0;
    1.13 +
    1.14 +	cells = 0;
    1.15 +	xsz = ysz = 0;
    1.16 +}
    1.17 +
    1.18 +Level::~Level()
    1.19 +{
    1.20 +	delete [] cells;
    1.21 +}
    1.22 +
    1.23 +bool Level::load(const char *fname)
    1.24 +{
    1.25 +	xsz = ysz = 64;
    1.26 +
    1.27 +	cells = new GridCell*[xsz * ysz];
    1.28 +	memset(cells, 0, xsz * ysz * sizeof *cells);
    1.29 +
    1.30 +	GridCell *g = new GridCell;
    1.31 +	g->add_tile(new Tile);
    1.32 +	cells[ysz / 2 * xsz + xsz / 2] = g;
    1.33 +
    1.34 +	return true;
    1.35 +}
    1.36 +
    1.37 +bool Level::save(const char *fname) const
    1.38 +{
    1.39 +	return false;
    1.40 +}
    1.41 +
    1.42 +const GridCell *Level::get_cell(int x, int y) const
    1.43 +{
    1.44 +	if(x < 0 || x >= xsz || y < 0 || y >= ysz) {
    1.45 +		return 0;
    1.46 +	}
    1.47 +	return cells[y * xsz + x];
    1.48 +}
    1.49 +
    1.50 +Vector3 Level::get_cell_pos(int x, int y) const
    1.51 +{
    1.52 +	float posx = (x - xsz / 2) * cell_size;
    1.53 +	float posy = (y - ysz / 2) * cell_size;
    1.54 +	return Vector3(posx, 0, posy);
    1.55 +}
    1.56 +
    1.57 +void Level::draw() const
    1.58 +{
    1.59 +	glMatrixMode(GL_MODELVIEW);
    1.60 +
    1.61 +	draw_grid();
    1.62 +
    1.63 +	for(int i=0; i<ysz; i++) {
    1.64 +		for(int j=0; j<xsz; j++) {
    1.65 +			const GridCell *cell = get_cell(j, i);
    1.66 +			if(cell) {
    1.67 +				Vector3 pos = get_cell_pos(j, i);
    1.68 +				glPushMatrix();
    1.69 +				glTranslatef(pos.x, pos.y, pos.z);
    1.70 +				glScalef(cell_size, cell_size, cell_size);
    1.71 +				cell->draw();
    1.72 +				glPopMatrix();
    1.73 +			}
    1.74 +		}
    1.75 +	}
    1.76 +}
    1.77 +
    1.78 +void Level::draw_grid() const
    1.79 +{
    1.80 +	float xlen = xsz * cell_size;
    1.81 +	float ylen = ysz * cell_size;
    1.82 +
    1.83 +	glPushAttrib(GL_ENABLE_BIT);
    1.84 +	glDisable(GL_LIGHTING);
    1.85 +
    1.86 +	glBegin(GL_LINES);
    1.87 +	glColor3f(0.4, 0.4, 0.4);
    1.88 +
    1.89 +	float y = -ylen / 2.0 - cell_size / 2.0;
    1.90 +	for(int i=0; i<ysz; i++) {
    1.91 +		glVertex3f(-xlen / 2.0, 0, y);
    1.92 +		glVertex3f(xlen / 2.0, 0, y);
    1.93 +		y += cell_size;
    1.94 +	}
    1.95 +
    1.96 +	float x = -xlen / 2.0 - cell_size / 2.0;
    1.97 +	for(int i=0; i<xsz; i++) {
    1.98 +		glVertex3f(x, 0, -ylen / 2.0);
    1.99 +		glVertex3f(x, 0, ylen / 2.0);
   1.100 +		x += cell_size;
   1.101 +	}
   1.102 +	glEnd();
   1.103 +
   1.104 +	glPopAttrib();
   1.105 +}
   1.106 +
   1.107 +void GridCell::add_tile(const Tile *tile)
   1.108 +{
   1.109 +	tiles.push_back(tile);
   1.110 +}
   1.111 +
   1.112 +void GridCell::draw() const
   1.113 +{
   1.114 +	for(size_t i=0; i<tiles.size(); i++) {
   1.115 +		tiles[i]->draw();
   1.116 +	}
   1.117 +}