spritefont takes a vector instead of a null-terminated charset

This commit is contained in:
Noel Berry 2021-03-20 23:24:18 -07:00
parent d388931dd3
commit 9b42bba16e
6 changed files with 75 additions and 50 deletions

View File

@ -1,6 +1,7 @@
#pragma once
#include <blah/core/common.h>
#include <type_traits>
#include <initializer_list>
#include <new>
namespace Blah
@ -19,6 +20,7 @@ namespace Blah
Vector(int capacity);
Vector(const Vector& src);
Vector(Vector&& src) noexcept;
Vector(std::initializer_list<T> list);
~Vector();
Vector& operator=(const Vector& src);
@ -95,6 +97,17 @@ namespace Blah
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>
inline Vector<T>::~Vector()
{

View File

@ -214,23 +214,23 @@ namespace Blah
scissor(0, 0, -1, -1) {}
};
static ShaderRef m_default_shader;
MaterialRef m_default_material;
MeshRef m_mesh;
Mat3x2 m_matrix;
ColorMode m_color_mode;
u8 m_tex_mult;
u8 m_tex_wash;
DrawBatch m_batch;
Vector<Vertex> m_vertices;
Vector<u32> m_indices;
Vector<Mat3x2> m_matrix_stack;
Vector<Rect> m_scissor_stack;
Vector<BlendMode> m_blend_stack;
Vector<MaterialRef> m_material_stack;
Vector<ColorMode> m_color_mode_stack;
Vector<int> m_layer_stack;
Vector<DrawBatch> m_batches;
static ShaderRef m_default_shader;
MaterialRef m_default_material;
MeshRef m_mesh;
Mat3x2 m_matrix;
ColorMode m_color_mode;
u8 m_tex_mult;
u8 m_tex_wash;
DrawBatch m_batch;
Vector<Vertex> m_vertices;
Vector<u32> m_indices;
Vector<Mat3x2> m_matrix_stack;
Vector<Rect> m_scissor_stack;
Vector<BlendMode> m_blend_stack;
Vector<MaterialRef> m_material_stack;
Vector<ColorMode> m_color_mode_stack;
Vector<int> m_layer_stack;
Vector<DrawBatch> m_batches;
void render_single_batch(RenderPass& pass, const DrawBatch& b, const Mat4x4& matrix);
};

View File

@ -10,6 +10,21 @@ namespace Blah
{
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
{
public:
@ -28,7 +43,6 @@ namespace Blah
Vector<TextureRef> m_atlas;
public:
static const u32* ASCII;
String name;
float size;
@ -36,14 +50,11 @@ namespace Blah
float descent;
float line_gap;
// Note:
// charset is a list of range pairs, until a 0 terminator (ex. 32,128,0)
SpriteFont();
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, const u32* charset);
SpriteFont(const Font& font, float size, const CharacterSet& charset);
SpriteFont(const SpriteFont&) = delete;
SpriteFont(SpriteFont&& src) noexcept;
~SpriteFont();
@ -62,8 +73,8 @@ namespace Blah
float width_of_line(const String& text, int start = 0) const;
float height_of(const String& text) const;
void build(const char* file, float size, const u32* charset);
void build(const Font& font, float size, const u32* charset);
void build(const char* file, float size, const CharacterSet& charset);
void build(const Font& font, float size, const CharacterSet& charset);
float get_kerning(u32 codepoint0, u32 codepoint1) const;
void set_kerning(u32 codepoint0, u32 codepoint1, float kerning);

View File

@ -4,16 +4,8 @@
#include <blah/math/color.h>
#include <memory>
// 4 color attachments + 1 depth/stencil
#define BLAH_ATTACHMENTS 5
namespace Blah
{
typedef StackVector<TextureRef, BLAH_ATTACHMENTS> Attachments;
class FrameBuffer;
typedef std::shared_ptr<FrameBuffer> FrameBufferRef;
enum class ClearMask
{
None = 0,
@ -23,6 +15,12 @@ namespace Blah
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
{
protected:

View File

@ -5,6 +5,15 @@
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()
{
size = 0;
@ -13,25 +22,22 @@ SpriteFont::SpriteFont()
line_gap = 0;
}
const u32 ascii[]{ 32, 128, 0 };
const u32* SpriteFont::ASCII = ascii;
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);
}
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);
}
@ -157,7 +163,7 @@ float SpriteFont::height_of(const String& text) const
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();
@ -166,7 +172,7 @@ void SpriteFont::build(const char* file, float sz, const u32* 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();
@ -187,11 +193,10 @@ void SpriteFont::build(const Font& font, float size, const u32* charset)
std::unordered_map<u32, int> glyphs;
Vector<Color> buffer;
auto ranges = charset;
while (*ranges != 0)
for (auto& range : charset)
{
auto from = *ranges;
auto to = *(ranges + 1);
auto from = range.from;
auto to = range.to + 1;
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());
}
}
ranges += 2;
}
buffer.clear();

View File

@ -12,7 +12,7 @@ FrameBufferRef FrameBuffer::create(int width, int height)
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(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");
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(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);
}