Oxygine  1
2g game engine
Resources.h
1 #pragma once
2 #include "oxygine-include.h"
3 #include <string>
4 #include <list>
5 #include <string.h>
6 #include "pugixml/pugixml.hpp"
7 #include "closure/closure.h"
8 #include "core/Object.h"
9 #include "Resource.h"
10 #ifdef __S3E__
11 #include <map>
12 #else
13 #include <unordered_map>
14 #endif
15 
16 
17 namespace oxygine
18 {
20  {
21  public:
22  ResourcesLoadOptions() : _loadCompletely(true), _useLoadCounter(false), _shortenIDS(false) {};
23 
24  //load only Resources definitions. Skips internal heavy data (atlasses/textures/buffers). Could be overridden in xml: <your_res_type ... load = "false"/>
25  ResourcesLoadOptions& dontLoadAll(bool v = false) { _loadCompletely = v; return *this; }
26 
27  //use load counter internally
28  ResourcesLoadOptions& useLoadCounter(bool v = true) { _useLoadCounter = v; return *this; }
29 
30  //use not standard folder with prebuilt resources (atlasses, fonts, etc)
31  ResourcesLoadOptions& prebuiltFolder(const std::string& folder) {_prebuilFolder = folder; return *this; }
32 
33  //use load counter internally
34  ResourcesLoadOptions& shortenIDS(bool v = true) { _shortenIDS = v; return *this; }
35 
36 
37  bool _loadCompletely;
38  bool _useLoadCounter;
39  bool _shortenIDS;
40  std::string _prebuilFolder;
41  };
42 
43  class Resources: public Resource
44  {
45  INHERITED(Resource);
46  public:
47  typedef std::vector<spResource> resources;
48 #ifdef __S3E__
49  typedef std::map<std::string, spResource> resourcesMap;
50 #else
51  typedef std::unordered_map<std::string, spResource> resourcesMap;
52 #endif
53 
54  typedef Resource* (*createResourceCallback)(CreateResourceContext& context);
55  typedef Closure<void (Resource*)> ResLoadedCallback;
56 
61  static void registerResourceType(createResourceCallback creationCallback, const char* resTypeID);
62  static void unregisterResourceType(const char* resTypeID);
63 
64  Resources();
65  ~Resources();
66 
71  bool loadXML(const std::string& xmlFile, const ResourcesLoadOptions& opt = ResourcesLoadOptions());
72 
74  void add(Resource* r, bool accessByShortenID = false);
75 
77  void load(ResLoadedCallback cb = ResLoadedCallback());
78 
80  void unload();
81 
83  void free();
84 
86  bool isEmpty() const;
87 
91  Resource* get(const std::string& id, error_policy ep = ep_show_error) const;
92 
94  //Resource* get(int index) const {return _fastAccessResources.at(index).get();}
95  //int getCount() const {return (int)_fastAccessResources.size();}
96 
97  Resource* operator[](const std::string& id) { return get(id); }
98 
102  ResAnim* getResAnim(const std::string& id, error_policy ep = ep_show_error) const;
103 
107  ResFont* getResFont(const std::string& id, error_policy ep = ep_show_error) const;
108 
109  template<class T>
110  T* getT(const std::string& id, error_policy ep = ep_show_error) const { return safeCast<T*>(get(id, ep)); }
111 
113  void print() const;
114 
116  void collect(resources&);
117 
118  resources& _getResources();
119  resourcesMap& _getResourcesMap();
120 
121  protected:
122  void updateName(const std::string& filename);
123  void _load(LoadResourcesContext* context) override;
124  void _unload() override;
125 
126 
128  {
129  registeredResource() {id[0] = 0;}
130  char id[16];
131  createResourceCallback cb;
132 
133  static bool comparePred2(const registeredResource& ob1, const char* ob)
134  {
135  return strcmp(ob1.id, ob) > 0;
136  }
137 
138  static bool comparePred(const registeredResource& ob1, const registeredResource& ob2)
139  {
140  return strcmp(ob1.id, ob2.id) > 0;
141  }
142 
143  bool operator < (const char* ob2) const
144  {
145  return strcmp(this->id, ob2) > 0;
146  }
147  };
148 
149 
150  resources _resources;
151  resourcesMap _resourcesMap;
152 
153 
154  typedef std::vector< registeredResource > registeredResources;
155  static registeredResources _registeredResources;
156 
157  std::string _name;
158  std::vector<pugi::xml_document*> _docs;
159  };
160 }
Definition: Resources.h:19
void print() const
Definition: ResFont.h:8
static void registerResourceType(createResourceCallback creationCallback, const char *resTypeID)
bool isEmpty() const
Definition: Resources.h:127
void load(ResLoadedCallback cb=ResLoadedCallback())
ResFont * getResFont(const std::string &id, error_policy ep=ep_show_error) const
Resource * operator[](const std::string &id)
Definition: Resources.h:97
–oxgl-end–!
Definition: Actor.h:14
bool loadXML(const std::string &xmlFile, const ResourcesLoadOptions &opt=ResourcesLoadOptions())
Definition: Resource.h:10
void add(Resource *r, bool accessByShortenID=false)
Definition: ResAnim.h:13
ResAnim * getResAnim(const std::string &id, error_policy ep=ep_show_error) const
Definition: CreateResourceContext.h:60
void collect(resources &)
Definition: Resources.h:43