diff --git a/examples/HelloAnimationcopy/main.cpp b/examples/HelloRotation/main.cpp similarity index 73% rename from examples/HelloAnimationcopy/main.cpp rename to examples/HelloRotation/main.cpp index 5e2c167..4279614 100644 --- a/examples/HelloAnimationcopy/main.cpp +++ b/examples/HelloRotation/main.cpp @@ -7,14 +7,14 @@ int main() { WindowWayland window(200, 200, "Hello Input!"); RenderingElementScalingRotating2D element( - OpaqueType::FullyOpaque, //opaque, wether the element is opague or semi-transparant + OpaqueType::SemiOpaque, //opaque, wether the element is opague or semi-transparant 1, //bufferWidth: the width of this elements pixel buffer 1, //bufferHeight: the height of this elements pixel buffer FractionalToMappedBoundlessU(0.125), FractionalToMapped(0.5), //anchorX: relative position where this elements x anchor (top-left) is placed to its parent x anchor FractionalToMapped(0.5), //anchorY: relative position where this elements y anchor (top-left) is placed to its parent y anchor - FractionalToMapped(0.1), //relativeSizeX: the relative x size this element should be scaled to compared to its parent - FractionalToMapped(0.1), //relativeSizeY: the relative y size this element should be scaled to compared to its parent + FractionalToMapped(0.5), //relativeSizeX: the relative x size this element should be scaled to compared to its parent + FractionalToMapped(0.5), //relativeSizeY: the relative y size this element should be scaled to compared to its parent FractionalToMapped(0.5), //anchorOffsetX: the amount this element's anchor should be offset from the top left corner (0.5 to in the middle) FractionalToMapped(0.5), //anchorOffsetY: the amount this element's anchor should be offset from the top left corner (0.5 to place it in the middle) 0, //z: this elements Z position @@ -24,10 +24,14 @@ int main() { window.elements.push_back(&element); element.buffer = {{255, 0, 0 ,255}}; element.UpdatePosition(window); - - - //window.StartUpdate(); - window.Render(); + EventListener updateListener(&window.onUpdate, [&](FrameTime time){ + element.rotation += 50000000000000000; + std::cout << element.rotation << std::endl; + element.UpdatePosition(window); + window.LogTiming(); + }); + + window.StartUpdate(); window.StartSync(); } diff --git a/examples/HelloAnimationcopy/project.json b/examples/HelloRotation/project.json similarity index 100% rename from examples/HelloAnimationcopy/project.json rename to examples/HelloRotation/project.json diff --git a/implementations/Crafter.Graphics-RenderingElement.cpp b/implementations/Crafter.Graphics-RenderingElement.cpp index 4e70351..ab25706 100644 --- a/implementations/Crafter.Graphics-RenderingElement.cpp +++ b/implementations/Crafter.Graphics-RenderingElement.cpp @@ -116,7 +116,9 @@ void RenderingElementScaling::CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, s void RenderingElementScaling::UpdatePosition(Window& window) { ScaleData oldScale = scaled; + std::cout << MappedToFractional(relativeWidth) << std::endl; window.ScaleElement(*this); + std::cout << scaled.width << std::endl; if(oldScale.width != scaled.width || oldScale.height != scaled.height) { diff --git a/implementations/Crafter.Graphics-RenderingElementScalingRotating2D.cpp b/implementations/Crafter.Graphics-RenderingElementScalingRotating2D.cpp index 4f996be..c807b6b 100644 --- a/implementations/Crafter.Graphics-RenderingElementScalingRotating2D.cpp +++ b/implementations/Crafter.Graphics-RenderingElementScalingRotating2D.cpp @@ -35,11 +35,10 @@ RenderingElementScalingRotating2D::RenderingElementScalingRotating2D(OpaqueType } -RenderingElementScalingRotating2D::RenderingElementScalingRotating2D(OpaqueType opaque, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::uint_fast32_t rotation, double scaleX, double scaleY, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) +RenderingElementScalingRotating2D::RenderingElementScalingRotating2D(OpaqueType opaque, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::uint_fast32_t rotation, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : bufferWidth(bufferWidth), bufferHeight(bufferHeight), buffer(bufferWidth*bufferHeight), rotation(rotation), RenderingElement(opaque, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { - } void RenderingElementScalingRotating2D::ResizeBuffer(std::uint_fast32_t width, std::uint_fast32_t height) { @@ -57,15 +56,22 @@ void RenderingElementScalingRotating2D::UpdateScaledBuffer(ScaleData& scaled) { // Rotation const double rad = (static_cast(rotation) / static_cast(std::numeric_limits::max())) * 2.0 * std::numbers::pi; - std::uint_fast32_t dstWidth = scaled.width; - std::uint_fast32_t dstHeight = scaled.height; + const std::uint_fast32_t dstWidth = scaled.width; + const std::uint_fast32_t dstHeight = scaled.height; - const double rotatedWidth = scaled.width * (std::abs(std::cos(rad)) + std::abs(std::sin(rad))); + const double rotatedWidth = scaled.width * (std::abs(std::cos(rad)) + std::abs(std::sin(rad))); const double rotatedHeight = scaled.height * (std::abs(std::cos(rad)) + std::abs(std::sin(rad))); - scaled.width = static_cast(std::ceil(rotatedWidth)); - scaled.height = static_cast(std::ceil(rotatedHeight)); + const std::uint_fast32_t diffX = std::ceil((rotatedWidth - scaled.width)/2); + const std::uint_fast32_t diffY = std::ceil((rotatedHeight - scaled.height)/2); + scaled.width += diffX + diffX; + scaled.height += diffY + diffY; + + scaled.x -= diffX; + scaled.y -= diffY; + + bufferScaled.clear(); bufferScaled.resize(scaled.width * scaled.height); // Destination center @@ -87,20 +93,12 @@ void RenderingElementScalingRotating2D::UpdateScaledBuffer(ScaleData& scaled) { for (std::uint_fast32_t xB = 0; xB < scaled.width; ++xB) { // ---- Destination pixel relative to center ---- - double dx = static_cast(xB) - dstCx; - double dy = static_cast(yB) - dstCy; - - // ---- Inverse scale ---- - dx *= scaleX; - dy *= scaleY; + const double dx = (static_cast(xB) - dstCx) * scaleX; + const double dy = (static_cast(yB) - dstCy) * scaleY; // ---- Inverse rotation ---- - double sx = c * dx - s * dy; - double sy = s * dx + c * dy; - - // ---- Move into source space ---- - sx += srcCx; - sy += srcCy; + const double sx = (c * dx - s * dy) + srcCx; + const double sy = (s * dx + c * dy) + srcCy; // ---- Nearest neighbour sampling ---- const std::int_fast32_t srcX = static_cast(std::round(sx)); @@ -108,8 +106,6 @@ void RenderingElementScalingRotating2D::UpdateScaledBuffer(ScaleData& scaled) { if (srcX >= 0 && srcX < bufferWidth && srcY >= 0 && srcY < bufferHeight) { bufferScaled[yB * scaled.width + xB] = buffer[srcY * bufferWidth + srcX]; - } else { - bufferScaled[yB * scaled.width + xB] = {0, 0, 0, 0}; } } } @@ -128,7 +124,6 @@ void RenderingElementScalingRotating2D::UpdatePosition(Window& window) { UpdateScaledBuffer(scaled); window.AddDirtyRect(oldScale); window.AddDirtyRect(scaled); - std::cout << scaled.width << std::endl; rotationUpdated = false; } else if(oldScale.x != scaled.x || oldScale.y != scaled.y) { diff --git a/implementations/Crafter.Graphics-Window_wayland.cpp b/implementations/Crafter.Graphics-Window_wayland.cpp index 9c06fcd..88e59fd 100644 --- a/implementations/Crafter.Graphics-Window_wayland.cpp +++ b/implementations/Crafter.Graphics-Window_wayland.cpp @@ -302,9 +302,9 @@ void WindowWayland::Render() { dirtyRects = std::move(newClip); - for(uint_fast32_t i = 0; i < width*height; i++) { - framebuffer[i] = {0, 0, 0, 255}; - } + // for(uint_fast32_t i = 0; i < width*height; i++) { + // framebuffer[i] = {0, 0, 0, 255}; + // } // std::cout << dirtyRects.size() << std::endl; // for (ClipRect rect : dirtyRects) { diff --git a/interfaces/Crafter.Graphics-RenderingElement.cppm b/interfaces/Crafter.Graphics-RenderingElement.cppm index 66a5672..10b30ed 100644 --- a/interfaces/Crafter.Graphics-RenderingElement.cppm +++ b/interfaces/Crafter.Graphics-RenderingElement.cppm @@ -81,7 +81,7 @@ export namespace Crafter { RenderingElementScalingRotating2D(OpaqueType opaque = OpaqueType::Transparent); RenderingElementScalingRotating2D(OpaqueType opaque, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling); - RenderingElementScalingRotating2D(OpaqueType opaque, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::uint_fast32_t rotationAngle = 0, double scaleX = 1.0, double scaleY = 1.0, std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); + RenderingElementScalingRotating2D(OpaqueType opaque, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::uint_fast32_t rotationAngle = 0, std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); RenderingElementScalingRotating2D(RenderingElementScalingRotating2D&) = delete; RenderingElementScalingRotating2D& operator=(RenderingElementScalingRotating2D&) = delete;