stratgame

view 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 source
1 #include <stdio.h>
2 #include <string.h>
3 #include "opengl.h"
4 #include "level.h"
5 #include "datapath.h"
6 #include "terrain.h"
8 using namespace tinyxml2;
10 Level::Level()
11 {
12 }
14 Level::~Level()
15 {
16 }
18 bool Level::load(const char *fname)
19 {
20 push_data_path();
21 add_data_path(fname);
23 if(xml.LoadFile(fname) != 0) {
24 fprintf(stderr, "failed to load level: %s\n", fname);
25 pop_data_path();
26 return false;
27 }
29 if(strcmp(xml.RootElement()->Name(), "level") != 0) {
30 fprintf(stderr, "invalid level file: %s\n", fname);
31 pop_data_path();
32 return false;
33 }
35 XMLNode *node = xml.RootElement()->FirstChild();
36 do {
37 XMLElement *elem = node->ToElement();
38 if(!elem) {
39 fprintf(stderr, "ignoring non-element child of <level>\n");
40 continue;
41 }
43 if(strcmp(elem->Name(), "terrain") == 0) {
44 if(terrain) {
45 fprintf(stderr, "<level> must only have a single terrain child! ignoring\n");
46 continue;
47 }
49 terrain = new Terrain;
50 if(!terrain->load(elem)) {
51 fprintf(stderr, "failed to load terrain\n");
52 pop_data_path();
53 return false;
54 }
56 float val;
57 if(elem->QueryFloatAttribute("size", &val) == 0) {
58 printf("terrain size: %f\n", val);
59 terrain->set_size(val);
60 }
61 } else {
62 fprintf(stderr, "ignoring unexpected child of <level>: %s\n", elem->Name());
63 continue;
64 }
66 } while((node = node->NextSibling()));
68 pop_data_path();
69 return true;
70 }
72 void Level::draw() const
73 {
74 terrain->draw();
75 }