Skip to content

Commit

Permalink
Texture & drawing working (kinda)
Browse files Browse the repository at this point in the history
  • Loading branch information
Y0L042 committed Jun 15, 2024
1 parent b34b284 commit 6000095
Show file tree
Hide file tree
Showing 25 changed files with 235 additions and 78 deletions.
1 change: 1 addition & 0 deletions Duin/Duin.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ IF EXIST ..\bin\Dist-windows-x86_64\Duin\Duin.dll\ (xcopy /Q /E /Y /I ..\bin\Dis
<ClInclude Include="src\Duin\Object\Blackboard.h" />
<ClInclude Include="src\Duin\Object\Node.h" />
<ClInclude Include="src\dnpch.h" />
<ClInclude Include="vendor\patches\include\RLWrapper.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Duin\Assets\AnimatedSprite2D.cpp" />
Expand Down
1 change: 1 addition & 0 deletions Duin/Duin.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<ClInclude Include="src\Duin\Assets\Sprite2D.h" />
<ClInclude Include="src\Duin\Assets\Texture.h" />
<ClInclude Include="src\Duin\Assets\Asset.h" />
<ClInclude Include="vendor\patches\include\RLWrapper.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\Duin\Core\Application.cpp">
Expand Down
7 changes: 6 additions & 1 deletion Duin/src/Duin.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@
#include "Duin/Events/InputMap.h"

// ---------- Assets -----------
#include "Duin/Assets/TextureResource.h"
#include "Duin/Assets/AssetManager.h"
#include "Duin/Assets/Asset.h"
#include "Duin/Assets/Texture.h"
#include "Duin/Assets/Image.h"
#include "Duin/Assets/Sprite2D.h"
#include "Duin/Assets/AnimatedSprite2D.h"
#include "Duin/Assets/Particle2D.h"

// ---------- Entities -----------
#include "Duin/Entity/Entity.h"
Expand Down
4 changes: 2 additions & 2 deletions Duin/src/Duin/Assets/AssetStructs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Duin
{
// TODO
::Texture rlTexture = ::LoadTexture(path);
std::shared_ptr<_TextureAsset> textureAsset = std::make_shared<_TextureAsset>(rlTexture);
std::shared_ptr<_TextureAsset> textureAsset = std::make_shared<_TextureAsset>(rlTexture, path);

return textureAsset;
}
Expand All @@ -18,7 +18,7 @@ namespace Duin
{
// TODO
::Image rlImage = ::LoadImage(path);
std::shared_ptr<_ImageAsset> imageAsset = std::make_shared<_ImageAsset>(rlImage);
std::shared_ptr<_ImageAsset> imageAsset = std::make_shared<_ImageAsset>(rlImage, path);

return imageAsset;
}
Expand Down
18 changes: 14 additions & 4 deletions Duin/src/Duin/Assets/AssetStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ namespace Duin

class DUIN_API _AssetFactory
{
_AssetFactory() = default;
~_AssetFactory() = default;
public:
_AssetFactory() {};
~_AssetFactory() {};

bool enabled = true;

virtual std::shared_ptr<_Asset> CreateAsset(AssetManager* assetManager, const char* path) = 0;
};
Expand All @@ -44,13 +47,16 @@ namespace Duin
int height; // Texture base height
int mipmaps; // Mipmap levels, 1 by default
int format; // Data format (PixelFormat type)
::Texture rlTextureCache;

_TextureAsset() {}

_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) {}
mipmaps(texture.mipmaps), format(texture.format), rlTextureCache(texture) {}

::Texture ToRaylibTexture() const // Method to convert back to raylib::Texture
{
Expand All @@ -74,13 +80,16 @@ namespace Duin
int height; // Image base height
int mipmaps; // Mipmap levels, 1 by default
int format; // Data format (PixelFormat type)
::Image rlImageCache;

_ImageAsset() {}

_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) {}
mipmaps(image.mipmaps), format(image.format), rlImageCache(image) {}

::Image ToRaylibImage() const // Method to convert back to raylib::Image
{
Expand All @@ -93,4 +102,5 @@ namespace Duin
public:
std::shared_ptr<_Asset> CreateAsset(AssetManager* assetManager, const char* path) override;
};

}
28 changes: 23 additions & 5 deletions Duin/src/Duin/Assets/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Duin
: Asset(assetManager)
{
_textureAsset = assetManager->LoadAsset<_TextureAsset, _TextureAssetFactory>(path);
_defaultSize = size = Vector2(_textureAsset->width, _textureAsset->height);
}

Texture::~Texture()
Expand All @@ -22,16 +23,33 @@ namespace Duin

Vector2 Texture::GetTextureSize()
{
return Vector2{ (int)_textureAsset->width, (int)_textureAsset->height };
return Vector2{ (float)_textureAsset->width, (float)_textureAsset->height };
}

void Texture::SetCentered(bool centered)
void Texture::SetTextureSize(Vector2 size)
{
this->centered = centered;
this->size = size;
}

void Texture::Draw(Vector2 position, float rotation)
void Texture::ResetTextureSize()
{
// TODO
size = _defaultSize;
}

void Texture::Draw(Vector2 position, float rotation_deg, Color color, float scale, bool centered)
{
float scaledWidth = size.x * scale;
float scaledHeight = size.y * scale;
float halfW = scaledWidth / 2.0f;
float halfH = scaledHeight / 2.0f;

::DrawTexturePro(
_textureAsset->rlTextureCache,
::Rectangle(0, 0, _textureAsset->width, _textureAsset->height),
::Rectangle(position.x, position.y, scaledWidth, scaledHeight),
::Vector2(halfW, halfH),
rotation_deg,
::Color(color.r, color.g, color.b, color.a)
);
}
}
12 changes: 7 additions & 5 deletions Duin/src/Duin/Assets/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Duin/Core/Core.h"
#include "Duin/Assets/AssetStructs.h"
#include "Duin/Core/Structures/RenderStructs.h"
#include "Duin/Assets/Asset.h"
#include "Duin/Assets/GUID.h"
#include "Duin/Core/Maths/DuinMaths.h"
Expand All @@ -24,14 +25,15 @@ namespace Duin
~Texture();

Vector2 GetTextureSize();
void SetTextureSize(Vector2 size);
void ResetTextureSize();

void SetCentered(bool centered);

void Draw(Vector2 position, float rotation);
void Draw(Vector2 position, float rotation_deg = 1.0f, Color color = Duin::WHITE, float scale = 1.0f, bool centered = true);

private:
std::shared_ptr<_TextureAsset> _textureAsset;
bool centered = false;


Vector2 _defaultSize;
Vector2 size;
};
}
14 changes: 10 additions & 4 deletions Duin/src/Duin/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ namespace Duin
backgroundColor = color;
}

void Application::SetWindowName(const char* string)
{
windowName = std::string(string);
}

void Application::Run()
{
EngineInitialize();
Initialize();

int screenWidth = 1280;
int screenHeight = 720;

::InitWindow(screenWidth, screenHeight, "Test");
::InitWindow(screenWidth, screenHeight, windowName.c_str());
::SetTargetFPS(144);
::rlImGuiSetup(true);

EngineInitialize();
EngineReady();

Initialize();
Ready();

while(!::WindowShouldClose())
Expand Down Expand Up @@ -117,6 +122,7 @@ namespace Duin
void Application::EngineDraw()
{
sceneManager.CallDraw();
DebugDraw::Draw();
}

void Application::Draw()
Expand Down
8 changes: 6 additions & 2 deletions Duin/src/Duin/Core/Application.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#pragma once
#include "dnpch.h"

#include "Duin/Core/Core.h"
#include "Duin/Events/InputMap.h"
#include "Duin/Events/InputEvent.h"
#include "Duin/Core/Scene/SceneManager.h"
#include "Duin/Core/Structures/RenderStructs.h"
#include "Duin/Core/Debug/DebugDraw.h"

#include <RayGuiComponent.h>

#include <string>

namespace Duin
{
Expand All @@ -24,6 +25,7 @@ namespace Duin
TARGET_PHYSICS_FRAMERATE = framerate;
}
void SetBackgroundColor(Color color);
void SetWindowName(const char* string);

void Run();

Expand All @@ -39,15 +41,17 @@ namespace Duin
virtual void PhysicsUpdate(double pDelta);
void EngineDraw();
virtual void Draw();
void EngineExit();
virtual void Exit();
void EngineExit();

SceneManager& GetSceneManager();

private:
int TARGET_RENDER_FRAMERATE = 60;
int TARGET_PHYSICS_FRAMERATE = 60;
Color backgroundColor = WHITE;
std::string windowName = "Game";

SceneManager sceneManager;
};

Expand Down
52 changes: 46 additions & 6 deletions Duin/src/Duin/Core/Debug/DebugDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,76 @@ namespace Duin
{
void DebugDraw::DrawLine(int x1, int y1, int x2, int y2, Color color)
{
auto& inst = GetInstance();
::Color rlColor{ color.r, color.g, color.b, color.a };
::DrawLine(x1, y1, x2, y2, rlColor);
inst.drawQueue.push_back(
[=]()
{
::DrawLine(x1, y1, x2, x2, rlColor);
}
);
}

void DebugDraw::DrawLine(Vector2 v1, Vector2 v2, Color color)
{
auto& inst = GetInstance();
::Color rlColor{ color.r, color.g, color.b, color.a };
::DrawLine((int)v1.x, (int)v1.y, (int)v2.x, (int)v2.y, rlColor);
inst.drawQueue.push_back(
[=]()
{
::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)
{
auto& inst = GetInstance();
::Color rlColor{ color.r, color.g, color.b, color.a };
::Vector2 rlVec1{ v1.x, v1.y };
::Vector2 rlVec2{ v2.x, v2.y };
::Vector2 rlVec3{ v3.x, v3.y };
::DrawTriangleLines(rlVec1, rlVec2, rlVec3, rlColor);
inst.drawQueue.push_back(
[=]()
{
::DrawTriangleLines(rlVec1, rlVec2, rlVec3, rlColor);
}
);
}

void DebugDraw::DrawCircle(Vector2 center, float radius, Color color)
{
auto& inst = GetInstance();
::Color rlColor{ color.r, color.g, color.b, color.a };
::Vector2 rlCenter{ center.x, center.y };
::DrawCircleV(rlCenter, radius, rlColor);
inst.drawQueue.push_back(
[=]()
{
::DrawCircleV(rlCenter, radius, rlColor);
}
);
}

void DebugDraw::DrawCircleLines(Vector2 center, float radius, Color color)
{
auto& inst = GetInstance();
::Color rlColor{ color.r, color.g, color.b, color.a };
::Vector2 rlCenter{ center.x, center.y };
::DrawCircleLinesV(rlCenter, radius, rlColor);
inst.drawQueue.push_back(
[=]()
{
::DrawCircleLines(rlCenter.x, rlCenter.y, radius, rlColor);
}
);
}
}

void DebugDraw::Draw()
{
auto& inst = GetInstance();
for (auto& command : inst.drawQueue)
{
command();
}
inst.drawQueue.clear();
}
}
16 changes: 16 additions & 0 deletions Duin/src/Duin/Core/Debug/DebugDraw.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <RLImGuiComponent.h>

#include <functional>

namespace Duin
{
class DUIN_API DebugDraw
Expand All @@ -18,5 +20,19 @@ namespace Duin

static void DrawCircle(Vector2 center, float radius, Color color);
static void DrawCircleLines(Vector2 center, float radius, Color color);

static void Draw();

private:
std::vector<std::function<void()>> drawQueue;

static DebugDraw& GetInstance()
{
static DebugDraw* instance = new DebugDraw();
return *instance;
}

DebugDraw() {}
~DebugDraw() {}
};
}
Loading

0 comments on commit 6000095

Please sign in to comment.