diff --git a/Duin/Duin.vcxproj b/Duin/Duin.vcxproj index a4f78a0..f94d50d 100644 --- a/Duin/Duin.vcxproj +++ b/Duin/Duin.vcxproj @@ -161,7 +161,14 @@ IF EXIST ..\bin\Dist-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Dis + + + + + + + @@ -180,6 +187,8 @@ IF EXIST ..\bin\Dist-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Dis + + @@ -193,19 +202,27 @@ IF EXIST ..\bin\Dist-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Dis - - + + + + + + + + + + @@ -214,10 +231,8 @@ IF EXIST ..\bin\Dist-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Dis - - Create diff --git a/Duin/Duin.vcxproj.filters b/Duin/Duin.vcxproj.filters index 3a5a8e1..8722eda 100644 --- a/Duin/Duin.vcxproj.filters +++ b/Duin/Duin.vcxproj.filters @@ -136,21 +136,24 @@ Duin\Events - - Duin\Graphics - Duin\Object Duin\Object\Node - - Duin\Object - + + + + + + + + + @@ -192,19 +195,23 @@ Duin\Events - - Duin\Graphics - Duin\Object Duin\Object\Node - - Duin\Object - + + + + + + + + + + \ No newline at end of file diff --git a/Duin/src/Duin.h b/Duin/src/Duin.h index d2fff36..3bedb55 100644 --- a/Duin/src/Duin.h +++ b/Duin/src/Duin.h @@ -18,14 +18,16 @@ #include "Duin/Core/Structures/RenderStructs.h" #include "Duin/Core/Structures/QuadTree.h" #include "Duin/Core/Structures/Delaunay.h" +#include "Duin/Core/Scene/Object.h" #include "Duin/Core/Scene/SceneManager.h" // ---------- Events ----------- #include "Duin/Events/InputEvent.h" #include "Duin/Events/InputMap.h" -// ---------- Graphics ----------- +// ---------- Assets ----------- #include "Duin/Assets/TextureResource.h" +#include "Duin/Assets/AssetManager.h" // ---------- Entities ----------- #include "Duin/Entity/Entity.h" @@ -38,7 +40,6 @@ // ---------- [DEPRECIATED] ----------- // ---------- Objects ----------- -#include "Duin/Object/Object.h" #include "Duin/Object/Node.h" #include "Duin/Object/Blackboard.h" diff --git a/Duin/src/Duin/Assets/AnimatedSprite2D.cpp b/Duin/src/Duin/Assets/AnimatedSprite2D.cpp new file mode 100644 index 0000000..111966d --- /dev/null +++ b/Duin/src/Duin/Assets/AnimatedSprite2D.cpp @@ -0,0 +1,8 @@ +#include "dnpch.h" + +#include "AnimatedSprite2D.h" + +namespace Duin +{ + +} \ No newline at end of file diff --git a/Duin/src/Duin/Assets/AnimatedSprite2D.h b/Duin/src/Duin/Assets/AnimatedSprite2D.h new file mode 100644 index 0000000..a7144c5 --- /dev/null +++ b/Duin/src/Duin/Assets/AnimatedSprite2D.h @@ -0,0 +1,15 @@ +#pragma once + +#include "Duin/Core/Core.h" +#include "Duin/Core/Scene/Object.h" + +namespace Duin +{ + class DUIN_API AnimatedSprite2D : public Object + { + public: + + private: + + }; +} \ No newline at end of file diff --git a/Duin/src/Duin/Assets/Asset.cpp b/Duin/src/Duin/Assets/Asset.cpp new file mode 100644 index 0000000..c1bb84e --- /dev/null +++ b/Duin/src/Duin/Assets/Asset.cpp @@ -0,0 +1,30 @@ +#include "dnpch.h" + +#include "Asset.h" +#include "Duin/Assets/AssetManager.h" + +namespace Duin +{ + Asset::Asset() + { + } + + Asset::Asset(AssetManager* assetManager) + : assetManager(assetManager) + { + } + + Asset::~Asset() + { + } + + void Asset::SetAssetManager(AssetManager* assetManager) + { + this->assetManager = assetManager; + } + + AssetManager* Asset::GetAssetManager() + { + return assetManager; + } +} \ No newline at end of file diff --git a/Duin/src/Duin/Assets/Asset.h b/Duin/src/Duin/Assets/Asset.h new file mode 100644 index 0000000..c6d8011 --- /dev/null +++ b/Duin/src/Duin/Assets/Asset.h @@ -0,0 +1,28 @@ +#pragma once + +#include "Duin/Core/Core.h" +#include "Duin/Core/UUID.h" + +namespace Duin +{ + class AssetManager; + + /* + * A wrapper for the _Asset class. + * The client will interact with this class, and children. + */ + class DUIN_API Asset + { + public: + Asset(); + Asset(AssetManager* assetManager); + ~Asset(); + + void SetAssetManager(AssetManager* assetManager); + AssetManager* GetAssetManager(); + + private: + UUID uuid; + AssetManager* assetManager = nullptr; + }; +} \ No newline at end of file diff --git a/Duin/src/Duin/Assets/AssetManager.cpp b/Duin/src/Duin/Assets/AssetManager.cpp index d0f9bec..ba7e365 100644 --- a/Duin/src/Duin/Assets/AssetManager.cpp +++ b/Duin/src/Duin/Assets/AssetManager.cpp @@ -12,7 +12,13 @@ namespace Duin { } - void AssetManager::LoadTexture(const char* texturePath) + void AssetManager::AddToAssetRegistry(std::shared_ptr<_Asset> asset) { + assetRegistry[asset->guid] = asset; + } + + void AssetManager::RemoveFromAssetRegistry(GUID guid) + { + assetRegistry.erase(guid); } } \ No newline at end of file diff --git a/Duin/src/Duin/Assets/AssetManager.h b/Duin/src/Duin/Assets/AssetManager.h index 337002f..7fd2e8e 100644 --- a/Duin/src/Duin/Assets/AssetManager.h +++ b/Duin/src/Duin/Assets/AssetManager.h @@ -3,10 +3,13 @@ #include "Duin/Core/Core.h" #include "Duin/Core/UUID.h" #include "Duin/Core/Structures/RenderStructs.h" +#include "Duin/Assets/GUID.h" +#include "Duin/Assets/AssetStructs.h" #include "RLImGuiComponent.h" #include +#include namespace Duin { @@ -16,9 +19,34 @@ namespace Duin AssetManager(); ~AssetManager(); - void LoadTexture(const char* texturePath); + template + std::shared_ptr LoadAsset(const char* path); + + void AddToAssetRegistry(std::shared_ptr<_Asset> asset); + void RemoveFromAssetRegistry(GUID guid); private: - std::unordered_map> textureMap; + std::unordered_map> assetRegistry; }; + + template + inline std::shared_ptr AssetManager::LoadAsset(const char* path) + { + static_assert(std::is_base_of<_Asset, ASSET>::value, "ASSET must be a _Asset derived struct!"); + static_assert(std::is_base_of<_AssetFactory, FACTORY>::value, "FACTORY must be a _AssetFactory derived class!"); + + // Check for existing Asset + GUID guid(path); + auto it = assetRegistry.find(guid); + if (it != assetRegistry.end()) + { + return std::dynamic_pointer_cast(it->second); + } + + // Else load/create Asset + FACTORY* factory = new FACTORY(); + std::shared_ptr<_Asset> asset = factory->CreateAsset(this, path); + AddToAssetRegistry(asset); + return std::dynamic_pointer_cast(asset); + } } \ No newline at end of file diff --git a/Duin/src/Duin/Assets/AssetStructs.cpp b/Duin/src/Duin/Assets/AssetStructs.cpp new file mode 100644 index 0000000..5796bb7 --- /dev/null +++ b/Duin/src/Duin/Assets/AssetStructs.cpp @@ -0,0 +1,25 @@ +#include "dnpch.h" + +#include "AssetStructs.h" +#include "AssetManager.h" + +namespace Duin +{ + std::shared_ptr<_Asset> _TextureAssetFactory::CreateAsset(AssetManager* assetManager, const char* path) + { + // TODO + ::Texture rlTexture = ::LoadTexture(path); + std::shared_ptr<_TextureAsset> textureAsset = std::make_shared<_TextureAsset>(rlTexture); + + return textureAsset; + } + + std::shared_ptr<_Asset> _ImageAssetFactory::CreateAsset(AssetManager* assetManager, const char* path) + { + // TODO + ::Image rlImage = ::LoadImage(path); + std::shared_ptr<_ImageAsset> imageAsset = std::make_shared<_ImageAsset>(rlImage); + + return imageAsset; + } +} \ No newline at end of file diff --git a/Duin/src/Duin/Assets/AssetStructs.h b/Duin/src/Duin/Assets/AssetStructs.h new file mode 100644 index 0000000..20ec110 --- /dev/null +++ b/Duin/src/Duin/Assets/AssetStructs.h @@ -0,0 +1,96 @@ +#pragma once + +#include "Duin/Core/Core.h" +#include "Duin/Assets/GUID.h" +#include "Duin/Core/Maths/DuinMaths.h" + +#include + +namespace Duin +{ + class AssetManager; + + /* + * Base Asset structs + * + * These structs are used by the engine to store the assets loaded from storage. + * + */ + + struct DUIN_API _Asset + { + virtual ~_Asset() = default; + GUID guid; // Base resource GUID + + _Asset() {} + _Asset(const char* path) : guid(path) {} + }; + + class DUIN_API _AssetFactory + { + _AssetFactory() = default; + ~_AssetFactory() = default; + + virtual std::shared_ptr<_Asset> CreateAsset(AssetManager* assetManager, const char* path) = 0; + }; + + + + // Texture, tex data stored in GPU memory (VRAM) + struct DUIN_API _TextureAsset : public _Asset + { + unsigned int id; // OpenGL texture id + int width; // Texture base width + int height; // Texture base height + int mipmaps; // Mipmap levels, 1 by default + int format; // Data format (PixelFormat type) + + _TextureAsset(const char* path) + : _Asset(path), id(0), width(0), height(0), mipmaps(1), format(0) {} // Default constructor + + _TextureAsset(const ::Texture& texture, const char* path) // Conversion constructor from raylib::Texture + : _Asset(path), id(texture.id), width(texture.width), height(texture.height), + mipmaps(texture.mipmaps), format(texture.format) {} + + ::Texture ToRaylibTexture() const // Method to convert back to raylib::Texture + { + return { id, width, height, mipmaps, format }; + } + }; + + class DUIN_API _TextureAssetFactory : public _AssetFactory + { + public: + std::shared_ptr<_Asset> CreateAsset(AssetManager* assetManager, const char* path) override; + }; + + + + // Image, pixel data stored in CPU memory (RAM) + struct DUIN_API _ImageAsset : public _Asset + { + void* data; // Image raw data + int width; // Image base width + int height; // Image base height + int mipmaps; // Mipmap levels, 1 by default + int format; // Data format (PixelFormat type) + + _ImageAsset(const char* path) + : _Asset(path), data(nullptr), width(0), height(0), mipmaps(1), format(0) {} // Default constructor + + _ImageAsset(const ::Image& image, const char* path) // Conversion constructor from raylib::Image + : _Asset(path), data(image.data), width(image.width), height(image.height), + mipmaps(image.mipmaps), format(image.format) {} + + ::Image ToRaylibImage() const // Method to convert back to raylib::Image + { + return { data, width, height, mipmaps, format }; + } + }; + + class DUIN_API _ImageAssetFactory : public _AssetFactory + { + public: + std::shared_ptr<_Asset> CreateAsset(AssetManager* assetManager, const char* path) override; + }; +} \ No newline at end of file diff --git a/Duin/src/Duin/Assets/GUID.cpp b/Duin/src/Duin/Assets/GUID.cpp new file mode 100644 index 0000000..b26e40e --- /dev/null +++ b/Duin/src/Duin/Assets/GUID.cpp @@ -0,0 +1,29 @@ +#include "dnpch.h" + +#include "GUID.h" + +namespace Duin +{ + GUID::GUID() + { + } + + GUID::GUID(const std::string& path) + { + Generate(path); + } + GUID::GUID(const char* path) + { + Generate(std::string(path)); + } + + void GUID::Generate(const std::string& path) + { + guid = std::hash{}(path); + } + + size_t GUID::Get() const + { + return guid; + } +} \ No newline at end of file diff --git a/Duin/src/Duin/Assets/GUID.h b/Duin/src/Duin/Assets/GUID.h new file mode 100644 index 0000000..9661819 --- /dev/null +++ b/Duin/src/Duin/Assets/GUID.h @@ -0,0 +1,50 @@ +#pragma once + +#include "Duin/Core/Core.h" + +#include +#include + +namespace Duin +{ + class DUIN_API GUID + { + public: + GUID(); + GUID(const std::string& path); + GUID(const char* path); + + void Generate(const std::string& path); + + size_t Get() const; + + bool operator==(const GUID& other) const + { + return guid == other.guid; + } + + bool operator!=(const GUID& other) const + { + return guid != other.guid; + } + + bool operator<(const GUID& other) const + { + return guid < other.guid; + } + private: + size_t guid = 0; + }; +} + +namespace std +{ + template <> + struct hash + { + size_t operator()(const Duin::GUID& guid) const + { + return guid.Get(); + } + }; +} \ No newline at end of file diff --git a/Duin/src/Duin/Assets/Image.cpp b/Duin/src/Duin/Assets/Image.cpp new file mode 100644 index 0000000..b87525e --- /dev/null +++ b/Duin/src/Duin/Assets/Image.cpp @@ -0,0 +1,14 @@ +#include "dnpch.h" + +#include "Image.h" + +namespace Duin +{ + Image::Image() + { + } + + Image::~Image() + { + } +} \ No newline at end of file diff --git a/Duin/src/Duin/Assets/Image.h b/Duin/src/Duin/Assets/Image.h new file mode 100644 index 0000000..995aa97 --- /dev/null +++ b/Duin/src/Duin/Assets/Image.h @@ -0,0 +1,20 @@ +#pragma once + +#include "Duin/Core/Core.h" +#include "Duin/Core/Scene/Object.h" +#include "Duin/Assets/AssetStructs.h" + +#include + +namespace Duin +{ + class DUIN_API Image : public Object + { + public: + Image(); + ~Image(); + + private: + std::shared_ptr<_ImageAsset> imageAsset; + }; +} \ No newline at end of file diff --git a/Duin/src/Duin/Assets/Particle2D.cpp b/Duin/src/Duin/Assets/Particle2D.cpp new file mode 100644 index 0000000..a7becaf --- /dev/null +++ b/Duin/src/Duin/Assets/Particle2D.cpp @@ -0,0 +1,8 @@ +#include "dnpch.h" + +#include "Particle2D.h" + +namespace Duin +{ + +} \ No newline at end of file diff --git a/Duin/src/Duin/Assets/Particle2D.h b/Duin/src/Duin/Assets/Particle2D.h index 03c8620..b935971 100644 --- a/Duin/src/Duin/Assets/Particle2D.h +++ b/Duin/src/Duin/Assets/Particle2D.h @@ -1,10 +1,11 @@ #pragma once #include "Duin/Core/Core.h" +#include "Duin/Core/Scene/Object.h" namespace Duin { - class DUIN_API Particle2D + class DUIN_API Particle2D : public Object { public: diff --git a/Duin/src/Duin/Assets/Sprite2D.cpp b/Duin/src/Duin/Assets/Sprite2D.cpp new file mode 100644 index 0000000..d7ceb20 --- /dev/null +++ b/Duin/src/Duin/Assets/Sprite2D.cpp @@ -0,0 +1,29 @@ +#include "dnpch.h" + +#include "Sprite2D.h" + +namespace Duin +{ + Sprite2D::Sprite2D() + { + } + + Sprite2D::Sprite2D(std::shared_ptr texture) + : texture(texture) + { + } + + Sprite2D::~Sprite2D() + { + } + + void Sprite2D::SetTexture(std::shared_ptr texture) + { + this->texture = texture; + } + + std::shared_ptr Sprite2D::GetTexture() + { + return texture; + } +} \ No newline at end of file diff --git a/Duin/src/Duin/Assets/Sprite2D.h b/Duin/src/Duin/Assets/Sprite2D.h new file mode 100644 index 0000000..9541fb9 --- /dev/null +++ b/Duin/src/Duin/Assets/Sprite2D.h @@ -0,0 +1,24 @@ +#pragma once + +#include "Duin/Core/Core.h" +#include "Duin/Core/Scene/Object.h" +#include "Duin/Assets/Texture.h" +#include "Duin/Core/Maths/DuinMaths.h" + +namespace Duin +{ + class DUIN_API Sprite2D : public Object + { + public: + Sprite2D(); + Sprite2D(std::shared_ptr texture); + ~Sprite2D(); + + void SetTexture(std::shared_ptr texture); + std::shared_ptr GetTexture(); + + private: + std::shared_ptr texture; + Transform2D transform; + }; +} \ No newline at end of file diff --git a/Duin/src/Duin/Assets/Texture.cpp b/Duin/src/Duin/Assets/Texture.cpp new file mode 100644 index 0000000..97ac333 --- /dev/null +++ b/Duin/src/Duin/Assets/Texture.cpp @@ -0,0 +1,37 @@ +#include "dnpch.h" + +#include "Texture.h" +#include "Duin/Assets/AssetManager.h" + + +namespace Duin +{ + Texture::Texture() + { + } + + Texture::Texture(AssetManager* assetManager, const char* path) + : Asset(assetManager) + { + _textureAsset = assetManager->LoadAsset<_TextureAsset, _TextureAssetFactory>(path); + } + + Texture::~Texture() + { + } + + Vector2 Texture::GetTextureSize() + { + return Vector2{ (int)_textureAsset->width, (int)_textureAsset->height }; + } + + void Texture::SetCentered(bool centered) + { + this->centered = centered; + } + + void Texture::Draw(Vector2 position, float rotation) + { + // TODO + } +} \ No newline at end of file diff --git a/Duin/src/Duin/Assets/Texture.h b/Duin/src/Duin/Assets/Texture.h new file mode 100644 index 0000000..3821ae9 --- /dev/null +++ b/Duin/src/Duin/Assets/Texture.h @@ -0,0 +1,37 @@ +#pragma once + +#include "Duin/Core/Core.h" +#include "Duin/Assets/AssetStructs.h" +#include "Duin/Assets/Asset.h" +#include "Duin/Assets/GUID.h" +#include "Duin/Core/Maths/DuinMaths.h" + +#include + +namespace Duin +{ + class AssetManager; + + /* + * Wrapper class for the _TextureAsset struct. + * The client will interact with the textures through this class. + */ + class DUIN_API Texture : public Asset + { + public: + Texture(); + Texture(AssetManager* assetManager, const char* path); + ~Texture(); + + Vector2 GetTextureSize(); + + void SetCentered(bool centered); + + void Draw(Vector2 position, float rotation); + + private: + std::shared_ptr<_TextureAsset> _textureAsset; + bool centered = false; + + }; +} \ No newline at end of file diff --git a/Duin/src/Duin/Assets/TextureResource.cpp b/Duin/src/Duin/Assets/TextureResource.cpp deleted file mode 100644 index 0f312b2..0000000 --- a/Duin/src/Duin/Assets/TextureResource.cpp +++ /dev/null @@ -1,113 +0,0 @@ -#include "dnpch.h" - -#include "TextureResource.h" - -namespace Duin -{ - TextureResource::TextureResource() - { - } - - TextureResource::TextureResource(const char* texturePath) - { - - LoadTexture(texturePath); - SetTextureSize(texturePtr->width, texturePtr->height); - } - - TextureResource::TextureResource(const char* texturePath, float width, float height) - { - LoadTexture(texturePath); - SetTextureSize(width, height); - } - - TextureResource::TextureResource(const char* texturePath, Vector2 size) - { - LoadTexture(texturePath); - SetTextureSize(size); - } - - TextureResource::~TextureResource() - { - - } - - TextureResource& TextureResource::LoadTexture(const char* texturePath) - { - ::Texture texture = ::LoadTexture(texturePath); // supposed to call Raylib's LoadTexture() - texturePtr.reset(new ::Texture(texture), [](::Texture* ptr) { - UnloadTexture(*ptr); // Custom deleter that calls Raylib's UnloadTexture - delete ptr; // Delete the pointer after unloading the texture - }); - return *this; - } - - TextureResource& TextureResource::SetTextureSize(float width, float height) - { - textureSize = Vector2(width, height); - return *this; - } - - TextureResource& TextureResource::SetTextureSize(Vector2 size) - { - textureSize = size; - return *this; - } - - TextureResource& TextureResource::Draw(float posX, float posY, float rotation_deg) - { - if (!texturePtr) - { - DN_CORE_WARN("TextureRes texture pointer not set!"); - return *this; - } - - Draw(Vector2{ posX, posY }, rotation_deg); - return *this; - } - - TextureResource& TextureResource::Draw(Vector2 position, float rotation_deg) - { - if (!texturePtr) - { - DN_CORE_WARN("TextureRes texture pointer not set!"); - return *this; - } - - Vector2 origin = Vector2::ZERO; - if (isCentered) - { - position -= textureSize / 2; - origin += textureSize / 2; - } - - ::DrawTexturePro - ( - *(texturePtr), - { 0, 0, (float)texturePtr->width, (float)texturePtr->height }, - { position.x, position.y, textureSize.x, textureSize.y }, - { origin.x, origin.y }, - rotation_deg, - { tintColor.r, tintColor.g, tintColor.b, tintColor.a } - ); - //std::cout << "Drawing texture with size: " << textureSize.x << ", " << textureSize.y << "\n"; - return *this; - } - - TextureResource& TextureResource::SetCentered(bool centered) - { - isCentered = centered; - return *this; - } - - TextureResource& TextureResource::SetTintColor(Color color) - { - tintColor = color; - return *this; - } - - void TextureResource::ClearTexture() - { - texturePtr.reset(); - } -} \ No newline at end of file diff --git a/Duin/src/Duin/Assets/TextureResource.h b/Duin/src/Duin/Assets/TextureResource.h deleted file mode 100644 index 1798901..0000000 --- a/Duin/src/Duin/Assets/TextureResource.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once -#include "dnpch.h" - -#include "Duin/Core/Core.h" -#include "Duin/Core/Debug/Log.h" -#include "Duin/Core/Maths/DuinMaths.h" -#include "Duin/Core/Structures/RenderStructs.h" - -#include - -namespace Duin -{ - class DUIN_API TextureResource - { - public: - TextureResource(); - TextureResource(const char* texturePath); - TextureResource(const char* texturePath, float width, float height); - TextureResource(const char* texturePath, Vector2 size); - ~TextureResource(); - - - TextureResource& LoadTexture(const char* texturePath); - TextureResource& SetTextureSize(float width, float height); - TextureResource& SetTextureSize(Vector2 size); - - TextureResource& Draw(float posX, float posY, float rotation_deg); - TextureResource& Draw(Vector2 position, float rotation_deg); - - TextureResource& SetCentered(bool centered); - TextureResource& SetTintColor(Color color); - - void ClearTexture(); - - private: - std::shared_ptr<::Texture> texturePtr; - Vector2 textureSize; - bool isCentered = false; - Color tintColor = WHITE; - }; -} \ No newline at end of file diff --git a/Duin/src/Duin/Core/Maths/src/Mat2x2.h b/Duin/src/Duin/Core/Maths/src/Mat2x2.h index bb9fc99..d3b5104 100644 --- a/Duin/src/Duin/Core/Maths/src/Mat2x2.h +++ b/Duin/src/Duin/Core/Maths/src/Mat2x2.h @@ -5,11 +5,15 @@ namespace Duin { - struct DUIN_MATHS_API Matrix2x2 + struct DUIN_MATHS_API Mat2x2 { + #define Mat2x2_ZERO Mat2x2(0, 0, 0, 0) + #define Mat2x2_IDENTITY Mat2x2(1, 0, 0, 1) + #define Mat2x2_ONE Mat2x2(1, 1, 1, 1) + float m[2][2]; - Matrix2x2() + Mat2x2() { for (int i = 0; i < 2; ++i) { @@ -20,7 +24,7 @@ namespace Duin } } - Matrix2x2(float a, float b, float c, float d) + Mat2x2(float a, float b, float c, float d) { m[0][0] = a; m[0][1] = b; @@ -28,21 +32,21 @@ namespace Duin m[1][1] = d; } - static Matrix2x2 Rotate(float angle) + static Mat2x2 Rotate(float angle) { float c = cos(angle); float s = sin(angle); - return Matrix2x2(c, -s, s, c); + return Mat2x2(c, -s, s, c); } - static Matrix2x2 Scale(float sx, float sy) + static Mat2x2 Scale(float sx, float sy) { - return Matrix2x2(sx, 0, 0, sy); + return Mat2x2(sx, 0, 0, sy); } - Matrix2x2 operator+(const Matrix2x2& other) const + Mat2x2 operator+(const Mat2x2& other) const { - return Matrix2x2( + return Mat2x2( m[0][0] + other.m[0][0], m[0][1] + other.m[0][1], m[1][0] + other.m[1][0], @@ -50,9 +54,9 @@ namespace Duin ); } - Matrix2x2 operator-(const Matrix2x2& other) const + Mat2x2 operator-(const Mat2x2& other) const { - return Matrix2x2( + return Mat2x2( m[0][0] - other.m[0][0], m[0][1] - other.m[0][1], m[1][0] - other.m[1][0], @@ -60,13 +64,13 @@ namespace Duin ); } - Matrix2x2 operator+=(const Matrix2x2& other) + Mat2x2 operator+=(const Mat2x2& other) { m[0][0] += other.m[0][0]; m[0][1] += other.m[0][1]; m[1][0] += other.m[1][0]; m[1][1] += other.m[1][1]; - return Matrix2x2( + return Mat2x2( m[0][0], m[0][1], m[1][0], @@ -74,13 +78,13 @@ namespace Duin ); } - Matrix2x2 operator-=(const Matrix2x2& other) + Mat2x2 operator-=(const Mat2x2& other) { m[0][0] -= other.m[0][0]; m[0][1] -= other.m[0][1]; m[1][0] -= other.m[1][0]; m[1][1] -= other.m[1][1]; - return Matrix2x2( + return Mat2x2( m[0][0], m[0][1], m[1][0], @@ -88,9 +92,9 @@ namespace Duin ); } - Matrix2x2 operator*(const Matrix2x2& other) const + Mat2x2 operator*(const Mat2x2& other) const { - return Matrix2x2( + return Mat2x2( m[0][0] * other.m[0][0] + m[0][1] * other.m[1][0], m[0][0] * other.m[0][1] + m[0][1] * other.m[1][1], m[1][0] * other.m[0][0] + m[1][1] * other.m[1][0], diff --git a/Duin/src/Duin/Core/Maths/src/Transform2D.h b/Duin/src/Duin/Core/Maths/src/Transform2D.h index d595f86..4e1d17c 100644 --- a/Duin/src/Duin/Core/Maths/src/Transform2D.h +++ b/Duin/src/Duin/Core/Maths/src/Transform2D.h @@ -11,19 +11,29 @@ namespace Duin struct DUIN_MATHS_API Transform2D { Vector2 origin; - Matrix2x2 transform; + Mat2x2 transform; Transform2D() : origin(0, 0), transform() {} - Transform2D(const Vector2& origin, const Matrix2x2& transform) + Transform2D(const Vector2& origin, const Mat2x2& transform) : origin(origin), transform(transform) {} static Transform2D Identity() { - return Transform2D(Vector2(0, 0), Matrix2x2()); + return Transform2D(Vector2(0, 0), Mat2x2()); + } + + Vector2 GetScale() const + { + return Vector2::ZERO; + } + + float GetRotation() const + { + return 0.0f; } Transform2D Translated(const Vector2& offset) const @@ -33,10 +43,10 @@ namespace Duin return result; } - Transform2D Rotated(float angle) const + Transform2D Rotated(float angle_rads) const { Transform2D result = *this; - Matrix2x2 rotation = Matrix2x2::Rotate(angle); + Mat2x2 rotation = Mat2x2::Rotate(angle_rads); result.transform = result.transform * rotation; return result; } @@ -44,7 +54,7 @@ namespace Duin Transform2D Scaled(float sx, float sy) const { Transform2D result = *this; - Matrix2x2 scale = Matrix2x2::Scale(sx, sy); + Mat2x2 scale = Mat2x2::Scale(sx, sy); result.transform = result.transform * scale; return result; } diff --git a/Duin/src/Duin/Object/Object.cpp b/Duin/src/Duin/Core/Scene/Object.cpp similarity index 92% rename from Duin/src/Duin/Object/Object.cpp rename to Duin/src/Duin/Core/Scene/Object.cpp index 5113db1..02b987e 100644 --- a/Duin/src/Duin/Object/Object.cpp +++ b/Duin/src/Duin/Core/Scene/Object.cpp @@ -1,18 +1,18 @@ -#include "dnpch.h" - -#include "Object.h" - -namespace Duin -{ - Object::Object() - { - } - - Object::~Object() - { - } - bool Object::operator==(Object& other) - { - return this->GetUUID() == other.GetUUID(); - } +#include "dnpch.h" + +#include "Object.h" + +namespace Duin +{ + Object::Object() + { + } + + Object::~Object() + { + } + bool Object::operator==(Object& other) + { + return this->GetUUID() == other.GetUUID(); + } } \ No newline at end of file diff --git a/Duin/src/Duin/Object/Object.h b/Duin/src/Duin/Core/Scene/Object.h similarity index 92% rename from Duin/src/Duin/Object/Object.h rename to Duin/src/Duin/Core/Scene/Object.h index 5220f41..5f9d2b7 100644 --- a/Duin/src/Duin/Object/Object.h +++ b/Duin/src/Duin/Core/Scene/Object.h @@ -1,29 +1,29 @@ -#pragma once - -#include "Duin/Core/Core.h" -#include "Duin/Core/UUID.h" -#include "Duin/Core/Signal.h" -#include "Duin/Events/InputEvent.h" - -#include -#include - -namespace Duin -{ - class DUIN_API Object - { - public: - Object(); - ~Object(); - - UUID GetUUID() { return objectUUID; } - - bool operator==(Object& other); - - protected: - UUID objectUUID; - - private: - - }; +#pragma once + +#include "Duin/Core/Core.h" +#include "Duin/Core/UUID.h" +#include "Duin/Core/Signal.h" +#include "Duin/Events/InputEvent.h" + +#include +#include + +namespace Duin +{ + class DUIN_API Object + { + public: + Object(); + ~Object(); + + UUID GetUUID() { return objectUUID; } + + bool operator==(Object& other); + + protected: + UUID objectUUID; + + private: + + }; } \ No newline at end of file diff --git a/Duin/src/Duin/Core/Scene/SharedRef.cpp b/Duin/src/Duin/Core/Scene/SharedRef.cpp new file mode 100644 index 0000000..bb0fe90 --- /dev/null +++ b/Duin/src/Duin/Core/Scene/SharedRef.cpp @@ -0,0 +1,8 @@ +#include "dnpch.h" + +#include "SharedRef.h" + +namespace Duin +{ + +} \ No newline at end of file diff --git a/Duin/src/Duin/Core/Scene/SharedRef.h b/Duin/src/Duin/Core/Scene/SharedRef.h new file mode 100644 index 0000000..3f57cb9 --- /dev/null +++ b/Duin/src/Duin/Core/Scene/SharedRef.h @@ -0,0 +1,158 @@ +#pragma once + +#include "Duin/Core/Core.h" +#include "Duin/Core/Scene/Object.h" + +#include +#include + +namespace Duin +{ + template + class DUIN_API SharedRef; + + template + class DUIN_API SilentRef : public Object + { + public: + SilentRef() + : ptr(nullptr), refCount(new uint32_t(0)), sharedCount(new uint32_t(0)) + {} + + SilentRef(T* rawPtr) + : ptr(rawPtr, [](T* p) { delete p; }), refCount(new uint32_t(1)), sharedCount(new uint32_t(0)) + {} + + uint32_t GetRefCount() const + { + return ptr.use_count(); + } + + uint32_t GetSharedCount() const + { + return *this->sharedCount; + } + + T* Get() const + { + return ptr.get(); + } + + SilentRef(const SilentRef& obj) + : ptr(obj.ptr), refCount(obj.refCount), sharedCount(obj.sharedCount) + { + if (ptr) + { + (*refCount)++; + } + } + + SilentRef& operator=(const SilentRef& obj) + { + if (this != &obj) + { + ptr = obj.ptr; + refCount = obj.refCount; + sharedCount = obj.sharedCount; + if (ptr) + { + (*refCount)++; + } + } + return *this; + } + + SilentRef(SilentRef&& dyingObj) noexcept + : ptr(std::move(dyingObj.ptr)), refCount(dyingObj.refCount), sharedCount(dyingObj.sharedCount) + { + dyingObj.refCount = nullptr; + dyingObj.sharedCount = nullptr; + } + + SilentRef& operator=(SilentRef&& dyingObj) noexcept + { + if (this != &dyingObj) + { + ptr = std::move(dyingObj.ptr); + refCount = dyingObj.refCount; + sharedCount = dyingObj.sharedCount; + dyingObj.refCount = nullptr; + dyingObj.sharedCount = nullptr; + } + return *this; + } + + T* operator->() const + { + return ptr.get(); + } + + T& operator*() const + { + return *ptr; + } + + SharedRef ToSharedRef(); + + private: + std::shared_ptr ptr; + std::shared_ptr refCount; + std::shared_ptr sharedCount; + + friend class SharedRef; + }; + + template + class DUIN_API SharedRef : public SilentRef + { + public: + SharedRef() : SilentRef() {} + + SharedRef(const SilentRef& obj) + : SilentRef(obj) + { + if (this->ptr) + { + (*this->sharedCount)++; + } + } + + SharedRef(const SharedRef& obj) + : SilentRef(obj) + { + if (this->ptr) + { + (*this->sharedCount)++; + } + } + + SharedRef& operator=(const SharedRef& obj) + { + if (this != &obj) + { + SilentRef::operator=(obj); + if (this->ptr) + { + (*this->sharedCount)++; + } + } + return *this; + } + + SharedRef(SharedRef&& dyingObj) noexcept + : SilentRef(std::move(dyingObj)) + {} + + SharedRef& operator=(SharedRef&& dyingObj) noexcept + { + SilentRef::operator=(std::move(dyingObj)); + return *this; + } + }; + + template + SharedRef SilentRef::ToSharedRef() + { + return SharedRef(*this); + } +} diff --git a/Duin/src/Duin/Core/Structures/RenderStructs.h b/Duin/src/Duin/Core/Structures/RenderStructs.h index 82795ea..b2daa97 100644 --- a/Duin/src/Duin/Core/Structures/RenderStructs.h +++ b/Duin/src/Duin/Core/Structures/RenderStructs.h @@ -149,57 +149,20 @@ namespace Duin }; - // Image, pixel data stored in CPU memory (RAM) - struct DUIN_API Image - { - void* data; // Image raw data - int width; // Image base width - int height; // Image base height - int mipmaps; // Mipmap levels, 1 by default - int format; // Data format (PixelFormat type) - UUID _uuid; // Base resource UUID - - Image() : data(nullptr), width(0), height(0), mipmaps(1), format(0) {} // Default constructor - - Image(const ::Image& image) // Conversion constructor from raylib::Image - : data(image.data), width(image.width), height(image.height), - mipmaps(image.mipmaps), format(image.format) {} - ::Image ToRaylibImage() const // Method to convert back to raylib::Image - { return { data, width, height, mipmaps, format }; } - }; - - - // Texture, tex data stored in GPU memory (VRAM) - struct DUIN_API Texture { - unsigned int id; // OpenGL texture id - int width; // Texture base width - int height; // Texture base height - int mipmaps; // Mipmap levels, 1 by default - int format; // Data format (PixelFormat type) - UUID _uuid; // Base resource UUID - - Texture() : id(0), width(0), height(0), mipmaps(1), format(0) {} // Default constructor - - Texture(const ::Texture& texture) // Conversion constructor from raylib::Texture - : id(texture.id), width(texture.width), height(texture.height), - mipmaps(texture.mipmaps), format(texture.format) {} - ::Texture ToRaylibTexture() const // Method to convert back to raylib::Texture - { return { id, width, height, mipmaps, format }; } - }; - // RenderTexture, fbo for texture rendering struct DUIN_API RenderTexture { unsigned int id; // OpenGL framebuffer object id - Texture texture; // Color buffer attachment texture - Texture depth; // Depth buffer attachment texture + _TextureAsset texture; // Color buffer attachment texture + _TextureAsset depth; // Depth buffer attachment texture UUID _uuid; // Base resource UUID RenderTexture() : id(0) {} // Default constructor RenderTexture(const ::RenderTexture& rt) // Conversion constructor from raylib::RenderTexture - : id(rt.id), texture(Texture(rt.texture)), depth(Texture(rt.depth)) {} - ::RenderTexture ToRaylibRenderTexture() const { // Method to convert back to raylib::RenderTexture + : id(rt.id), texture(_TextureAsset(rt.texture)), depth(_TextureAsset(rt.depth)) {} + ::RenderTexture ToRaylibRenderTexture() const + { // Method to convert back to raylib::RenderTexture return { id, texture.ToRaylibTexture(), depth.ToRaylibTexture() }; } }; @@ -212,19 +175,22 @@ namespace Duin Shader() : id(0), locs(nullptr) {} // Default constructor - ~Shader() { // Destructor to clean up allocated memory + ~Shader() + { // Destructor to clean up allocated memory delete[] locs; } Shader(const ::Shader& shader) // Conversion constructor from raylib::Shader : id(shader.id) { locs = new int[32]; - for (int i = 0; i < 32; ++i) { + for (int i = 0; i < 32; ++i) + { locs[i] = shader.locs[i]; } } - ::Shader ToRaylibShader() const { // Method to convert back to raylib::Shader + ::Shader ToRaylibShader() const + { // Method to convert back to raylib::Shader ::Shader shader; shader.id = id; shader.locs = locs; @@ -234,44 +200,52 @@ namespace Duin // MaterialMap - struct DUIN_API MaterialMap { - Texture texture; // Material map texture + struct DUIN_API MaterialMap + { + _TextureAsset texture; // Material map texture Color color; // Material map color float value; // Material map value MaterialMap() : value(0.0f) {} // Default constructor MaterialMap(const ::MaterialMap& map) // Conversion constructor from raylib::MaterialMap - : texture(Texture(map.texture)), color(map.color), value(map.value) {} - ::MaterialMap ToRaylibMaterialMap() const { // Method to convert back to raylib::MaterialMap + : texture(_TextureAsset(map.texture)), color(map.color), value(map.value) {} + ::MaterialMap ToRaylibMaterialMap() const + { // Method to convert back to raylib::MaterialMap return { texture.ToRaylibTexture(), color.ToRaylibColor(), value }; } }; // Material, includes shader and maps - struct DUIN_API Material { + struct DUIN_API Material + { Shader shader; // Material shader MaterialMap* maps; // Material maps array (MAX_MATERIAL_MAPS) float params[4]; // Material generic parameters (if required) - Material() : maps(nullptr) { // Default constructor + Material() : maps(nullptr) + { // Default constructor std::fill(std::begin(params), std::end(params), 0.0f); } Material(const ::Material& material) // Conversion constructor from raylib::Material - : shader(material.shader), maps(new MaterialMap[32]) { - for (int i = 0; i < 32; ++i) { + : shader(material.shader), maps(new MaterialMap[32]) + { + for (int i = 0; i < 32; ++i) + { maps[i] = MaterialMap(material.maps[i]); } std::copy(std::begin(material.params), std::end(material.params), std::begin(params)); } - ~Material() { // Destructor to clean up allocated memory + ~Material() + { // Destructor to clean up allocated memory delete[] maps; } - ::Material ToRaylibMaterial() const { // Method to convert back to raylib::Material + ::Material ToRaylibMaterial() const + { // Method to convert back to raylib::Material ::Material material; material.shader = shader.ToRaylibShader(); material.maps = (::MaterialMap*)maps; @@ -282,7 +256,8 @@ namespace Duin // NPatchInfo, n-patch layout info - struct DUIN_API NPatchInfo { + struct DUIN_API NPatchInfo + { Rectangle source; // Texture source rectangle int left; // Left border offset int top; // Top border offset @@ -292,15 +267,18 @@ namespace Duin NPatchInfo() : left(0), top(0), right(0), bottom(0), layout(0) {} // Default constructor - //NPatchInfo(const ::NPatchInfo& npi) // Conversion constructor from raylib::NPatchInfo - // : source(npi.source), left(npi.left), top(npi.top), - // right(npi.right), bottom(npi.bottom), layout(npi.layout) {} - //::NPatchInfo ToRaylibNPatchInfo() const { // Method to convert back to raylib::NPatchInfo - // return { source, left, top, right, bottom, layout }; + NPatchInfo(const ::NPatchInfo& npi) // Conversion constructor from raylib::NPatchInfo + : source({ npi.source.x, npi.source.y, npi.source.width, npi.source.height }), left(npi.left), top(npi.top), + right(npi.right), bottom(npi.bottom), layout(npi.layout) {} + ::NPatchInfo ToRaylibNPatchInfo() const + { // Method to convert back to raylib::NPatchInfo + return { { source.origin.x, source.origin.y, source.width, source.height }, left, top, right, bottom, layout }; + } }; // GlyphInfo, font characters glyphs info - struct DUIN_API GlyphInfo { + struct DUIN_API GlyphInfo + { int value; // Character value (Unicode) int offsetX; // Character offset X when drawing int offsetY; // Character offset Y when drawing @@ -312,13 +290,15 @@ namespace Duin GlyphInfo(const ::GlyphInfo& gi) // Conversion constructor from raylib::GlyphInfo : value(gi.value), offsetX(gi.offsetX), offsetY(gi.offsetY), advanceX(gi.advanceX), image(Image(gi.image)) {} - ::GlyphInfo ToRaylibGlyphInfo() const { // Method to convert back to raylib::GlyphInfo + ::GlyphInfo ToRaylibGlyphInfo() const + { // Method to convert back to raylib::GlyphInfo return { value, offsetX, offsetY, advanceX, image.ToRaylibImage() }; } }; // Font, font texture and GlyphInfo array data - struct DUIN_API Font { + struct DUIN_API Font + { int baseSize; // Base size (default chars height) int glyphCount; // Number of glyph characters int glyphPadding; // Padding around the glyph characters @@ -328,7 +308,8 @@ namespace Duin }; // Camera, defines position/orientation in 3d space - struct DUIN_API Camera3D { + struct DUIN_API Camera3D + { Vector3 position; // Camera position Vector3 target; // Camera target it looks-at Vector3 up; // Camera up vector (rotation over its axis) @@ -339,7 +320,8 @@ namespace Duin typedef Camera3D Camera; // Camera type fallback, defaults to Camera3D // Camera2D, defines position/orientation in 2d space - struct DUIN_API Camera2D { + struct DUIN_API Camera2D + { Vector2 offset; // Camera offset (displacement from target) Vector2 target; // Camera target (rotation and zoom origin) float rotation; // Camera rotation in degrees @@ -347,7 +329,8 @@ namespace Duin }; // Mesh, vertex data and vao/vbo - struct DUIN_API Mesh { + struct DUIN_API Mesh + { int vertexCount; // Number of vertices stored in arrays int triangleCount; // Number of triangles stored (indexed or not) @@ -372,20 +355,23 @@ namespace Duin }; // Transform, vertex transformation data - struct DUIN_API Transform { + struct DUIN_API Transform + { Vector3 translation; // Translation Quaternion rotation; // Rotation Vector3 scale; // Scale }; // Bone, skeletal animation bone - struct DUIN_API BoneInfo { + struct DUIN_API BoneInfo + { char name[32]; // Bone name int parent; // Bone parent }; // Model, meshes, materials and animation data - struct DUIN_API Model { + struct DUIN_API Model + { Matrix transform; // Local transform matrix int meshCount; // Number of meshes @@ -401,7 +387,8 @@ namespace Duin }; // ModelAnimation - struct DUIN_API ModelAnimation { + struct DUIN_API ModelAnimation + { int boneCount; // Number of bones int frameCount; // Number of animation frames BoneInfo* bones; // Bones information (skeleton) diff --git a/Duin/src/Duin/Object/Node.h b/Duin/src/Duin/Object/Node.h index 6eb8123..5203623 100644 --- a/Duin/src/Duin/Object/Node.h +++ b/Duin/src/Duin/Object/Node.h @@ -1,7 +1,7 @@ #pragma once #include "Duin/Core/Core.h" -#include "Duin/Object/Object.h" +#include "Duin/Core/Scene/Object.h" #include "Duin/Core/Scene/SceneManager.h" #include "Duin/Events/InputEvent.h" #include "Duin/Events/InputMap.h" diff --git a/ExampleProjects/Astroids/Astroids.vcxproj b/ExampleProjects/Astroids/Astroids.vcxproj index 8508af0..f1a4d85 100644 --- a/ExampleProjects/Astroids/Astroids.vcxproj +++ b/ExampleProjects/Astroids/Astroids.vcxproj @@ -139,11 +139,13 @@ + + diff --git a/ExampleProjects/Astroids/Astroids.vcxproj.filters b/ExampleProjects/Astroids/Astroids.vcxproj.filters index 0ed17f8..d7aebf2 100644 --- a/ExampleProjects/Astroids/Astroids.vcxproj.filters +++ b/ExampleProjects/Astroids/Astroids.vcxproj.filters @@ -9,9 +9,11 @@ + + \ No newline at end of file diff --git a/ExampleProjects/Astroids/src/AstroidCluster.cpp b/ExampleProjects/Astroids/src/AstroidCluster.cpp new file mode 100644 index 0000000..103f799 --- /dev/null +++ b/ExampleProjects/Astroids/src/AstroidCluster.cpp @@ -0,0 +1,10 @@ +#include "AstroidCluster.h" + +AstroidCluster::AstroidCluster(Duin::Registry* registry) + : registry(registry) +{ +} + +AstroidCluster::~AstroidCluster() +{ +} diff --git a/ExampleProjects/Astroids/src/AstroidCluster.h b/ExampleProjects/Astroids/src/AstroidCluster.h new file mode 100644 index 0000000..84bec1f --- /dev/null +++ b/ExampleProjects/Astroids/src/AstroidCluster.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +#include "Components/Components.h" + +class AstroidCluster : public Duin::Node +{ +public: + AstroidCluster(Duin::Registry* registry); + ~AstroidCluster(); + +private: + Duin::Registry* registry; +}; \ No newline at end of file diff --git a/ExampleProjects/Astroids/src/Astroids.cpp b/ExampleProjects/Astroids/src/Astroids.cpp index cabf95e..25ccc7e 100644 --- a/ExampleProjects/Astroids/src/Astroids.cpp +++ b/ExampleProjects/Astroids/src/Astroids.cpp @@ -6,6 +6,7 @@ #include "Components/Components.h" #include "Components/Handlers.h" #include "Player.h" +#include "AstroidCluster.h" class Astroids : public Duin::Application { @@ -26,7 +27,9 @@ class Astroids : public Duin::Application Handler_PlayerMovement* handlerPlayerMovement; Handler_UpdateTransform* handlerUpdateTransform; Handler_RenderableEntity* handlerRenderableEntity; + std::shared_ptr player; + std::shared_ptr astroidCluster; }; Duin::Application* Duin::CreateApplication() { return new Astroids(); } @@ -53,6 +56,7 @@ void Astroids::Exit() void Astroids::Ready() { player = GetSceneManager().CreateNode(registry); + astroidCluster = GetSceneManager().CreateNode(registry); } void Astroids::HandleInputs(Duin::InputEvent e)