Separated atlases from Font class

First use of shared_ptr, yay!


Former-commit-id: 4c22341fba5674746d5299cf0e8dbf6dc31d5362
This commit is contained in:
Lynix
2015-01-07 23:46:26 +01:00
parent b61ff3a0d1
commit b6c5668232
7 changed files with 522 additions and 253 deletions

View File

@@ -0,0 +1,24 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Graphics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_GUILLOTINETEXTUREATLAS_HPP
#define NAZARA_GUILLOTINETEXTUREATLAS_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Utility/GuillotineImageAtlas.hpp>
class NAZARA_API NzGuillotineTextureAtlas : public NzGuillotineImageAtlas
{
public:
NzGuillotineTextureAtlas() = default;
~NzGuillotineTextureAtlas() = default;
private:
unsigned int GetMaxAtlasSize() const override;
bool ResizeImage(Layer& layer, const NzVector2ui& size) const override;
};
#endif // NAZARA_GUILLOTINETEXTUREATLAS_HPP

View File

@@ -0,0 +1,40 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_ABSTRACTFONTATLAS_HPP
#define NAZARA_ABSTRACTFONTATLAS_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/SparsePtr.hpp>
#include <Nazara/Math/Rect.hpp>
#include <set>
class NzAbstractImage;
class NzFont;
class NzImage;
class NAZARA_API NzAbstractFontAtlas
{
public:
NzAbstractFontAtlas() = default;
virtual ~NzAbstractFontAtlas();
virtual void Clear() = 0;
virtual void Free(NzSparsePtr<const NzRectui> rects, NzSparsePtr<unsigned int> layers, unsigned int count) = 0;
virtual NzAbstractImage* GetLayer(unsigned int layerIndex) const = 0;
virtual unsigned int GetLayerCount() const = 0;
virtual bool Insert(const NzImage& image, NzRectui* rect, bool* flipped, unsigned int* layerIndex) = 0;
void RegisterFont(NzFont* font);
void UnregisterFont(NzFont* font);
protected:
void NotifyCleared();
private:
std::set<NzFont*> m_registredFonts;
};
#endif // NAZARA_ABSTRACTFONTATLAS_HPP

View File

@@ -8,11 +8,10 @@
#define NAZARA_FONT_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/GuillotineBinPack.hpp>
#include <Nazara/Core/NonCopyable.hpp>
#include <Nazara/Core/ResourceRef.hpp>
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Utility/Image.hpp>
#include <Nazara/Utility/AbstractFontAtlas.hpp>
#include <memory>
#include <unordered_map>
@@ -24,7 +23,7 @@ struct NAZARA_API NzFontParams
class NzFont;
class NzFontData;
struct NzFontGlyph; // TEMP
struct NzFontGlyph;
using NzFontConstRef = NzResourceRef<const NzFont>;
using NzFontLoader = NzResourceLoader<NzFont, NzFontParams>;
@@ -32,31 +31,12 @@ using NzFontRef = NzResourceRef<NzFont>;
class NAZARA_API NzFont : public NzResource, NzNonCopyable
{
friend NzAbstractFontAtlas;
friend NzFontLoader;
public:
struct Atlas
{
NzGuillotineBinPack binPack;
NzImage image;
};
struct Glyph
{
NzRecti aabb;
NzRectui atlasRect;
bool flipped;
bool valid;
int advance;
unsigned int atlasIndex;
};
struct SizeInfo
{
unsigned int lineHeight;
float underlinePosition;
float underlineThickness;
};
struct Glyph;
struct SizeInfo;
NzFont();
NzFont(NzFont&& font) = default;
@@ -71,14 +51,12 @@ class NAZARA_API NzFont : public NzResource, NzNonCopyable
bool ExtractGlyph(unsigned int characterSize, char32_t character, nzUInt32 style, NzFontGlyph* glyph) const;
const Atlas& GetAtlas(unsigned int atlasIndex) const;
unsigned int GetAtlasCount() const;
const NzAbstractFontAtlas* GetAtlas() const;
unsigned int GetCachedGlyphCount(unsigned int characterSize, nzUInt32 style) const;
unsigned int GetCachedGlyphCount() const;
NzString GetFamilyName() const;
int GetKerning(unsigned int characterSize, char32_t first, char32_t second) const;
const Glyph& GetGlyph(unsigned int characterSize, nzUInt32 style, char32_t character) const;
unsigned int GetMaxAtlasSize() const;
unsigned int GetMinimumStepSize() const;
const SizeInfo& GetSizeInfo(unsigned int characterSize) const;
NzString GetStyleName() const;
@@ -93,41 +71,50 @@ class NAZARA_API NzFont : public NzResource, NzNonCopyable
bool OpenFromMemory(const void* data, std::size_t size, const NzFontParams& params = NzFontParams());
bool OpenFromStream(NzInputStream& stream, const NzFontParams& params = NzFontParams());
void SetMaxAtlasSize(unsigned int maxAtlasSize);
void SetAtlas(std::shared_ptr<NzAbstractFontAtlas> atlas);
void SetMinimumStepSize(unsigned int minimumSizeStep);
NzFont& operator=(NzFont&& font) = default;
static unsigned int GetDefaultMaxAtlasSize();
static void SetDefaultMaxAtlasSize(unsigned int maxAtlasSize);
enum ModicationCode
{
ModificationCode_GlyphCacheCleared,
ModificationCode_KerningCacheCleared,
ModificationCode_SizeInfoCacheCleared
};
struct Glyph
{
NzRecti aabb;
NzRectui atlasRect;
bool flipped;
bool valid;
int advance;
unsigned int layerIndex;
};
struct SizeInfo
{
unsigned int lineHeight;
float underlinePosition;
float underlineThickness;
};
private:
using GlyphMap = std::unordered_map<char32_t, Glyph>;
struct QueuedGlyph
{
char32_t codepoint;
NzImage image;
GlyphMap* map;
};
nzUInt64 ComputeKey(unsigned int characterSize, nzUInt32 style) const;
unsigned int GetRealMaxAtlasSize() const;
unsigned int InsertRect(NzRectui* rect, bool* flipped) const;
void OnAtlasCleared();
const Glyph& PrecacheGlyph(GlyphMap& glyphMap, unsigned int characterSize, bool bold, char32_t character) const;
void ProcessGlyphQueue() const;
std::shared_ptr<NzAbstractFontAtlas> m_atlas;
std::unique_ptr<NzFontData> m_data;
mutable std::unordered_map<nzUInt64, std::unordered_map<nzUInt64, int>> m_kerningCache;
mutable std::unordered_map<nzUInt64, GlyphMap> m_glyphes;
mutable std::unordered_map<nzUInt64, SizeInfo> m_sizeInfoCache;
mutable std::vector<Atlas> m_atlases;
mutable std::vector<QueuedGlyph> m_glyphQueue;
unsigned int m_maxAtlasSize;
unsigned int m_minimumSizeStep;
static NzFontLoader::LoaderList s_loaders;
static unsigned int s_maxAtlasSize;
};
#endif // NAZARA_FONT_HPP

View File

@@ -0,0 +1,68 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_GUILLOTINEIMAGEATLAS_HPP
#define NAZARA_GUILLOTINEIMAGEATLAS_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/GuillotineBinPack.hpp>
#include <Nazara/Utility/AbstractFontAtlas.hpp>
#include <Nazara/Utility/AbstractImage.hpp>
#include <Nazara/Utility/Image.hpp>
#include <memory>
#include <set>
#include <vector>
class NAZARA_API NzGuillotineImageAtlas : public NzAbstractFontAtlas
{
public:
NzGuillotineImageAtlas();
virtual ~NzGuillotineImageAtlas();
void Clear();
void Free(NzSparsePtr<const NzRectui> rects, NzSparsePtr<unsigned int> layers, unsigned int count);
NzGuillotineBinPack::FreeRectChoiceHeuristic GetRectChoiceHeuristic() const;
NzGuillotineBinPack::GuillotineSplitHeuristic GetRectSplitHeuristic() const;
NzAbstractImage* GetLayer(unsigned int layerIndex) const;
unsigned int GetLayerCount() const;
bool Insert(const NzImage& image, NzRectui* rect, bool* flipped, unsigned int* layerIndex);
void SetRectChoiceHeuristic(NzGuillotineBinPack::FreeRectChoiceHeuristic heuristic);
void SetRectSplitHeuristic(NzGuillotineBinPack::GuillotineSplitHeuristic heuristic);
protected:
struct Layer;
virtual unsigned int GetMaxAtlasSize() const;
virtual bool ResizeImage(Layer& layer, const NzVector2ui& size) const;
struct QueuedGlyph
{
NzImage image;
NzRectui rect;
bool flipped;
};
struct Layer
{
std::vector<QueuedGlyph> queuedGlyphs;
std::unique_ptr<NzAbstractImage> image;
NzGuillotineBinPack binPack;
unsigned int freedRectangles = 0;
};
private:
void ProcessGlyphQueue(Layer& layer) const;
std::set<NzFont*> m_fonts;
mutable std::vector<Layer> m_layers;
NzGuillotineBinPack::FreeRectChoiceHeuristic m_rectChoiceHeuristic;
NzGuillotineBinPack::GuillotineSplitHeuristic m_rectSplitHeuristic;
};
#endif // NAZARA_GUILLOTINEIMAGEATLAS_HPP