stratgame
diff level/src/level.cc @ 2:369b51c9e4a8
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 23 May 2012 07:25:43 +0300 |
parents | 55a43e27339a |
children | 8d95187cb3ee |
line diff
1.1 --- a/level/src/level.cc Tue May 22 05:02:00 2012 +0300 1.2 +++ b/level/src/level.cc Wed May 23 07:25:43 2012 +0300 1.3 @@ -1,6 +1,9 @@ 1.4 #include <stdio.h> 1.5 #include <string.h> 1.6 #include "level.h" 1.7 +#include "datapath.h" 1.8 + 1.9 +using namespace tinyxml2; 1.10 1.11 Level::Level() 1.12 { 1.13 @@ -12,14 +15,162 @@ 1.14 1.15 bool Level::load(const char *fname) 1.16 { 1.17 - if(!xml.LoadFile(fname)) { 1.18 + push_data_path(); 1.19 + add_data_path(fname); 1.20 + 1.21 + if(xml.LoadFile(fname) != 0) { 1.22 fprintf(stderr, "failed to load level: %s\n", fname); 1.23 + pop_data_path(); 1.24 return false; 1.25 } 1.26 1.27 if(strcmp(xml.RootElement()->Name(), "level") != 0) { 1.28 fprintf(stderr, "invalid level file: %s\n", fname); 1.29 + pop_data_path(); 1.30 return false; 1.31 } 1.32 + 1.33 + XMLNode *node = xml.RootElement()->FirstChild(); 1.34 + do { 1.35 + XMLElement *elem = node->ToElement(); 1.36 + if(elem) { 1.37 + if(strcmp(elem->Name(), "map") == 0) { 1.38 + LevelMap map; 1.39 + 1.40 + if(!map.load(elem)) { 1.41 + pop_data_path(); 1.42 + return false; 1.43 + } 1.44 + levelmaps[map.get_name()] = std::move(map); 1.45 + 1.46 + } else { 1.47 + fprintf(stderr, "ignoring unrecognized element: %s\n", elem->Name()); 1.48 + } 1.49 + } 1.50 + } while((node = node->NextSibling())); 1.51 + 1.52 + pop_data_path(); 1.53 + return true; 1.54 +} 1.55 + 1.56 + 1.57 +LevelMap::LevelMap() 1.58 +{ 1.59 + init(); 1.60 +} 1.61 + 1.62 +void LevelMap::init() 1.63 +{ 1.64 + scale = 1.0f; 1.65 + img_init(&img); 1.66 + name = 0; 1.67 +} 1.68 + 1.69 +LevelMap::~LevelMap() 1.70 +{ 1.71 + destroy(); 1.72 +} 1.73 + 1.74 +void LevelMap::destroy() 1.75 +{ 1.76 + if(img.pixels) { 1.77 + img_destroy(&img); 1.78 + } 1.79 + delete [] name; 1.80 +} 1.81 + 1.82 +// copy 1.83 +LevelMap::LevelMap(const LevelMap &map) 1.84 +{ 1.85 + init(); 1.86 + *this = map; 1.87 +} 1.88 + 1.89 +LevelMap &LevelMap::operator =(const LevelMap &map) 1.90 +{ 1.91 + if(this != &map) { 1.92 + destroy(); 1.93 + 1.94 + scale = map.scale; 1.95 + img_init(&img); 1.96 + img_copy(&img, (img_pixmap*)&map.img); 1.97 + 1.98 + name = new char[strlen(map.name) + 1]; 1.99 + strcpy(name, map.name); 1.100 + } 1.101 + return *this; 1.102 +} 1.103 + 1.104 +// move semantics 1.105 +LevelMap::LevelMap(LevelMap &&map) 1.106 +{ 1.107 + init(); 1.108 + *this = std::move(map); 1.109 +} 1.110 + 1.111 +LevelMap &LevelMap::operator =(LevelMap &&map) 1.112 +{ 1.113 + if(this != &map) { 1.114 + destroy(); 1.115 + 1.116 + scale = map.scale; 1.117 + 1.118 + img = map.img; 1.119 + map.img.pixels = 0; 1.120 + map.img.name = 0; 1.121 + 1.122 + name = map.name; 1.123 + map.name = 0; 1.124 + } 1.125 + 1.126 + return *this; 1.127 +} 1.128 + 1.129 +bool LevelMap::load(XMLElement *xelem) 1.130 +{ 1.131 + char fname[PATH_MAX]; 1.132 + 1.133 + const char *attr = xelem->Attribute("type"); 1.134 + if(!attr) { 1.135 + fprintf(stderr, "map element without a type attribute\n"); 1.136 + return false; 1.137 + } 1.138 + name = new char[strlen(attr) + 1]; 1.139 + strcpy(name, attr); 1.140 + 1.141 + attr = xelem->Attribute("file"); 1.142 + if(!attr) { 1.143 + fprintf(stderr, "map element without a file attribute\n"); 1.144 + return false; 1.145 + } 1.146 + if(!find_file(attr, fname)) { 1.147 + fprintf(stderr, "failed to locate image: %s\n", attr); 1.148 + return false; 1.149 + } 1.150 + 1.151 + if(img_load(&img, fname) == -1) { 1.152 + fprintf(stderr, "failed to load image: %s\n", fname); 1.153 + return false; 1.154 + } 1.155 + 1.156 + float val; 1.157 + if(xelem->QueryFloatAttribute("scale", &val) == 0) { 1.158 + scale = val; 1.159 + } 1.160 return true; 1.161 } 1.162 + 1.163 +float LevelMap::get_scale() const 1.164 +{ 1.165 + return scale; 1.166 +} 1.167 + 1.168 +const img_pixmap *LevelMap::get_pixmap() const 1.169 +{ 1.170 + return &img; 1.171 +} 1.172 + 1.173 +const char *LevelMap::get_name() const 1.174 +{ 1.175 + return name; 1.176 +}