diff --git a/CMakeLists.txt b/CMakeLists.txt index 9edfd02..abf53ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,30 +11,16 @@ add_library(blah src/common.cpp src/time.cpp src/input.cpp - - src/graphics/batch.cpp - src/graphics/blend.cpp - src/graphics/material.cpp - src/graphics/mesh.cpp - src/graphics/renderpass.cpp - src/graphics/shader.cpp - src/graphics/spritefont.cpp - src/graphics/subtexture.cpp - src/graphics/target.cpp - src/graphics/texture.cpp - + src/stream.cpp + src/graphics.cpp src/containers/str.cpp - + src/drawing/batch.cpp + src/drawing/spritefont.cpp + src/drawing/subtexture.cpp src/images/aseprite.cpp src/images/font.cpp src/images/image.cpp src/images/packer.cpp - - src/streams/bufferstream.cpp - src/streams/filestream.cpp - src/streams/memorystream.cpp - src/streams/stream.cpp - src/internal/renderer_opengl.cpp src/internal/renderer_d3d11.cpp src/internal/platform_sdl2.cpp diff --git a/include/blah.h b/include/blah.h index bed0b83..ed30ef0 100644 --- a/include/blah.h +++ b/include/blah.h @@ -5,22 +5,16 @@ #include "blah/filesystem.h" #include "blah/time.h" #include "blah/input.h" +#include "blah/stream.h" +#include "blah/graphics.h" #include "blah/containers/vector.h" #include "blah/containers/stackvector.h" #include "blah/containers/str.h" -#include "blah/graphics/batch.h" -#include "blah/graphics/spritefont.h" -#include "blah/graphics/subtexture.h" -#include "blah/graphics/blend.h" -#include "blah/graphics/material.h" -#include "blah/graphics/mesh.h" -#include "blah/graphics/renderpass.h" -#include "blah/graphics/sampler.h" -#include "blah/graphics/shader.h" -#include "blah/graphics/target.h" -#include "blah/graphics/texture.h" +#include "blah/drawing/batch.h" +#include "blah/drawing/spritefont.h" +#include "blah/drawing/subtexture.h" #include "blah/images/aseprite.h" #include "blah/images/font.h" @@ -30,9 +24,4 @@ #include "blah/math/calc.h" #include "blah/math/spatial.h" #include "blah/math/color.h" -#include "blah/math/ease.h" - -#include "blah/streams/bufferstream.h" -#include "blah/streams/filestream.h" -#include "blah/streams/memorystream.h" -#include "blah/streams/stream.h" \ No newline at end of file +#include "blah/math/ease.h" \ No newline at end of file diff --git a/include/blah/app.h b/include/blah/app.h index 29cbefd..cb2d292 100644 --- a/include/blah/app.h +++ b/include/blah/app.h @@ -1,5 +1,6 @@ #pragma once #include +#include #include namespace Blah @@ -10,31 +11,6 @@ namespace Blah // Application Logging Functions using AppLogFn = Func; - // Type of Renderer the Application is using - enum class RendererType - { - None = -1, - OpenGL, - D3D11, - }; - - // Renderer Information - struct RendererInfo - { - // The type of Renderer being used - RendererType type = RendererType::None; - - // 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; - }; - // Application Configuration struct Config { @@ -84,10 +60,6 @@ namespace Blah AppLogFn on_log = nullptr; }; - // Forward declare Target for the BackBuffer - class Target; - using TargetRef = Ref; - // Application namespace App { diff --git a/include/blah/graphics/batch.h b/include/blah/drawing/batch.h similarity index 96% rename from include/blah/graphics/batch.h rename to include/blah/drawing/batch.h index cb0bf5f..800acd7 100644 --- a/include/blah/graphics/batch.h +++ b/include/blah/drawing/batch.h @@ -1,14 +1,11 @@ #pragma once +#include #include +#include +#include #include #include -#include -#include -#include -#include -#include -#include -#include +#include namespace Blah { @@ -235,6 +232,6 @@ namespace Blah Vector m_batches; int m_batch_insert = 0; - void render_single_batch(RenderPass& pass, const DrawBatch& b, const Mat4x4f& matrix); + void render_single_batch(DrawCall& pass, const DrawBatch& b, const Mat4x4f& matrix); }; } \ No newline at end of file diff --git a/include/blah/graphics/spritefont.h b/include/blah/drawing/spritefont.h similarity index 97% rename from include/blah/graphics/spritefont.h rename to include/blah/drawing/spritefont.h index 73229fd..fd404a6 100644 --- a/include/blah/graphics/spritefont.h +++ b/include/blah/drawing/spritefont.h @@ -2,10 +2,8 @@ #include #include #include -#include +#include #include -#include -#include namespace Blah { diff --git a/include/blah/graphics/subtexture.h b/include/blah/drawing/subtexture.h similarity index 97% rename from include/blah/graphics/subtexture.h rename to include/blah/drawing/subtexture.h index 95e6137..15211b5 100644 --- a/include/blah/graphics/subtexture.h +++ b/include/blah/drawing/subtexture.h @@ -1,5 +1,5 @@ #pragma once -#include +#include #include namespace Blah diff --git a/include/blah/filesystem.h b/include/blah/filesystem.h index cecb2c2..bca774d 100644 --- a/include/blah/filesystem.h +++ b/include/blah/filesystem.h @@ -42,6 +42,9 @@ namespace Blah // Default Destructor virtual ~File() = default; + // Gets the Mode the File was opened with + FileMode mode() const; + // Gets the File Length virtual size_t length() = 0; @@ -52,10 +55,13 @@ namespace Blah virtual size_t seek(size_t position) = 0; // Reads from the File into the buffer, and returns how many bytes were successfully read - virtual size_t read(unsigned char* buffer, size_t length) = 0; + virtual size_t read(void* buffer, size_t length) = 0; // Writes from the buffer into the File, nd returns how many bytes were successfully written - virtual size_t write(const unsigned char* buffer, size_t length) = 0; + virtual size_t write(const void* buffer, size_t length) = 0; + + private: + FileMode m_mode; }; namespace Directory diff --git a/include/blah/graphics.h b/include/blah/graphics.h new file mode 100644 index 0000000..398fb26 --- /dev/null +++ b/include/blah/graphics.h @@ -0,0 +1,663 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include + +namespace Blah +{ + class Shader; using ShaderRef = Ref; + class Texture; using TextureRef = Ref; + class Target; using TargetRef = Ref; + class Mesh; using MeshRef = Ref; + class Material; using MaterialRef = Ref; + + // Type of Renderer the Application is using + enum class RendererType + { + None = -1, + OpenGL, + D3D11, + }; + + // Renderer Information + struct RendererInfo + { + // The type of Renderer being used + RendererType type = RendererType::None; + + // 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; + }; + + // Depth comparison function to use during a draw call + enum class Compare + { + None, + Always, + Never, + Less, + Equal, + LessOrEqual, + Greater, + NotEqual, + GreatorOrEqual + }; + + // Cull mode during a draw call + enum class Cull + { + // No Culling enabled + None = 0, + + // Cull front faces + Front = 1, + + // Cull back faces + Back = 2, + }; + + enum class BlendOp + { + Add, + Subtract, + ReverseSubtract, + Min, + Max + }; + + enum class BlendFactor + { + Zero, + One, + SrcColor, + OneMinusSrcColor, + DstColor, + OneMinusDstColor, + SrcAlpha, + OneMinusSrcAlpha, + DstAlpha, + OneMinusDstAlpha, + ConstantColor, + OneMinusConstantColor, + ConstantAlpha, + OneMinusConstantAlpha, + SrcAlphaSaturate, + Src1Color, + OneMinusSrc1Color, + Src1Alpha, + OneMinusSrc1Alpha + }; + + enum class BlendMask + { + None = 0, + Red = 1, + Green = 2, + Blue = 4, + Alpha = 8, + RGB = Red | Green | Blue, + RGBA = Red | Green | Blue | Alpha, + }; + + // BlendMode using for rendering + struct BlendMode + { + // Normal, pre-multiplied, Blend Mode + static const BlendMode Normal; + + // Subtractive Blend Mode + static const BlendMode Subtract; + + BlendOp color_op; + BlendFactor color_src; + BlendFactor color_dst; + BlendOp alpha_op; + BlendFactor alpha_src; + BlendFactor alpha_dst; + BlendMask mask; + u32 rgba; + + BlendMode() = default; + + BlendMode(BlendOp op, BlendFactor src, BlendFactor dst) : + color_op(op), color_src(src), color_dst(dst), + alpha_op(op), alpha_src(src), alpha_dst(dst), + mask(BlendMask::RGBA), rgba(0xffffffff) {} + + BlendMode( + BlendOp color_op, BlendFactor color_src, BlendFactor color_dst, + BlendOp alpha_op, BlendFactor alpha_src, BlendFactor alpha_dst, + BlendMask blend_mask, u32 blend_rgba) : + color_op(color_op), color_src(color_src), color_dst(color_dst), + alpha_op(alpha_op), alpha_src(alpha_src), alpha_dst(alpha_dst), + mask(blend_mask), rgba(blend_rgba) {} + + constexpr bool operator==(const BlendMode& rhs) const + { + return + color_op == rhs.color_op && color_src == rhs.color_src && color_dst == rhs.color_dst && + alpha_op == rhs.alpha_op && alpha_src == rhs.alpha_src && alpha_dst == rhs.alpha_dst && + mask == rhs.mask && rgba == rhs.rgba; + } + + constexpr bool operator!=(const BlendMode& rhs) const + { + return !(*this == rhs); + } + }; + + // Texture filter + enum class TextureFilter + { + None, // Will fallback to whatever default the driver sets + Linear, // Linear interpolation + Nearest // Nearest Neighbour interpolation + }; + + // Texture Wrap Mode + enum class TextureWrap + { + None, // Will fallback to whatever default the driver sets + Clamp, // Clamps the texture to the edges + Repeat // Repeats the texture + }; + + // Texture Sampler State, applied during rendering + struct TextureSampler + { + // Filter Mode + TextureFilter filter; + + // Wrap X Mode + TextureWrap wrap_x; + + // Wrap Y Mode + TextureWrap wrap_y; + + TextureSampler() : + filter(TextureFilter::Linear), wrap_x(TextureWrap::Repeat), wrap_y(TextureWrap::Repeat) {} + + TextureSampler(TextureFilter filter) : + filter(filter), wrap_x(TextureWrap::Repeat), wrap_y(TextureWrap::Repeat) {} + + TextureSampler(TextureFilter filter, TextureWrap wrap_x, TextureWrap wrap_y) : + filter(filter), wrap_x(wrap_x), wrap_y(wrap_y) {} + + bool operator==(const TextureSampler& rhs) const + { + return filter == rhs.filter && wrap_x == rhs.wrap_x && wrap_y == rhs.wrap_y; + } + + bool operator!=(const TextureSampler& rhs) const + { + return !(*this == rhs); + } + }; + + enum class TextureFormat + { + None, // Invalid Format + R, // Single 8-bit channe; + RG, // 2 8-bit channels + RGBA, // 4 8-bit channels + DepthStencil, // Depth 24, Stencil 8 + Count // Total Formats + }; + + enum class ClearMask + { + None = 0, + Color = 1, + Depth = 2, + Stencil = 4, + All = (int)Color | (int)Depth | (int)Stencil + }; + + // Supported Uniform Types + enum class UniformType + { + None, + Float, + Float2, + Float3, + Float4, + Mat3x2, + Mat4x4, + Texture2D, + Sampler2D + }; + + // Supported Shader Types + enum class ShaderType + { + None = 0, + Vertex = 1 << 0, + Fragment = 1 << 1 + }; + + // Uniform Info, provided by the Shader + struct UniformInfo + { + // Name of the Uniform + String name; + + // The Value type of the Uniform + UniformType type; + + // The Shader type the Uniform is a part of + ShaderType shader; + + // Some rendering APIs have uniform buffers. The `buffer_index` + // specifies which buffer the uniform belongs to + int buffer_index; + + // Array length of the Uniform (ex. a vec2[4] would be 4) + int array_length; + }; + + // Supported Vertex value types + enum class VertexType + { + None, + Float, + Float2, + Float3, + Float4, + Byte4, + UByte4, + Short2, + UShort2, + Short4, + UShort4 + }; + + // Vertex Attribute information + struct VertexAttribute + { + // Location / Attribute Index + int index = 0; + + // Vertex Type + VertexType type = VertexType::None; + + // Whether the Vertex should be normalized (doesn't apply to Floats) + bool normalized = false; + }; + + // Vertex Format information. + // Holds a list of attributes and total stride per-vertex. + struct VertexFormat + { + // List of Attributes + StackVector attributes; + + // Total size in bytes of each Vertex element + int stride = 0; + + VertexFormat() = default; + VertexFormat(const StackVector& attributes, int stride = 0); + }; + + // Supported Vertex Index formats + enum class IndexFormat + { + // Indices are 16 bit unsigned integers + UInt16, + + // Indices are 32 bit unsigned integers + UInt32 + }; + + // Data to be passed to the shader to construct it + struct ShaderData + { + struct HLSL_Attribute + { + // Semantic Name + const char* semantic_name = nullptr; + + // (optional) Semantic Index + int semantic_index = 0; + }; + + // Vertex Shader Program data + String vertex; + + // Fragment Shader Program data + String fragment; + + // HLSL Attributes - required for D3D11 + StackVector hlsl_attributes; + }; + + // A shader used during Rendering + class Shader + { + protected: + Shader() = default; + + public: + // Copy / Moves not allowed + Shader(const Shader&) = delete; + Shader(Shader&&) = delete; + Shader& operator=(const Shader&) = delete; + Shader& operator=(Shader&&) = delete; + + // Default Destructor + virtual ~Shader() = default; + + // Creates a Shader with the given Shader Data. + // If the Shader creation fails, it will return an invalid ShaderRef. + static ShaderRef create(const ShaderData& data); + + // Gets a list of Shader Uniforms from Shader + virtual Vector& uniforms() = 0; + + // Gets a list of Shader Uniforms from Shader + virtual const Vector& uniforms() const = 0; + }; + + // A 2D Texture held by the GPU to be used during rendering + class Texture + { + protected: + Texture() = default; + + public: + // Copy / Moves not allowed + Texture(const Texture&) = delete; + Texture(Texture&&) = delete; + Texture& operator=(const Texture&) = delete; + Texture& operator=(Texture&&) = delete; + + // Default Destructor + virtual ~Texture() = default; + + // Creates a new Texture. + // If the Texture creation fails, it will return an invalid TextureRef. + static TextureRef create(const Image& image); + + // Creates a new Texture. + // If image data is provided, it should be the full size of the texture. + // If the Texture creation fails, it will return an invalid TextureRef. + static TextureRef create(int width, int height, TextureFormat format, unsigned char* data = nullptr); + + // Creates a new Texture from a Stream. + // If the Texture creation fails, it will return an invalid TextureRef. + static TextureRef create(Stream& stream); + + // Creates a new Texture from a File. + // If the Texture creation fails, it will return an invalid TextureRef. + static TextureRef create(const FilePath& file); + + // gets the width of the texture + virtual int width() const = 0; + + // gets the height of the texture + virtual int height() const = 0; + + // Gets the format of the Texture + virtual TextureFormat format() const = 0; + + // Sets the data of the Texture. + // Note that the data should be the same format and size as the Texture. There is no row padding. + virtual void set_data(const u8* data) = 0; + + // Sets the data of the Texture to the provided Color buffer. + // If the Texture Format is not RGBA, this won't do anything. + void set_data(const Color* data); + + // Gets the data of the Texture. + // Note that the data will be written to in the same format as the Texture, + // and you should allocate enough space for the full texture. There is no row padding. + virtual void get_data(u8* data) = 0; + + // Gets the data of the Texture. + // If the Texture Format is not RGBA, this won't do anything. + void get_data(Color* data); + + // Returns true if the Texture is part of a FrameBuffer + virtual bool is_framebuffer() const = 0; + }; + + // Up to 4 color textures + 1 depth/stencil + using Attachments = StackVector; + using AttachmentFormats = StackVector; + + // Target is a 2D Buffer that can be drawn to. + // It can hold up to 4 color Textures, and 1 Depth/Stencil Texture. + class Target + { + protected: + Target() = default; + + public: + // Copy / Moves not allowed + Target(const Target&) = delete; + Target(Target&&) = delete; + Target& operator=(const Target&) = delete; + Target& operator=(Target&&) = delete; + + // Default Destructor + virtual ~Target() = default; + + // Creates a new Target with a single Color texture + // If the Target creation fails, it will return an invalid TargetRef. + static TargetRef create(int width, int height); + + // Creates a new Target with the given Texture Attachments. You must provide at least one Attachment. + // If the Target creation fails, it will return an invalid TargetRef. + static TargetRef create(int width, int height, const AttachmentFormats& textures); + + // Gets the list of Attachments from the Target + virtual Attachments& textures() = 0; + + // Gets the list of Attachments from the Target + virtual const Attachments& textures() const = 0; + + // Gets the Attachment at a given index from the Target + TextureRef& texture(int index); + + // Gets the Attachment at a given index from the Target + const TextureRef& texture(int index) const; + + // Gets the width of the Target + virtual int width() const; + + // Gets the height of the Target + virtual int height() const; + + // Clears the Target + virtual void clear(Color color = Color::black, float depth = 1.0f, u8 stencil = 0, ClearMask mask = ClearMask::All) = 0; + }; + + // A Mesh is a set of Indices and Vertices which are used for drawing + class Mesh + { + protected: + Mesh() = default; + + public: + // Copy / Moves not allowed + Mesh(const Mesh&) = delete; + Mesh(Mesh&&) = delete; + Mesh& operator=(const Mesh&) = delete; + Mesh& operator=(Mesh&&) = delete; + + // Default Destructor + virtual ~Mesh() = default; + + // Creates a new Mesh. + // If the Mesh creation fails, it will return an invalid Mesh. + static MeshRef create(); + + // Uploads the given index buffer to the Mesh + virtual void index_data(IndexFormat format, const void* indices, i64 count) = 0; + + // Uploads the given vertex buffer to the Mesh + virtual void vertex_data(const VertexFormat& format, const void* vertices, i64 count) = 0; + + // Uploads the given instance buffer to the Mesh + virtual void instance_data(const VertexFormat& format, const void* instances, i64 count) = 0; + + // Gets the index count of the Mesh + virtual i64 index_count() const = 0; + + // Gets the vertex count of the Mesh + virtual i64 vertex_count() const = 0; + + // Gets the instance count of the Mesh + virtual i64 instance_count() const = 0; + }; + + // Materials hold values that can be assigned to a shader during rendering + class Material final + { + private: + Material(const ShaderRef& shader); + + public: + // Copy / Moves not allowed + Material(const Material&) = delete; + Material(Material&&) = delete; + Material& operator=(const Material&) = delete; + Material& operator=(Material&&) = delete; + + // Default destructor + ~Material() = default; + + // Creates a new Material from the given Shader. + // If the Shader is invalid, it will return an invalid MaterialRef. + static MaterialRef create(const ShaderRef& shader); + + // Clones the material and returns a new one + MaterialRef clone() const; + + // Returns the Shader assigned to the Material. + ShaderRef shader() const; + + // Sets the texture + void set_texture(const char* name, const TextureRef& texture, int array_index = 0); + + // Sets the texture + void set_texture(int slot, const TextureRef& texture, int array_index = 0); + + // Gets the texture, or an empty reference if invalid + TextureRef get_texture(const char* name, int array_index = 0) const; + + // Gets the texture, or an empty reference if invalid + TextureRef get_texture(int slot, int array_index = 0) const; + + // Sets the sampler + void set_sampler(const char* name, const TextureSampler& sampler, int array_index = 0); + + // Sets the sampler + void set_sampler(int slot, const TextureSampler& sampler, int array_index = 0); + + // Gets the sampler + TextureSampler get_sampler(const char* name, int array_index = 0) const; + + // Gets the sampler + TextureSampler get_sampler(int slot, int array_index = 0) const; + + // Sets the value. `length` is the total number of floats to set + // For example if the uniform is a float2[4], a total of 8 float values + // can be set. + void set_value(const char* name, const float* value, i64 length); + + // Shorthands to more easily assign uniform values + void set_value(const char* name, float value); + void set_value(const char* name, const Vec2f& value); + void set_value(const char* name, const Vec3f& value); + void set_value(const char* name, const Vec4f& value); + void set_value(const char* name, const Mat3x2f& value); + void set_value(const char* name, const Mat4x4f& value); + void set_value(const char* name, const Vector& value); + void set_value(const char* name, const Vector& value); + void set_value(const char* name, const Vector& value); + void set_value(const char* name, const Vector& value); + void set_value(const char* name, const Vector& value); + void set_value(const char* name, const Vector& value); + + // Gets a pointer to the values of the given Uniform, or nullptr if it doesn't exist. + const float* get_value(const char* name, i64* length = nullptr) const; + + // Checks if the shader attached to the material has a uniform value with the given name + bool has_value(const char* name) const; + + // Returns the internal Texture buffer + const Vector& textures() const; + + // Returns the internal Sampler buffer + const Vector& samplers() const; + + // Returns the interal float buffer of all the values + const float* data() const; + + private: + ShaderRef m_shader; + Vector m_textures; + Vector m_samplers; + Vector m_data; + }; + + // A single draw call + struct DrawCall + { + // Framebuffer to draw to + TargetRef target; + + // Mesh to draw with + MeshRef mesh; + + // Material to draw with + MaterialRef material; + + // Whether the DrawCall should use a specific viewport + bool has_viewport; + + // Whether the DrawCall should use a scissor rectangle + bool has_scissor; + + // The viewport (only used if hasViewport is true) + Rectf viewport; + + // The scissor rectangle (only used if hasScissor is true) + Rectf scissor; + + // First index in the Mesh to draw from + i64 index_start; + + // Total amount of indices to draw from the Mesh + i64 index_count; + + // Total amount of instances to draw from the Mesh + i64 instance_count; + + // Depth Compare Function + Compare depth; + + // Cull Mode + Cull cull; + + // Blend Mode + BlendMode blend; + + // Initializes a default DrawCall + DrawCall(); + + // Performs the render + void perform(); + }; +} \ No newline at end of file diff --git a/include/blah/graphics/blend.h b/include/blah/graphics/blend.h deleted file mode 100644 index 685d8ed..0000000 --- a/include/blah/graphics/blend.h +++ /dev/null @@ -1,105 +0,0 @@ -#pragma once -#include - -namespace Blah -{ - enum class BlendOp - { - Add, - Subtract, - ReverseSubtract, - Min, - Max - }; - - enum class BlendFactor - { - Zero, - One, - SrcColor, - OneMinusSrcColor, - DstColor, - OneMinusDstColor, - SrcAlpha, - OneMinusSrcAlpha, - DstAlpha, - OneMinusDstAlpha, - ConstantColor, - OneMinusConstantColor, - ConstantAlpha, - OneMinusConstantAlpha, - SrcAlphaSaturate, - Src1Color, - OneMinusSrc1Color, - Src1Alpha, - OneMinusSrc1Alpha - }; - - enum class BlendMask - { - None = 0, - Red = 1, - Green = 2, - Blue = 4, - Alpha = 8, - RGB = Red | Green | Blue, - RGBA = Red | Green | Blue | Alpha, - }; - - // BlendMode using for rendering - struct BlendMode - { - // Normal, pre-multiplied, Blend Mode - static const BlendMode Normal; - - // Subtractive Blend Mode - static const BlendMode Subtract; - - BlendOp color_op; - BlendFactor color_src; - BlendFactor color_dst; - BlendOp alpha_op; - BlendFactor alpha_src; - BlendFactor alpha_dst; - BlendMask mask; - u32 rgba; - - BlendMode() = default; - - BlendMode(BlendOp op, BlendFactor src, BlendFactor dst) : - color_op(op), - color_src(src), - color_dst(dst), - alpha_op(op), - alpha_src(src), - alpha_dst(dst), - mask(BlendMask::RGBA), - rgba(0xffffffff) {} - - BlendMode( - BlendOp color_op, BlendFactor color_src, BlendFactor color_dst, - BlendOp alpha_op, BlendFactor alpha_src, BlendFactor alpha_dst, - BlendMask blend_mask, u32 blend_rgba) : - color_op(color_op), - color_src(color_src), - color_dst(color_dst), - alpha_op(alpha_op), - alpha_src(alpha_src), - alpha_dst(alpha_dst), - mask(blend_mask), - rgba(blend_rgba) {} - - bool operator==(const BlendMode& rhs) const - { - return - color_op == rhs.color_op && color_src == rhs.color_src && color_dst == rhs.color_dst && - alpha_op == rhs.alpha_op && alpha_src == rhs.alpha_src && alpha_dst == rhs.alpha_dst && - mask == rhs.mask && rgba == rhs.rgba; - } - - bool operator!=(const BlendMode& rhs) const - { - return !(*this == rhs); - } - }; -} \ No newline at end of file diff --git a/include/blah/graphics/material.h b/include/blah/graphics/material.h deleted file mode 100644 index 72f64d7..0000000 --- a/include/blah/graphics/material.h +++ /dev/null @@ -1,104 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include - -namespace Blah -{ - class Material; - using MaterialRef = Ref; - - // Materials hold values that can be assigned to a shader during rendering - class Material final - { - private: - Material(const ShaderRef& shader); - - public: - // Copy / Moves not allowed - Material(const Material&) = delete; - Material(Material&&) = delete; - Material& operator=(const Material&) = delete; - Material& operator=(Material&&) = delete; - - // Default destructor - ~Material() = default; - - // Creates a new Material from the given Shader. - // If the Shader is invalid, it will return an invalid MaterialRef. - static MaterialRef create(const ShaderRef& shader); - - // Clones the material and returns a new one - MaterialRef clone() const; - - // Returns the Shader assigned to the Material. - ShaderRef shader() const; - - // Sets the texture - void set_texture(const char* name, const TextureRef& texture, int array_index = 0); - - // Sets the texture - void set_texture(int slot, const TextureRef& texture, int array_index = 0); - - // Gets the texture, or an empty reference if invalid - TextureRef get_texture(const char* name, int array_index = 0) const; - - // Gets the texture, or an empty reference if invalid - TextureRef get_texture(int slot, int array_index = 0) const; - - // Sets the sampler - void set_sampler(const char* name, const TextureSampler& sampler, int array_index = 0); - - // Sets the sampler - void set_sampler(int slot, const TextureSampler& sampler, int array_index = 0); - - // Gets the sampler - TextureSampler get_sampler(const char* name, int array_index = 0) const; - - // Gets the sampler - TextureSampler get_sampler(int slot, int array_index = 0) const; - - // Sets the value. `length` is the total number of floats to set - // For example if the uniform is a float2[4], a total of 8 float values - // can be set. - void set_value(const char* name, const float* value, i64 length); - - // Shorthands to more easily assign uniform values - void set_value(const char* name, float value); - void set_value(const char* name, const Vec2f& value); - void set_value(const char* name, const Vec3f& value); - void set_value(const char* name, const Vec4f& value); - void set_value(const char* name, const Mat3x2f& value); - void set_value(const char* name, const Mat4x4f& value); - void set_value(const char* name, const Vector& value); - void set_value(const char* name, const Vector& value); - void set_value(const char* name, const Vector& value); - void set_value(const char* name, const Vector& value); - void set_value(const char* name, const Vector& value); - void set_value(const char* name, const Vector& value); - - // Gets a pointer to the values of the given Uniform, or nullptr if it doesn't exist. - const float* get_value(const char* name, i64* length = nullptr) const; - - // Checks if the shader attached to the material has a uniform value with the given name - bool has_value(const char* name) const; - - // Returns the internal Texture buffer - const Vector& textures() const; - - // Returns the internal Sampler buffer - const Vector& samplers() const; - - // Returns the interal float buffer of all the values - const float* data() const; - - private: - ShaderRef m_shader; - Vector m_textures; - Vector m_samplers; - Vector m_data; - }; -} \ No newline at end of file diff --git a/include/blah/graphics/mesh.h b/include/blah/graphics/mesh.h deleted file mode 100644 index 6948f03..0000000 --- a/include/blah/graphics/mesh.h +++ /dev/null @@ -1,101 +0,0 @@ -#pragma once -#include -#include - -namespace Blah -{ - // Supported Vertex value types - enum class VertexType - { - None, - Float, - Float2, - Float3, - Float4, - Byte4, - UByte4, - Short2, - UShort2, - Short4, - UShort4 - }; - - // Vertex Attribute information - struct VertexAttribute - { - // Location / Attribute Index - int index = 0; - - // Vertex Type - VertexType type = VertexType::None; - - // Whether the Vertex should be normalized (doesn't apply to Floats) - bool normalized = false; - }; - - // Vertex Format information. - // Holds a list of attributes and total stride per-vertex. - struct VertexFormat - { - // List of Attributes - StackVector attributes; - - // Total size in bytes of each Vertex element - int stride = 0; - - VertexFormat() = default; - VertexFormat(const StackVector& attributes, int stride = 0); - }; - - // Supported Vertex Index formats - enum class IndexFormat - { - // Indices are 16 bit unsigned integers - UInt16, - - // Indices are 32 bit unsigned integers - UInt32 - }; - - class Mesh; - using MeshRef = Ref; - - // A Mesh is a set of Indices and Vertices which are used for drawing - class Mesh - { - protected: - Mesh() = default; - - public: - // Copy / Moves not allowed - Mesh(const Mesh&) = delete; - Mesh(Mesh&&) = delete; - Mesh& operator=(const Mesh&) = delete; - Mesh& operator=(Mesh&&) = delete; - - // Default Destructor - virtual ~Mesh() = default; - - // Creates a new Mesh. - // If the Mesh creation fails, it will return an invalid Mesh. - static MeshRef create(); - - // Uploads the given index buffer to the Mesh - virtual void index_data(IndexFormat format, const void* indices, i64 count) = 0; - - // Uploads the given vertex buffer to the Mesh - virtual void vertex_data(const VertexFormat& format, const void* vertices, i64 count) = 0; - - // Uploads the given instance buffer to the Mesh - virtual void instance_data(const VertexFormat& format, const void* instances, i64 count) = 0; - - // Gets the index count of the Mesh - virtual i64 index_count() const = 0; - - // Gets the vertex count of the Mesh - virtual i64 vertex_count() const = 0; - - // Gets the instance count of the Mesh - virtual i64 instance_count() const = 0; - }; -} \ No newline at end of file diff --git a/include/blah/graphics/renderpass.h b/include/blah/graphics/renderpass.h deleted file mode 100644 index 79f37b7..0000000 --- a/include/blah/graphics/renderpass.h +++ /dev/null @@ -1,89 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace Blah -{ - // Depth comparison function to use during a draw call - enum class Compare - { - None, - Always, - Never, - Less, - Equal, - LessOrEqual, - Greater, - NotEqual, - GreatorOrEqual - }; - - // Cull mode during a draw call - enum class Cull - { - // No Culling enabled - None = 0, - - // Cull front faces - Front = 1, - - // Cull back faces - Back = 2, - }; - - // A single draw call - struct RenderPass - { - // Framebuffer to draw to - TargetRef target; - - // Mesh to draw with - MeshRef mesh; - - // Material to draw with - MaterialRef material; - - // Whether the RenderPass should use a specific viewport - bool has_viewport; - - // Whether the RenderPass should use a scissor rectangle - bool has_scissor; - - // The viewport (only used if hasViewport is true) - Rectf viewport; - - // The scissor rectangle (only used if hasScissor is true) - Rectf scissor; - - // First index in the Mesh to draw from - i64 index_start; - - // Total amount of indices to draw from the Mesh - i64 index_count; - - // Total amount of instances to draw from the Mesh - i64 instance_count; - - // Depth Compare Function - Compare depth; - - // Cull Mode - Cull cull; - - // Blend Mode - BlendMode blend; - - // Initializes a default RenderPass - RenderPass(); - - // Performs the render - void perform(); - }; -} \ No newline at end of file diff --git a/include/blah/graphics/sampler.h b/include/blah/graphics/sampler.h deleted file mode 100644 index c75fb94..0000000 --- a/include/blah/graphics/sampler.h +++ /dev/null @@ -1,71 +0,0 @@ -#pragma once - -namespace Blah -{ - // Texture filter - enum class TextureFilter - { - // None will fallback to whatever default the driver sets - None, - - // Linear interpolation - Linear, - - // Nearest Neighbour interpolation - Nearest - }; - - // Texture Wrap Mode - enum class TextureWrap - { - // None will fallback to whatever default the driver sets - None, - - // Clamps the texture to the edges - Clamp, - - // Repeats the texture - Repeat - }; - - // Texture Sampler State, applied during rendering - struct TextureSampler - { - // Filter Mode - TextureFilter filter; - - // Wrap X Mode - TextureWrap wrap_x; - - // Wrap Y Mode - TextureWrap wrap_y; - - TextureSampler() : - filter(TextureFilter::Linear), - wrap_x(TextureWrap::Repeat), - wrap_y(TextureWrap::Repeat) {} - - TextureSampler(TextureFilter filter) : - filter(filter), - wrap_x(TextureWrap::Repeat), - wrap_y(TextureWrap::Repeat) {} - - TextureSampler(TextureFilter filter, TextureWrap wrap_x, TextureWrap wrap_y) : - filter(filter), - wrap_x(wrap_x), - wrap_y(wrap_y) {} - - bool operator==(const TextureSampler& rhs) const - { - return - filter == rhs.filter && - wrap_x == rhs.wrap_x && - wrap_y == rhs.wrap_y; - } - - bool operator!=(const TextureSampler& rhs) const - { - return !(*this == rhs); - } - }; -} \ No newline at end of file diff --git a/include/blah/graphics/shader.h b/include/blah/graphics/shader.h deleted file mode 100644 index 339ae18..0000000 --- a/include/blah/graphics/shader.h +++ /dev/null @@ -1,102 +0,0 @@ -#pragma once -#include -#include -#include - -namespace Blah -{ - // Supported Uniform Types - enum class UniformType - { - None, - Float, - Float2, - Float3, - Float4, - Mat3x2, - Mat4x4, - Texture2D, - Sampler2D - }; - - // Supported Shader Types - enum class ShaderType - { - None = 0, - Vertex = 1 << 0, - Fragment = 1 << 1 - }; - - // Uniform Info, provided by the Shader - struct UniformInfo - { - // Name of the Uniform - String name; - - // The Value type of the Uniform - UniformType type; - - // The Shader type the Uniform is a part of - ShaderType shader; - - // Some rendering APIs have uniform buffers. The `buffer_index` - // specifies which buffer the uniform belongs to - int buffer_index; - - // Array length of the Uniform (ex. a vec2[4] would be 4) - int array_length; - }; - - // Data to be passed to the shader to construct it - struct ShaderData - { - struct HLSL_Attribute - { - // Semantic Name - const char* semantic_name = nullptr; - - // (optional) Semantic Index - int semantic_index = 0; - }; - - // Vertex Shader Program data - String vertex; - - // Fragment Shader Program data - String fragment; - - // HLSL Attributes - required for D3D11 - StackVector hlsl_attributes; - }; - - class Shader; - using ShaderRef = Ref; - - // A shader used during Rendering - class Shader - { - protected: - Shader() = default; - - public: - // Copy / Moves not allowed - Shader(const Shader&) = delete; - Shader(Shader&&) = delete; - Shader& operator=(const Shader&) = delete; - Shader& operator=(Shader&&) = delete; - - // Default Destructor - virtual ~Shader() = default; - - // Creates a Shader with the given Shader Data. - // If the Shader creation fails, it will return an invalid ShaderRef. - static ShaderRef create(const ShaderData& data); - - // Gets a list of Shader Uniforms from Shader - virtual Vector& uniforms() = 0; - - // Gets a list of Shader Uniforms from Shader - virtual const Vector& uniforms() const = 0; - }; - -} \ No newline at end of file diff --git a/include/blah/graphics/target.h b/include/blah/graphics/target.h deleted file mode 100644 index b41b938..0000000 --- a/include/blah/graphics/target.h +++ /dev/null @@ -1,72 +0,0 @@ -#pragma once -#include -#include -#include -#include - -namespace Blah -{ - enum class ClearMask - { - None = 0, - Color = 1, - Depth = 2, - Stencil = 4, - All = (int)Color | (int)Depth | (int)Stencil - }; - - // Up to 4 color textures + 1 depth/stencil - using Attachments = StackVector; - using AttachmentFormats = StackVector; - - class Target; - using TargetRef = Ref; - - // Target is a 2D Buffer that can be drawn to. - // It can hold up to 4 color Textures, and 1 Depth/Stencil Texture. - class Target - { - protected: - Target() = default; - - public: - // Copy / Moves not allowed - Target(const Target&) = delete; - Target(Target&&) = delete; - Target& operator=(const Target&) = delete; - Target& operator=(Target&&) = delete; - - // Default Destructor - virtual ~Target() = default; - - // Creates a new Target with a single Color texture - // If the Target creation fails, it will return an invalid TargetRef. - static TargetRef create(int width, int height); - - // Creates a new Target with the given Texture Attachments. You must provide at least one Attachment. - // If the Target creation fails, it will return an invalid TargetRef. - static TargetRef create(int width, int height, const AttachmentFormats& textures); - - // Gets the list of Attachments from the Target - virtual Attachments& textures() = 0; - - // Gets the list of Attachments from the Target - virtual const Attachments& textures() const = 0; - - // Gets the Attachment at a given index from the Target - TextureRef& texture(int index); - - // Gets the Attachment at a given index from the Target - const TextureRef& texture(int index) const; - - // Gets the width of the Target - virtual int width() const; - - // Gets the height of the Target - virtual int height() const; - - // Clears the Target - virtual void clear(Color color = Color::black, float depth = 1.0f, u8 stencil = 0, ClearMask mask = ClearMask::All) = 0; - }; - -} diff --git a/include/blah/graphics/texture.h b/include/blah/graphics/texture.h deleted file mode 100644 index a123341..0000000 --- a/include/blah/graphics/texture.h +++ /dev/null @@ -1,96 +0,0 @@ -#pragma once -#include -#include -#include - -namespace Blah -{ - enum class TextureFormat - { - // Invalid Format - None, - - // Single 8-bit channe; - R, - - // 2 8-bit channels - RG, - - // 4 8-bit channels - RGBA, - - // Depth 24, Stencil 8 - DepthStencil, - - // Total Formats - Count - }; - - class Image; - class Stream; - class Texture; - using TextureRef = Ref; - - // A 2D Texture held by the GPU to be used during rendering - class Texture - { - protected: - Texture() = default; - - public: - // Copy / Moves not allowed - Texture(const Texture&) = delete; - Texture(Texture&&) = delete; - Texture& operator=(const Texture&) = delete; - Texture& operator=(Texture&&) = delete; - - // Default Destructor - virtual ~Texture() = default; - - // Creates a new Texture. - // If the Texture creation fails, it will return an invalid TextureRef. - static TextureRef create(const Image& image); - - // Creates a new Texture. - // If image data is provided, it should be the full size of the texture. - // If the Texture creation fails, it will return an invalid TextureRef. - static TextureRef create(int width, int height, TextureFormat format, unsigned char* data = nullptr); - - // Creates a new Texture from a Stream. - // If the Texture creation fails, it will return an invalid TextureRef. - static TextureRef create(Stream& stream); - - // Creates a new Texture from a File. - // If the Texture creation fails, it will return an invalid TextureRef. - static TextureRef create(const FilePath& file); - - // gets the width of the texture - virtual int width() const = 0; - - // gets the height of the texture - virtual int height() const = 0; - - // Gets the format of the Texture - virtual TextureFormat format() const = 0; - - // Sets the data of the Texture. - // Note that the data should be the same format and size as the Texture. There is no row padding. - virtual void set_data(const u8* data) = 0; - - // Sets the data of the Texture to the provided Color buffer. - // If the Texture Format is not RGBA, this won't do anything. - void set_data(const Color* data); - - // Gets the data of the Texture. - // Note that the data will be written to in the same format as the Texture, - // and you should allocate enough space for the full texture. There is no row padding. - virtual void get_data(u8* data) = 0; - - // Gets the data of the Texture. - // If the Texture Format is not RGBA, this won't do anything. - void get_data(Color* data); - - // Returns true if the Texture is part of a FrameBuffer - virtual bool is_framebuffer() const = 0; - }; -} \ No newline at end of file diff --git a/include/blah/images/aseprite.h b/include/blah/images/aseprite.h index 6eca495..8cf2a3b 100644 --- a/include/blah/images/aseprite.h +++ b/include/blah/images/aseprite.h @@ -1,9 +1,9 @@ #pragma once +#include #include #include #include -#include -#include +#include namespace Blah { diff --git a/include/blah/images/font.h b/include/blah/images/font.h index 6d4add0..0aedcec 100644 --- a/include/blah/images/font.h +++ b/include/blah/images/font.h @@ -1,6 +1,6 @@ #pragma once #include -#include +#include #include #include #include diff --git a/include/blah/images/image.h b/include/blah/images/image.h index 1399baf..49c53be 100644 --- a/include/blah/images/image.h +++ b/include/blah/images/image.h @@ -2,6 +2,7 @@ #include #include #include +#include namespace Blah { diff --git a/include/blah/images/packer.h b/include/blah/images/packer.h index be48bad..638354c 100644 --- a/include/blah/images/packer.h +++ b/include/blah/images/packer.h @@ -1,10 +1,11 @@ #pragma once +#include #include #include #include #include #include -#include +#include #include namespace Blah diff --git a/include/blah/math/color.h b/include/blah/math/color.h index ca425ae..4223341 100644 --- a/include/blah/math/color.h +++ b/include/blah/math/color.h @@ -233,15 +233,15 @@ namespace Blah static const Color teal; }; - inline const Color Color::transparent = Color(0, 0, 0, 0); - inline const Color Color::white = Color(255, 255, 255, 255); - inline const Color Color::black = Color(0, 0, 0, 255); - inline const Color Color::red = Color(255, 0, 0, 255); - inline const Color Color::green = Color(0, 255, 0, 255); - inline const Color Color::blue = Color(0, 0, 255, 255); - inline const Color Color::yellow = Color(255, 255, 0, 255); - inline const Color Color::purple = Color(255, 0, 255, 255); - inline const Color Color::teal = Color(0, 255, 255, 255); + inline const Color Color::transparent = Color( 0, 0, 0, 0); + inline const Color Color::white = Color(255, 255, 255); + inline const Color Color::black = Color( 0, 0, 0); + inline const Color Color::red = Color(255, 0, 0); + inline const Color Color::green = Color( 0, 255, 0); + inline const Color Color::blue = Color( 0, 0, 255); + inline const Color Color::yellow = Color(255, 255, 0); + inline const Color Color::purple = Color(255, 0, 255); + inline const Color Color::teal = Color( 0, 255, 255); } #undef BLAH_HEX_VALUE diff --git a/include/blah/math/ease.h b/include/blah/math/ease.h index e9d70a6..09028d5 100644 --- a/include/blah/math/ease.h +++ b/include/blah/math/ease.h @@ -1,6 +1,6 @@ #pragma once -#include #include +#include namespace Blah { @@ -9,16 +9,16 @@ namespace Blah enum class Easers { Linear, - QuadIn, QuadOut, QuadInOut, - CubeIn, CubeOut, CubeInOut, - QuartIn, QuartOut, QuartInOut, - QuintIn, QuintOut, QuintInOut, - SineIn, SineOut, SineInOut, - CircIn, CircOut, CircInOut, - ExpIn, ExpOut, ExpInOut, + QuadIn, QuadOut, QuadInOut, + CubeIn, CubeOut, CubeInOut, + QuartIn, QuartOut, QuartInOut, + QuintIn, QuintOut, QuintInOut, + SineIn, SineOut, SineInOut, + CircIn, CircOut, CircInOut, + ExpIn, ExpOut, ExpInOut, ElasticIn, ElasticOut, ElasticInOut, - BackIn, BackOut, BackInOut, - BounceIn, BounceOut, BounceInOut, + BackIn, BackOut, BackInOut, + BounceIn, BounceOut, BounceInOut, Count }; diff --git a/include/blah/streams/stream.h b/include/blah/stream.h similarity index 54% rename from include/blah/streams/stream.h rename to include/blah/stream.h index d076ace..434483a 100644 --- a/include/blah/streams/stream.h +++ b/include/blah/stream.h @@ -1,10 +1,15 @@ #pragma once #include #include +#include #include +#include namespace Blah { + // Stream is a generic handler to read and write data to various buffers. + // The purpose is to allow serializers to not care what they're writing data to, + // be it a file or memory. class Stream { public: @@ -44,7 +49,7 @@ namespace Blah // reads a string until a newline '\n' or null-terminator '\0' is found String read_line(); - // reads a number + // reads a number, returns the value read. u8 read_u8 (Endian endian = Endian::Little); u16 read_u16(Endian endian = Endian::Little); u32 read_u32(Endian endian = Endian::Little); @@ -81,4 +86,86 @@ namespace Blah // writes the amount of bytes to the stream from the given buffer, and returns the amount written virtual size_t write_data(const void* buffer, size_t length) = 0; }; + + // File Stream reads & writes over a File handle + class FileStream : public Stream + { + public: + FileStream() = default; + FileStream(const FilePath& path, FileMode mode); + FileStream(const FileRef& file); + + size_t length() const override; + size_t position() const override; + size_t seek(size_t position) override; + bool is_open() const override; + bool is_readable() const override; + bool is_writable() const override; + + protected: + size_t read_data(void* ptr, size_t length) override; + size_t write_data(const void* ptr, size_t length) override; + + private: + FileRef m_file; + }; + + // Memory Stream moves over an existing buffer. + // The Buffer must exist for the duration of the Memory Stream. + class MemoryStream : public Stream + { + public: + MemoryStream() = default; + MemoryStream(u8* data, size_t length); + MemoryStream(const u8* data, size_t length); + + size_t length() const override; + size_t position() const override; + size_t seek(size_t seek_to) override; + bool is_open() const override; + bool is_readable() const override; + bool is_writable() const override; + + u8* data(); + const u8* data() const; + + protected: + size_t read_data(void* ptr, size_t length) override; + size_t write_data(const void* ptr, size_t length) override; + + private: + u8* m_data = nullptr; + const u8* m_const_data = nullptr; + size_t m_length = 0; + size_t m_position = 0; + }; + + // Buffer Stream reads and writes to an internal buffer. + // It will grow the capacity if it needs to while writing. + class BufferStream : public Stream + { + public: + BufferStream() = default; + BufferStream(int capacity); + + size_t length() const override; + size_t position() const override; + size_t seek(size_t seek_to) override; + bool is_open() const override; + bool is_readable() const override; + bool is_writable() const override; + + void resize(size_t length); + void clear(); + u8* data(); + const u8* data() const; + + protected: + size_t read_data(void* ptr, size_t length) override; + size_t write_data(const void* ptr, size_t length) override; + + private: + Vector m_buffer; + size_t m_position = 0; + }; } \ No newline at end of file diff --git a/include/blah/streams/bufferstream.h b/include/blah/streams/bufferstream.h deleted file mode 100644 index 3f917df..0000000 --- a/include/blah/streams/bufferstream.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once -#include - -namespace Blah -{ - // Buffer Stream reads and writes to an internal buffer. - // It will grow the capacity if it needs to while writing. - class BufferStream : public Stream - { - public: - BufferStream() = default; - BufferStream(int capacity); - - size_t length() const override; - size_t position() const override; - size_t seek(size_t seek_to) override; - bool is_open() const override; - bool is_readable() const override; - bool is_writable() const override; - - void resize(size_t length); - void clear(); - - u8* data(); - const u8* data() const; - - protected: - size_t read_data(void* ptr, size_t length) override; - size_t write_data(const void* ptr, size_t length) override; - - private: - Vector m_buffer; - size_t m_position = 0; - }; -} \ No newline at end of file diff --git a/include/blah/streams/filestream.h b/include/blah/streams/filestream.h deleted file mode 100644 index d0f6152..0000000 --- a/include/blah/streams/filestream.h +++ /dev/null @@ -1,30 +0,0 @@ -#pragma once -#include -#include - -namespace Blah -{ - class FileStream : public Stream - { - public: - FileStream() = default; - FileStream(const FilePath& path, FileMode mode); - FileStream(FileStream&& fs) noexcept; - FileStream& operator=(FileStream&& fs) noexcept; - - size_t length() const override; - size_t position() const override; - size_t seek(size_t position) override; - bool is_open() const override; - bool is_readable() const override; - bool is_writable() const override; - - protected: - size_t read_data(void* ptr, size_t length) override; - size_t write_data(const void* ptr, size_t length) override; - - private: - FileMode m_mode = FileMode::OpenRead; - FileRef m_file; - }; -} \ No newline at end of file diff --git a/include/blah/streams/memorystream.h b/include/blah/streams/memorystream.h deleted file mode 100644 index bb03092..0000000 --- a/include/blah/streams/memorystream.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once -#include - -namespace Blah -{ - // Memory Stream moves over an existing buffer. - // The Buffer must exist for the duration of the Memory Stream. - class MemoryStream : public Stream - { - public: - MemoryStream() = default; - MemoryStream(u8* data, size_t length); - MemoryStream(const u8* data, size_t length); - - size_t length() const override; - size_t position() const override; - size_t seek(size_t seek_to) override; - bool is_open() const override; - bool is_readable() const override; - bool is_writable() const override; - - u8* data(); - const u8* data() const; - - protected: - size_t read_data(void* ptr, size_t length) override; - size_t write_data(const void* ptr, size_t length) override; - - private: - u8* m_data = nullptr; - const u8* m_const_data = nullptr; - size_t m_length = 0; - size_t m_position = 0; - }; -} \ No newline at end of file diff --git a/src/app.cpp b/src/app.cpp index c788a0f..4831737 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include "internal/internal.h" #include "internal/platform.h" #include "internal/renderer.h" diff --git a/src/graphics/batch.cpp b/src/drawing/batch.cpp similarity index 98% rename from src/graphics/batch.cpp rename to src/drawing/batch.cpp index 3f27481..f28e54a 100644 --- a/src/graphics/batch.cpp +++ b/src/drawing/batch.cpp @@ -1,13 +1,7 @@ -#include -#include -#include -#include -#include -#include +#include #include #include #include "../internal/internal.h" -#include using namespace Blah; @@ -296,7 +290,7 @@ void Batch::render(const TargetRef& target, const Mat4x4f& matrix) m_mesh->index_data(IndexFormat::UInt32, m_indices.data(), m_indices.size()); m_mesh->vertex_data(format, m_vertices.data(), m_vertices.size()); - RenderPass pass; + DrawCall pass; pass.target = target; pass.mesh = m_mesh; pass.has_viewport = false; @@ -321,7 +315,7 @@ void Batch::render(const TargetRef& target, const Mat4x4f& matrix) render_single_batch(pass, m_batch, matrix); } -void Batch::render_single_batch(RenderPass& pass, const DrawBatch& b, const Mat4x4f& matrix) +void Batch::render_single_batch(DrawCall& pass, const DrawBatch& b, const Mat4x4f& matrix) { // get the material pass.material = b.material; diff --git a/src/graphics/spritefont.cpp b/src/drawing/spritefont.cpp similarity index 98% rename from src/graphics/spritefont.cpp rename to src/drawing/spritefont.cpp index 5a9389a..00e9a43 100644 --- a/src/graphics/spritefont.cpp +++ b/src/drawing/spritefont.cpp @@ -1,7 +1,5 @@ -#include -#include +#include #include -#include using namespace Blah; diff --git a/src/graphics/subtexture.cpp b/src/drawing/subtexture.cpp similarity index 97% rename from src/graphics/subtexture.cpp rename to src/drawing/subtexture.cpp index 86c6402..77a9dc5 100644 --- a/src/graphics/subtexture.cpp +++ b/src/drawing/subtexture.cpp @@ -1,4 +1,4 @@ -#include +#include #include using namespace Blah; diff --git a/src/filesystem.cpp b/src/filesystem.cpp index 0bf23cb..76db0b5 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -1,5 +1,4 @@ #include -#include #include "internal/internal.h" using namespace Blah; @@ -7,9 +6,13 @@ using namespace Blah; FileRef File::open(const FilePath& path, FileMode mode) { BLAH_ASSERT_PLATFORM(); + + FileRef ref; if (App::Internal::platform) - return App::Internal::platform->file_open(path.cstr(), mode); - return FileRef(); + ref = App::Internal::platform->file_open(path.cstr(), mode); + if (ref) + ref->m_mode = mode; + return ref; } bool File::exists(const FilePath& path) @@ -28,6 +31,11 @@ bool File::destroy(const FilePath& path) return false; } +FileMode File::mode() const +{ + return m_mode; +} + bool Directory::create(const FilePath& path) { BLAH_ASSERT_PLATFORM(); diff --git a/src/graphics/material.cpp b/src/graphics.cpp similarity index 55% rename from src/graphics/material.cpp rename to src/graphics.cpp index 2d6610c..803d449 100644 --- a/src/graphics/material.cpp +++ b/src/graphics.cpp @@ -1,9 +1,211 @@ -#include -#include -#include +#include +#include "internal/internal.h" using namespace Blah; +const BlendMode BlendMode::Normal = BlendMode( + BlendOp::Add, + BlendFactor::One, + BlendFactor::OneMinusSrcAlpha, + BlendOp::Add, + BlendFactor::One, + BlendFactor::OneMinusSrcAlpha, + BlendMask::RGBA, + 0xffffffff +); + +const BlendMode BlendMode::Subtract = BlendMode( + BlendOp::ReverseSubtract, + BlendFactor::One, + BlendFactor::One, + BlendOp::Add, + BlendFactor::One, + BlendFactor::One, + BlendMask::RGBA, + 0xffffffff +); + +VertexFormat::VertexFormat(const StackVector& attr, int str) +{ + attributes = attr; + stride = str; + + if (stride <= 0) + { + stride = 0; + + for (auto& it : attributes) + { + switch (it.type) + { + case VertexType::None: break; + case VertexType::Float: stride += 4; break; + case VertexType::Float2: stride += 8; break; + case VertexType::Float3: stride += 12; break; + case VertexType::Float4: stride += 16; break; + case VertexType::Byte4: stride += 4; break; + case VertexType::UByte4: stride += 4; break; + case VertexType::Short2: stride += 4; break; + case VertexType::UShort2: stride += 4; break; + case VertexType::Short4: stride += 8; break; + case VertexType::UShort4: stride += 8; break; + } + } + } +} + +ShaderRef Shader::create(const ShaderData& data) +{ + BLAH_ASSERT_RENDERER(); + BLAH_ASSERT(data.vertex.length() > 0, "Must provide a Vertex Shader"); + BLAH_ASSERT(data.fragment.length() > 0, "Must provide a Fragment Shader"); + BLAH_ASSERT(data.hlsl_attributes.size() > 0 || App::renderer().type != RendererType::D3D11, "D3D11 Shaders must have hlsl_attributes assigned"); + + ShaderRef shader; + + if (App::Internal::renderer) + shader = App::Internal::renderer->create_shader(&data); + + // validate the shader + if (shader) + { + auto& uniforms = shader->uniforms(); + + // make sure its uniforms are valid + for (auto& it : uniforms) + if (it.type == UniformType::None) + { + auto error = String::fmt("Uniform '%s' has an invalid type!\n\tOnly Float/Float2/Float3/Float4/Mat3x2/Mat4x4/Texture are allowed!", it.name.cstr()); + BLAH_ASSERT(false, error.cstr()); + return ShaderRef(); + } + + // make sure uniform names don't overlap + for (int i = 0; i < uniforms.size(); i++) + for (int j = i + 1; j < uniforms.size(); j++) + if (uniforms[i].name == uniforms[j].name) + { + auto error = String::fmt("Shader Uniform names '%s' overlap! All Names must be unique.", uniforms[0].name.cstr()); + BLAH_ASSERT(false, error.cstr()); + return ShaderRef(); + } + } + + return shader; +} + +TextureRef Texture::create(const Image& image) +{ + return create(image.width, image.height, TextureFormat::RGBA, (unsigned char*)image.pixels); +} + +TextureRef Texture::create(int width, int height, TextureFormat format, unsigned char* data) +{ + BLAH_ASSERT_RENDERER(); + BLAH_ASSERT(width > 0 && height > 0, "Texture width and height must be larger than 0"); + BLAH_ASSERT((int)format > (int)TextureFormat::None && (int)format < (int)TextureFormat::Count, "Invalid texture format"); + + if (App::Internal::renderer) + { + auto tex = App::Internal::renderer->create_texture(width, height, format); + + if (tex && data != nullptr) + tex->set_data(data); + + return tex; + } + + return TextureRef(); +} + +TextureRef Texture::create(Stream& stream) +{ + return create(Image(stream)); +} + +TextureRef Texture::create(const FilePath& file) +{ + return create(Image(file)); +} + +void Texture::set_data(const Color* data) +{ + if (format() == TextureFormat::RGBA) + set_data((u8*)data); +} + +void Texture::get_data(Color* data) +{ + if (format() == TextureFormat::RGBA) + get_data((u8*)data); +} + +TargetRef Target::create(int width, int height) +{ + AttachmentFormats formats; + formats.push_back(TextureFormat::RGBA); + return create(width, height, formats); +} + +TargetRef Target::create(int width, int height, const AttachmentFormats& textures) +{ + BLAH_ASSERT_RENDERER(); + BLAH_ASSERT(width > 0 && height > 0, "Target width and height must be larger than 0"); + BLAH_ASSERT(textures.size() > 0, "At least one texture must be provided"); + + int color_count = 0; + int depth_count = 0; + + for (int i = 0; i < textures.size(); i++) + { + BLAH_ASSERT((int)textures[i] > (int)TextureFormat::None && (int)textures[i] < (int)TextureFormat::Count, "Invalid texture format"); + + if (textures[i] == TextureFormat::DepthStencil) + depth_count++; + else + color_count++; + } + + BLAH_ASSERT(depth_count <= 1, "Target can only have 1 Depth/Stencil Texture"); + BLAH_ASSERT(color_count <= Attachments::capacity - 1, "Exceeded maximum Color texture count"); + + if (App::Internal::renderer) + return App::Internal::renderer->create_target(width, height, textures.data(), textures.size()); + + return TargetRef(); +} + +TextureRef& Target::texture(int index) +{ + return textures()[index]; +} + +const TextureRef& Target::texture(int index) const +{ + return textures()[index]; +} + +int Target::width() const +{ + return textures()[0]->width(); +} + +int Target::height() const +{ + return textures()[0]->height(); +} + +MeshRef Mesh::create() +{ + BLAH_ASSERT_RENDERER(); + + if (App::Internal::renderer) + return App::Internal::renderer->create_mesh(); + + return MeshRef(); +} + + namespace { int blah_calc_uniform_size(const UniformInfo& uniform) @@ -42,7 +244,7 @@ Material::Material(const ShaderRef& shader) if (uniform.type == UniformType::Texture2D) { - for (int i = 0; i < uniform.array_length; i ++) + for (int i = 0; i < uniform.array_length; i++) m_textures.push_back(TextureRef()); continue; } @@ -412,7 +614,7 @@ const float* Material::get_value(const char* name, i64* length) const bool Material::has_value(const char* name) const { BLAH_ASSERT(m_shader, "Material Shader is invalid"); - + if (name != nullptr && name[0] != '\0') { for (auto& uniform : m_shader->uniforms()) @@ -437,3 +639,93 @@ const float* Material::data() const { return m_data.begin(); } + + +DrawCall::DrawCall() +{ + blend = BlendMode::Normal; + target = App::backbuffer(); + mesh = MeshRef(); + material = MaterialRef(); + has_viewport = false; + has_scissor = false; + viewport = Rectf(); + scissor = Rectf(); + index_start = 0; + index_count = 0; + instance_count = 0; + depth = Compare::None; + cull = Cull::None; +} + +void DrawCall::perform() +{ + BLAH_ASSERT_RENDERER(); + BLAH_ASSERT(material, "Trying to draw with an invalid Material"); + BLAH_ASSERT(material->shader(), "Trying to draw with an invalid Shader"); + BLAH_ASSERT(mesh, "Trying to draw with an invalid Mesh"); + + if (!App::Internal::renderer) + return; + + // copy call + DrawCall pass = *this; + + // Validate Backbuffer + if (!pass.target) + { + pass.target = App::backbuffer(); + Log::warn("Trying to draw with an invalid Target; falling back to Back Buffer"); + } + + // Validate Index Count + i64 index_count = pass.mesh->index_count(); + if (pass.index_start + pass.index_count > index_count) + { + Log::warn( + "Trying to draw more indices than exist in the index buffer (%i-%i / %i); trimming extra indices", + pass.index_start, + pass.index_start + pass.index_count, + index_count); + + if (pass.index_start > pass.index_count) + return; + + pass.index_count = pass.index_count - pass.index_start; + } + + // Validate Instance Count + i64 instance_count = pass.mesh->instance_count(); + if (pass.instance_count > instance_count) + { + Log::warn( + "Trying to draw more instances than exist in the index buffer (%i / %i); trimming extra instances", + pass.instance_count, + instance_count); + + pass.instance_count = instance_count; + } + + // get the total drawable size + auto draw_size = Vec2f(pass.target->width(), pass.target->height()); + + // Validate Viewport + if (!pass.has_viewport) + { + pass.viewport.x = 0; + pass.viewport.y = 0; + pass.viewport.w = draw_size.x; + pass.viewport.h = draw_size.y; + } + else + { + pass.viewport = pass.viewport.overlap_rect(Rectf(0, 0, draw_size.x, draw_size.y)); + } + + // Validate Scissor + if (pass.has_scissor) + pass.scissor = pass.scissor.overlap_rect(Rectf(0, 0, draw_size.x, draw_size.y)); + + // perform render + App::Internal::renderer->render(pass); +} diff --git a/src/graphics/blend.cpp b/src/graphics/blend.cpp deleted file mode 100644 index 2eeb355..0000000 --- a/src/graphics/blend.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include - -using namespace Blah; - -const BlendMode BlendMode::Normal = BlendMode( - BlendOp::Add, - BlendFactor::One, - BlendFactor::OneMinusSrcAlpha, - BlendOp::Add, - BlendFactor::One, - BlendFactor::OneMinusSrcAlpha, - BlendMask::RGBA, - 0xffffffff -); - -const BlendMode BlendMode::Subtract = BlendMode( - BlendOp::ReverseSubtract, - BlendFactor::One, - BlendFactor::One, - BlendOp::Add, - BlendFactor::One, - BlendFactor::One, - BlendMask::RGBA, - 0xffffffff -); diff --git a/src/graphics/mesh.cpp b/src/graphics/mesh.cpp deleted file mode 100644 index dc31693..0000000 --- a/src/graphics/mesh.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include -#include "../internal/internal.h" - -using namespace Blah; - -MeshRef Mesh::create() -{ - BLAH_ASSERT_RENDERER(); - - if (App::Internal::renderer) - return App::Internal::renderer->create_mesh(); - - return MeshRef(); -} - -VertexFormat::VertexFormat(const StackVector& attr, int stride) -{ - attributes = attr; - - if (stride <= 0) - { - stride = 0; - - for (auto& it : attributes) - { - switch (it.type) - { - case VertexType::None: break; - case VertexType::Float: stride += 4; break; - case VertexType::Float2: stride += 8; break; - case VertexType::Float3: stride += 12; break; - case VertexType::Float4: stride += 16; break; - case VertexType::Byte4: stride += 4; break; - case VertexType::UByte4: stride += 4; break; - case VertexType::Short2: stride += 4; break; - case VertexType::UShort2: stride += 4; break; - case VertexType::Short4: stride += 8; break; - case VertexType::UShort4: stride += 8; break; - } - } - } - - this->stride = stride; -} diff --git a/src/graphics/renderpass.cpp b/src/graphics/renderpass.cpp deleted file mode 100644 index 97233a9..0000000 --- a/src/graphics/renderpass.cpp +++ /dev/null @@ -1,94 +0,0 @@ -#include -#include -#include "../internal/internal.h" - -using namespace Blah; - -RenderPass::RenderPass() -{ - blend = BlendMode::Normal; - target = App::backbuffer(); - mesh = MeshRef(); - material = MaterialRef(); - has_viewport = false; - has_scissor = false; - viewport = Rectf(); - scissor = Rectf(); - index_start = 0; - index_count = 0; - instance_count = 0; - depth = Compare::None; - cull = Cull::None; -} - -void RenderPass::perform() -{ - BLAH_ASSERT_RENDERER(); - BLAH_ASSERT(material, "Trying to draw with an invalid Material"); - BLAH_ASSERT(material->shader(), "Trying to draw with an invalid Shader"); - BLAH_ASSERT(mesh, "Trying to draw with an invalid Mesh"); - - if (!App::Internal::renderer) - return; - - // copy call - RenderPass pass = *this; - - // Validate Backbuffer - if (!pass.target) - { - pass.target = App::backbuffer(); - Log::warn("Trying to draw with an invalid Target; falling back to Back Buffer"); - } - - // Validate Index Count - i64 index_count = pass.mesh->index_count(); - if (pass.index_start + pass.index_count > index_count) - { - Log::warn( - "Trying to draw more indices than exist in the index buffer (%i-%i / %i); trimming extra indices", - pass.index_start, - pass.index_start + pass.index_count, - index_count); - - if (pass.index_start > pass.index_count) - return; - - pass.index_count = pass.index_count - pass.index_start; - } - - // Validate Instance Count - i64 instance_count = pass.mesh->instance_count(); - if (pass.instance_count > instance_count) - { - Log::warn( - "Trying to draw more instances than exist in the index buffer (%i / %i); trimming extra instances", - pass.instance_count, - instance_count); - - pass.instance_count = instance_count; - } - - // get the total drawable size - auto draw_size = Vec2f(pass.target->width(), pass.target->height()); - - // Validate Viewport - if (!pass.has_viewport) - { - pass.viewport.x = 0; - pass.viewport.y = 0; - pass.viewport.w = draw_size.x; - pass.viewport.h = draw_size.y; - } - else - { - pass.viewport = pass.viewport.overlap_rect(Rectf(0, 0, draw_size.x, draw_size.y)); - } - - // Validate Scissor - if (pass.has_scissor) - pass.scissor = pass.scissor.overlap_rect(Rectf(0, 0, draw_size.x, draw_size.y)); - - // perform render - App::Internal::renderer->render(pass); -} diff --git a/src/graphics/shader.cpp b/src/graphics/shader.cpp deleted file mode 100644 index 5ae6a97..0000000 --- a/src/graphics/shader.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include -#include "../internal/internal.h" - -using namespace Blah; - -ShaderRef Shader::create(const ShaderData& data) -{ - BLAH_ASSERT_RENDERER(); - BLAH_ASSERT(data.vertex.length() > 0, "Must provide a Vertex Shader"); - BLAH_ASSERT(data.fragment.length() > 0, "Must provide a Fragment Shader"); - BLAH_ASSERT(data.hlsl_attributes.size() > 0 || App::renderer().type != RendererType::D3D11, "D3D11 Shaders must have hlsl_attributes assigned"); - - ShaderRef shader; - - if (App::Internal::renderer) - shader = App::Internal::renderer->create_shader(&data); - - // validate the shader - if (shader) - { - auto& uniforms = shader->uniforms(); - - // make sure its uniforms are valid - for (auto& it : uniforms) - if (it.type == UniformType::None) - { - auto error = String::fmt("Uniform '%s' has an invalid type!\n\tOnly Float/Float2/Float3/Float4/Mat3x2/Mat4x4/Texture are allowed!", it.name.cstr()); - BLAH_ASSERT(false, error.cstr()); - return ShaderRef(); - } - - // make sure uniform names don't overlap - for (int i = 0; i < uniforms.size(); i ++) - for (int j = i + 1; j < uniforms.size(); j ++) - if (uniforms[i].name == uniforms[j].name) - { - auto error = String::fmt("Shader Uniform names '%s' overlap! All Names must be unique.", uniforms[0].name.cstr()); - BLAH_ASSERT(false, error.cstr()); - return ShaderRef(); - } - } - - return shader; -} diff --git a/src/graphics/target.cpp b/src/graphics/target.cpp deleted file mode 100644 index 5c7845c..0000000 --- a/src/graphics/target.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include "../internal/internal.h" - -using namespace Blah; - -TargetRef Target::create(int width, int height) -{ - AttachmentFormats formats; - formats.push_back(TextureFormat::RGBA); - return create(width, height, formats); -} - -TargetRef Target::create(int width, int height, const AttachmentFormats& textures) -{ - BLAH_ASSERT_RENDERER(); - BLAH_ASSERT(width > 0 && height > 0, "Target width and height must be larger than 0"); - BLAH_ASSERT(textures.size() > 0, "At least one texture must be provided"); - - int color_count = 0; - int depth_count = 0; - - for (int i = 0; i < textures.size(); i++) - { - BLAH_ASSERT((int)textures[i] > (int)TextureFormat::None && (int)textures[i] < (int)TextureFormat::Count, "Invalid texture format"); - - if (textures[i] == TextureFormat::DepthStencil) - depth_count++; - else - color_count++; - } - - BLAH_ASSERT(depth_count <= 1, "Target can only have 1 Depth/Stencil Texture"); - BLAH_ASSERT(color_count <= Attachments::capacity - 1, "Exceeded maximum Color texture count"); - - if (App::Internal::renderer) - return App::Internal::renderer->create_target(width, height, textures.data(), textures.size()); - - return TargetRef(); -} - -TextureRef& Target::texture(int index) -{ - return textures()[index]; -} - -const TextureRef& Target::texture(int index) const -{ - return textures()[index]; -} - -int Target::width() const -{ - return textures()[0]->width(); -} - -int Target::height() const -{ - return textures()[0]->height(); -} diff --git a/src/graphics/texture.cpp b/src/graphics/texture.cpp deleted file mode 100644 index 95a7c80..0000000 --- a/src/graphics/texture.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include -#include -#include "../internal/internal.h" - -using namespace Blah; - -TextureRef Texture::create(const Image& image) -{ - return create(image.width, image.height, TextureFormat::RGBA, (unsigned char*)image.pixels); -} - -TextureRef Texture::create(int width, int height, TextureFormat format, unsigned char* data) -{ - BLAH_ASSERT_RENDERER(); - BLAH_ASSERT(width > 0 && height > 0, "Texture width and height must be larger than 0"); - BLAH_ASSERT((int)format > (int)TextureFormat::None && (int)format < (int)TextureFormat::Count, "Invalid texture format"); - - if (App::Internal::renderer) - { - auto tex = App::Internal::renderer->create_texture(width, height, format); - - if (tex && data != nullptr) - tex->set_data(data); - - return tex; - } - - return TextureRef(); -} - -TextureRef Texture::create(Stream& stream) -{ - return create(Image(stream)); -} - -TextureRef Texture::create(const FilePath& file) -{ - return create(Image(file)); -} - -void Texture::set_data(const Color* data) -{ - if (format() == TextureFormat::RGBA) - set_data((u8*)data); -} - -void Texture::get_data(Color* data) -{ - if (format() == TextureFormat::RGBA) - get_data((u8*)data); -} diff --git a/src/images/aseprite.cpp b/src/images/aseprite.cpp index a3fdd66..ceeabc3 100644 --- a/src/images/aseprite.cpp +++ b/src/images/aseprite.cpp @@ -1,7 +1,5 @@ #include -#include #include -#include #include #define STBI_NO_STDIO diff --git a/src/images/font.cpp b/src/images/font.cpp index 72917ac..e4a0918 100644 --- a/src/images/font.cpp +++ b/src/images/font.cpp @@ -1,7 +1,5 @@ #include -#include #include -#include using namespace Blah; diff --git a/src/images/image.cpp b/src/images/image.cpp index 7934f49..83b5c03 100644 --- a/src/images/image.cpp +++ b/src/images/image.cpp @@ -1,7 +1,4 @@ #include -#include -#include -#include using namespace Blah; diff --git a/src/images/packer.cpp b/src/images/packer.cpp index 284a7c2..e0e83be 100644 --- a/src/images/packer.cpp +++ b/src/images/packer.cpp @@ -1,5 +1,4 @@ #include -#include #include #include diff --git a/src/input.cpp b/src/input.cpp index aca14a2..5fda7cf 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -5,7 +5,6 @@ #include #include "internal/internal.h" #include "internal/platform.h" -#include #include using namespace Blah; diff --git a/src/internal/platform.h b/src/internal/platform.h index 0413575..f765d67 100644 --- a/src/internal/platform.h +++ b/src/internal/platform.h @@ -1,7 +1,7 @@ #pragma once #include -#include #include +#include #include namespace Blah diff --git a/src/internal/platform_sdl2.cpp b/src/internal/platform_sdl2.cpp index 9f65ad6..a6de913 100644 --- a/src/internal/platform_sdl2.cpp +++ b/src/internal/platform_sdl2.cpp @@ -67,8 +67,8 @@ namespace Blah size_t length() override { return SDL_RWsize(handle); } size_t position() override { return SDL_RWtell(handle); } size_t seek(size_t position) override { return SDL_RWseek(handle, position, RW_SEEK_SET); } - size_t read(unsigned char* buffer, size_t length) override { return SDL_RWread(handle, buffer, sizeof(char), length); } - size_t write(const unsigned char* buffer, size_t length) override { return SDL_RWwrite(handle, buffer, sizeof(char), length); } + size_t read(void* buffer, size_t length) override { return SDL_RWread(handle, buffer, sizeof(char), length); } + size_t write(const void* buffer, size_t length) override { return SDL_RWwrite(handle, buffer, sizeof(char), length); } }; struct SDL2_Platform : public Platform diff --git a/src/internal/platform_win32.cpp b/src/internal/platform_win32.cpp index 4089eeb..d5ad918 100644 --- a/src/internal/platform_win32.cpp +++ b/src/internal/platform_win32.cpp @@ -44,8 +44,8 @@ namespace Blah size_t length() override; size_t position() override; size_t seek(size_t position) override; - size_t read(unsigned char* buffer, size_t length) override; - size_t write(const unsigned char* buffer, size_t length) override; + size_t read(void* buffer, size_t length) override; + size_t write(const void* buffer, size_t length) override; }; struct Win32_Platform : public Platform @@ -181,7 +181,7 @@ size_t Win32File::seek(size_t position) return result.QuadPart; } -size_t Win32File::read(unsigned char* buffer, size_t length) +size_t Win32File::read(void* buffer, size_t length) { static const DWORD read_step = 65536; @@ -204,7 +204,7 @@ size_t Win32File::read(unsigned char* buffer, size_t length) return read; } -size_t Win32File::write(const unsigned char* buffer, size_t length) +size_t Win32File::write(const void* buffer, size_t length) { static const DWORD write_step = 65536; diff --git a/src/internal/renderer.h b/src/internal/renderer.h index f71d939..ade558b 100644 --- a/src/internal/renderer.h +++ b/src/internal/renderer.h @@ -1,11 +1,6 @@ #pragma once #include -#include -#include -#include -#include -#include -#include +#include #include namespace Blah @@ -38,7 +33,7 @@ namespace Blah virtual void after_render() = 0; // Performs a draw call - virtual void render(const RenderPass& pass) = 0; + virtual void render(const DrawCall& pass) = 0; // Clears the backbuffer virtual void clear_backbuffer(Color color, float depth, u8 stencil, ClearMask mask) = 0; diff --git a/src/internal/renderer_d3d11.cpp b/src/internal/renderer_d3d11.cpp index 9ac9bca..40fc7bf 100644 --- a/src/internal/renderer_d3d11.cpp +++ b/src/internal/renderer_d3d11.cpp @@ -136,7 +136,7 @@ namespace Blah void update() override; void before_render() override; void after_render() override; - void render(const RenderPass& pass) override; + void render(const DrawCall& pass) override; void clear_backbuffer(Color color, float depth, u8 stencil, ClearMask mask) override; TextureRef create_texture(int width, int height, TextureFormat format) override; TargetRef create_target(int width, int height, const TextureFormat* attachments, int attachment_count) override; @@ -145,9 +145,9 @@ namespace Blah ID3D11InputLayout* get_layout(D3D11_Shader* shader, const VertexFormat& format); ID3D11BlendState* get_blend(const BlendMode& blend); - ID3D11RasterizerState* get_rasterizer(const RenderPass& pass); + ID3D11RasterizerState* get_rasterizer(const DrawCall& pass); ID3D11SamplerState* get_sampler(const TextureSampler& sampler); - ID3D11DepthStencilState* get_depthstencil(const RenderPass& pass); + ID3D11DepthStencilState* get_depthstencil(const DrawCall& pass); }; // Utility Methods @@ -947,7 +947,7 @@ namespace Blah return MeshRef(new D3D11_Mesh()); } - void Renderer_D3D11::render(const RenderPass& pass) + void Renderer_D3D11::render(const DrawCall& pass) { auto ctx = context; auto mesh = (D3D11_Mesh*)pass.mesh.get(); @@ -1531,7 +1531,7 @@ namespace Blah return nullptr; } - ID3D11RasterizerState* Renderer_D3D11::get_rasterizer(const RenderPass& pass) + ID3D11RasterizerState* Renderer_D3D11::get_rasterizer(const DrawCall& pass) { for (auto& it : rasterizer_cache) if (it.cull == pass.cull && it.has_scissor == pass.has_scissor) @@ -1572,7 +1572,7 @@ namespace Blah return nullptr; } - ID3D11DepthStencilState* Renderer_D3D11::get_depthstencil(const RenderPass& pass) + ID3D11DepthStencilState* Renderer_D3D11::get_depthstencil(const DrawCall& pass) { for (auto& it : depthstencil_cache) if (it.depth == pass.depth) diff --git a/src/internal/renderer_opengl.cpp b/src/internal/renderer_opengl.cpp index 8ff1b00..18104bc 100644 --- a/src/internal/renderer_opengl.cpp +++ b/src/internal/renderer_opengl.cpp @@ -418,7 +418,7 @@ namespace Blah void update() override; void before_render() override; void after_render() override; - void render(const RenderPass& pass) override; + void render(const DrawCall& pass) override; void clear_backbuffer(Color color, float depth, u8 stencil, ClearMask mask) override; TextureRef create_texture(int width, int height, TextureFormat format) override; TargetRef create_target(int width, int height, const TextureFormat* attachments, int attachment_count) override; @@ -1272,7 +1272,7 @@ namespace Blah return MeshRef(resource); } - void Renderer_OpenGL::render(const RenderPass& pass) + void Renderer_OpenGL::render(const DrawCall& pass) { // Bind the Target if (pass.target == App::backbuffer()) diff --git a/src/streams/stream.cpp b/src/stream.cpp similarity index 51% rename from src/streams/stream.cpp rename to src/stream.cpp index 49a56cf..c794f31 100644 --- a/src/streams/stream.cpp +++ b/src/stream.cpp @@ -1,9 +1,11 @@ -#include +#include #include #include using namespace Blah; +// Stream Base Class Implementation + size_t Stream::pipe(Stream& stream, size_t length) { const int BUFFER_LENGTH = 4096; @@ -33,7 +35,6 @@ size_t Stream::read(void* buffer, size_t length) return read_data(buffer, length); } -// reads a string. if length < 0, assumes null-terminated String Stream::read_string(int length) { String result; @@ -204,3 +205,190 @@ size_t Stream::write_f64(double value, Endian endian) return write_data(&value, sizeof(double)); } + +// File Stream Implementation + +FileStream::FileStream(const FilePath& path, FileMode mode) + : m_file(File::open(path, mode)) {} + +FileStream::FileStream(const FileRef& file) + : m_file(file) {} + +size_t FileStream::length() const +{ + return (m_file ? m_file->length() : 0); +} + +size_t FileStream::position() const +{ + return (m_file ? m_file->position() : 0); +} + +size_t FileStream::seek(size_t seek_to) +{ + return (m_file ? m_file->seek(seek_to) : 0); +} + +size_t FileStream::read_data(void* ptr, size_t length) +{ + return (m_file ? m_file->read(ptr, length) : 0); +} + +size_t FileStream::write_data(const void* ptr, size_t length) +{ + return (m_file ? m_file->write(ptr, length) : 0); +} + +bool FileStream::is_open() const +{ + return m_file ? true : false; +} + +bool FileStream::is_readable() const +{ + return m_file && m_file->mode() != FileMode::CreateWrite; +} + +bool FileStream::is_writable() const +{ + return m_file && m_file->mode() != FileMode::OpenRead; +} + + +// Memory Stream Implementation + +MemoryStream::MemoryStream(u8* data, size_t length) + : m_data(data), m_const_data(nullptr), m_length(length), m_position(0) {} + +MemoryStream::MemoryStream(const u8* data, size_t length) + : m_data(nullptr), m_const_data(data), m_length(length), m_position(0) {} + +size_t MemoryStream::length() const +{ + return m_length; +} + +size_t MemoryStream::position() const +{ + return m_position; +} + +size_t MemoryStream::seek(size_t seek_to) +{ + return m_position = (seek_to < 0 ? 0 : (seek_to > m_length ? m_length : seek_to)); +} + +size_t MemoryStream::read_data(void* ptr, size_t len) +{ + const u8* src = (m_data ? m_data : m_const_data); + + if (src == nullptr || ptr == nullptr || len <= 0 || m_length <= 0 || m_position >= m_length) + return 0; + + if (len > m_length - m_position) + len = m_length - m_position; + + memcpy(ptr, src + m_position, len); + m_position += len; + return len; +} + +size_t MemoryStream::write_data(const void* ptr, size_t len) +{ + if (m_data == nullptr || ptr == nullptr || len <= 0 || m_length <= 0 || m_position >= m_length) + return 0; + + if (len > m_length - m_position) + len = m_length - m_position; + + memcpy(m_data + m_position, ptr, len); + m_position += len; + return len; +} + +bool MemoryStream::is_open() const { return (m_data || m_const_data) && m_length > 0; } +bool MemoryStream::is_readable() const { return (m_data || m_const_data) && m_length > 0; } +bool MemoryStream::is_writable() const { return m_data != nullptr && m_length > 0; } +u8* MemoryStream::data() { return m_data; } +const u8* MemoryStream::data() const { return (m_data ? m_data : m_const_data); } + + +// Buffer Stream Implementation + +BufferStream::BufferStream(int capacity) +{ + m_buffer.resize(capacity); +} + +size_t BufferStream::length() const +{ + return m_buffer.size(); +} + +size_t BufferStream::position() const +{ + return m_position; +} + +size_t BufferStream::seek(size_t seek_to) +{ + return m_position = (seek_to < 0 ? 0 : (seek_to > m_buffer.size() ? m_buffer.size() : seek_to)); +} + +size_t BufferStream::read_data(void* ptr, size_t len) +{ + if (ptr == nullptr || len <= 0) + return 0; + + if (len > m_buffer.size() - m_position) + len = m_buffer.size() - m_position; + + memcpy(ptr, m_buffer.data() + m_position, (size_t)len); + m_position += len; + return len; +} + +size_t BufferStream::write_data(const void* ptr, size_t len) +{ + if (len < 0) + return 0; + + // resize + if (m_position + len > m_buffer.size()) + resize(m_position + len); + + // copy data + if (ptr != nullptr) + memcpy(m_buffer.data() + m_position, ptr, (size_t)len); + + // increment position + m_position += len; + + // return the amount we wrote + return len; +} + +bool BufferStream::is_open() const { return true; } +bool BufferStream::is_readable() const { return true; } +bool BufferStream::is_writable() const { return true; } + +void BufferStream::resize(size_t length) +{ + m_buffer.resize(length); +} + +void BufferStream::clear() +{ + m_buffer.clear(); + m_position = 0; +} + +u8* BufferStream::data() +{ + return m_buffer.data(); +} + +const u8* BufferStream::data() const +{ + return m_buffer.data(); +} diff --git a/src/streams/bufferstream.cpp b/src/streams/bufferstream.cpp deleted file mode 100644 index 80977f7..0000000 --- a/src/streams/bufferstream.cpp +++ /dev/null @@ -1,82 +0,0 @@ -#include "blah/streams/bufferstream.h" -#include - -using namespace Blah; - -BufferStream::BufferStream(int capacity) -{ - m_buffer.resize(capacity); -} - -size_t BufferStream::length() const -{ - return m_buffer.size(); -} - -size_t BufferStream::position() const -{ - return m_position; -} - -size_t BufferStream::seek(size_t seek_to) -{ - return m_position = (seek_to < 0 ? 0 : (seek_to > m_buffer.size() ? m_buffer.size() : seek_to)); -} - -size_t BufferStream::read_data(void* ptr, size_t len) -{ - if (ptr == nullptr || len <= 0) - return 0; - - if (len > m_buffer.size() - m_position) - len = m_buffer.size() - m_position; - - memcpy(ptr, m_buffer.data() + m_position, (size_t)len); - m_position += len; - return len; -} - -size_t BufferStream::write_data(const void* ptr, size_t len) -{ - if (len < 0) - return 0; - - // resize - if (m_position + len > m_buffer.size()) - resize(m_position + len); - - // copy data - if (ptr != nullptr) - memcpy(m_buffer.data() + m_position, ptr, (size_t)len); - - // increment position - m_position += len; - - // return the amount we wrote - return len; -} - -bool BufferStream::is_open() const { return true; } -bool BufferStream::is_readable() const { return true; } -bool BufferStream::is_writable() const { return true; } - -void BufferStream::resize(size_t length) -{ - m_buffer.resize(length); -} - -void BufferStream::clear() -{ - m_buffer.clear(); - m_position = 0; -} - -u8* BufferStream::data() -{ - return m_buffer.data(); -} - -const u8* BufferStream::data() const -{ - return m_buffer.data(); -} diff --git a/src/streams/filestream.cpp b/src/streams/filestream.cpp deleted file mode 100644 index e97e178..0000000 --- a/src/streams/filestream.cpp +++ /dev/null @@ -1,82 +0,0 @@ -#include -#include -#include "../internal/platform.h" -#include - -using namespace Blah; - -FileStream::FileStream(const FilePath& path, FileMode mode) - : m_mode(mode) - , m_file(File::open(path, mode)) -{ - -} - -FileStream::FileStream(FileStream&& src) noexcept -{ - m_file = src.m_file; - m_mode = src.m_mode; -} - -FileStream& FileStream::operator=(FileStream&& src) noexcept -{ - m_file = src.m_file; - m_mode = src.m_mode; - return *this; -} - -size_t FileStream::length() const -{ - if (m_file) - return m_file->length(); - return 0; -} - -size_t FileStream::position() const -{ - if (m_file) - return m_file->position(); - return 0; -} - -size_t FileStream::seek(size_t seek_to) -{ - if (m_file) - return m_file->seek(seek_to); - return 0; -} - -size_t FileStream::read_data(void* ptr, size_t length) -{ - if (length <= 0) - return 0; - - if (m_file) - return m_file->read((unsigned char*)ptr, length); - return 0; -} - -size_t FileStream::write_data(const void* ptr, size_t length) -{ - if (length <= 0) - return 0; - - if (m_file) - return m_file->write((const unsigned char*)ptr, length); - return 0; -} - -bool FileStream::is_open() const -{ - return m_file.get(); -} - -bool FileStream::is_readable() const -{ - return m_file.get() && (m_mode != FileMode::CreateWrite); -} - -bool FileStream::is_writable() const -{ - return m_file.get() && (m_mode != FileMode::OpenRead); -} \ No newline at end of file diff --git a/src/streams/memorystream.cpp b/src/streams/memorystream.cpp deleted file mode 100644 index d7175a5..0000000 --- a/src/streams/memorystream.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include - -using namespace Blah; - -MemoryStream::MemoryStream(u8* data, size_t length) - : m_data(data), m_const_data(nullptr), m_length(length), m_position(0) {} - -MemoryStream::MemoryStream(const u8* data, size_t length) - : m_data(nullptr), m_const_data(data), m_length(length), m_position(0) {} - -size_t MemoryStream::length() const -{ - return m_length; -} - -size_t MemoryStream::position() const -{ - return m_position; -} - -size_t MemoryStream::seek(size_t seek_to) -{ - return m_position = (seek_to < 0 ? 0 : (seek_to > m_length ? m_length : seek_to)); -} - -size_t MemoryStream::read_data(void* ptr, size_t len) -{ - const u8* src = (m_data ? m_data : m_const_data); - - if (src == nullptr || ptr == nullptr || len <= 0 || m_length <= 0 || m_position >= m_length) - return 0; - - if (len > m_length - m_position) - len = m_length - m_position; - - memcpy(ptr, src + m_position, len); - m_position += len; - return len; -} - -size_t MemoryStream::write_data(const void* ptr, size_t len) -{ - if (m_data == nullptr || ptr == nullptr || len <= 0 || m_length <= 0 || m_position >= m_length) - return 0; - - if (len > m_length - m_position) - len = m_length - m_position; - - memcpy(m_data + m_position, ptr, len); - m_position += len; - return len; -} - -bool MemoryStream::is_open() const -{ - return (m_data || m_const_data) && m_length > 0; -} - -bool MemoryStream::is_readable() const -{ - return (m_data || m_const_data) && m_length > 0; -} - -bool MemoryStream::is_writable() const -{ - return m_data != nullptr && m_length > 0; -} - -u8* MemoryStream::data() -{ - return m_data; -} - -const u8* MemoryStream::data() const -{ - return (m_data ? m_data : m_const_data); -}