CG-Project2
Loading...
Searching...
No Matches
Resource.hpp
1#pragma once
2
3#include "Utils.hpp"
4
5#include <map>
6#include <string>
7
8namespace ogl {
9
10namespace res {
11const std::string DEFAULT_LOCATION = "./resources/";
12}
13
14class Resource {
15 public:
16 inline std::string getResourceLocation() const { return this->m_location.c_str(); }
17
18 inline std::string getResourceName() const { return this->m_name.c_str(); }
19
20 inline std::string getResourceContent() const { return this->m_content.c_str(); }
21
22 inline unsigned int getResourceId() const { return this->m_id; }
23
24 inline void setResourceId(const unsigned int &id) { this->m_id = id; }
25
26 inline bool isResourceLoaded() const { return !this->m_content.empty(); }
27
28 void loadResource();
29
30 void unloadResource();
31
32 Resource() = delete;
33
34 Resource(const std::string &location, const std::string &file) : m_location(std::move(location)), m_file(std::move(file)) {
35 this->m_name = std::string(this->m_file);
36 }
37
38 Resource(const std::string &file) : Resource(res::DEFAULT_LOCATION, file) {}
39
40 ~Resource() = default;
41
42 private:
43 unsigned int m_id = 0;
44 std::string m_location;
45 std::string m_file;
46 std::string m_name;
47 std::string m_content;
48};
49
50namespace res {
51const Resource EMPTY_RESOURCE = Resource("", "");
52}
53
54class ResourceManager {
55 public:
56 unsigned int addResource(const std::string &location, const std::string &file);
57
58 unsigned int addResource(const std::string &file);
59
60 bool removeResource(const unsigned int& id);
61
62 inline unsigned int getCurrentId() const { return this->m_currentId; }
63
64 Resource getResource(const unsigned int &id) const;
65
66 ResourceManager(ResourceManager &other) = delete;
67
68 void operator=(const ResourceManager &other) = delete;
69
70 inline static Shared<ResourceManager> instance() {
71 if (s_pointer == nullptr) {
72 Shared<ResourceManager> copy(new ResourceManager());
73 copy.swap(s_pointer);
74 }
75 return s_pointer;
76 }
77
78 private:
79 inline static Shared<ResourceManager> s_pointer = nullptr;
80 unsigned int m_currentId = 1;
81
82 std::map<unsigned int, Resource> m_map{};
83
84 ResourceManager() = default;
85};
86
87} // namespace ogl
Definition Resource.hpp:14