Lots of documentation & commenting

This commit is contained in:
Noel Berry
2021-03-21 02:08:28 -07:00
parent 9b42bba16e
commit 088851b43f
25 changed files with 551 additions and 213 deletions

View File

@ -1,4 +1,5 @@
#include <blah/core/filesystem.h>
#include <blah/streams/filestream.h>
#include "../internal/platform_backend.h"
using namespace Blah;
@ -13,6 +14,11 @@ bool File::remove(const FilePath& path)
return PlatformBackend::file_delete(path.cstr());
}
FileStream File::open(const FilePath& path , FileMode mode)
{
return FileStream(path, mode);
}
bool Directory::create(const FilePath& path)
{
return PlatformBackend::dir_create(path.cstr());

View File

@ -5,14 +5,14 @@
using namespace Blah;
CharacterRange::CharacterRange()
SpriteFont::CharRange::CharRange()
: from(0), to(0) {}
CharacterRange::CharacterRange(u32 single)
SpriteFont::CharRange::CharRange(Codepoint single)
: from(single), to(single) {}
CharacterRange::CharacterRange(u32 from, u32 to)
SpriteFont::CharRange::CharRange(Codepoint from, Codepoint to)
: from(from), to(to) {}
const CharacterSet CharacterRange::ASCII = CharacterSet({ CharacterRange(32, 128) });
const SpriteFont::CharSet SpriteFont::CharRange::ASCII = SpriteFont::CharSet({ CharRange(32, 128) });
SpriteFont::SpriteFont()
{
@ -22,24 +22,24 @@ SpriteFont::SpriteFont()
line_gap = 0;
}
SpriteFont::SpriteFont(const char* file, float size)
SpriteFont::SpriteFont(const FilePath& file, float size)
{
build(file, size, CharacterRange::ASCII);
rebuild(file, size, CharRange::ASCII);
}
SpriteFont::SpriteFont(const char* file, float size, const CharacterSet& charset)
SpriteFont::SpriteFont(const FilePath& file, float size, const CharSet& charset)
{
build(file, size, charset);
rebuild(file, size, charset);
}
SpriteFont::SpriteFont(const Font& font, float size)
{
build(font, size, CharacterRange::ASCII);
rebuild(font, size, CharRange::ASCII);
}
SpriteFont::SpriteFont(const Font& font, float size, const CharacterSet& charset)
SpriteFont::SpriteFont(const Font& font, float size, const CharSet& charset)
{
build(font, size, charset);
rebuild(font, size, charset);
}
SpriteFont::SpriteFont(SpriteFont&& src) noexcept
@ -85,7 +85,7 @@ float SpriteFont::width_of(const String& text) const
float width = 0;
float line_width = 0;
u32 last = 0;
Codepoint last = 0;
for (int i = 0; i < text.length(); i ++)
{
if (text[i] == '\n')
@ -122,7 +122,7 @@ float SpriteFont::width_of_line(const String& text, int start) const
float width = 0;
u32 last;
Codepoint last = 0;
for (int i = start; i < text.length(); i ++)
{
if (text[i] == '\n')
@ -163,16 +163,16 @@ float SpriteFont::height_of(const String& text) const
return height - line_gap;
}
void SpriteFont::build(const char* file, float sz, const CharacterSet& charset)
void SpriteFont::rebuild(const FilePath& file, float sz, const CharSet& charset)
{
dispose();
Font font(file);
if (font.is_valid())
build(font, sz, charset);
rebuild(font, sz, charset);
}
void SpriteFont::build(const Font& font, float size, const CharacterSet& charset)
void SpriteFont::rebuild(const Font& font, float size, const CharSet& charset)
{
dispose();
@ -190,7 +190,7 @@ void SpriteFont::build(const Font& font, float size, const CharacterSet& charset
packer.max_size = 8192;
packer.power_of_two = true;
std::unordered_map<u32, int> glyphs;
std::unordered_map<Codepoint, int> glyphs;
Vector<Color> buffer;
for (auto& range : charset)
@ -209,7 +209,7 @@ void SpriteFont::build(const Font& font, float size, const CharacterSet& charset
glyphs[i] = glyph;
// add character
Font::Char ch = font.get_character(glyph, scale);
auto ch = font.get_character(glyph, scale);
m_characters[i].advance = ch.advance;
m_characters[i].offset = Vec2(ch.offset_x, ch.offset_y);
@ -232,9 +232,9 @@ void SpriteFont::build(const Font& font, float size, const CharacterSet& charset
m_atlas.push_back(Texture::create(it));
// add character subtextures
for (auto& it : packer.entries)
for (auto& it : packer.entries())
if (!it.empty)
m_characters[(u32)it.id].subtexture = Subtexture(m_atlas[it.page], it.packed, it.frame);
m_characters[(Codepoint)it.id].subtexture = Subtexture(m_atlas[it.page], it.packed, it.frame);
// add kerning
for (auto a = glyphs.begin(); a != glyphs.end(); a++)
@ -246,7 +246,7 @@ void SpriteFont::build(const Font& font, float size, const CharacterSet& charset
}
}
float SpriteFont::get_kerning(u32 codepoint0, u32 codepoint1) const
float SpriteFont::get_kerning(Codepoint codepoint0, Codepoint codepoint1) const
{
u64 index = ((u64)codepoint0 << 32) | codepoint1;
@ -256,7 +256,7 @@ float SpriteFont::get_kerning(u32 codepoint0, u32 codepoint1) const
return 0.0f;
}
void SpriteFont::set_kerning(u32 codepoint0, u32 codepoint1, float value)
void SpriteFont::set_kerning(Codepoint codepoint0, Codepoint codepoint1, float value)
{
u64 index = ((u64)codepoint0 << 32) | codepoint1;
@ -270,7 +270,12 @@ void SpriteFont::set_kerning(u32 codepoint0, u32 codepoint1, float value)
}
}
const SpriteFont::Character& SpriteFont::get_character(u32 codepoint) const
SpriteFont::Character& SpriteFont::get_character(Codepoint codepoint)
{
return m_characters[codepoint];
}
const SpriteFont::Character& SpriteFont::get_character(Codepoint codepoint) const
{
static const Character empty;
auto it = m_characters.find(codepoint);
@ -279,7 +284,12 @@ const SpriteFont::Character& SpriteFont::get_character(u32 codepoint) const
return empty;
}
const SpriteFont::Character& SpriteFont::operator[](u32 codepoint) const
SpriteFont::Character& SpriteFont::operator[](Codepoint codepoint)
{
return m_characters[codepoint];
}
const SpriteFont::Character& SpriteFont::operator[](Codepoint codepoint) const
{
static const Character empty;
auto it = m_characters.find(codepoint);

View File

@ -2,16 +2,12 @@
#include <blah/streams/filestream.h>
#include <blah/core/filesystem.h>
#include <blah/core/common.h>
#include <blah/math/calc.h>
#define STBI_NO_STDIO
#define STBI_ONLY_ZLIB
#include "../third_party/stb_image.h"
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MUL_UN8(a, b, t) \
((t) = (a) * (u16)(b) + 0x80, ((((t) >> 8) + (t) ) >> 8))
using namespace Blah;
Aseprite::Aseprite()
@ -19,7 +15,7 @@ Aseprite::Aseprite()
}
Aseprite::Aseprite(const char* path)
Aseprite::Aseprite(const FilePath& path)
{
FileStream fs(path, FileMode::Read);
parse(fs);
@ -406,6 +402,9 @@ void Aseprite::parse_slice(Stream& stream, int frame)
}
}
#define MUL_UN8(a, b, t) \
((t) = (a) * (u16)(b) + 0x80, ((((t) >> 8) + (t) ) >> 8))
void Aseprite::render_cel(Cel* cel, Frame* frame)
{
Layer& layer = layers[cel->layer_index];
@ -436,16 +435,16 @@ void Aseprite::render_cel(Cel* cel, Frame* frame)
auto dstH = frame->image.height;
// blit pixels
int left = MAX(0, srcX);
int right = MIN(dstW, srcX + srcW);
int top = MAX(0, srcY);
int bottom = MIN(dstH, srcY + srcH);
int left = Calc::max(0, srcX);
int right = Calc::min(dstW, srcX + srcW);
int top = Calc::max(0, srcY);
int bottom = Calc::min(dstH, srcY + srcH);
if (layer.blendmode == 0)
{
for (int dx = left, sx = -MIN(srcX, 0); dx < right; dx++, sx++)
for (int dx = left, sx = -Calc::min(srcX, 0); dx < right; dx++, sx++)
{
for (int dy = top, sy = -MIN(srcY, 0); dy < bottom; dy++, sy++)
for (int dy = top, sy = -Calc::min(srcY, 0); dy < bottom; dy++, sy++)
{
Color* srcColor = (src + sx + sy * srcW);
Color* dstColor = (dst + dx + dy * dstW);

View File

@ -56,7 +56,7 @@ Font::Font(Stream& stream) : Font()
load(stream);
}
Font::Font(const char* path) : Font()
Font::Font(const FilePath& path) : Font()
{
FileStream fs(path, FileMode::Read);
if (fs.is_readable())
@ -142,14 +142,14 @@ void Font::dispose()
m_style_name.dispose();
}
const char* Font::family_name() const
const String& Font::family_name() const
{
return m_family_name.cstr();
return m_family_name;
}
const char* Font::style_name() const
const String& Font::style_name() const
{
return m_style_name.cstr();
return m_style_name;
}
int Font::ascent() const
@ -199,9 +199,9 @@ float Font::get_kerning(int glyph1, int glyph2, float scale) const
return stbtt_GetGlyphKernAdvance((stbtt_fontinfo*)m_font, glyph1, glyph2) * scale;
}
Font::Char Font::get_character(int glyph, float scale) const
Font::Character Font::get_character(int glyph, float scale) const
{
Char ch;
Character ch;
if (!m_font)
return ch;
@ -227,7 +227,7 @@ Font::Char Font::get_character(int glyph, float scale) const
return ch;
}
bool Font::get_image(const Font::Char& ch, Color* pixels) const
bool Font::get_image(const Font::Character& ch, Color* pixels) const
{
if (ch.has_glyph)
{
@ -251,6 +251,16 @@ bool Font::get_image(const Font::Char& ch, Color* pixels) const
return false;
}
Image Font::get_image(const Font::Character& ch) const
{
Image img(ch.width, ch.height);
if (get_image(ch, img.pixels))
return img;
return Image();
}
bool Font::is_valid() const
{
return m_valid;

View File

@ -8,7 +8,6 @@ using namespace Blah;
#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_JPEG
#define STBI_ONLY_PNG
#define STBI_ONLY_BMP
#include "../third_party/stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
@ -59,7 +58,7 @@ Image::Image(Stream& stream)
from_stream(stream);
}
Image::Image(const char* file)
Image::Image(const FilePath& file)
{
width = height = 0;
pixels = nullptr;
@ -175,6 +174,7 @@ void Image::dispose()
stbi_image_free(pixels);
else
delete[] pixels;
pixels = nullptr;
width = height = 0;
m_stbi_ownership = false;
@ -203,7 +203,7 @@ void Image::set_pixels(const RectI& rect, Color* data)
}
}
bool Image::save_png(const char* file) const
bool Image::save_png(const FilePath& file) const
{
FileStream fs(file, FileMode::Write);
return save_png(fs);
@ -232,7 +232,7 @@ bool Image::save_png(Stream& stream) const
return false;
}
bool Image::save_jpg(const char* file, int quality) const
bool Image::save_jpg(const FilePath& file, int quality) const
{
FileStream fs(file, FileMode::Write);
return save_jpg(fs, quality);
@ -269,33 +269,31 @@ bool Image::save_jpg(Stream& stream, int quality) const
return false;
}
void Image::get_pixels(Color* dest, const Point& destPos, const Point& destSize, RectI sourceRect)
void Image::get_pixels(Color* dest, const Point& dest_pos, const Point& dest_size, RectI source_rect)
{
// can't be outside of the source image
if (sourceRect.x < 0) sourceRect.x = 0;
if (sourceRect.y < 0) sourceRect.y = 0;
if (sourceRect.x + sourceRect.w > width) sourceRect.w = width - sourceRect.x;
if (sourceRect.y + sourceRect.h > height) sourceRect.h = height - sourceRect.y;
if (source_rect.x < 0) source_rect.x = 0;
if (source_rect.y < 0) source_rect.y = 0;
if (source_rect.x + source_rect.w > width) source_rect.w = width - source_rect.x;
if (source_rect.y + source_rect.h > height) source_rect.h = height - source_rect.y;
// can't be larger than our destination
if (sourceRect.w > destSize.x - destPos.x)
sourceRect.w = destSize.x - destPos.x;
if (sourceRect.h > destSize.y - destPos.y)
sourceRect.h = destSize.y - destPos.y;
if (source_rect.w > dest_size.x - dest_pos.x)
source_rect.w = dest_size.x - dest_pos.x;
if (source_rect.h > dest_size.y - dest_pos.y)
source_rect.h = dest_size.y - dest_pos.y;
for (int y = 0; y < sourceRect.h; y++)
for (int y = 0; y < source_rect.h; y++)
{
int to = destPos.x + (destPos.y + y) * destSize.x;
int from = sourceRect.x + (sourceRect.y + y) * width;
memcpy(dest + to, pixels + from, sizeof(Color) * (int)sourceRect.w);
int to = dest_pos.x + (dest_pos.y + y) * dest_size.x;
int from = source_rect.x + (source_rect.y + y) * width;
memcpy(dest + to, pixels + from, sizeof(Color) * (int)source_rect.w);
}
}
Image Image::get_sub_image(const RectI& sourceRect)
Image Image::get_sub_image(const RectI& source_rect)
{
Image img(sourceRect.w, sourceRect.h);
get_pixels(img.pixels, Point::zero, Point(img.width, img.height), sourceRect);
Image img(source_rect.w, source_rect.h);
get_pixels(img.pixels, Point::zero, Point(img.width, img.height), source_rect);
return img;
}

View File

@ -19,7 +19,7 @@ Packer::Packer(Packer&& src) noexcept
padding = src.padding;
m_dirty = src.m_dirty;
pages = std::move(src.pages);
entries = std::move(src.entries);
m_entries = std::move(src.m_entries);
m_buffer = std::move(src.m_buffer);
}
@ -31,7 +31,7 @@ Packer& Packer::operator=(Packer&& src) noexcept
padding = src.padding;
m_dirty = src.m_dirty;
pages = std::move(src.pages);
entries = std::move(src.entries);
m_entries = std::move(src.m_entries);
m_buffer = std::move(src.m_buffer);
return *this;
}
@ -51,7 +51,7 @@ void Packer::add(u64 id, const Image& image)
add_entry(id, image.width, image.height, image.pixels);
}
void Packer::add(u64 id, const String& path)
void Packer::add(u64 id, const FilePath& path)
{
add(id, Image(path.cstr()));
}
@ -125,7 +125,12 @@ void Packer::add_entry(u64 id, int w, int h, const Color* pixels)
}
}
entries.push_back(entry);
m_entries.push_back(entry);
}
const Vector<Packer::Entry>& Packer::entries() const
{
return m_entries;
}
void Packer::pack()
@ -137,7 +142,7 @@ void Packer::pack()
pages.clear();
// only if we have stuff to pack
auto count = entries.size();
auto count = m_entries.size();
if (count > 0)
{
// get all the sources sorted largest -> smallest
@ -146,8 +151,8 @@ void Packer::pack()
sources.resize(count);
int index = 0;
for (int i = 0; i < entries.size(); i++)
sources[index++] = &entries[i];
for (int i = 0; i < m_entries.size(); i++)
sources[index++] = &m_entries[i];
std::sort(sources.begin(), sources.end(), [](Packer::Entry* a, Packer::Entry* b)
{
@ -292,14 +297,14 @@ void Packer::pack()
void Packer::clear()
{
pages.clear();
entries.clear();
m_entries.clear();
m_dirty = false;
}
void Packer::dispose()
{
pages.clear();
entries.clear();
m_entries.clear();
m_buffer.close();
max_size = 0;
power_of_two = 0;

View File

@ -1,4 +1,5 @@
#include <blah/math/color.h>
#include <blah/math/vec3.h>
#include <blah/math/vec4.h>
using namespace Blah;
@ -7,45 +8,69 @@ char const hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B
#define LT_HEX_VALUE(n) ((n >= '0' && n <= '9') ? (n - '0') : ((n >= 'A' && n <= 'F') ? (10 + n - 'A') : ((n >= 'a' && n <= 'f') ? (10 + n - 'a') : 0)))
Color::Color()
: r(0), g(0), b(0), a(0) {}
: r(0)
, g(0)
, b(0)
, a(0) {}
Color::Color(int rgb) :
r((u8)((rgb & 0xFF0000) >> 16)),
g((u8)((rgb & 0x00FF00) >> 8)),
b((u8)(rgb & 0x0000FF)),
a(255) {}
Color::Color(int rgb)
: r((u8)((rgb & 0xFF0000) >> 16))
, g((u8)((rgb & 0x00FF00) >> 8))
, b((u8)(rgb & 0x0000FF))
, a(255) {}
Color::Color(int rgb, float alpha) :
r((int)(((u8)((rgb & 0xFF0000) >> 16)) * alpha)),
g((int)(((u8)((rgb & 0x00FF00) >> 8)) * alpha)),
b((int)(((u8)(rgb & 0x0000FF)) * alpha)),
a((int)(255 * alpha)) {}
Color::Color(int rgb, float alpha)
: r((int)(((u8)((rgb & 0xFF0000) >> 16)) * alpha))
, g((int)(((u8)((rgb & 0x00FF00) >> 8)) * alpha))
, b((int)(((u8)(rgb & 0x0000FF)) * alpha))
, a((int)(255 * alpha)) {}
Color::Color(u8 r, u8 g, u8 b)
: r(r), g(g), b(b), a(255) {}
: r(r)
, g(g)
, b(b)
, a(255) {}
Color::Color(u8 r, u8 g, u8 b, u8 a)
: r(r), g(g), b(b), a(a) {}
: r(r)
, g(g)
, b(b)
, a(a) {}
Color::Color(const Vec3& vec3)
: r((int)(vec3.x * 255))
, g((int)(vec3.y * 255))
, b((int)(vec3.z * 255))
, a((int)(255)) {}
Color::Color(const Vec3& vec3, float alpha)
: r((int)(vec3.x * alpha * 255))
, g((int)(vec3.y* alpha * 255))
, b((int)(vec3.z* alpha * 255))
, a((int)(alpha * 255)) {}
Color::Color(const Vec4& vec4)
: r((int)(vec4.x * 255)), g((int)(vec4.y * 255)), b((int)(vec4.z * 255)), a((int)(vec4.w * 255)) {}
: r((int)(vec4.x * 255))
, g((int)(vec4.y * 255))
, b((int)(vec4.z * 255))
, a((int)(vec4.w * 255)) {}
Color::Color(const char* value) : r(0), g(0), b(0), a(255)
Color::Color(const String& value)
: r(0)
, g(0)
, b(0)
, a(255)
{
int length = 0;
while (value[length] != '\0' && length < 10)
length ++;
int offset = 0;
if (length > 0 && value[0] == '#')
if (value.length() > 0 && value[0] == '#')
offset = 1;
else if (length >= 1 && value[0] == '0' && (value[1] == 'x' || value[1] == 'X'))
else if (value.length() >= 1 && value[0] == '0' && (value[1] == 'x' || value[1] == 'X'))
offset = 2;
if (length - offset >= 8)
if (value.length() - offset >= 8)
a = (LT_HEX_VALUE(value[offset + 6]) << 4) + LT_HEX_VALUE(value[offset + 7]);
if (length - offset >= 6)
if (value.length() - offset >= 6)
{
r = (LT_HEX_VALUE(value[offset + 0]) << 4) + LT_HEX_VALUE(value[offset + 1]);
g = (LT_HEX_VALUE(value[offset + 2]) << 4) + LT_HEX_VALUE(value[offset + 3]);

View File

@ -11,7 +11,7 @@ FileStream::FileStream()
m_mode = FileMode::None;
}
FileStream::FileStream(const char* path, FileMode mode)
FileStream::FileStream(const FilePath& path, FileMode mode)
: m_mode(mode)
{
if (!PlatformBackend::file_open(path, &m_handle, mode))
@ -63,6 +63,21 @@ i64 FileStream::seek(i64 seek_to)
return PlatformBackend::file_seek(m_handle, seek_to);
}
bool FileStream::is_open() const
{
return m_handle != nullptr;
}
bool FileStream::is_readable() const
{
return m_handle != nullptr && (m_mode == FileMode::ReadWrite || m_mode == FileMode::Read);
}
bool FileStream::is_writable() const
{
return m_handle != nullptr && (m_mode == FileMode::ReadWrite || m_mode == FileMode::Write);
}
i64 FileStream::read_into(void* ptr, i64 length)
{
if (m_handle == nullptr)