I am having an issue and can't seem to resolve it.
Here is the code
FontCache.h
#pragma once
#include <SFML/Graphics/Font.hpp>
#include <unordered_map>
#include <memory>
#include <string>
class FontCache
{
public:
typedef std::shared_ptr<sf::Font> FontPtr;
typedef std::unordered_map<std::string, FontPtr> FontMap;
typedef FontMap::iterator FontMapIterator;
public:
FontCache(unsigned int size = 10);
~FontCache();
bool add(std::string name, std::string file);
void remove(std::string name);
sf::Font& get(std::string name);
void unload();
private:
FontMap resources_;
};
FontCache.cpp
#include "StandardInc.h"
#include "FontCache.h"
#include "PhysFsStream.h"
FontCache::FontCache(unsigned size)
: resources_(size)
{
}
FontCache::~FontCache()
{
unload();
}
bool FontCache::add(std::string name, std::string file)
{
FontMapIterator it = resources_.find(name);
if(it != resources_.end())
{
return true;
}
PhysFsStream stream;
stream.open(file.c_str());
FontPtr newFont = std::make_shared<sf::Font>();
if (!newFont->loadFromStream(stream))
{
printf("Error: Unable to load %s\n", file.c_str());
return false;
}
resources_[name] = std::move(newFont);
printf("Added Font: %s\n", file.c_str());
return true;
}
void FontCache::remove(std::string name)
{
FontMapIterator it = resources_.find(name);
if(it != resources_.end())
{
resources_.erase(it);
}
}
sf::Font& FontCache::get(std::string name)
{
FontMapIterator it = resources_.find(name);
if(it == resources_.end())
add(name, name);
return *resources_[name];
}
void FontCache::unload()
{
resources_.clear();
}
Part of GameScene.hpp
sf::Text buildDetails_ = new sf::Text();
buildDetails_->setFont(fonts_.get("fonts/PTM55FT.ttf"));
buildDetails_->setString("Build 0.0.0");
buildDetails_->setPosition(10, 10);
Whenever I try to use the font with an sf::Text class I get an access violation error, I have tried making a simple version of the code and not using the FontCache class and basically copied and pasted the code from the FontCache class into the GameScene class where the sf::Text is being created and it worked fine.
Any ideas on what might be cause this would be great my mind has gone blank tryign to fix it.