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

@ -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;