diff --git a/Hazel/src/Hazel.h b/Hazel/src/Hazel.h index f7832c0..ead27cf 100644 --- a/Hazel/src/Hazel.h +++ b/Hazel/src/Hazel.h @@ -22,6 +22,7 @@ #include "Hazel/Renderer/Buffer.h" #include "Hazel/Renderer/Shader.h" #include "Hazel/Renderer/Texture.h" +#include "Hazel/Renderer/SubTexture2D.h" #include "Hazel/Renderer/VertexArray.h" #include "Hazel/Renderer/OrthographicCameraController.h" diff --git a/Hazel/src/Hazel/Core/Core.h b/Hazel/src/Hazel/Core/Core.h index 2340158..9f3e17f 100644 --- a/Hazel/src/Hazel/Core/Core.h +++ b/Hazel/src/Hazel/Core/Core.h @@ -19,5 +19,11 @@ namespace Hazel template using Ref = std::shared_ptr; + + template + constexpr Ref CreateRef(Args&& ... args) + { + return std::make_shared(std::forward(args)...); + } } diff --git a/Hazel/src/Hazel/Renderer/Renderer2D.cpp b/Hazel/src/Hazel/Renderer/Renderer2D.cpp index cca335a..17a2de7 100644 --- a/Hazel/src/Hazel/Renderer/Renderer2D.cpp +++ b/Hazel/src/Hazel/Renderer/Renderer2D.cpp @@ -29,10 +29,10 @@ namespace Hazel struct Renderer2DStorage { - const uint32_t MaxQuad = 100; - const uint32_t MaxVertices = MaxQuad * 4; - const uint32_t MaxIndices = MaxQuad * 6; - static const uint32_t MaxTextureSlots = 32; //TODO: RenderCaps + const uint32_t MaxQuad = 10000; + const uint32_t MaxVertices = MaxQuad * 4; + const uint32_t MaxIndices = MaxQuad * 6; + static constexpr uint32_t MaxTextureSlots = 32; //TODO: RenderCaps Ref QuadVertexArray; Ref QuadVertexBuffer; @@ -159,57 +159,6 @@ namespace Hazel s_Data.Stats.DrawCalls++; } - void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color) - { - DrawQuad({position.x, position.y, 0.0f}, size, color); - } - - void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color) - { - HZ_PROFILE_FUNCTION(); - if (s_Data.QuadIndexCount >= s_Data.MaxIndices) - { - FlushAndReset(); - } - - constexpr float texIndex = 0.0f; - constexpr float tilingFactor = 1.0f; - - const glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); - - s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[0]; - s_Data.QuadVertexBufferPtr->Color = color; - s_Data.QuadVertexBufferPtr->TexCoord = {0.0f, 0.0f}; - s_Data.QuadVertexBufferPtr->TexIndex = texIndex; - s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; - s_Data.QuadVertexBufferPtr ++; - - s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[1]; - s_Data.QuadVertexBufferPtr->Color = color; - s_Data.QuadVertexBufferPtr->TexCoord = {1.0f, 0.0f}; - s_Data.QuadVertexBufferPtr->TexIndex = texIndex; - s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; - s_Data.QuadVertexBufferPtr ++; - - s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[2]; - s_Data.QuadVertexBufferPtr->Color = color; - s_Data.QuadVertexBufferPtr->TexCoord = {1.0f, 1.0f}; - s_Data.QuadVertexBufferPtr->TexIndex = texIndex; - s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; - s_Data.QuadVertexBufferPtr ++; - - s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[3]; - s_Data.QuadVertexBufferPtr->Color = color; - s_Data.QuadVertexBufferPtr->TexCoord = {0.0f, 1.0f}; - s_Data.QuadVertexBufferPtr->TexIndex = texIndex; - s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; - s_Data.QuadVertexBufferPtr ++; - - s_Data.QuadIndexCount += 6; - - s_Data.Stats.QuadCount ++; - } - void Renderer2D::Shutdown() { HZ_PROFILE_FUNCTION(); @@ -236,10 +185,46 @@ namespace Hazel } + + void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const glm::vec4& color) + { + DrawQuad({position.x, position.y, 0.0f}, size, color); + } + + void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color) + { + HZ_PROFILE_FUNCTION(); + if (s_Data.QuadIndexCount >= s_Data.MaxIndices) + { + FlushAndReset(); + } + + constexpr float texIndex = 0.0f; + constexpr float tilingFactor = 1.0f; + glm::vec2 texCoords[] = {{0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}}; + + const glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); + + for (unsigned int i = 0; i < 4; i++) + { + s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[i]; + s_Data.QuadVertexBufferPtr->Color = color; + s_Data.QuadVertexBufferPtr->TexCoord = texCoords[i]; + s_Data.QuadVertexBufferPtr->TexIndex = texIndex; + s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; + s_Data.QuadVertexBufferPtr++; + } + + s_Data.QuadIndexCount += 6; + + s_Data.Stats.QuadCount ++; + } + void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref& texture, const float tilingFactor, const glm::vec4& tintColor) { DrawQuad({position.x, position.y, 0.0f}, size, texture, tilingFactor, tintColor); } + void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref& texture, const float tilingFactor, const glm::vec4& tintColor) { HZ_PROFILE_FUNCTION(); @@ -248,7 +233,7 @@ namespace Hazel FlushAndReset(); } - constexpr glm::vec4 color = {1.0f, 1.0f, 1.0f, 1.0f}; + glm::vec2 texCoords[] = {{0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}}; float textureIndex = 0.0f; @@ -271,41 +256,74 @@ namespace Hazel const glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); - s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[0]; - s_Data.QuadVertexBufferPtr->Color = color; - s_Data.QuadVertexBufferPtr->TexCoord = {0.0f, 0.0f}; - s_Data.QuadVertexBufferPtr->TexIndex = textureIndex; - s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; - s_Data.QuadVertexBufferPtr ++; - - s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[1]; - s_Data.QuadVertexBufferPtr->Color = color; - s_Data.QuadVertexBufferPtr->TexCoord = {1.0f, 0.0f}; - s_Data.QuadVertexBufferPtr->TexIndex = textureIndex; - s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; - s_Data.QuadVertexBufferPtr ++; - - s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[2]; - s_Data.QuadVertexBufferPtr->Color = color; - s_Data.QuadVertexBufferPtr->TexCoord = {1.0f, 1.0f}; - s_Data.QuadVertexBufferPtr->TexIndex = textureIndex; - s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; - s_Data.QuadVertexBufferPtr ++; - - s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[3]; - s_Data.QuadVertexBufferPtr->Color = color; - s_Data.QuadVertexBufferPtr->TexCoord = {0.0f, 1.0f}; - s_Data.QuadVertexBufferPtr->TexIndex = textureIndex; - s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; - s_Data.QuadVertexBufferPtr ++; - + for (unsigned int i = 0; i < 4; i++) + { + s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[i]; + s_Data.QuadVertexBufferPtr->Color = tintColor; + s_Data.QuadVertexBufferPtr->TexCoord = texCoords[i]; + s_Data.QuadVertexBufferPtr->TexIndex = textureIndex; + s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; + s_Data.QuadVertexBufferPtr++; + } s_Data.QuadIndexCount += 6; s_Data.Stats.QuadCount ++; } + void Renderer2D::DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref& subTexture, const float tilingFactor, const glm::vec4& tintColor) + { + DrawQuad({position.x, position.y, 0.0f}, size, subTexture, tilingFactor, tintColor); + } + + void Renderer2D::DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref& subTexture, const float tilingFactor, const glm::vec4& tintColor) + { + HZ_PROFILE_FUNCTION(); + if (s_Data.QuadIndexCount >= s_Data.MaxIndices) + { + FlushAndReset(); + } + + const glm::vec2* texCoords = subTexture->GetTexCoords(); + const Ref texture = subTexture->GetTexture(); + + float textureIndex = 0.0f; + + for (uint32_t i = 1; i < s_Data.TextureSlotIndex; i++) + { + if (*s_Data.TextureSlots[i].get() == *texture.get()) + { + textureIndex = (float)i; + break; + } + + } + + if (textureIndex == 0.0f) + { + textureIndex = (float)s_Data.TextureSlotIndex; + s_Data.TextureSlots[s_Data.TextureSlotIndex] = texture; + s_Data.TextureSlotIndex++; + } + + const glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); + + for (unsigned int i = 0; i < 4; i++) + { + s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[i]; + s_Data.QuadVertexBufferPtr->Color = tintColor; + s_Data.QuadVertexBufferPtr->TexCoord = texCoords[i]; + s_Data.QuadVertexBufferPtr->TexIndex = textureIndex; + s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; + s_Data.QuadVertexBufferPtr++; + } + s_Data.QuadIndexCount += 6; + + s_Data.Stats.QuadCount ++; + } + + void Renderer2D::DrawRotateQuad(const glm::vec2& position, const glm::vec2& size, const float rotation, - const glm::vec4& color) + const glm::vec4& color) { DrawRotateQuad({position.x, position.y, 0.0f}, size, rotation, color); } @@ -323,38 +341,19 @@ namespace Hazel constexpr float texIndex = 0.0f; constexpr float tilingFactor = 1.0f; + glm::vec2 texCoords[] = {{0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}}; const glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::rotate(glm::mat4(1.0f), rotation, {0.0f, 0.0f, 1.0f}) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); - s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[0]; - s_Data.QuadVertexBufferPtr->Color = color; - s_Data.QuadVertexBufferPtr->TexCoord = {0.0f, 0.0f}; - s_Data.QuadVertexBufferPtr->TexIndex = texIndex; - s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; - s_Data.QuadVertexBufferPtr ++; - - s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[1]; - s_Data.QuadVertexBufferPtr->Color = color; - s_Data.QuadVertexBufferPtr->TexCoord = {1.0f, 0.0f}; - s_Data.QuadVertexBufferPtr->TexIndex = texIndex; - s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; - s_Data.QuadVertexBufferPtr ++; - - s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[2]; - s_Data.QuadVertexBufferPtr->Color = color; - s_Data.QuadVertexBufferPtr->TexCoord = {1.0f, 1.0f}; - s_Data.QuadVertexBufferPtr->TexIndex = texIndex; - s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; - s_Data.QuadVertexBufferPtr ++; - - s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[3]; - s_Data.QuadVertexBufferPtr->Color = color; - s_Data.QuadVertexBufferPtr->TexCoord = {0.0f, 1.0f}; - s_Data.QuadVertexBufferPtr->TexIndex = texIndex; - s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; - s_Data.QuadVertexBufferPtr ++; - - + for (unsigned int i = 0; i < 4; i++) + { + s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[i]; + s_Data.QuadVertexBufferPtr->Color = color; + s_Data.QuadVertexBufferPtr->TexCoord = texCoords[i]; + s_Data.QuadVertexBufferPtr->TexIndex = texIndex; + s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; + s_Data.QuadVertexBufferPtr++; + } s_Data.QuadIndexCount += 6; s_Data.Stats.QuadCount ++; @@ -377,6 +376,7 @@ namespace Hazel } float textureIndex = 0.0f; + glm::vec2 texCoords[] = {{0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}}; for (uint32_t i = 1; i < s_Data.TextureSlotIndex; i++) { @@ -396,38 +396,69 @@ namespace Hazel const glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::rotate(glm::mat4(1.0f), rotation, {0.0f, 0.0f, 1.0f}) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); - - s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[0]; - s_Data.QuadVertexBufferPtr->Color = tintColor; - s_Data.QuadVertexBufferPtr->TexCoord = {0.0f, 0.0f}; - s_Data.QuadVertexBufferPtr->TexIndex = textureIndex; - s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; - s_Data.QuadVertexBufferPtr ++; - - s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[1]; - s_Data.QuadVertexBufferPtr->Color = tintColor; - s_Data.QuadVertexBufferPtr->TexCoord = {1.0f, 0.0f}; - s_Data.QuadVertexBufferPtr->TexIndex = textureIndex; - s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; - s_Data.QuadVertexBufferPtr ++; - - s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[2]; - s_Data.QuadVertexBufferPtr->Color = tintColor; - s_Data.QuadVertexBufferPtr->TexCoord = {1.0f, 1.0f}; - s_Data.QuadVertexBufferPtr->TexIndex = textureIndex; - s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; - s_Data.QuadVertexBufferPtr ++; - - s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[3]; - s_Data.QuadVertexBufferPtr->Color = tintColor; - s_Data.QuadVertexBufferPtr->TexCoord = {0.0f, 1.0f}; - s_Data.QuadVertexBufferPtr->TexIndex = textureIndex; - s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; - s_Data.QuadVertexBufferPtr ++; - + for (unsigned int i = 0; i < 4; i++) + { + s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[i]; + s_Data.QuadVertexBufferPtr->Color = tintColor; + s_Data.QuadVertexBufferPtr->TexCoord = texCoords[i]; + s_Data.QuadVertexBufferPtr->TexIndex = textureIndex; + s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; + s_Data.QuadVertexBufferPtr++; + } s_Data.QuadIndexCount += 6; s_Data.Stats.QuadCount ++; } + void Renderer2D::DrawRotateQuad(const glm::vec2& position, const glm::vec2& size, const float rotation, + const Ref& subTexture, const float tilingFactor, const glm::vec4& tintColor) + { + DrawRotateQuad({position.x, position.y, 0.0f}, size, rotation, subTexture, tilingFactor, tintColor); + } + + void Renderer2D::DrawRotateQuad(const glm::vec3& position, const glm::vec2& size, const float rotation, + const Ref& subTexture, const float tilingFactor, const glm::vec4& tintColor) + { + HZ_PROFILE_FUNCTION(); + + if (s_Data.QuadIndexCount >= s_Data.MaxIndices) + { + FlushAndReset(); + } + + float textureIndex = 0.0f; + const glm::vec2* texCoords = subTexture->GetTexCoords(); + const Ref& texture = subTexture->GetTexture(); + + for (uint32_t i = 1; i < s_Data.TextureSlotIndex; i++) + { + if (*s_Data.TextureSlots[i].get() == *texture.get()) + { + textureIndex = (float)i; + break; + } + } + + if (textureIndex == 0.0f) + { + textureIndex = (float)s_Data.TextureSlotIndex; + s_Data.TextureSlots[s_Data.TextureSlotIndex] = texture; + s_Data.TextureSlotIndex++; + } + + const glm::mat4 transform = glm::translate(glm::mat4(1.0f), position) * glm::rotate(glm::mat4(1.0f), rotation, {0.0f, 0.0f, 1.0f}) * glm::scale(glm::mat4(1.0f), { size.x, size.y, 1.0f }); + + for (unsigned int i = 0; i < 4; i++) + { + s_Data.QuadVertexBufferPtr->Position = transform * s_Data.QuadVertexPosition[i]; + s_Data.QuadVertexBufferPtr->Color = tintColor; + s_Data.QuadVertexBufferPtr->TexCoord = texCoords[i]; + s_Data.QuadVertexBufferPtr->TexIndex = textureIndex; + s_Data.QuadVertexBufferPtr->TilingFactor = tilingFactor; + s_Data.QuadVertexBufferPtr++; + } + s_Data.QuadIndexCount += 6; + + s_Data.Stats.QuadCount ++; + } } diff --git a/Hazel/src/Hazel/Renderer/Renderer2D.h b/Hazel/src/Hazel/Renderer/Renderer2D.h index 55ff91a..e892322 100644 --- a/Hazel/src/Hazel/Renderer/Renderer2D.h +++ b/Hazel/src/Hazel/Renderer/Renderer2D.h @@ -6,6 +6,7 @@ #define RENDERER2D_H #include "OrthographicCamera.h" +#include "SubTexture2D.h" #include "Texture.h" #include "Hazel/Core/Core.h" @@ -24,11 +25,15 @@ namespace Hazel static void DrawQuad(const glm::vec3& position, const glm::vec2& size, const glm::vec4& color); static void DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref& texture, float tilingFactor = 1.0f, const glm::vec4& tintColor = glm::vec4(1.0f)); static void DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref& texture, float tilingFactor = 1.0f, const glm::vec4& tintColor = glm::vec4(1.0f)); + static void DrawQuad(const glm::vec2& position, const glm::vec2& size, const Ref& subTexture, float tilingFactor = 1.0f, const glm::vec4& tintColor = glm::vec4(1.0f)); + static void DrawQuad(const glm::vec3& position, const glm::vec2& size, const Ref& subTexture, float tilingFactor = 1.0f, const glm::vec4& tintColor = glm::vec4(1.0f)); static void DrawRotateQuad(const glm::vec2& position, const glm::vec2& size, float rotation, const glm::vec4& color); static void DrawRotateQuad(const glm::vec3& position, const glm::vec2& size, float rotation, const glm::vec4& color); static void DrawRotateQuad(const glm::vec2& position, const glm::vec2& size, float rotation, const Ref& texture, float tilingFactor = 1.0f, const glm::vec4& tintColor = glm::vec4(1.0f)); static void DrawRotateQuad(const glm::vec3& position, const glm::vec2& size, float rotation, const Ref& texture, float tilingFactor = 1.0f, const glm::vec4& tintColor = glm::vec4(1.0f)); + static void DrawRotateQuad(const glm::vec2& position, const glm::vec2& size, float rotation, const Ref& subTexture, float tilingFactor = 1.0f, const glm::vec4& tintColor = glm::vec4(1.0f)); + static void DrawRotateQuad(const glm::vec3& position, const glm::vec2& size, float rotation, const Ref& subTexture, float tilingFactor = 1.0f, const glm::vec4& tintColor = glm::vec4(1.0f)); static void Shutdown(); diff --git a/Hazel/src/Hazel/Renderer/SubTexture2D.cpp b/Hazel/src/Hazel/Renderer/SubTexture2D.cpp new file mode 100644 index 0000000..b743a9c --- /dev/null +++ b/Hazel/src/Hazel/Renderer/SubTexture2D.cpp @@ -0,0 +1,25 @@ +// +// Created by sfd on 25-5-24. +// + +#include "SubTexture2D.h" + +namespace Hazel +{ + SubTexture2D::SubTexture2D(const Ref& texture, const glm::vec2& min, const glm::vec2& max) + : m_Texture(texture) + { + m_TexCoords[0] = {min.x, min.y}; + m_TexCoords[1] = {max.x, min.y}; + m_TexCoords[2] = {max.x, max.y}; + m_TexCoords[3] = {min.x, max.y}; + } + + Ref SubTexture2D::CreateFromCoords(const Ref& texture, const glm::vec2& coords, const glm::vec2& cellSize, const glm::vec2& spriteSize) + { + glm::vec2 min = {(coords.x * cellSize.x) / texture->GetWidth(), (coords.y * cellSize.y) / texture->GetHeight()}; + glm::vec2 max = {((coords.x + spriteSize.x) * cellSize.x) / texture->GetWidth(), ((coords.y + spriteSize.y) * cellSize.y) / texture->GetHeight()}; + + return CreateRef(texture, min, max); + } +} diff --git a/Hazel/src/Hazel/Renderer/SubTexture2D.h b/Hazel/src/Hazel/Renderer/SubTexture2D.h new file mode 100644 index 0000000..d2fd4d4 --- /dev/null +++ b/Hazel/src/Hazel/Renderer/SubTexture2D.h @@ -0,0 +1,31 @@ +// +// Created by sfd on 25-5-24. +// + +#ifndef SUBTEXTURE2D_H +#define SUBTEXTURE2D_H +#include + +#include "Texture.h" +#include "glm/vec2.hpp" + + +namespace Hazel +{ + class HAZEL_API SubTexture2D + { + public: + SubTexture2D(const Ref& texture, const glm::vec2& min, const glm::vec2& max); + + const Ref& GetTexture() const { return m_Texture; } + const glm::vec2* GetTexCoords() const { return m_TexCoords; } + + static Ref CreateFromCoords(const Ref& texture, const glm::vec2& coords, const glm::vec2& cellSize, const glm::vec2& spriteSize = {1.0f,1.0f}); + private: + Ref m_Texture; + glm::vec2 m_TexCoords[4]; + }; +} + + +#endif //SUBTEXTURE2D_H diff --git a/Sandbox/assets/textures/spritesheet-tiles-double.png b/Sandbox/assets/textures/spritesheet-tiles-double.png new file mode 100644 index 0000000..76d6860 Binary files /dev/null and b/Sandbox/assets/textures/spritesheet-tiles-double.png differ diff --git a/Sandbox/src/SandBox2D/ParticleSystem.h b/Sandbox/src/SandBox2D/ParticleSystem.h index e19e7d3..46d2f75 100644 --- a/Sandbox/src/SandBox2D/ParticleSystem.h +++ b/Sandbox/src/SandBox2D/ParticleSystem.h @@ -43,7 +43,7 @@ private: }; std::vector m_particlePool; - uint32_t m_PoolIndex = 5000; + uint32_t m_PoolIndex = 1000; }; diff --git a/Sandbox/src/SandBox2D/SandBox2D.cpp b/Sandbox/src/SandBox2D/SandBox2D.cpp index c526cf6..928bcfc 100644 --- a/Sandbox/src/SandBox2D/SandBox2D.cpp +++ b/Sandbox/src/SandBox2D/SandBox2D.cpp @@ -17,10 +17,12 @@ void SandBox2D::OnAttach() { HZ_PROFILE_FUNCTION(); m_LogoTexture = Hazel::Texture2D::Create("assets/textures/iceLogo.png"); + m_Texture = Hazel::Texture2D::Create("assets/textures/spritesheet-tiles-double.png"); + m_SubTexture = Hazel::SubTexture2D::CreateFromCoords(m_Texture, {8, 5}, {128, 128}, {1, 2}); m_Particle.ColorBegin = {0 / 255.f, 212 /255.f, 123 / 255.f,1.0f}; m_Particle.ColorEnd = {254 / 255.f, 109 /255.f, 41 / 255.f,1.0f}; - m_Particle.SizeBegin = 0.5f, m_Particle.SizeVariation = 0.3f, m_Particle.SizeEnd = 0.0f; + m_Particle.SizeBegin = 0.3f, m_Particle.SizeVariation = 0.3f, m_Particle.SizeEnd = 0.0f; m_Particle.LifeTime = 1.0f; m_Particle.Velocity = {0.0f, 0.0f}; m_Particle.VelocityVariation = {2.0f, 2.0f}; @@ -53,22 +55,25 @@ void SandBox2D::OnUpdate(Hazel::TimeStep& ts) Hazel::Renderer2D::BeginScene(m_CameraController.GetCamera()); - Hazel::Renderer2D::DrawRotateQuad({0.0f, 0.0f}, {1.0f, 1.0f}, 45.f, {1.0f, 1.0f, 1.0f, 1.0f}); + // Hazel::Renderer2D::DrawQuad({0, 0}, {1.0f,1.0f}, {1.0f, 1.0f, 1.0f, 1.0f}); + Hazel::Renderer2D::DrawRotateQuad({-0.5f, 0}, {1.0f,1.0f}, glm::radians(0.f), m_LogoTexture); + // Hazel::Renderer2D::DrawQuad({0.5f, 0}, {1.0f,1.0f}, m_Texture); + Hazel::Renderer2D::DrawQuad({0.5f, 0}, {1.0f,2.0f}, m_SubTexture); + // Hazel::Renderer2D::DrawRotateQuad({-1.0f, 0.0f}, {0.5f, 0.5f}, 75.f, {1.0f, 0.0f, 1.0f, 1.0f}); // Hazel::Renderer2D::DrawRotateQuad({0.0f, 0.0f}, {1.0f, 1.0f}, rotation, m_LogoTexture, 10.f); - // for (float y = -5.0f; y < 5.0f; y += 0.5f) - // { - // for (float x = -5.0f; x < 5.0f; x += 0.5f) - // { - // auto color = glm::vec4((x + 5.0f ) /10.0f, 0.4f, (y + 5.0f) / 10.0f, 1.0f); - // Hazel::Renderer2D::DrawQuad({x, y}, {0.45f, 0.45f}, color); - // } - // } + for (float y = -5.0f; y < 5.0f; y += 0.5f) + { + for (float x = -5.0f; x < 5.0f; x += 0.5f) + { + auto color = glm::vec4((x + 5.0f ) /10.0f, 0.4f, (y + 5.0f) / 10.0f, 1.0f); + Hazel::Renderer2D::DrawQuad({x, y, -0.1f}, {0.45f, 0.45f}, m_LogoTexture, 1, color); + } + } Hazel::Renderer2D::EndScene(); - - if (mouseState & SDL_BUTTON_LMASK) + if ((mouseState & SDL_BUTTON_LMASK) && m_CurrentWindowID == Hazel::Application::Get().GetWindow().GetMainWindowID()) { auto width = Hazel::Application::Get().GetWindow().GetWidth(); auto height = Hazel::Application::Get().GetWindow().GetHeight(); @@ -145,5 +150,8 @@ void SandBox2D::OnImGuiRender() void SandBox2D::OnEvent(SDL_Event& e) { + if (m_CurrentWindowID != e.window.windowID) + m_CurrentWindowID = e.window.windowID; + m_CameraController.OnEvent(e); } diff --git a/Sandbox/src/SandBox2D/SandBox2D.h b/Sandbox/src/SandBox2D/SandBox2D.h index 5a46798..e783e5e 100644 --- a/Sandbox/src/SandBox2D/SandBox2D.h +++ b/Sandbox/src/SandBox2D/SandBox2D.h @@ -24,6 +24,7 @@ private: Hazel::Ref m_FlatColorShader; Hazel::Ref m_Texture; Hazel::Ref m_LogoTexture; + Hazel::Ref m_SubTexture; glm::vec4 m_BackgroundColor = {0.2f, 0.2f, 0.2f, 1.0f}; @@ -37,6 +38,8 @@ private: ParticleSystem m_ParticleSystem; ParticleProp m_Particle; + + SDL_WindowID m_CurrentWindowID; };