summary refs log tree commit diff stats
path: root/tools/mapedit/src/object.cpp
blob: 4cd267d193cbc699425ca615e13530e8dfcdd5fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "object.h"
#include <dirent.h>
#include <libxml/parser.h>
#include <memory>

static std::map<std::string, std::shared_ptr<MapObject>> allObjects;
static bool objsInit = false;

const std::map<std::string, std::shared_ptr<MapObject>> MapObject::getAllObjects()
{
  if (!objsInit)
  {
    DIR* dir = opendir("../../../entities/");
    if (dir != NULL)
    {
      struct dirent* ent;
      while ((ent = readdir(dir)) != NULL)
      {
        std::string path = ent->d_name;
        if ((path.length() >= 4) && (path.substr(path.length() - 4, 4) == ".xml"))
        {
          std::string name = path.substr(0, path.length() - 4);
          auto obj = std::make_shared<MapObject>(name.c_str());
        
          allObjects[name] = obj;
        }
      }
    }
    
    objsInit = true;
  }
  
  return allObjects;
}

MapObject::MapObject(const char* filename)
{
  type = filename;
  
  xmlDocPtr doc = xmlParseFile(("../../../entities/" + std::string(filename) + ".xml").c_str());
  if (doc == nullptr) throw MapObjectLoadException(filename);

  xmlNodePtr top = xmlDocGetRootElement(doc);
  if (top == nullptr) throw MapObjectLoadException(filename);

  if (xmlStrcmp(top->name, (const xmlChar*) "entity-def"))
  {
    throw MapObjectLoadException(filename);
  }

  for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next)
  {
    if (!xmlStrcmp(node->name, (const xmlChar*) "sprite"))
    {
      xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
      std::string spriteFile = (char*) key;
      xmlFree(key);
      
      sprite = wxImage("../../" + spriteFile);
    } else if (!xmlStrcmp(node->name, (const xmlChar*) "action"))
    {
      xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
      action = (char*) key;
      xmlFree(key);
    } else if (!xmlStrcmp(node->name, (const xmlChar*) "size"))
    {
      xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
      sscanf((char*) key, "%d,%d", &width, &height);
      xmlFree(key);
    }
  }

  xmlFreeDoc(doc);
}

wxBitmap MapObject::getSprite() const
{
  return sprite;
}

std::string MapObject::getAction() const
{
  return action;
}

int MapObject::getWidth() const
{
  return width;
}

int MapObject::getHeight() const
{
  return height;
}

std::string MapObject::getType() const
{
  return type;
}