Fix font loading crashing in the IDE

This is an ugly fix. The whole font loading process and FontManager still
need an overhaul (to be more C++11-ish).
This commit is contained in:
Victor Levasseur
2016-11-23 02:34:32 +01:00
parent ab9403bece
commit 59b9373974
6 changed files with 57 additions and 25 deletions

View File

@@ -38,18 +38,20 @@ void ResourcesLoader::LoadSFMLTexture( const gd::String & filename, sf::Texture
cout << "Failed to load a SFML texture: " << filename << endl;
}
std::pair<sf::Font *, char *> ResourcesLoader::LoadFont(const gd::String & filename)
std::pair<sf::Font *, StreamHolder *> ResourcesLoader::LoadFont(const gd::String & filename)
{
sf::Font * font = new sf::Font;
gd::SFMLFileStream stream;
if (!stream.open(filename) || !font->loadFromStream(stream))
sf::Font * font = new sf::Font();
StreamHolder * streamHolder = new StreamHolder();
if (!streamHolder->stream.open(filename) || !font->loadFromStream(streamHolder->stream))
{
cout << "Failed to load a font from a file: " << filename << endl;
return std::make_pair<sf::Font*, char*>(NULL, NULL);
delete font;
delete streamHolder;
return std::make_pair((sf::Font*)nullptr, (StreamHolder*)nullptr);
}
//Extra (sf::Font*) added to avoid a compilation error with Clang 3.3
return std::make_pair<sf::Font*, char*>((sf::Font*)font, NULL);
return std::make_pair(font, streamHolder);
}
sf::SoundBuffer ResourcesLoader::LoadSoundBuffer( const gd::String & filename )

View File

@@ -8,11 +8,25 @@
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "GDCore/String.h"
#include "GDCore/Tools/FileStream.h"
#undef LoadImage //Undef macro from windows.h
namespace gd
{
/**
* \brief A class holding a buffer and/or a file stream (useful for SFML classes
* that needs their buffer/stream continuously opened)
*/
struct StreamHolder
{
StreamHolder() : buffer(nullptr), stream() {}
~StreamHolder() { if ( buffer ) delete buffer; }
char* buffer;
gd::SFMLFileStream stream;
};
/**
* \brief Class used by games to load resources from files: this is purely an abstraction
* to most SFML loadFrom* functions.
@@ -42,7 +56,7 @@ public:
* that need to be kept alive while the font is used (can be NULL if the font was loaded
* from a file).
*/
std::pair<sf::Font *, char *> LoadFont( const gd::String & filename );
std::pair<sf::Font *, StreamHolder *> LoadFont( const gd::String & filename );
/**
* Load a SFML Sound Buffer

View File

@@ -1,5 +1,4 @@
#include "GDCpp/Runtime/FontManager.h"
#include "GDCpp/Runtime/ResourcesLoader.h"
#include <SFML/Graphics.hpp>
#include <string>
#include <vector>
@@ -17,12 +16,12 @@ FontManager::~FontManager()
void FontManager::UnloadAllFonts()
{
//Need to explicit delete fonts...
for ( map<gd::String, sf::Font*>::iterator it=fonts.begin() ; it != fonts.end(); ++it )
for ( auto it = fonts.begin() ; it != fonts.end(); ++it )
{
if ((*it).second) delete (*it).second;
}
//...and their buffers
for ( map<gd::String, char*>::iterator it=fontsBuffer.begin() ; it != fontsBuffer.end(); ++it )
for ( auto it = fontsBuffer.begin() ; it != fontsBuffer.end(); ++it )
{
if ((*it).second) delete (*it).second;
}
@@ -30,7 +29,7 @@ void FontManager::UnloadAllFonts()
fonts.clear();
fontsBuffer.clear();
if ( defaultFont ) delete defaultFont;
defaultFont = NULL;
defaultFont = nullptr;
}
void FontManager::EnsureDefaultFontIsLoaded()
@@ -65,7 +64,7 @@ const sf::Font * FontManager::GetFont(const gd::String & fontName)
//Load an new font
gd::ResourcesLoader * ressourcesLoader = gd::ResourcesLoader::Get();
std::pair<sf::Font *, char*> font = ressourcesLoader->LoadFont(fontName);
std::pair<sf::Font *, gd::StreamHolder*> font = ressourcesLoader->LoadFont(fontName);
if (font.first)
{
if (font.second) //Store the buffer if any.

View File

@@ -9,6 +9,7 @@
#include <string>
#include <vector>
#include "GDCpp/Runtime/String.h"
#include "GDCpp/Runtime/ResourcesLoader.h"
/**
* \brief FontManager loads and manages SFML fonts.
@@ -59,7 +60,7 @@ private:
void EnsureDefaultFontIsLoaded();
std::map < gd::String, sf::Font* > fonts; ///< The font being loaded.
std::map < gd::String, char* > fontsBuffer; ///< The buffer associated to each font, if any.
std::map < gd::String, gd::StreamHolder* > fontsBuffer; ///< The buffer associated to each font, if any.
sf::Font * defaultFont; ///< The default font used when no font is specified. Initialized at first use.
FontManager() : defaultFont(NULL) {};

View File

@@ -5,7 +5,6 @@
*/
#if !defined(GD_IDE_ONLY)
#include "GDCpp/Runtime/ResourcesLoader.h"
#include "GDCpp/Runtime/Tools/FileStream.h"
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <string>
@@ -84,15 +83,15 @@ void ResourcesLoader::LoadSFMLTexture( const gd::String & filename, sf::Texture
}
}
std::pair<sf::Font *, char *> ResourcesLoader::LoadFont(const gd::String & filename)
std::pair<sf::Font *, StreamHolder *> ResourcesLoader::LoadFont(const gd::String & filename)
{
if (resFile.ContainsFile(filename))
{
char* buffer = resFile.GetFile(filename);
size_t bufferSize = resFile.GetFileSize(filename);
if (buffer==NULL) {
if (buffer==nullptr) {
cout << "Failed to get the file of a font from resource file:" << filename << endl;
return std::make_pair((sf::Font*)NULL, (char*)NULL);
return std::make_pair((sf::Font*)nullptr, (StreamHolder*)nullptr);
}
sf::Font * font = new sf::Font();
@@ -104,24 +103,27 @@ std::pair<sf::Font *, char *> ResourcesLoader::LoadFont(const gd::String & filen
cout << "Failed to load a font from resource file: " << filename << endl;
delete font;
delete fontBuffer;
return std::make_pair((sf::Font*)NULL, (char*)NULL);
return std::make_pair((sf::Font*)nullptr, (StreamHolder*)nullptr);
}
return std::make_pair(font, fontBuffer);
StreamHolder * streamHolder = new StreamHolder();
streamHolder->buffer = fontBuffer;
return std::make_pair(font, streamHolder);
}
else
{
sf::Font * font = new sf::Font();
gd::SFMLFileStream stream;
StreamHolder * streamHolder = new StreamHolder();
if (!stream.open(filename) || !font->loadFromStream(stream))
if (!streamHolder->stream.open(filename) || !font->loadFromStream(streamHolder->stream))
{
cout << "Failed to load a font from a file: " << filename << endl;
delete font;
return std::make_pair<sf::Font*, char*>(NULL, NULL);
delete streamHolder;
return std::make_pair((sf::Font*)nullptr, (StreamHolder*)nullptr);
}
return std::make_pair(font, (char*)nullptr);
return std::make_pair(font, streamHolder);
}
}

View File

@@ -18,11 +18,25 @@ class Music;
#include <SFML/Audio.hpp>
#include <string>
#include "GDCpp/Runtime/String.h"
#include "GDCpp/Runtime/Tools/FileStream.h"
#undef LoadImage //Undef macro from windows.h
namespace gd
{
/**
* \brief A class holding a buffer and/or a file stream (useful for SFML classes
* that needs their buffer/stream continuously opened)
*/
struct StreamHolder
{
StreamHolder() : buffer(nullptr), stream() {}
~StreamHolder() { if ( buffer ) delete buffer; }
char* buffer;
gd::SFMLFileStream stream;
};
/**
* \brief Class used by games to load resources from files or from a DatFile.
* \note See GDCore documentation for the documentation of most functions.
@@ -44,7 +58,7 @@ public:
sf::Texture LoadSFMLTexture( const gd::String & filename );
void LoadSFMLTexture( const gd::String & filename, sf::Texture & texture );
std::pair<sf::Font *, char *> LoadFont( const gd::String & filename );
std::pair<sf::Font *, StreamHolder *> LoadFont( const gd::String & filename );
sf::SoundBuffer LoadSoundBuffer( const gd::String & filename );