mirror of
https://github.com/NoelFB/blah.git
synced 2025-04-11 01:26:05 +08:00
spritefont takes a vector instead of a null-terminated charset
This commit is contained in:
parent
d388931dd3
commit
9b42bba16e
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <blah/core/common.h>
|
#include <blah/core/common.h>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <initializer_list>
|
||||||
#include <new>
|
#include <new>
|
||||||
|
|
||||||
namespace Blah
|
namespace Blah
|
||||||
@ -19,6 +20,7 @@ namespace Blah
|
|||||||
Vector(int capacity);
|
Vector(int capacity);
|
||||||
Vector(const Vector& src);
|
Vector(const Vector& src);
|
||||||
Vector(Vector&& src) noexcept;
|
Vector(Vector&& src) noexcept;
|
||||||
|
Vector(std::initializer_list<T> list);
|
||||||
~Vector();
|
~Vector();
|
||||||
|
|
||||||
Vector& operator=(const Vector& src);
|
Vector& operator=(const Vector& src);
|
||||||
@ -95,6 +97,17 @@ namespace Blah
|
|||||||
src.m_count = 0;
|
src.m_count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
inline Vector<T>::Vector(std::initializer_list<T> list)
|
||||||
|
{
|
||||||
|
m_buffer = nullptr;
|
||||||
|
m_count = m_capacity = 0;
|
||||||
|
reserve(list.size());
|
||||||
|
for (auto& it : list)
|
||||||
|
push_back(std::move(it));
|
||||||
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
inline Vector<T>::~Vector()
|
inline Vector<T>::~Vector()
|
||||||
{
|
{
|
||||||
|
@ -214,23 +214,23 @@ namespace Blah
|
|||||||
scissor(0, 0, -1, -1) {}
|
scissor(0, 0, -1, -1) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
static ShaderRef m_default_shader;
|
static ShaderRef m_default_shader;
|
||||||
MaterialRef m_default_material;
|
MaterialRef m_default_material;
|
||||||
MeshRef m_mesh;
|
MeshRef m_mesh;
|
||||||
Mat3x2 m_matrix;
|
Mat3x2 m_matrix;
|
||||||
ColorMode m_color_mode;
|
ColorMode m_color_mode;
|
||||||
u8 m_tex_mult;
|
u8 m_tex_mult;
|
||||||
u8 m_tex_wash;
|
u8 m_tex_wash;
|
||||||
DrawBatch m_batch;
|
DrawBatch m_batch;
|
||||||
Vector<Vertex> m_vertices;
|
Vector<Vertex> m_vertices;
|
||||||
Vector<u32> m_indices;
|
Vector<u32> m_indices;
|
||||||
Vector<Mat3x2> m_matrix_stack;
|
Vector<Mat3x2> m_matrix_stack;
|
||||||
Vector<Rect> m_scissor_stack;
|
Vector<Rect> m_scissor_stack;
|
||||||
Vector<BlendMode> m_blend_stack;
|
Vector<BlendMode> m_blend_stack;
|
||||||
Vector<MaterialRef> m_material_stack;
|
Vector<MaterialRef> m_material_stack;
|
||||||
Vector<ColorMode> m_color_mode_stack;
|
Vector<ColorMode> m_color_mode_stack;
|
||||||
Vector<int> m_layer_stack;
|
Vector<int> m_layer_stack;
|
||||||
Vector<DrawBatch> m_batches;
|
Vector<DrawBatch> m_batches;
|
||||||
|
|
||||||
void render_single_batch(RenderPass& pass, const DrawBatch& b, const Mat4x4& matrix);
|
void render_single_batch(RenderPass& pass, const DrawBatch& b, const Mat4x4& matrix);
|
||||||
};
|
};
|
||||||
|
@ -10,6 +10,21 @@ namespace Blah
|
|||||||
{
|
{
|
||||||
class Font;
|
class Font;
|
||||||
|
|
||||||
|
struct CharacterRange;
|
||||||
|
using CharacterSet = Vector<CharacterRange>;
|
||||||
|
|
||||||
|
struct CharacterRange
|
||||||
|
{
|
||||||
|
u32 from;
|
||||||
|
u32 to;
|
||||||
|
|
||||||
|
CharacterRange();
|
||||||
|
CharacterRange(u32 single);
|
||||||
|
CharacterRange(u32 from, u32 to);
|
||||||
|
|
||||||
|
static const CharacterSet ASCII;
|
||||||
|
};
|
||||||
|
|
||||||
class SpriteFont
|
class SpriteFont
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -28,7 +43,6 @@ namespace Blah
|
|||||||
Vector<TextureRef> m_atlas;
|
Vector<TextureRef> m_atlas;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static const u32* ASCII;
|
|
||||||
|
|
||||||
String name;
|
String name;
|
||||||
float size;
|
float size;
|
||||||
@ -36,14 +50,11 @@ namespace Blah
|
|||||||
float descent;
|
float descent;
|
||||||
float line_gap;
|
float line_gap;
|
||||||
|
|
||||||
// Note:
|
|
||||||
// charset is a list of range pairs, until a 0 terminator (ex. 32,128,0)
|
|
||||||
|
|
||||||
SpriteFont();
|
SpriteFont();
|
||||||
SpriteFont(const char* file, float size);
|
SpriteFont(const char* file, float size);
|
||||||
SpriteFont(const char* file, float size, const u32* charset);
|
SpriteFont(const char* file, float size, const CharacterSet& charset);
|
||||||
SpriteFont(const Font& font, float size);
|
SpriteFont(const Font& font, float size);
|
||||||
SpriteFont(const Font& font, float size, const u32* charset);
|
SpriteFont(const Font& font, float size, const CharacterSet& charset);
|
||||||
SpriteFont(const SpriteFont&) = delete;
|
SpriteFont(const SpriteFont&) = delete;
|
||||||
SpriteFont(SpriteFont&& src) noexcept;
|
SpriteFont(SpriteFont&& src) noexcept;
|
||||||
~SpriteFont();
|
~SpriteFont();
|
||||||
@ -62,8 +73,8 @@ namespace Blah
|
|||||||
float width_of_line(const String& text, int start = 0) const;
|
float width_of_line(const String& text, int start = 0) const;
|
||||||
float height_of(const String& text) const;
|
float height_of(const String& text) const;
|
||||||
|
|
||||||
void build(const char* file, float size, const u32* charset);
|
void build(const char* file, float size, const CharacterSet& charset);
|
||||||
void build(const Font& font, float size, const u32* charset);
|
void build(const Font& font, float size, const CharacterSet& charset);
|
||||||
|
|
||||||
float get_kerning(u32 codepoint0, u32 codepoint1) const;
|
float get_kerning(u32 codepoint0, u32 codepoint1) const;
|
||||||
void set_kerning(u32 codepoint0, u32 codepoint1, float kerning);
|
void set_kerning(u32 codepoint0, u32 codepoint1, float kerning);
|
||||||
|
@ -4,16 +4,8 @@
|
|||||||
#include <blah/math/color.h>
|
#include <blah/math/color.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
// 4 color attachments + 1 depth/stencil
|
|
||||||
#define BLAH_ATTACHMENTS 5
|
|
||||||
|
|
||||||
namespace Blah
|
namespace Blah
|
||||||
{
|
{
|
||||||
typedef StackVector<TextureRef, BLAH_ATTACHMENTS> Attachments;
|
|
||||||
|
|
||||||
class FrameBuffer;
|
|
||||||
typedef std::shared_ptr<FrameBuffer> FrameBufferRef;
|
|
||||||
|
|
||||||
enum class ClearMask
|
enum class ClearMask
|
||||||
{
|
{
|
||||||
None = 0,
|
None = 0,
|
||||||
@ -23,6 +15,12 @@ namespace Blah
|
|||||||
All = (int)Color | (int)Depth | (int)Stencil
|
All = (int)Color | (int)Depth | (int)Stencil
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Up to 4 color attachments + 1 depth/stencil
|
||||||
|
using Attachments = StackVector<TextureRef, 5>;
|
||||||
|
|
||||||
|
class FrameBuffer;
|
||||||
|
using FrameBufferRef = std::shared_ptr<FrameBuffer>;
|
||||||
|
|
||||||
class FrameBuffer
|
class FrameBuffer
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
|
@ -5,6 +5,15 @@
|
|||||||
|
|
||||||
using namespace Blah;
|
using namespace Blah;
|
||||||
|
|
||||||
|
CharacterRange::CharacterRange()
|
||||||
|
: from(0), to(0) {}
|
||||||
|
CharacterRange::CharacterRange(u32 single)
|
||||||
|
: from(single), to(single) {}
|
||||||
|
CharacterRange::CharacterRange(u32 from, u32 to)
|
||||||
|
: from(from), to(to) {}
|
||||||
|
|
||||||
|
const CharacterSet CharacterRange::ASCII = CharacterSet({ CharacterRange(32, 128) });
|
||||||
|
|
||||||
SpriteFont::SpriteFont()
|
SpriteFont::SpriteFont()
|
||||||
{
|
{
|
||||||
size = 0;
|
size = 0;
|
||||||
@ -13,25 +22,22 @@ SpriteFont::SpriteFont()
|
|||||||
line_gap = 0;
|
line_gap = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const u32 ascii[]{ 32, 128, 0 };
|
|
||||||
const u32* SpriteFont::ASCII = ascii;
|
|
||||||
|
|
||||||
SpriteFont::SpriteFont(const char* file, float size)
|
SpriteFont::SpriteFont(const char* file, float size)
|
||||||
{
|
{
|
||||||
build(file, size, ASCII);
|
build(file, size, CharacterRange::ASCII);
|
||||||
}
|
}
|
||||||
|
|
||||||
SpriteFont::SpriteFont(const char* file, float size, const u32* charset)
|
SpriteFont::SpriteFont(const char* file, float size, const CharacterSet& charset)
|
||||||
{
|
{
|
||||||
build(file, size, charset);
|
build(file, size, charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
SpriteFont::SpriteFont(const Font& font, float size)
|
SpriteFont::SpriteFont(const Font& font, float size)
|
||||||
{
|
{
|
||||||
build(font, size, ASCII);
|
build(font, size, CharacterRange::ASCII);
|
||||||
}
|
}
|
||||||
|
|
||||||
SpriteFont::SpriteFont(const Font& font, float size, const u32* charset)
|
SpriteFont::SpriteFont(const Font& font, float size, const CharacterSet& charset)
|
||||||
{
|
{
|
||||||
build(font, size, charset);
|
build(font, size, charset);
|
||||||
}
|
}
|
||||||
@ -157,7 +163,7 @@ float SpriteFont::height_of(const String& text) const
|
|||||||
return height - line_gap;
|
return height - line_gap;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpriteFont::build(const char* file, float sz, const u32* charset)
|
void SpriteFont::build(const char* file, float sz, const CharacterSet& charset)
|
||||||
{
|
{
|
||||||
dispose();
|
dispose();
|
||||||
|
|
||||||
@ -166,7 +172,7 @@ void SpriteFont::build(const char* file, float sz, const u32* charset)
|
|||||||
build(font, sz, charset);
|
build(font, sz, charset);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SpriteFont::build(const Font& font, float size, const u32* charset)
|
void SpriteFont::build(const Font& font, float size, const CharacterSet& charset)
|
||||||
{
|
{
|
||||||
dispose();
|
dispose();
|
||||||
|
|
||||||
@ -187,11 +193,10 @@ void SpriteFont::build(const Font& font, float size, const u32* charset)
|
|||||||
std::unordered_map<u32, int> glyphs;
|
std::unordered_map<u32, int> glyphs;
|
||||||
Vector<Color> buffer;
|
Vector<Color> buffer;
|
||||||
|
|
||||||
auto ranges = charset;
|
for (auto& range : charset)
|
||||||
while (*ranges != 0)
|
|
||||||
{
|
{
|
||||||
auto from = *ranges;
|
auto from = range.from;
|
||||||
auto to = *(ranges + 1);
|
auto to = range.to + 1;
|
||||||
|
|
||||||
BLAH_ASSERT(to >= from, "Charset Range must be in pairs of [min,max]");
|
BLAH_ASSERT(to >= from, "Charset Range must be in pairs of [min,max]");
|
||||||
|
|
||||||
@ -218,8 +223,6 @@ void SpriteFont::build(const Font& font, float size, const u32* charset)
|
|||||||
packer.add(i, ch.width, ch.height, buffer.data());
|
packer.add(i, ch.width, ch.height, buffer.data());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ranges += 2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
buffer.clear();
|
buffer.clear();
|
||||||
|
@ -12,7 +12,7 @@ FrameBufferRef FrameBuffer::create(int width, int height)
|
|||||||
FrameBufferRef FrameBuffer::create(int width, int height, const TextureFormat* attachments, int attachment_count)
|
FrameBufferRef FrameBuffer::create(int width, int height, const TextureFormat* attachments, int attachment_count)
|
||||||
{
|
{
|
||||||
BLAH_ASSERT(width > 0 && height > 0, "FrameBuffer width and height must be larger than 0");
|
BLAH_ASSERT(width > 0 && height > 0, "FrameBuffer width and height must be larger than 0");
|
||||||
BLAH_ASSERT(attachment_count <= BLAH_ATTACHMENTS, "Exceeded maximum attachment count");
|
BLAH_ASSERT(attachment_count <= Attachments::MaxCapacity, "Exceeded maximum attachment count");
|
||||||
BLAH_ASSERT(attachment_count > 0, "At least one attachment must be provided");
|
BLAH_ASSERT(attachment_count > 0, "At least one attachment must be provided");
|
||||||
|
|
||||||
int color_count = 0;
|
int color_count = 0;
|
||||||
@ -29,7 +29,7 @@ FrameBufferRef FrameBuffer::create(int width, int height, const TextureFormat* a
|
|||||||
}
|
}
|
||||||
|
|
||||||
BLAH_ASSERT(depth_count <= 1, "FrameBuffer can only have 1 Depth/Stencil Texture");
|
BLAH_ASSERT(depth_count <= 1, "FrameBuffer can only have 1 Depth/Stencil Texture");
|
||||||
BLAH_ASSERT(color_count <= BLAH_ATTACHMENTS - 1, "Exceeded maximum Color attachment count");
|
BLAH_ASSERT(color_count <= Attachments::MaxCapacity - 1, "Exceeded maximum Color attachment count");
|
||||||
|
|
||||||
return GraphicsBackend::create_framebuffer(width, height, attachments, attachment_count);
|
return GraphicsBackend::create_framebuffer(width, height, attachments, attachment_count);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user