Added default font

Former-commit-id: 84fc15f2c6d46bcfb55236863611f2fb96961a6d
This commit is contained in:
Lynix 2015-01-18 10:38:50 +01:00
parent c02fe6c8ff
commit e30c447bd5
6 changed files with 61 additions and 5 deletions

View File

@ -75,6 +75,7 @@ class NAZARA_API NzFont : public NzResource, NzAbstractAtlas::Listener, NzNonCop
void SetMinimumStepSize(unsigned int minimumStepSize); void SetMinimumStepSize(unsigned int minimumStepSize);
static std::shared_ptr<NzAbstractAtlas> GetDefaultAtlas(); static std::shared_ptr<NzAbstractAtlas> GetDefaultAtlas();
static NzFont* GetDefault();
static unsigned int GetDefaultGlyphBorder(); static unsigned int GetDefaultGlyphBorder();
static unsigned int GetDefaultMinimumStepSize(); static unsigned int GetDefaultMinimumStepSize();
@ -133,6 +134,7 @@ class NAZARA_API NzFont : public NzResource, NzAbstractAtlas::Listener, NzNonCop
unsigned int m_minimumStepSize; unsigned int m_minimumStepSize;
static std::shared_ptr<NzAbstractAtlas> s_defaultAtlas; static std::shared_ptr<NzAbstractAtlas> s_defaultAtlas;
static NzFont* s_defaultFont;
static NzFontLoader::LoaderList s_loaders; static NzFontLoader::LoaderList s_loaders;
static unsigned int s_defaultGlyphBorder; static unsigned int s_defaultGlyphBorder;
static unsigned int s_defaultMinimumStepSize; static unsigned int s_defaultMinimumStepSize;

View File

@ -96,9 +96,30 @@ void NzGraphics::Uninitialize()
s_moduleReferenceCounter = 0; s_moduleReferenceCounter = 0;
// Libération de l'atlas s'il vient de nous // Libération de l'atlas s'il vient de nous
if (NzFont::GetDefaultAtlas()->GetStorage() & nzDataStorage_Hardware) std::shared_ptr<NzAbstractAtlas> defaultAtlas = NzFont::GetDefaultAtlas();
if (defaultAtlas && defaultAtlas->GetStorage() & nzDataStorage_Hardware)
{
NzFont::SetDefaultAtlas(nullptr); NzFont::SetDefaultAtlas(nullptr);
// La police par défaut peut faire vivre un atlas hardware après la libération du module (ce qui va être problématique)
// du coup, si la police par défaut utilise un atlas hardware, on lui enlève.
// Je n'aime pas cette solution mais je n'en ai pas de meilleure sous la main pour l'instant
if (!defaultAtlas.unique())
{
// Encore au moins une police utilise l'atlas
NzFont* defaultFont = NzFont::GetDefault();
defaultFont->SetAtlas(nullptr);
if (!defaultAtlas.unique())
{
// Toujours pas seuls propriétaires ? Ah ben zut.
NazaraWarning("Default font atlas uses hardware storage and is still used");
}
}
}
defaultAtlas.reset();
// Loaders // Loaders
NzLoaders_Mesh_Unregister(); NzLoaders_Mesh_Unregister();
NzLoaders_OBJ_Unregister(); NzLoaders_OBJ_Unregister();

View File

@ -9,6 +9,13 @@
#include <Nazara/Utility/GuillotineImageAtlas.hpp> #include <Nazara/Utility/GuillotineImageAtlas.hpp>
#include <Nazara/Utility/Debug.hpp> #include <Nazara/Utility/Debug.hpp>
namespace
{
const nzUInt8 r_cabinRegular[] = {
#include <Nazara/Utility/Resources/Fonts/Cabin-Regular.ttf.h>
};
}
bool NzFontParams::IsValid() const bool NzFontParams::IsValid() const
{ {
return true; // Rien à tester return true; // Rien à tester
@ -325,6 +332,29 @@ std::shared_ptr<NzAbstractAtlas> NzFont::GetDefaultAtlas()
return s_defaultAtlas; return s_defaultAtlas;
} }
NzFont* NzFont::GetDefault()
{
// Nous n'initialisons la police par défaut qu'à la demande pour qu'elle prenne
// les paramètres par défaut (qui peuvent avoir étés changés par l'utilisateur),
// et pour ne pas consommer de la mémoire vive inutilement (si elle n'est jamais utilisée, elle n'est jamais ouverte).
if (!s_defaultFont)
{
std::unique_ptr<NzFont> cabin(new NzFont);
cabin->SetPersistent(true);
if (!cabin->OpenFromMemory(r_cabinRegular, sizeof(r_cabinRegular)))
{
NazaraError("Failed to open default font");
return nullptr;
}
s_defaultFont = cabin.release();
}
return s_defaultFont;
}
unsigned int NzFont::GetDefaultGlyphBorder() unsigned int NzFont::GetDefaultGlyphBorder()
{ {
return s_defaultGlyphBorder; return s_defaultGlyphBorder;
@ -370,6 +400,10 @@ void NzFont::SetDefaultMinimumStepSize(unsigned int minimumStepSize)
void NzFont::Uninitialize() void NzFont::Uninitialize()
{ {
s_defaultAtlas.reset(); s_defaultAtlas.reset();
// On rend la police non-persistente et on demande la vérification du compteur (pouvant entraîner la libération de la ressource)
s_defaultFont->SetPersistent(false, true);
s_defaultFont = nullptr;
} }
nzUInt64 NzFont::ComputeKey(unsigned int characterSize, nzUInt32 style) const nzUInt64 NzFont::ComputeKey(unsigned int characterSize, nzUInt32 style) const
@ -545,6 +579,7 @@ const NzFont::Glyph& NzFont::PrecacheGlyph(GlyphMap& glyphMap, unsigned int char
} }
std::shared_ptr<NzAbstractAtlas> NzFont::s_defaultAtlas; std::shared_ptr<NzAbstractAtlas> NzFont::s_defaultAtlas;
NzFont* NzFont::s_defaultFont;
NzFontLoader::LoaderList NzFont::s_loaders; NzFontLoader::LoaderList NzFont::s_loaders;
unsigned int NzFont::s_defaultGlyphBorder; unsigned int NzFont::s_defaultGlyphBorder;
unsigned int NzFont::s_defaultMinimumStepSize; unsigned int NzFont::s_defaultMinimumStepSize;

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@ -6,13 +6,11 @@
#include <memory> #include <memory>
#include <Nazara/Utility/Debug.hpp> #include <Nazara/Utility/Debug.hpp>
///TODO: Listener de font (cas où l'atlas a changé)
NzSimpleTextDrawer::NzSimpleTextDrawer() : NzSimpleTextDrawer::NzSimpleTextDrawer() :
m_color(NzColor::White), m_color(NzColor::White),
m_style(nzTextStyle_Regular) m_style(nzTextStyle_Regular)
{ {
// SetFont(NzFont::GetDefault()); SetFont(NzFont::GetDefault());
} }
NzSimpleTextDrawer::~NzSimpleTextDrawer() NzSimpleTextDrawer::~NzSimpleTextDrawer()
@ -91,7 +89,6 @@ void NzSimpleTextDrawer::SetText(const NzString& str)
NzSimpleTextDrawer NzSimpleTextDrawer::Draw(const NzString& str, unsigned int characterSize, nzUInt32 style, const NzColor& color) NzSimpleTextDrawer NzSimpleTextDrawer::Draw(const NzString& str, unsigned int characterSize, nzUInt32 style, const NzColor& color)
{ {
///FIXME: Sans default font ça n'a aucun intérêt
NzSimpleTextDrawer drawer; NzSimpleTextDrawer drawer;
drawer.SetCharacterSize(characterSize); drawer.SetCharacterSize(characterSize);
drawer.SetColor(color); drawer.SetColor(color);