diff --git a/src/Font.cpp b/src/Font.cpp index 329b6c12e..b49b4fe1d 100644 --- a/src/Font.cpp +++ b/src/Font.cpp @@ -79,9 +79,9 @@ void Font::initLibrary() } } -Font::Font(const ResourceManager& rm, const std::string& path, int size) : fontScale(1.0f), mSize(size), mPath(path) +Font::Font(int size, const std::string& path) : fontScale(1.0f), mSize(size), mPath(path) { - reload(rm); + reload(ResourceManager::getInstance()); } Font::~Font() @@ -89,17 +89,17 @@ Font::~Font() deinit(); } -void Font::reload(const ResourceManager& rm) +void Font::reload(std::shared_ptr& rm) { - init(rm.getFileData(mPath)); + init(rm->getFileData(mPath)); } -void Font::unload(const ResourceManager& rm) +void Font::unload(std::shared_ptr& rm) { deinit(); } -std::shared_ptr Font::get(ResourceManager& rm, const std::string& path, int size) +std::shared_ptr Font::get(int size, const std::string& path) { if(path.empty()) { @@ -115,9 +115,9 @@ std::shared_ptr Font::get(ResourceManager& rm, const std::string& path, in return foundFont->second.lock(); } - std::shared_ptr font = std::shared_ptr(new Font(rm, path, size)); + std::shared_ptr font = std::shared_ptr(new Font(size, path)); sFontMap[def] = std::weak_ptr(font); - rm.addReloadable(font); + ResourceManager::getInstance()->addReloadable(font); return font; } diff --git a/src/Font.h b/src/Font.h index a5e75d0ef..9298b3195 100644 --- a/src/Font.h +++ b/src/Font.h @@ -22,7 +22,7 @@ class Font : public IReloadable public: static void initLibrary(); - static std::shared_ptr get(ResourceManager& rm, const std::string& path, int size); + static std::shared_ptr get(int size, const std::string& path = getDefaultPath()); ~Font(); @@ -63,8 +63,8 @@ public: int getHeight() const; - void unload(const ResourceManager& rm) override; - void reload(const ResourceManager& rm) override; + void unload(std::shared_ptr& rm) override; + void reload(std::shared_ptr& rm) override; int getSize() const; @@ -78,7 +78,7 @@ private: static std::map< std::pair, std::weak_ptr > sFontMap; - Font(const ResourceManager& rm, const std::string& path, int size); + Font(int size, const std::string& path); void init(ResourceData data); void deinit(); diff --git a/src/Window.cpp b/src/Window.cpp index 8c4ce6dfb..cf1784443 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -58,14 +58,14 @@ bool Window::init(unsigned int width, unsigned int height) mInputManager->init(); - mResourceManager.reloadAll(); + ResourceManager::getInstance()->reloadAll(); //keep a reference to the default fonts, so they don't keep getting destroyed/recreated if(mDefaultFonts.empty()) { - mDefaultFonts.push_back(Font::get(mResourceManager, Font::getDefaultPath(), FONT_SIZE_SMALL)); - mDefaultFonts.push_back(Font::get(mResourceManager, Font::getDefaultPath(), FONT_SIZE_MEDIUM)); - mDefaultFonts.push_back(Font::get(mResourceManager, Font::getDefaultPath(), FONT_SIZE_LARGE)); + mDefaultFonts.push_back(Font::get(FONT_SIZE_SMALL)); + mDefaultFonts.push_back(Font::get(FONT_SIZE_MEDIUM)); + mDefaultFonts.push_back(Font::get(FONT_SIZE_LARGE)); } return true; @@ -74,7 +74,7 @@ bool Window::init(unsigned int width, unsigned int height) void Window::deinit() { mInputManager->deinit(); - mResourceManager.unloadAll(); + ResourceManager::getInstance()->unloadAll(); Renderer::deinit(); } @@ -153,11 +153,6 @@ InputManager* Window::getInputManager() return mInputManager; } -ResourceManager* Window::getResourceManager() -{ - return &mResourceManager; -} - void Window::setZoomFactor(const float& zoom) { mZoomFactor = zoom; diff --git a/src/Window.h b/src/Window.h index 9cb694d85..610ca817b 100644 --- a/src/Window.h +++ b/src/Window.h @@ -36,7 +36,6 @@ public: private: InputManager* mInputManager; - ResourceManager mResourceManager; std::vector mGuiStack; std::vector< std::shared_ptr > mDefaultFonts; diff --git a/src/components/ButtonComponent.cpp b/src/components/ButtonComponent.cpp index 19ad42939..6ae9f5ebd 100644 --- a/src/components/ButtonComponent.cpp +++ b/src/components/ButtonComponent.cpp @@ -76,7 +76,7 @@ void ButtonComponent::render(const Eigen::Affine3f& parentTrans) std::shared_ptr ButtonComponent::getFont() { - return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL); + return Font::get(FONT_SIZE_SMALL); } unsigned int ButtonComponent::getCurTextColor() const diff --git a/src/components/DateTimeComponent.cpp b/src/components/DateTimeComponent.cpp index cd6266f12..aaf624dfc 100644 --- a/src/components/DateTimeComponent.cpp +++ b/src/components/DateTimeComponent.cpp @@ -231,7 +231,7 @@ std::string DateTimeComponent::getDisplayString(DisplayMode mode) const std::shared_ptr DateTimeComponent::getFont() const { - return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_MEDIUM); + return Font::get(FONT_SIZE_MEDIUM); } void DateTimeComponent::updateTextCache() diff --git a/src/components/GuiDetectDevice.cpp b/src/components/GuiDetectDevice.cpp index 2777ae26d..79cf2c74b 100644 --- a/src/components/GuiDetectDevice.cpp +++ b/src/components/GuiDetectDevice.cpp @@ -82,7 +82,7 @@ void GuiDetectDevice::render(const Eigen::Affine3f& parentTrans) Eigen::Affine3f trans = parentTrans * getTransform(); Renderer::setMatrix(trans); - std::shared_ptr font = Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_MEDIUM); + std::shared_ptr font = Font::get(FONT_SIZE_MEDIUM); std::string playerString; std::stringstream stream; diff --git a/src/components/GuiGameList.cpp b/src/components/GuiGameList.cpp index a6746c571..27e17707f 100644 --- a/src/components/GuiGameList.cpp +++ b/src/components/GuiGameList.cpp @@ -25,7 +25,7 @@ bool GuiGameList::isDetailed() const GuiGameList::GuiGameList(Window* window) : GuiComponent(window), mTheme(new ThemeComponent(mWindow)), - mList(window, 0.0f, 0.0f, Font::get(*window->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_MEDIUM)), + mList(window, 0.0f, 0.0f, Font::get(FONT_SIZE_MEDIUM)), mScreenshot(window), mDescription(window), mRating(window), @@ -60,7 +60,7 @@ GuiGameList::GuiGameList(Window* window) : GuiComponent(window), mTransitionImage.setOrigin(0, 0); mHeaderText.setColor(0xFF0000FF); - mHeaderText.setFont(Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_LARGE)); + mHeaderText.setFont(Font::get(FONT_SIZE_LARGE)); mHeaderText.setPosition(0, 1); mHeaderText.setSize((float)Renderer::getScreenWidth(), 0); mHeaderText.setCentered(true); @@ -329,7 +329,7 @@ void GuiGameList::updateTheme() mList.setScrollSound(mTheme->getSound("menuScroll")); mList.setFont(mTheme->getListFont()); - mList.setPosition(0.0f, Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_LARGE)->getHeight() + 2.0f); + mList.setPosition(0.0f, Font::get(FONT_SIZE_LARGE)->getHeight() + 2.0f); if(!mTheme->getBool("hideHeader")) { diff --git a/src/components/GuiGameScraper.cpp b/src/components/GuiGameScraper.cpp index 701cafb95..55a10ef07 100644 --- a/src/components/GuiGameScraper.cpp +++ b/src/components/GuiGameScraper.cpp @@ -7,13 +7,13 @@ GuiGameScraper::GuiGameScraper(Window* window, ScraperSearchParams params, std::function doneFunc, std::function skipFunc) : GuiComponent(window), mList(window, Eigen::Vector2i(2, 7 + MAX_SCRAPER_RESULTS)), mBox(window, ":/frame.png"), - mHeader(window, params.game->getBaseName(), Font::get(*window->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_MEDIUM)), - mResultName(window, "", Font::get(*window->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_MEDIUM)), + mHeader(window, params.game->getBaseName(), Font::get(FONT_SIZE_MEDIUM)), + mResultName(window, "", Font::get(FONT_SIZE_MEDIUM)), mResultInfo(window), - mResultDesc(window, "", Font::get(*window->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL)), + mResultDesc(window, "", Font::get(FONT_SIZE_SMALL)), mResultThumbnail(window), - mSearchLabel(window, "Search for: ", Font::get(*window->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL)), + mSearchLabel(window, "Search for: ", Font::get(FONT_SIZE_SMALL)), mSearchText(window), mSearchParams(params), @@ -75,7 +75,7 @@ GuiGameScraper::GuiGameScraper(Window* window, ScraperSearchParams params, std:: //y = 5 is a spacer row - std::shared_ptr font = Font::get(*window->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL); + std::shared_ptr font = Font::get(FONT_SIZE_SMALL); mResultNames.reserve(MAX_SCRAPER_RESULTS); for(int i = 0; i < MAX_SCRAPER_RESULTS; i ++) { diff --git a/src/components/GuiInputConfig.cpp b/src/components/GuiInputConfig.cpp index 15fa2f21b..050a6122b 100644 --- a/src/components/GuiInputConfig.cpp +++ b/src/components/GuiInputConfig.cpp @@ -79,7 +79,7 @@ void GuiInputConfig::render(const Eigen::Affine3f& parentTrans) Eigen::Affine3f trans = parentTrans * getTransform(); Renderer::setMatrix(trans); - std::shared_ptr font = Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_MEDIUM); + std::shared_ptr font = Font::get(FONT_SIZE_MEDIUM); std::stringstream stream; stream << "PLAYER " << mTargetConfig->getPlayerNum() + 1 << ", press..."; diff --git a/src/components/GuiMenu.cpp b/src/components/GuiMenu.cpp index b5963790b..18eaffd9f 100644 --- a/src/components/GuiMenu.cpp +++ b/src/components/GuiMenu.cpp @@ -11,7 +11,7 @@ GuiMenu::GuiMenu(Window* window, GuiGameList* parent) : GuiComponent(window) { mParent = parent; - std::shared_ptr font = Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_LARGE); + std::shared_ptr font = Font::get(FONT_SIZE_LARGE); mList = new TextListComponent(mWindow, 0.0f, font->getHeight() + 2.0f, font); mList->setSelectedTextColor(0x0000FFFF); populateList(); diff --git a/src/components/ImageComponent.cpp b/src/components/ImageComponent.cpp index e86015237..6c129099e 100644 --- a/src/components/ImageComponent.cpp +++ b/src/components/ImageComponent.cpp @@ -4,7 +4,6 @@ #include #include "../Log.h" #include "../Renderer.h" -#include "../Window.h" Eigen::Vector2i ImageComponent::getTextureSize() const { @@ -73,10 +72,10 @@ void ImageComponent::setImage(std::string path) { mPath = path; - if(mPath.empty() || !mWindow->getResourceManager()->fileExists(mPath)) + if(mPath.empty() || !ResourceManager::getInstance()->fileExists(mPath)) mTexture.reset(); else - mTexture = TextureResource::get(*mWindow->getResourceManager(), mPath); + mTexture = TextureResource::get(mPath); resize(); } @@ -85,7 +84,7 @@ void ImageComponent::setImage(const char* path, size_t length) { mTexture.reset(); - mTexture = TextureResource::get(*mWindow->getResourceManager(), ""); + mTexture = TextureResource::get(""); mTexture->initFromMemory(path, length); resize(); @@ -231,12 +230,11 @@ bool ImageComponent::hasImage() return !mPath.empty(); } - void ImageComponent::copyScreen() { mTexture.reset(); - mTexture = TextureResource::get(*mWindow->getResourceManager(), ""); + mTexture = TextureResource::get(""); mTexture->initFromScreen(); resize(); diff --git a/src/components/NinePatchComponent.cpp b/src/components/NinePatchComponent.cpp index e4bc675d8..4fdc62a30 100644 --- a/src/components/NinePatchComponent.cpp +++ b/src/components/NinePatchComponent.cpp @@ -26,7 +26,7 @@ void NinePatchComponent::buildVertices() if(mColors != NULL) delete[] mColors; - mTexture = TextureResource::get(*mWindow->getResourceManager(), mPath); + mTexture = TextureResource::get(mPath); if(mTexture->getSize() == Eigen::Vector2i::Zero()) { diff --git a/src/components/OptionListComponent.h b/src/components/OptionListComponent.h index e2955d38c..f64392875 100644 --- a/src/components/OptionListComponent.h +++ b/src/components/OptionListComponent.h @@ -175,7 +175,7 @@ private: std::shared_ptr getFont() { - return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL); + return Font::get(FONT_SIZE_SMALL); } diff --git a/src/components/RatingComponent.cpp b/src/components/RatingComponent.cpp index 63ce05a4a..6a827cb78 100644 --- a/src/components/RatingComponent.cpp +++ b/src/components/RatingComponent.cpp @@ -4,8 +4,8 @@ RatingComponent::RatingComponent(Window* window) : GuiComponent(window) { - mFilledTexture = TextureResource::get(*window->getResourceManager(), ":/star_filled.png"); - mUnfilledTexture = TextureResource::get(*window->getResourceManager(), ":/star_unfilled.png"); + mFilledTexture = TextureResource::get(":/star_filled.png"); + mUnfilledTexture = TextureResource::get(":/star_unfilled.png"); mValue = 0.5f; mSize << 64 * 5.0f, 64; updateVertices(); diff --git a/src/components/SwitchComponent.cpp b/src/components/SwitchComponent.cpp index ef7156522..96ae1d0fb 100644 --- a/src/components/SwitchComponent.cpp +++ b/src/components/SwitchComponent.cpp @@ -8,7 +8,7 @@ SwitchComponent::SwitchComponent(Window* window, bool state) : GuiComponent(wind //mSize = Vector2u((unsigned int)(Renderer::getScreenWidth() * 0.05), // (unsigned int)(Renderer::getScreenHeight() * 0.05)); - mSize = Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_MEDIUM)->sizeText("OFF"); + mSize = Font::get(FONT_SIZE_MEDIUM)->sizeText("OFF"); } bool SwitchComponent::input(InputConfig* config, Input input) @@ -29,7 +29,7 @@ void SwitchComponent::render(const Eigen::Affine3f& parentTrans) Eigen::Affine3f trans = parentTrans * getTransform(); Renderer::setMatrix(trans); - Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_MEDIUM)->drawText(mState ? "ON" : "OFF", Eigen::Vector2f(0, 0), mState ? 0x00FF00FF : 0xFF0000FF); + Font::get(FONT_SIZE_MEDIUM)->drawText(mState ? "ON" : "OFF", Eigen::Vector2f(0, 0), mState ? 0x00FF00FF : 0xFF0000FF); //Renderer::popClipRect(); diff --git a/src/components/TextComponent.cpp b/src/components/TextComponent.cpp index e0484a53d..293624ef1 100644 --- a/src/components/TextComponent.cpp +++ b/src/components/TextComponent.cpp @@ -52,7 +52,7 @@ std::shared_ptr TextComponent::getFont() const if(mFont) return mFont; else - return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_MEDIUM); + return Font::get(FONT_SIZE_MEDIUM); } void TextComponent::render(const Eigen::Affine3f& parentTrans) diff --git a/src/components/TextEditComponent.cpp b/src/components/TextEditComponent.cpp index 6beb5afee..048a4ca03 100644 --- a/src/components/TextEditComponent.cpp +++ b/src/components/TextEditComponent.cpp @@ -210,7 +210,7 @@ void TextEditComponent::render(const Eigen::Affine3f& parentTrans) std::shared_ptr TextEditComponent::getFont() { - return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL); + return Font::get(FONT_SIZE_SMALL); } bool TextEditComponent::isMultiline() diff --git a/src/components/ThemeComponent.cpp b/src/components/ThemeComponent.cpp index 7d5638d99..43703f214 100644 --- a/src/components/ThemeComponent.cpp +++ b/src/components/ThemeComponent.cpp @@ -38,7 +38,7 @@ std::shared_ptr ThemeComponent::getListFont() if(mListFont) return mListFont; else - return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_MEDIUM); + return Font::get(FONT_SIZE_MEDIUM); } std::shared_ptr ThemeComponent::getDescriptionFont() @@ -46,7 +46,7 @@ std::shared_ptr ThemeComponent::getDescriptionFont() if(mDescFont) return mDescFont; else - return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL); + return Font::get(FONT_SIZE_SMALL); } std::shared_ptr ThemeComponent::getFastSelectFont() @@ -54,7 +54,7 @@ std::shared_ptr ThemeComponent::getFastSelectFont() if(mFastSelectFont) return mFastSelectFont; else - return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_LARGE); + return Font::get(FONT_SIZE_LARGE); } ThemeComponent::ThemeComponent(Window* window) : GuiComponent(window) @@ -389,5 +389,5 @@ std::shared_ptr ThemeComponent::resolveFont(pugi::xml_node node, std::stri size = defaultSize; } - return Font::get(*mWindow->getResourceManager(), path, size); + return Font::get(size, path); } diff --git a/src/resources/ResourceManager.cpp b/src/resources/ResourceManager.cpp index 0184ff74d..5f2021607 100644 --- a/src/resources/ResourceManager.cpp +++ b/src/resources/ResourceManager.cpp @@ -9,6 +9,20 @@ namespace fs = boost::filesystem; auto array_deleter = [](unsigned char* p) { delete[] p; }; auto nop_deleter = [](unsigned char* p) { }; +std::shared_ptr ResourceManager::sInstance = nullptr; + +ResourceManager::ResourceManager() +{ +} + +std::shared_ptr& ResourceManager::getInstance() +{ + if(!sInstance) + sInstance = std::shared_ptr(new ResourceManager()); + + return sInstance; +} + const ResourceData ResourceManager::getFileData(const std::string& path) const { //check if its embedded @@ -69,7 +83,7 @@ void ResourceManager::unloadAll() { if(!iter->expired()) { - iter->lock()->unload(*this); + iter->lock()->unload(sInstance); iter++; }else{ iter = mReloadables.erase(iter); @@ -84,7 +98,7 @@ void ResourceManager::reloadAll() { if(!iter->expired()) { - iter->lock()->reload(*this); + iter->lock()->reload(sInstance); iter++; }else{ iter = mReloadables.erase(iter); diff --git a/src/resources/ResourceManager.h b/src/resources/ResourceManager.h index ed1d04998..d9d4aa3b8 100644 --- a/src/resources/ResourceManager.h +++ b/src/resources/ResourceManager.h @@ -20,13 +20,15 @@ class ResourceManager; class IReloadable { public: - virtual void unload(const ResourceManager& rm) = 0; - virtual void reload(const ResourceManager& rm) = 0; + virtual void unload(std::shared_ptr& rm) = 0; + virtual void reload(std::shared_ptr& rm) = 0; }; class ResourceManager { public: + static std::shared_ptr& getInstance(); + void addReloadable(std::weak_ptr reloadable); void unloadAll(); @@ -36,6 +38,10 @@ public: bool fileExists(const std::string& path) const; private: + ResourceManager(); + + static std::shared_ptr sInstance; + ResourceData loadFile(const std::string& path) const; std::list< std::weak_ptr > mReloadables; diff --git a/src/resources/TextureResource.cpp b/src/resources/TextureResource.cpp index e0540796a..f53f75360 100644 --- a/src/resources/TextureResource.cpp +++ b/src/resources/TextureResource.cpp @@ -7,9 +7,10 @@ std::map< std::string, std::weak_ptr > TextureResource::sTextureMap; -TextureResource::TextureResource(const ResourceManager& rm, const std::string& path) : mTextureID(0), mPath(path), mTextureSize(Eigen::Vector2i::Zero()) +TextureResource::TextureResource(const std::string& path) : + mTextureID(0), mPath(path), mTextureSize(Eigen::Vector2i::Zero()) { - reload(rm); + reload(ResourceManager::getInstance()); } TextureResource::~TextureResource() @@ -17,15 +18,15 @@ TextureResource::~TextureResource() deinit(); } -void TextureResource::unload(const ResourceManager& rm) +void TextureResource::unload(std::shared_ptr& rm) { deinit(); } -void TextureResource::reload(const ResourceManager& rm) +void TextureResource::reload(std::shared_ptr& rm) { if(!mPath.empty()) - initFromResource(rm.getFileData(mPath)); + initFromResource(rm->getFileData(mPath)); } void TextureResource::initFromResource(const ResourceData data) @@ -130,12 +131,14 @@ void TextureResource::bind() const } -std::shared_ptr TextureResource::get(ResourceManager& rm, const std::string& path) +std::shared_ptr TextureResource::get(const std::string& path) { + std::shared_ptr& rm = ResourceManager::getInstance(); + if(path.empty()) { - std::shared_ptr tex(new TextureResource(rm, "")); - rm.addReloadable(tex); //make sure we're deinitialized even though we do nothing on reinitialization + std::shared_ptr tex(new TextureResource("")); + rm->addReloadable(tex); //make sure we're deinitialized even though we do nothing on reinitialization return tex; } @@ -148,8 +151,8 @@ std::shared_ptr TextureResource::get(ResourceManager& rm, const } } - std::shared_ptr tex = std::shared_ptr(new TextureResource(rm, path)); + std::shared_ptr tex = std::shared_ptr(new TextureResource(path)); sTextureMap[path] = std::weak_ptr(tex); - rm.addReloadable(tex); + rm->addReloadable(tex); return tex; } diff --git a/src/resources/TextureResource.h b/src/resources/TextureResource.h index 85230aff3..0b4006ee1 100644 --- a/src/resources/TextureResource.h +++ b/src/resources/TextureResource.h @@ -10,12 +10,12 @@ class TextureResource : public IReloadable { public: - static std::shared_ptr get(ResourceManager& rm, const std::string& path); + static std::shared_ptr get(const std::string& path); virtual ~TextureResource(); - void unload(const ResourceManager& rm) override; - void reload(const ResourceManager& rm) override; + void unload(std::shared_ptr& rm) override; + void reload(std::shared_ptr& rm) override; Eigen::Vector2i getSize() const; void bind() const; @@ -24,7 +24,7 @@ public: void initFromMemory(const char* image, size_t length); private: - TextureResource(const ResourceManager& rm, const std::string& path); + TextureResource(const std::string& path); void initFromPath(); void initFromResource(const ResourceData data);