Skip to content

Commit

Permalink
reworking textures, RenderStructs, and adding an AssetManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Y0L042 committed Jun 10, 2024
1 parent 7b9ec68 commit f388cb4
Show file tree
Hide file tree
Showing 21 changed files with 298 additions and 90 deletions.
7 changes: 5 additions & 2 deletions Duin/Duin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ IF EXIST ..\bin\Dist-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Dis
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="src\Duin.h" />
<ClInclude Include="src\Duin\Assets\AssetManager.h" />
<ClInclude Include="src\Duin\Core\Application.h" />
<ClInclude Include="src\Duin\Core\Core.h" />
<ClInclude Include="src\Duin\Core\Debug\DebugDraw.h" />
Expand Down Expand Up @@ -191,13 +192,15 @@ IF EXIST ..\bin\Dist-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Dis
<ClInclude Include="src\Duin\EntryPoint.h" />
<ClInclude Include="src\Duin\Events\InputEvent.h" />
<ClInclude Include="src\Duin\Events\InputMap.h" />
<ClInclude Include="src\Duin\Graphics\TextureResource.h" />
<ClInclude Include="src\Duin\Assets\Particle2D.h" />
<ClInclude Include="src\Duin\Assets\TextureResource.h" />
<ClInclude Include="src\Duin\Object\Blackboard.h" />
<ClInclude Include="src\Duin\Object\Node.h" />
<ClInclude Include="src\Duin\Object\Object.h" />
<ClInclude Include="src\dnpch.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Duin\Assets\AssetManager.cpp" />
<ClCompile Include="src\Duin\Core\Application.cpp" />
<ClCompile Include="src\Duin\Core\Debug\DebugDraw.cpp" />
<ClCompile Include="src\Duin\Core\Debug\Log.cpp" />
Expand All @@ -211,7 +214,7 @@ IF EXIST ..\bin\Dist-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Dis
<ClCompile Include="src\Duin\Entity\Entity.cpp" />
<ClCompile Include="src\Duin\Entity\Registry.cpp" />
<ClCompile Include="src\Duin\Events\InputEvent.cpp" />
<ClCompile Include="src\Duin\Graphics\TextureResource.cpp" />
<ClCompile Include="src\Duin\Assets\TextureResource.cpp" />
<ClCompile Include="src\Duin\Object\Blackboard.cpp" />
<ClCompile Include="src\Duin\Object\Node.cpp" />
<ClCompile Include="src\Duin\Object\Object.cpp" />
Expand Down
7 changes: 5 additions & 2 deletions Duin/Duin.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
<ClInclude Include="src\Duin\Events\InputMap.h">
<Filter>Duin\Events</Filter>
</ClInclude>
<ClInclude Include="src\Duin\Graphics\TextureResource.h">
<ClInclude Include="src\Duin\Assets\TextureResource.h">
<Filter>Duin\Graphics</Filter>
</ClInclude>
<ClInclude Include="src\Duin\Object\Blackboard.h">
Expand All @@ -149,6 +149,8 @@
<Filter>Duin\Object</Filter>
</ClInclude>
<ClInclude Include="src\dnpch.h" />
<ClInclude Include="src\Duin\Assets\Particle2D.h" />
<ClInclude Include="src\Duin\Assets\AssetManager.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Duin\Core\Application.cpp">
Expand Down Expand Up @@ -190,7 +192,7 @@
<ClCompile Include="src\Duin\Events\InputEvent.cpp">
<Filter>Duin\Events</Filter>
</ClCompile>
<ClCompile Include="src\Duin\Graphics\TextureResource.cpp">
<ClCompile Include="src\Duin\Assets\TextureResource.cpp">
<Filter>Duin\Graphics</Filter>
</ClCompile>
<ClCompile Include="src\Duin\Object\Blackboard.cpp">
Expand All @@ -203,5 +205,6 @@
<Filter>Duin\Object</Filter>
</ClCompile>
<ClCompile Include="src\dnpch.cpp" />
<ClCompile Include="src\Duin\Assets\AssetManager.cpp" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Duin/src/Duin.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "Duin/Events/InputMap.h"

// ---------- Graphics -----------
#include "Duin/Graphics/TextureResource.h"
#include "Duin/Assets/TextureResource.h"

// ---------- Entities -----------
#include "Duin/Entity/Entity.h"
Expand Down
18 changes: 18 additions & 0 deletions Duin/src/Duin/Assets/AssetManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "dnpch.h"

#include "AssetManager.h"

namespace Duin
{
AssetManager::AssetManager()
{
}

AssetManager::~AssetManager()
{
}

void AssetManager::LoadTexture(const char* texturePath)
{
}
}
24 changes: 24 additions & 0 deletions Duin/src/Duin/Assets/AssetManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "Duin/Core/Core.h"
#include "Duin/Core/UUID.h"
#include "Duin/Core/Structures/RenderStructs.h"

#include "RLImGuiComponent.h"

#include <unordered_map>

namespace Duin
{
class DUIN_API AssetManager
{
public:
AssetManager();
~AssetManager();

void LoadTexture(const char* texturePath);

private:
std::unordered_map<UUID, std::shared_ptr<Texture>> textureMap;
};
}
12 changes: 12 additions & 0 deletions Duin/src/Duin/Assets/Particle2D.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "Duin/Core/Core.h"

namespace Duin
{
class DUIN_API Particle2D
{
public:

};
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ namespace Duin

TextureResource& TextureResource::LoadTexture(const char* texturePath)
{
Texture texture = ::LoadTexture(texturePath); // supposed to call Raylib's LoadTexture()
texturePtr.reset(new Texture(texture), [](Texture* ptr) {
::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
});
Expand Down Expand Up @@ -88,7 +88,7 @@ namespace Duin
{ position.x, position.y, textureSize.x, textureSize.y },
{ origin.x, origin.y },
rotation_deg,
{ 255, 255, 255, 255 }
{ tintColor.r, tintColor.g, tintColor.b, tintColor.a }
);
//std::cout << "Drawing texture with size: " << textureSize.x << ", " << textureSize.y << "\n";
return *this;
Expand All @@ -100,6 +100,12 @@ namespace Duin
return *this;
}

TextureResource& TextureResource::SetTintColor(Color color)
{
tintColor = color;
return *this;
}

void TextureResource::ClearTexture()
{
texturePtr.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Duin/Core/Core.h"
#include "Duin/Core/Debug/Log.h"
#include "Duin/Core/Maths/DuinMaths.h"
#include "Duin/Core/Structures/RenderStructs.h"

#include <RayGuiComponent.h>

Expand All @@ -27,12 +28,14 @@ namespace Duin
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;
};
}
7 changes: 6 additions & 1 deletion Duin/src/Duin/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ namespace Duin
{
}

void Application::SetBackgroundColor(Color color)
{
backgroundColor = color;
}

void Application::Run()
{
int screenWidth = 1280;
Expand Down Expand Up @@ -48,7 +53,7 @@ namespace Duin
PhysicsUpdate(::GetFrameTime()); // TODO
}

::ClearBackground(RAYWHITE);
::ClearBackground(::Color{ backgroundColor.r, backgroundColor.g, backgroundColor.b, backgroundColor.a });


EngineDraw();
Expand Down
3 changes: 3 additions & 0 deletions Duin/src/Duin/Core/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Duin/Events/InputMap.h"
#include "Duin/Events/InputEvent.h"
#include "Duin/Core/Scene/SceneManager.h"
#include "Duin/Core/Structures/RenderStructs.h"

#include <RayGuiComponent.h>

Expand All @@ -22,6 +23,7 @@ namespace Duin
TARGET_RENDER_FRAMERATE = framerate;
TARGET_PHYSICS_FRAMERATE = framerate;
}
void SetBackgroundColor(Color color);

void Run();

Expand All @@ -45,6 +47,7 @@ namespace Duin
private:
int TARGET_RENDER_FRAMERATE = 60;
int TARGET_PHYSICS_FRAMERATE = 60;
Color backgroundColor = WHITE;
SceneManager sceneManager;
};

Expand Down
2 changes: 1 addition & 1 deletion Duin/src/Duin/Core/Debug/DebugDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Duin
void DebugDraw::DrawLine(Vector2 v1, Vector2 v2, Color color)
{
::Color rlColor{ color.r, color.g, color.b, color.a };
::DrawLine(v1.x, v1.y, v2.x, v2.y, rlColor);
::DrawLine((int)v1.x, (int)v1.y, (int)v2.x, (int)v2.y, rlColor);
}

void DebugDraw::DrawTriangleLines(Vector2 v1, Vector2 v2, Vector2 v3, Color color)
Expand Down
6 changes: 3 additions & 3 deletions Duin/src/Duin/Core/Maths/src/Maths.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

namespace Duin
{
#define EPSILON 0.05
#define EPSILON std::numeric_limits<float>::epsilon()

class DUIN_MATHS_API Maths
{
public:
static bool AreSame(double a, double b)
{
return fabs(a - b) < EPSILON;
return std::fabs(a - b) < EPSILON;
}

static bool AreSame(float a, float b)
{
return fabs(a - b) < EPSILON;
return std::fabs(a - b) < EPSILON;
}

static double RadiansToDegrees(double radians) {
Expand Down
27 changes: 19 additions & 8 deletions Duin/src/Duin/Core/Maths/src/Vector2.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
namespace Duin
{
struct DUIN_MATHS_API Vector2 {
#define ZERO Vector2(0, 0)
#define ZERO Vector2(0.0f, 0.0f)
#define ONE Vector2(1.0f, 1.0f)
#define UP Vector2(0.0f, -1.0f)
#define DOWN Vector2(0.0f, 1.0f)
#define LEFT Vector2(-1.0f, 0.0f)
#define RIGHT Vector2(1.0f, 0.0f)

float x, y;

Expand Down Expand Up @@ -97,30 +102,36 @@ namespace Duin
{
return x * other.x + y * other.y;
}

float Magnitude() const
float Length() const
{
return sqrt(x * x + y * y);
}

float LengthSqrd() const
{
return (x * x + y * y);
}

Vector2 Normalized() const
{
float mag = Magnitude();
float mag = Length();
if (Maths::AreSame(mag, 0))
{
return ZERO;
}
return Vector2(x / mag, y / mag);
float invMag = 1.0f / mag;
return Vector2(x * invMag, y * invMag);
}

Vector2 LimitLength(float minLength, float maxLength) const
{
float mag = Magnitude();
if (mag < minLength)
float mag = LengthSqrd();
if (mag < (minLength * minLength))
{
return Normalized() * minLength;
}
else if (mag > maxLength)
else if (mag > (maxLength * maxLength))
{
return Normalized() * maxLength;
}
Expand Down
Loading

0 comments on commit f388cb4

Please sign in to comment.