stratgame

diff level/src/level.cc @ 5:2e38715de41b

terrain
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 27 May 2012 07:00:48 +0300
parents 8d95187cb3ee
children
line diff
     1.1 --- a/level/src/level.cc	Fri May 25 05:28:20 2012 +0300
     1.2 +++ b/level/src/level.cc	Sun May 27 07:00:48 2012 +0300
     1.3 @@ -1,8 +1,9 @@
     1.4  #include <stdio.h>
     1.5  #include <string.h>
     1.6 +#include "opengl.h"
     1.7  #include "level.h"
     1.8  #include "datapath.h"
     1.9 -#include "opengl.h"
    1.10 +#include "terrain.h"
    1.11  
    1.12  using namespace tinyxml2;
    1.13  
    1.14 @@ -34,20 +35,34 @@
    1.15  	XMLNode *node = xml.RootElement()->FirstChild();
    1.16  	do {
    1.17  		XMLElement *elem = node->ToElement();
    1.18 -		if(elem) {
    1.19 -			if(strcmp(elem->Name(), "map") == 0) {
    1.20 -				LevelMap map;
    1.21 +		if(!elem) {
    1.22 +			fprintf(stderr, "ignoring non-element child of <level>\n");
    1.23 +			continue;
    1.24 +		}
    1.25  
    1.26 -				if(!map.load(elem)) {
    1.27 -					pop_data_path();
    1.28 -					return false;
    1.29 -				}
    1.30 -				levelmaps[map.get_name()] = std::move(map);
    1.31 +		if(strcmp(elem->Name(), "terrain") == 0) {
    1.32 +			if(terrain) {
    1.33 +				fprintf(stderr, "<level> must only have a single terrain child! ignoring\n");
    1.34 +				continue;
    1.35 +			}
    1.36  
    1.37 -			} else {
    1.38 -				fprintf(stderr, "ignoring unrecognized element: %s\n", elem->Name());
    1.39 +			terrain = new Terrain;
    1.40 +			if(!terrain->load(elem)) {
    1.41 +				fprintf(stderr, "failed to load terrain\n");
    1.42 +				pop_data_path();
    1.43 +				return false;
    1.44  			}
    1.45 +
    1.46 +			float val;
    1.47 +			if(elem->QueryFloatAttribute("size", &val) == 0) {
    1.48 +				printf("terrain size: %f\n", val);
    1.49 +				terrain->set_size(val);
    1.50 +			}
    1.51 +		} else {
    1.52 +			fprintf(stderr, "ignoring unexpected child of <level>: %s\n", elem->Name());
    1.53 +			continue;
    1.54  		}
    1.55 +
    1.56  	} while((node = node->NextSibling()));
    1.57  
    1.58  	pop_data_path();
    1.59 @@ -56,148 +71,5 @@
    1.60  
    1.61  void Level::draw() const
    1.62  {
    1.63 -	auto iter = levelmaps.find("height");
    1.64 -	if(iter == levelmaps.end()) {
    1.65 -		return;
    1.66 -	}
    1.67 -
    1.68 -	img_pixmap *hmap = (img_pixmap*)iter->second.get_pixmap();
    1.69 -
    1.70 -	glBegin(GL_POINTS);
    1.71 -	for(int i=0; i<hmap->height; i++) {
    1.72 -		for(int j=0; j<hmap->width; j++) {
    1.73 -			int height;
    1.74 -			img_getpixel1i(hmap, j, i, &height);
    1.75 -
    1.76 -			float x = (float)j / (float)hmap->width - 0.5;
    1.77 -			float y = height / 255.0;
    1.78 -			float z = (float)i / (float)hmap->height - 0.5;
    1.79 -
    1.80 -			glColor3f(y, 0.0, 1.0 - y);
    1.81 -			glVertex3f(x, y, z);
    1.82 -		}
    1.83 -	}
    1.84 -	glEnd();
    1.85 +	terrain->draw();
    1.86  }
    1.87 -
    1.88 -
    1.89 -LevelMap::LevelMap()
    1.90 -{
    1.91 -	init();
    1.92 -}
    1.93 -
    1.94 -void LevelMap::init()
    1.95 -{
    1.96 -	scale = 1.0f;
    1.97 -	img_init(&img);
    1.98 -	name = 0;
    1.99 -}
   1.100 -
   1.101 -LevelMap::~LevelMap()
   1.102 -{
   1.103 -	destroy();
   1.104 -}
   1.105 -
   1.106 -void LevelMap::destroy()
   1.107 -{
   1.108 -	if(img.pixels) {
   1.109 -		img_destroy(&img);
   1.110 -	}
   1.111 -	delete [] name;
   1.112 -}
   1.113 -
   1.114 -// copy
   1.115 -LevelMap::LevelMap(const LevelMap &map)
   1.116 -{
   1.117 -	init();
   1.118 -	*this = map;
   1.119 -}
   1.120 -
   1.121 -LevelMap &LevelMap::operator =(const LevelMap &map)
   1.122 -{
   1.123 -	if(this != &map) {
   1.124 -		destroy();
   1.125 -
   1.126 -		scale = map.scale;
   1.127 -		img_init(&img);
   1.128 -		img_copy(&img, (img_pixmap*)&map.img);
   1.129 -
   1.130 -		name = new char[strlen(map.name) + 1];
   1.131 -		strcpy(name, map.name);
   1.132 -	}
   1.133 -	return *this;
   1.134 -}
   1.135 -
   1.136 -// move semantics
   1.137 -LevelMap::LevelMap(LevelMap &&map)
   1.138 -{
   1.139 -	init();
   1.140 -	*this = std::move(map);
   1.141 -}
   1.142 -
   1.143 -LevelMap &LevelMap::operator =(LevelMap &&map)
   1.144 -{
   1.145 -	if(this != &map) {
   1.146 -		destroy();
   1.147 -
   1.148 -		scale = map.scale;
   1.149 -
   1.150 -		img = map.img;
   1.151 -		map.img.pixels = 0;
   1.152 -		map.img.name = 0;
   1.153 -
   1.154 -		name = map.name;
   1.155 -		map.name = 0;
   1.156 -	}
   1.157 -
   1.158 -	return *this;
   1.159 -}
   1.160 -
   1.161 -bool LevelMap::load(XMLElement *xelem)
   1.162 -{
   1.163 -	char fname[PATH_MAX];
   1.164 -
   1.165 -	const char *attr = xelem->Attribute("type");
   1.166 -	if(!attr) {
   1.167 -		fprintf(stderr, "map element without a type attribute\n");
   1.168 -		return false;
   1.169 -	}
   1.170 -	name = new char[strlen(attr) + 1];
   1.171 -	strcpy(name, attr);
   1.172 -
   1.173 -	attr = xelem->Attribute("file");
   1.174 -	if(!attr) {
   1.175 -		fprintf(stderr, "map element without a file attribute\n");
   1.176 -		return false;
   1.177 -	}
   1.178 -	if(!find_file(attr, fname)) {
   1.179 -		fprintf(stderr, "failed to locate image: %s\n", attr);
   1.180 -		return false;
   1.181 -	}
   1.182 -
   1.183 -	if(img_load(&img, fname) == -1) {
   1.184 -		fprintf(stderr, "failed to load image: %s\n", fname);
   1.185 -		return false;
   1.186 -	}
   1.187 -
   1.188 -	float val;
   1.189 -	if(xelem->QueryFloatAttribute("scale", &val) == 0) {
   1.190 -		scale = val;
   1.191 -	}
   1.192 -	return true;
   1.193 -}
   1.194 -
   1.195 -float LevelMap::get_scale() const
   1.196 -{
   1.197 -	return scale;
   1.198 -}
   1.199 -
   1.200 -const img_pixmap *LevelMap::get_pixmap() const
   1.201 -{
   1.202 -	return &img;
   1.203 -}
   1.204 -
   1.205 -const char *LevelMap::get_name() const
   1.206 -{
   1.207 -	return name;
   1.208 -}