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

@ -5,27 +5,68 @@
namespace Blah
{
// Application Event Functions
using AppEventFn = std::function<void()>;
// Application Logging Functions
using AppLogFn = std::function<void(const char* message, Log::Category category)>;
// Application Configuration
struct Config
{
// Application name.
// This has no default and must be set.
const char* name;
// Starting width, in pixels.
// Depending on the OS DPI, the true window size may be a multiple of this.
// This has no default and must be set.
int width;
// Starting height, in pixels.
// Depending on the OS DPI, the true window size may be a multiple of this.
// This has no default and must be set.
int height;
// maximum updates to run before "giving up" and reducing frame rate.
// this avoids the 'spiral of death'.
// defaults to 5.
int max_updates;
// target framerate.
// defaults to 60.
int target_framerate;
// Callback on application startup
// Defaults to nothing.
AppEventFn on_startup;
// Callback on application shutdown
// Defaults to nothing.
AppEventFn on_shutdown;
// Callback on application update
// Defaults to nothing.
AppEventFn on_update;
// Callback on application render
// Defaults to nothing.
AppEventFn on_render;
// Callback when the user has requested the application close.
// For example, pressing the Close button
// By default this calls `App::exit()`
AppEventFn on_exit_request;
// Callback when the application logs info/warning/errors
// Defaults to printf.
AppLogFn on_log;
// Default config setup
Config();
};
// Renderer the Application is using
enum class Renderer
{
None = -1,
@ -35,16 +76,24 @@ namespace Blah
Count
};
// Features available on the current Renderer
struct RendererFeatures
{
// Whether Mesh Instancing is available
bool instancing = false;
// Whether the Texture origin is the bottom left.
// This is true for OpenGL.
bool origin_bottom_left = false;
// Maximum Texture Size available
int max_texture_size = 0;
};
class FrameBuffer;
using FrameBufferRef = std::shared_ptr<FrameBuffer>;
// Application
namespace App
{
// Runs the application

View File

@ -5,40 +5,71 @@
namespace Blah
{
using FilePath = StrOf<265>;
class FileStream;
enum class FileMode
{
None = 0,
Read = 1 << 1,
Write = 1 << 2,
ReadWrite = Read | Write,
None = 0,
Read = 1 << 0,
Write = 1 << 1,
ReadWrite = Read | Write,
};
namespace Directory
{
// Creates a new directory at the given location.
// Returns false if unable to create the directory.
bool create(const FilePath& path);
// Returns whether the given directory exists
bool exists(const FilePath& path);
// Tries to delete a path and returns whether it was successful
bool remove(const FilePath& path);
Vector<FilePath> enumerate(const FilePath& str, bool recursive = true);
// Enumerates over a directory and returns a list of files & directories
Vector<FilePath> enumerate(const FilePath& path, bool recursive = true);
// Opens the path in the File Explorer / Finder
void explore(const FilePath& path);
}
namespace File
{
// Checks if the given file exists
bool exists(const FilePath& path);
// Tries to delete a file and returns whether it was successful
bool remove(const FilePath& path);
// Opens the given file and returns a stream
FileStream open(const FilePath& path, FileMode mode = FileMode::ReadWrite);
}
namespace Path
{
// Returns the file name of the path
FilePath get_file_name(const FilePath& path);
// Returns the file name of the path, without the file extension
FilePath get_file_name_no_ext(const FilePath& path);
// Returns the path without any file extensions
FilePath get_path_no_ext(const FilePath& path);
// Returns relative path
FilePath get_path_after(const FilePath& path, const FilePath& after);
// Gets the top directory name from the path
FilePath get_directory_name(const FilePath& path);
// Normalizes a path (removes ../, changes \\ to /, removes redundant slashes, etc)
FilePath normalize(const FilePath& path);
// Joins two paths together
FilePath join(const FilePath& a, const FilePath& b);
// Joins two paths together
template<typename ... Args>
FilePath join(const FilePath& a, const FilePath& b, const Args&... args)
{

View File

@ -4,84 +4,129 @@
#include <blah/containers/vector.h>
#include <blah/drawing/subtexture.h>
#include <blah/math/vec2.h>
#include <blah/core/filesystem.h>
#include <unordered_map>
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;
};
// Sprite Font is a bitmap font implementation for font rendering.
// It can be constructed from a Font (ttf) and will automatically create
// texture atlases for rendering. You can add your own characters
// and textures to it.
class SpriteFont
{
public:
// Spritefont uses u32 codepoints
using Codepoint = u32;
// CharSet is a Vector of Character Ranges
struct CharRange;
using CharSet = Vector<CharRange>;
// Character range, used for building the Sprite Font
struct CharRange
{
Codepoint from;
Codepoint to;
CharRange();
CharRange(Codepoint single);
CharRange(Codepoint from, Codepoint to);
static const CharSet ASCII;
};
// Character Entry
struct Character
{
Subtexture subtexture;
float advance = 0;
Vec2 offset;
};
// SpriteFont name
String name;
// Height, in pixels
float size;
// Ascent, in pixels
float ascent;
// Descent, in pixels
float descent;
// Line Gap, in pixels
float line_gap;
SpriteFont();
SpriteFont(const FilePath& file, float size);
SpriteFont(const FilePath& file, float size, const CharSet& charset);
SpriteFont(const Font& font, float size);
SpriteFont(const Font& font, float size, const CharSet& charset);
SpriteFont(const SpriteFont&) = delete;
SpriteFont& operator=(const SpriteFont&) = delete;
SpriteFont(SpriteFont&& src) noexcept;
SpriteFont& operator=(SpriteFont&& src) noexcept;
~SpriteFont();
// releases all assets used by the spritefont
void dispose();
// gets the height of the sprite font
float height() const { return ascent - descent; }
// gets the line height of the sprite font (height + line gap)
float line_height() const { return ascent - descent + line_gap; }
// returns a list of all texture atlases
const Vector<TextureRef>& textures() { return m_atlas; }
// calculates the width of the given string
float width_of(const String& text) const;
// calculates the width of the next line
float width_of_line(const String& text, int start = 0) const;
// calculates the height of the given string
float height_of(const String& text) const;
// disposes the existing spritefont data and rebuilds from the given font file
void rebuild(const FilePath& file, float size, const CharSet& charset);
// disposes the existing spritefont data and rebuilds from the given font
void rebuild(const Font& font, float size, const CharSet& charset);
// gets the kerning between two characters
float get_kerning(Codepoint codepoint0, Codepoint codepoint1) const;
// sets the kerning between two characters
void set_kerning(Codepoint codepoint0, Codepoint codepoint1, float kerning);
// gets the character at the given codepoint
Character& get_character(Codepoint codepoint);
// gets the character at the given codepoint
const Character& get_character(Codepoint codepoint) const;
// gets the character at the given codepoint
Character& operator[](Codepoint codepoint);
// gets the character at the given codepoint
const Character& operator[](Codepoint codepoint) const;
private:
// charset & kerning maps
std::unordered_map<u32, Character> m_characters;
// character set
std::unordered_map<Codepoint, Character> m_characters;
// kerning
// key is 2 codepoints combined ((first << 32) | second)
std::unordered_map<u64, float> m_kerning;
// built texture
Vector<TextureRef> m_atlas;
public:
String name;
float size;
float ascent;
float descent;
float line_gap;
SpriteFont();
SpriteFont(const char* file, float size);
SpriteFont(const char* file, float size, const CharacterSet& charset);
SpriteFont(const Font& font, float size);
SpriteFont(const Font& font, float size, const CharacterSet& charset);
SpriteFont(const SpriteFont&) = delete;
SpriteFont(SpriteFont&& src) noexcept;
~SpriteFont();
void dispose();
SpriteFont& operator=(const SpriteFont&) = delete;
SpriteFont& operator=(SpriteFont&& src) noexcept;
float height() const { return ascent - descent; }
float line_height() const { return ascent - descent + line_gap; }
const Vector<TextureRef>& textures() { return m_atlas; }
float width_of(const String& text) const;
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 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);
Character& get_character(u32 codepoint) { return m_characters[codepoint]; }
const Character& get_character(u32 codepoint) const;
Character& operator[](u32 codepoint) { return m_characters[codepoint]; }
const Character& operator[](u32 codepoint) const;
};
}

View File

@ -21,6 +21,8 @@ namespace Blah
class FrameBuffer;
using FrameBufferRef = std::shared_ptr<FrameBuffer>;
// FrameBuffer is a 2D Buffer that can be drawn to.
// It can hold up to 4 color Textures, and 1 Depth/Stencil Texture.
class FrameBuffer
{
protected:

View File

@ -10,6 +10,7 @@ namespace Blah
class Material;
typedef std::shared_ptr<Material> MaterialRef;
// Materials hold values that can be assigned to a shader during rendering
class Material final
{
private:

View File

@ -53,6 +53,7 @@ namespace Blah
class Mesh;
typedef std::shared_ptr<Mesh> MeshRef;
// A Mesh is a set of Indices and Vertices which are used for drawing
class Mesh
{
protected:

View File

@ -31,6 +31,7 @@ namespace Blah
Back = 2,
};
// A single draw call
struct RenderPass
{
// Framebuffer to draw to

View File

@ -18,6 +18,7 @@ namespace Blah
class Texture;
typedef std::shared_ptr<Texture> TextureRef;
// A 2D Texture held by the GPU to be used during rendering
class Texture
{
protected:

View File

@ -3,12 +3,13 @@
#include <blah/images/image.h>
#include <blah/containers/str.h>
#include <blah/streams/stream.h>
#include <blah/core/filesystem.h>
namespace Blah
{
// A simple Aseprite file parser.
// This implementation does not support Aseprite blendmodes,
// besides the default blend mode.
// aside from the default blend mode.
class Aseprite
{
public:
@ -125,12 +126,12 @@ namespace Blah
Vector<Layer> layers;
Vector<Frame> frames;
Vector<Tag> tags;
Vector<Tag> tags;
Vector<Slice> slices;
Vector<Color> palette;
Aseprite();
Aseprite(const char* path);
Aseprite(const FilePath& path);
Aseprite(Stream& stream);
Aseprite(const Aseprite& src);
Aseprite(Aseprite&& src) noexcept;

View File

@ -2,50 +2,102 @@
#include <blah/streams/stream.h>
#include <blah/images/image.h>
#include <blah/containers/str.h>
#include <blah/core/filesystem.h>
namespace Blah
{
typedef u32 Codepoint;
// Loads fonts from file and can blit individual characters to images
class Font
{
public:
struct Char
// Font uses u32 Codepoints
using Codepoint = u32;
// Information provided for a single Character
struct Character
{
// character's glyph index
int glyph = 0;
// width of the character, in pixels
int width = 0;
// height of the character, in pixels
int height = 0;
// advance (how much to move horizontally)
float advance = 0;
// render x-offset
float offset_x = 0;
// render y-offset
float offset_y = 0;
// scale the character was created at
float scale = 0;
// whether the character has a visible glyph
bool has_glyph = false;
};
Font();
Font(Stream& stream);
Font(const char* path);
Font(const FilePath& path);
Font(const Font&) = delete;
Font& operator=(const Font&) = delete;
Font(Font&& src) noexcept;
Font& operator=(Font&& src) noexcept;
~Font();
// Releases all Font resources
// Note that after doing this various properties may become invalid (ex. font name)
void dispose();
const char* family_name() const;
const char* style_name() const;
// returns the font family name
const String& family_name() const;
// returns the font style name
const String& style_name() const;
// the font ascent
int ascent() const;
// the font descent
int descent() const;
// the font line gap (space between lines)
int line_gap() const;
// the height of the font
int height() const;
// the height of the line, including line gap
int line_height() const;
// gets the glyph index for the given codepoint
int get_glyph(Codepoint codepoint) const;
// gets the font scale for the given font size in pixels
float get_scale(float size) const;
// gets the font kerning between 2 glyphs
float get_kerning(int glyph1, int glyph2, float scale) const;
Char get_character(int glyph, float scale) const;
bool get_image(const Char& ch, Color* pixels) const;
// gets character data for the given glyph, at the provided scale
Character get_character(int glyph, float scale) const;
// Blits a character to the provided pixel array.
// The pixel array must be at least ch.width * ch.height in size!
// If the character doesn't exist, this will do nothing and return false.
bool get_image(const Character& ch, Color* pixels) const;
// Returns an image of the provided character.
// If the character doesn't exist, this will return an empty image.
Image get_image(const Character& ch) const;
// checks if the Font is valid
bool is_valid() const;
private:

View File

@ -2,21 +2,30 @@
#include <blah/math/color.h>
#include <blah/math/rectI.h>
#include <blah/math/point.h>
#include <blah/core/filesystem.h>
namespace Blah
{
class Stream;
// A simple 2D Bitmap
class Image
{
public:
// width of the image, in pixels.
int width = 0;
// height of the image, in pixels.
int height = 0;
// pixel data of the image.
// this can be nullptr if the image is never assigned to anything.
Color* pixels = nullptr;
Image();
Image(Stream& stream);
Image(const char* file);
Image(const FilePath& file);
Image(int width, int height);
Image(const Image& src);
Image& operator=(const Image& src);
@ -24,19 +33,41 @@ namespace Blah
Image& operator=(Image&& src) noexcept;
~Image();
// disposes the existing image and recreates it from a stream
void from_stream(Stream& stream);
// disposes the image and resets its values to defaults
void dispose();
// applies alpha premultiplication to the image data
void premultiply();
// sets the pixels at the provided rectangle to the given data
// data must be at least rect.w * rect.h in size!
void set_pixels(const RectI& rect, Color* data);
bool save_png(const char* file) const;
// saves the image to a png file
bool save_png(const FilePath& file) const;
// saves the image to a png file
bool save_png(Stream& stream) const;
bool save_jpg(const char* file, int quality) const;
// saves the image to a jpg file
bool save_jpg(const FilePath& file, int quality) const;
// saves the image to a jpg file
bool save_jpg(Stream& stream, int quality) const;
void get_pixels(Color* dest, const Point& destPos, const Point& destSize, RectI sourceRect);
Image get_sub_image(const RectI& sourceRect);
// gets the pixels from the given source rectangle
void get_pixels(Color* dest, const Point& dest_pos, const Point& dest_size, RectI source_rect);
// gets a sub image from this image
Image get_sub_image(const RectI& source_rect);
private:
// whether the stbi library owns the image data.
// we should let it free the data if it created it.
bool m_stbi_ownership;
};
}

View File

@ -6,6 +6,7 @@
#include <blah/containers/str.h>
#include <blah/containers/vector.h>
#include <blah/streams/bufferstream.h>
#include <blah/core/filesystem.h>
namespace Blah
{
@ -14,29 +15,58 @@ namespace Blah
class Packer
{
public:
// Packer Entry, which stores information about the resulting packed texture
class Entry
{
friend class Packer;
private:
i64 memory_index;
public:
// entry ID
u64 id;
// Texture Page that it was packed into.
// This won't be set until after the packer has run.
int page;
// Whether the entry is empty
bool empty;
// Packed frame rectangle.
// This won't be set until after the packer has run.
RectI frame;
// Packed position and size.
// This won't be set until after the packer has run.
RectI packed;
Entry(u64 id, const RectI& frame)
: memory_index(0), id(id), page(0), empty(true), frame(frame), packed(0, 0, 0, 0) {}
: memory_index(0)
, id(id)
, page(0)
, empty(true)
, frame(frame)
, packed(0, 0, 0, 0) {}
};
// maximum width / height of the generated texture
int max_size;
// whether the generated texture must be a power of 2
bool power_of_two;
// spacing between each packed subtexture
int spacing;
// padding on each subtexture (extrudes their borders outwards)
int padding;
// generated textures. There can be more than one if the packer was
// unable to fit all of the provided subtextures into the max_size.
Vector<Image> pages;
Vector<Entry> entries;
Packer();
Packer(int max_size, int spacing, bool power_of_two);
@ -46,12 +76,25 @@ namespace Blah
Packer& operator=(Packer&& src) noexcept;
~Packer();
// add a new entry
void add(u64 id, int width, int height, const Color* pixels);
void add(u64 id, const Image& bitmap);
void add(u64 id, const String& path);
// add a new entry
void add(u64 id, const Image& bitmap);
// add a new entry
void add(u64 id, const FilePath& path);
// returns a vector of all the resulting entries
const Vector<Entry>& entries() const;
// perform the packing
void pack();
// clear the current packer data
void clear();
// dispose all resources used by the packer
void dispose();
private:
@ -67,9 +110,16 @@ namespace Blah
Node* Reset(const RectI& rect);
};
// whether the packer has any changes that require it to run again
bool m_dirty;
// buffer of all the image data we will be packing
BufferStream m_buffer;
// Entries to pack & their resulting data
Vector<Entry> m_entries;
// adds a new entry
void add_entry(u64 id, int w, int h, const Color* pixels);
};
}

View File

@ -4,6 +4,7 @@
namespace Blah
{
struct Vec3;
struct Vec4;
struct Color
@ -18,21 +19,23 @@ namespace Blah
Color(int rgb, float alpha);
Color(u8 r, u8 g, u8 b);
Color(u8 r, u8 g, u8 b, u8 a);
Color(const Vec3& vec3);
Color(const Vec3& vec3, float alpha);
Color(const Vec4& vec4);
// Parses a Hex string in the format of "#00000000" or "0x00000000" or "00000000"
Color(const char* hexCstr);
Color(const String& hex_string);
// Premultiplies the Color
void premultiply();
// Returns an RGBA hex string of the color
String to_hex_rgba() const;
// Sets a Hex string to the given buffer, in the format of RRGGBBAA
// The buffer must be at least 8 bytes long
void to_hex_rgba(char* buffer) const;
// Returns an RGBA hex string of the color
String to_hex_rgba() const;
// Sets a Hex string to the given buffer, in the format of RRGGBB
// The buffer must be at least 6 bytes long
void to_hex_rgb(char* buffer) const;

View File

@ -8,6 +8,7 @@ namespace Blah
enum class Easers
{
Linear,
QuadIn, QuadOut, QuadInOut,
CubeIn, CubeOut, CubeInOut,
QuartIn, QuartOut, QuartInOut,
@ -28,43 +29,41 @@ namespace Blah
For previews go here: https://easings.net/
*/
inline float linear(float t)
constexpr float linear(float t)
{
return t;
}
inline float quad_in(float t)
constexpr float quad_in(float t)
{
return t * t;
}
inline float quad_out(float t)
constexpr float quad_out(float t)
{
return -(t * (t - 2));
}
inline float quad_in_out(float t)
constexpr float quad_in_out(float t)
{
if (t < 0.5f)
return 2 * t * t;
else
{
return (-2 * t * t) + (4 * t) - 1;
}
}
inline float cube_in(float t)
constexpr float cube_in(float t)
{
return t * t * t;
}
inline float cube_out(float t)
constexpr float cube_out(float t)
{
float f = (t - 1);
return f * f * f + 1;
}
inline float cube_in_out(float t)
constexpr float cube_in_out(float t)
{
if (t < 0.5f)
return 4 * t * t * t;
@ -75,18 +74,18 @@ namespace Blah
}
}
inline float quart_in(float t)
constexpr float quart_in(float t)
{
return t * t * t * t;
}
inline float quart_out(float t)
constexpr float quart_out(float t)
{
float f = (t - 1);
return f * f * f * (1 - t) + 1;
}
inline float quart_in_out(float t)
constexpr float quart_in_out(float t)
{
if (t < 0.5f)
return 8 * t * t * t * t;
@ -97,18 +96,18 @@ namespace Blah
}
}
inline float quint_in(float t)
constexpr float quint_in(float t)
{
return t * t * t * t * t;
}
inline float quint_out(float t)
constexpr float quint_out(float t)
{
float f = (t - 1);
return f * f * f * f * f + 1;
}
inline float quint_in_out(float t)
constexpr float quint_in_out(float t)
{
if (t < 0.5f)
return 16 * t * t * t * t * t;
@ -216,7 +215,7 @@ namespace Blah
}
}
inline float bounce_out(float t)
constexpr float bounce_out(float t)
{
if (t < 4 / 11.0f)
return (121 * t * t) / 16.0f;
@ -228,12 +227,12 @@ namespace Blah
return (54 / 5.0f * t * t) - (513 / 25.0f * t) + 268 / 25.0f;
}
inline float bounce_in(float t)
constexpr float bounce_in(float t)
{
return 1 - bounce_out(1 - t);
}
inline float bounce_in_out(float t)
constexpr float bounce_in_out(float t)
{
if (t < 0.5f)
return 0.5f * bounce_in(t * 2);
@ -245,6 +244,8 @@ namespace Blah
{
switch (e)
{
case Easers::Linear: return &linear;
case Easers::CubeIn: return &cube_in;
case Easers::CubeOut: return &cube_out;
case Easers::CubeInOut: return &cube_in_out;
@ -296,6 +297,7 @@ namespace Blah
{
switch (e)
{
case Easers::Linear: return "Linear";
case Easers::CubeIn: return "CubeIn";
case Easers::CubeOut: return "CubeOut";
case Easers::CubeInOut: return "CubeInOut";

View File

@ -10,7 +10,7 @@ namespace Blah
Vec2 a;
Vec2 b;
Line() {}
Line() = default;
Line(float x0, float y0, float x1, float y1);
Line(const Vec2& start, const Vec2& end);

View File

@ -8,7 +8,7 @@ namespace Blah
{
public:
FileStream();
FileStream(const char* path, FileMode mode = FileMode::ReadWrite);
FileStream(const FilePath& path, FileMode mode = FileMode::ReadWrite);
FileStream(FileStream&& fs) noexcept;
FileStream& operator=(FileStream&& fs) noexcept;
~FileStream();
@ -16,9 +16,9 @@ namespace Blah
virtual i64 length() const override;
virtual i64 position() const override;
virtual i64 seek(i64 seekTo) override;
virtual bool is_open() const override { return m_handle != nullptr; }
virtual bool is_readable() const override { return m_handle != nullptr && (m_mode == FileMode::ReadWrite || m_mode == FileMode::Read); }
virtual bool is_writable() const override { return m_handle != nullptr && (m_mode == FileMode::ReadWrite || m_mode == FileMode::Write); }
virtual bool is_open() const override;
virtual bool is_readable() const override;
virtual bool is_writable() const override;
virtual void close() override;
protected:
@ -26,7 +26,7 @@ namespace Blah
virtual i64 write_from(const void* ptr, i64 length) override;
private:
FileMode m_mode;
void* m_handle;
FileMode m_mode;
void* m_handle;
};
}

View File

@ -36,7 +36,7 @@ namespace Blah
// closes the stream
virtual void close() = 0;
// pipes the contents of this tream to another stream
// pipes the contents of this stream to another stream
i64 pipe(Stream& to, i64 length);
// reads the amount of bytes into the given buffer, and returns the amount read