Moved ResourceManager to be a singleton.

The character count of the average Font::get decreased by 310%...
This commit is contained in:
Aloshi 2013-10-04 18:09:54 -05:00
parent a4185176da
commit b510aa8cd4
23 changed files with 87 additions and 72 deletions

View file

@ -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<ResourceManager>& rm)
{
init(rm.getFileData(mPath));
init(rm->getFileData(mPath));
}
void Font::unload(const ResourceManager& rm)
void Font::unload(std::shared_ptr<ResourceManager>& rm)
{
deinit();
}
std::shared_ptr<Font> Font::get(ResourceManager& rm, const std::string& path, int size)
std::shared_ptr<Font> Font::get(int size, const std::string& path)
{
if(path.empty())
{
@ -115,9 +115,9 @@ std::shared_ptr<Font> Font::get(ResourceManager& rm, const std::string& path, in
return foundFont->second.lock();
}
std::shared_ptr<Font> font = std::shared_ptr<Font>(new Font(rm, path, size));
std::shared_ptr<Font> font = std::shared_ptr<Font>(new Font(size, path));
sFontMap[def] = std::weak_ptr<Font>(font);
rm.addReloadable(font);
ResourceManager::getInstance()->addReloadable(font);
return font;
}

View file

@ -22,7 +22,7 @@ class Font : public IReloadable
public:
static void initLibrary();
static std::shared_ptr<Font> get(ResourceManager& rm, const std::string& path, int size);
static std::shared_ptr<Font> 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<ResourceManager>& rm) override;
void reload(std::shared_ptr<ResourceManager>& rm) override;
int getSize() const;
@ -78,7 +78,7 @@ private:
static std::map< std::pair<std::string, int>, std::weak_ptr<Font> > sFontMap;
Font(const ResourceManager& rm, const std::string& path, int size);
Font(int size, const std::string& path);
void init(ResourceData data);
void deinit();

View file

@ -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;

View file

@ -36,7 +36,6 @@ public:
private:
InputManager* mInputManager;
ResourceManager mResourceManager;
std::vector<GuiComponent*> mGuiStack;
std::vector< std::shared_ptr<Font> > mDefaultFonts;

View file

@ -76,7 +76,7 @@ void ButtonComponent::render(const Eigen::Affine3f& parentTrans)
std::shared_ptr<Font> ButtonComponent::getFont()
{
return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL);
return Font::get(FONT_SIZE_SMALL);
}
unsigned int ButtonComponent::getCurTextColor() const

View file

@ -231,7 +231,7 @@ std::string DateTimeComponent::getDisplayString(DisplayMode mode) const
std::shared_ptr<Font> DateTimeComponent::getFont() const
{
return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_MEDIUM);
return Font::get(FONT_SIZE_MEDIUM);
}
void DateTimeComponent::updateTextCache()

View file

@ -82,7 +82,7 @@ void GuiDetectDevice::render(const Eigen::Affine3f& parentTrans)
Eigen::Affine3f trans = parentTrans * getTransform();
Renderer::setMatrix(trans);
std::shared_ptr<Font> font = Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_MEDIUM);
std::shared_ptr<Font> font = Font::get(FONT_SIZE_MEDIUM);
std::string playerString;
std::stringstream stream;

View file

@ -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"))
{

View file

@ -7,13 +7,13 @@
GuiGameScraper::GuiGameScraper(Window* window, ScraperSearchParams params, std::function<void(MetaDataList)> doneFunc, std::function<void()> 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 = Font::get(*window->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL);
std::shared_ptr<Font> font = Font::get(FONT_SIZE_SMALL);
mResultNames.reserve(MAX_SCRAPER_RESULTS);
for(int i = 0; i < MAX_SCRAPER_RESULTS; i ++)
{

View file

@ -79,7 +79,7 @@ void GuiInputConfig::render(const Eigen::Affine3f& parentTrans)
Eigen::Affine3f trans = parentTrans * getTransform();
Renderer::setMatrix(trans);
std::shared_ptr<Font> font = Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_MEDIUM);
std::shared_ptr<Font> font = Font::get(FONT_SIZE_MEDIUM);
std::stringstream stream;
stream << "PLAYER " << mTargetConfig->getPlayerNum() + 1 << ", press...";

View file

@ -11,7 +11,7 @@ GuiMenu::GuiMenu(Window* window, GuiGameList* parent) : GuiComponent(window)
{
mParent = parent;
std::shared_ptr<Font> font = Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_LARGE);
std::shared_ptr<Font> font = Font::get(FONT_SIZE_LARGE);
mList = new TextListComponent<std::string>(mWindow, 0.0f, font->getHeight() + 2.0f, font);
mList->setSelectedTextColor(0x0000FFFF);
populateList();

View file

@ -4,7 +4,6 @@
#include <math.h>
#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();

View file

@ -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())
{

View file

@ -175,7 +175,7 @@ private:
std::shared_ptr<Font> getFont()
{
return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL);
return Font::get(FONT_SIZE_SMALL);
}

View file

@ -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();

View file

@ -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();

View file

@ -52,7 +52,7 @@ std::shared_ptr<Font> 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)

View file

@ -210,7 +210,7 @@ void TextEditComponent::render(const Eigen::Affine3f& parentTrans)
std::shared_ptr<Font> TextEditComponent::getFont()
{
return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL);
return Font::get(FONT_SIZE_SMALL);
}
bool TextEditComponent::isMultiline()

View file

@ -38,7 +38,7 @@ std::shared_ptr<Font> 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<Font> ThemeComponent::getDescriptionFont()
@ -46,7 +46,7 @@ std::shared_ptr<Font> 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<Font> ThemeComponent::getFastSelectFont()
@ -54,7 +54,7 @@ std::shared_ptr<Font> 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<Font> ThemeComponent::resolveFont(pugi::xml_node node, std::stri
size = defaultSize;
}
return Font::get(*mWindow->getResourceManager(), path, size);
return Font::get(size, path);
}

View file

@ -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> ResourceManager::sInstance = nullptr;
ResourceManager::ResourceManager()
{
}
std::shared_ptr<ResourceManager>& ResourceManager::getInstance()
{
if(!sInstance)
sInstance = std::shared_ptr<ResourceManager>(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);

View file

@ -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<ResourceManager>& rm) = 0;
virtual void reload(std::shared_ptr<ResourceManager>& rm) = 0;
};
class ResourceManager
{
public:
static std::shared_ptr<ResourceManager>& getInstance();
void addReloadable(std::weak_ptr<IReloadable> reloadable);
void unloadAll();
@ -36,6 +38,10 @@ public:
bool fileExists(const std::string& path) const;
private:
ResourceManager();
static std::shared_ptr<ResourceManager> sInstance;
ResourceData loadFile(const std::string& path) const;
std::list< std::weak_ptr<IReloadable> > mReloadables;

View file

@ -7,9 +7,10 @@
std::map< std::string, std::weak_ptr<TextureResource> > 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<ResourceManager>& rm)
{
deinit();
}
void TextureResource::reload(const ResourceManager& rm)
void TextureResource::reload(std::shared_ptr<ResourceManager>& 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> TextureResource::get(ResourceManager& rm, const std::string& path)
std::shared_ptr<TextureResource> TextureResource::get(const std::string& path)
{
std::shared_ptr<ResourceManager>& rm = ResourceManager::getInstance();
if(path.empty())
{
std::shared_ptr<TextureResource> tex(new TextureResource(rm, ""));
rm.addReloadable(tex); //make sure we're deinitialized even though we do nothing on reinitialization
std::shared_ptr<TextureResource> 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> TextureResource::get(ResourceManager& rm, const
}
}
std::shared_ptr<TextureResource> tex = std::shared_ptr<TextureResource>(new TextureResource(rm, path));
std::shared_ptr<TextureResource> tex = std::shared_ptr<TextureResource>(new TextureResource(path));
sTextureMap[path] = std::weak_ptr<TextureResource>(tex);
rm.addReloadable(tex);
rm->addReloadable(tex);
return tex;
}

View file

@ -10,12 +10,12 @@
class TextureResource : public IReloadable
{
public:
static std::shared_ptr<TextureResource> get(ResourceManager& rm, const std::string& path);
static std::shared_ptr<TextureResource> get(const std::string& path);
virtual ~TextureResource();
void unload(const ResourceManager& rm) override;
void reload(const ResourceManager& rm) override;
void unload(std::shared_ptr<ResourceManager>& rm) override;
void reload(std::shared_ptr<ResourceManager>& 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);