#pragma once #include <string> #include "ResourceLibrary.hpp" #include "Singleton.hpp" #include "FileLoader.hpp" #include "Projector.hpp" class ResourceLoadCaller: public Singleton<ResourceLoadCaller> { friend class Singleton<ResourceLoadCaller>; private: ResourceLibrary& resLib; unordered_map<string,unique_ptr<FileLoader>> extensionMap; private: //초기화 explicit ResourceLoadCaller(ResourceLibrary& resLib); //파일:원시객체 1:1 대응을 위한 로더들을 넣어준다. void initExtensionMap(); //리소스를 extensionMap에 따라서 resLib에 넣어준다. void insertResources(); //확장자 or 이름이 없는 경우 빈 string 반환. string getFileExtension(const string& filePath); string getFileName(const string& filePath); }; //helper macro #define RES_LOAD_CALLER ResourceLoadCaller::Instance() | cs |
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 | #include <memory> #include "FileLoader.hpp" #include "Singleton.hpp" using std::unordered_map; using std::shared_ptr; using std::static_pointer_cast; class ResourceLibrary: public Singleton<ResourceLibrary> { friend class Singleton<ResourceLibrary>; private: unordered_map<string, shared_ptr<LoadedData>> resourceMap; public: //할당 실패시 false bool insert(const string& key, shared_ptr<LoadedData> item); //Data를 이용해 반환type을 얻고, HashStr을 이용해 해쉬key를 얻는다. template<typename Data> shared_ptr<Data> operator[](const Data& keyObj){ auto result = resourceMap.find(keyObj.HashStr()); if(result == resourceMap.end()) { return nullptr; } else { return static_pointer_cast<Data>(result->second); } } template<> shared_ptr<LoadedData> operator[](const LoadedData& keyObj){ return nullptr; //글쎄, 허용할수도 있지 않나?...? } private: ResourceLibrary(){} }; //helper macro #define RES_LIB ResourceLibrary::Instance() | cs |
어떤 파일과 1:1 대응하는 클래스를 원시리소스 혹은 원시클래스라고 하자.
ResourceLoadCaller는 실행파일 옆의 resources폴더 하부의 모든 리소스를 로드한다.
이렇게 로드한 리소스들을 ResourceLibrary의 해쉬맵에 원시클래스의 객체로 저장한다.
이 때 확장자를 포함한 파일 이름이 key가 되며
원시클래스는 파일의 확장자에 따라 정해진다.
(확장자:원시클래스 관계는 1:1이지만 바뀔지도 모르겠다.. 아직 덜 정해진 부분)
그래서 확장자가 없는 파일은 기본적으로 로드하지 않는다.
또 사용자가 원시클래스를 정해주지 않은 확장자의 파일은 로드하지 않는다.
1.사용자는 파일확장자에 대응하는 LoadedData상속 클래스를 만들어서 ResourcLoadCaller에 줘야 한다.
2.기본적으로 .png파일은 Png객체로, mp3는 Mp3객체로 .txt는 Txt객체로 로드되어 ResourceLibrary에 저장된다.
그 외에도 내가 이 엔진을 사용하면서 필요할 때마다 추가할 생각이다.
3.사용자는 key를 이용하여 ResourceLibrary에서 원하는 원시리소스를 가져올 수 있다.
4.ResourceLoadCaller와 ResourceLibrary는 시스템에 반드시 하나만 존재하는 것이 보장된다.
그냥 코딩이 좀 질려서 이런 거 올려봅니다...
댓글 2