stratgame

view level/src/level.cc @ 4:cd12944a8ea8

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 25 May 2012 05:28:20 +0300
parents 369b51c9e4a8
children 2e38715de41b
line source
1 #include <stdio.h>
2 #include <string.h>
3 #include "level.h"
4 #include "datapath.h"
5 #include "opengl.h"
7 using namespace tinyxml2;
9 Level::Level()
10 {
11 }
13 Level::~Level()
14 {
15 }
17 bool Level::load(const char *fname)
18 {
19 push_data_path();
20 add_data_path(fname);
22 if(xml.LoadFile(fname) != 0) {
23 fprintf(stderr, "failed to load level: %s\n", fname);
24 pop_data_path();
25 return false;
26 }
28 if(strcmp(xml.RootElement()->Name(), "level") != 0) {
29 fprintf(stderr, "invalid level file: %s\n", fname);
30 pop_data_path();
31 return false;
32 }
34 XMLNode *node = xml.RootElement()->FirstChild();
35 do {
36 XMLElement *elem = node->ToElement();
37 if(elem) {
38 if(strcmp(elem->Name(), "map") == 0) {
39 LevelMap map;
41 if(!map.load(elem)) {
42 pop_data_path();
43 return false;
44 }
45 levelmaps[map.get_name()] = std::move(map);
47 } else {
48 fprintf(stderr, "ignoring unrecognized element: %s\n", elem->Name());
49 }
50 }
51 } while((node = node->NextSibling()));
53 pop_data_path();
54 return true;
55 }
57 void Level::draw() const
58 {
59 auto iter = levelmaps.find("height");
60 if(iter == levelmaps.end()) {
61 return;
62 }
64 img_pixmap *hmap = (img_pixmap*)iter->second.get_pixmap();
66 glBegin(GL_POINTS);
67 for(int i=0; i<hmap->height; i++) {
68 for(int j=0; j<hmap->width; j++) {
69 int height;
70 img_getpixel1i(hmap, j, i, &height);
72 float x = (float)j / (float)hmap->width - 0.5;
73 float y = height / 255.0;
74 float z = (float)i / (float)hmap->height - 0.5;
76 glColor3f(y, 0.0, 1.0 - y);
77 glVertex3f(x, y, z);
78 }
79 }
80 glEnd();
81 }
84 LevelMap::LevelMap()
85 {
86 init();
87 }
89 void LevelMap::init()
90 {
91 scale = 1.0f;
92 img_init(&img);
93 name = 0;
94 }
96 LevelMap::~LevelMap()
97 {
98 destroy();
99 }
101 void LevelMap::destroy()
102 {
103 if(img.pixels) {
104 img_destroy(&img);
105 }
106 delete [] name;
107 }
109 // copy
110 LevelMap::LevelMap(const LevelMap &map)
111 {
112 init();
113 *this = map;
114 }
116 LevelMap &LevelMap::operator =(const LevelMap &map)
117 {
118 if(this != &map) {
119 destroy();
121 scale = map.scale;
122 img_init(&img);
123 img_copy(&img, (img_pixmap*)&map.img);
125 name = new char[strlen(map.name) + 1];
126 strcpy(name, map.name);
127 }
128 return *this;
129 }
131 // move semantics
132 LevelMap::LevelMap(LevelMap &&map)
133 {
134 init();
135 *this = std::move(map);
136 }
138 LevelMap &LevelMap::operator =(LevelMap &&map)
139 {
140 if(this != &map) {
141 destroy();
143 scale = map.scale;
145 img = map.img;
146 map.img.pixels = 0;
147 map.img.name = 0;
149 name = map.name;
150 map.name = 0;
151 }
153 return *this;
154 }
156 bool LevelMap::load(XMLElement *xelem)
157 {
158 char fname[PATH_MAX];
160 const char *attr = xelem->Attribute("type");
161 if(!attr) {
162 fprintf(stderr, "map element without a type attribute\n");
163 return false;
164 }
165 name = new char[strlen(attr) + 1];
166 strcpy(name, attr);
168 attr = xelem->Attribute("file");
169 if(!attr) {
170 fprintf(stderr, "map element without a file attribute\n");
171 return false;
172 }
173 if(!find_file(attr, fname)) {
174 fprintf(stderr, "failed to locate image: %s\n", attr);
175 return false;
176 }
178 if(img_load(&img, fname) == -1) {
179 fprintf(stderr, "failed to load image: %s\n", fname);
180 return false;
181 }
183 float val;
184 if(xelem->QueryFloatAttribute("scale", &val) == 0) {
185 scale = val;
186 }
187 return true;
188 }
190 float LevelMap::get_scale() const
191 {
192 return scale;
193 }
195 const img_pixmap *LevelMap::get_pixmap() const
196 {
197 return &img;
198 }
200 const char *LevelMap::get_name() const
201 {
202 return name;
203 }