This commit is contained in:
Jorijn van der Graaf 2025-12-28 00:47:27 +01:00
commit 8489c57d40
8 changed files with 135 additions and 14 deletions

View file

@ -0,0 +1,34 @@
/*
Crafter®.Graphics
Copyright (C) 2025 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
export module Crafter.Graphics:Image;
import std;
import :Types;
namespace Crafter {
export struct Image {
std::vector<Pixel_BU8_GU8_RU8_AU8> buffer;
std::uint_fast32_t width;
std::uint_fast32_t height;
OpaqueType opaque;
Image(const std::string_view path);
};
}

View file

@ -38,6 +38,7 @@ export namespace Crafter {
Event<MousePoint> onMouseRightRelease;
Event<MousePoint> onMouseLeftRelease;
MouseElement(WindowMouse& window);
MouseElement(Anchor anchor);
MouseElement(Anchor anchor, WindowMouse& window);
void UpdatePosition(Window& window) override;

View file

@ -24,16 +24,10 @@ export module Crafter.Graphics:RenderingElement;
import std;
import :Transform;
import :Types;
import :Image;
import :Window;
export namespace Crafter {
enum class OpaqueType {
FullyOpaque, // All pixels have A of 255
SemiOpaque, // All pixels have A of 0 or 255 (no blending needed)
Transparent // Color blending is used
};
struct RenderElementScalingOwning {
std::vector<Pixel_BU8_GU8_RU8_AU8> scalingBuffer;
std::uint_fast32_t bufferWidth;
@ -91,6 +85,9 @@ export namespace Crafter {
public:
std::vector<Pixel_BU8_GU8_RU8_AU8> buffer;
OpaqueType opaque;
RenderingElementBase(Anchor anchor) : Transform(anchor) {
scaled.width = 0;
}
RenderingElementBase(Anchor anchor, OpaqueType opaque) : Transform(anchor), opaque(opaque) {
scaled.width = 0;
}
@ -107,10 +104,10 @@ export namespace Crafter {
}
RenderingElement(Anchor anchor, const std::string_view imagePath) : Transform(anchor) {
RenderingElement(Anchor anchor, const std::string_view imagePath) : RenderingElementBase(anchor) {
LoadImage(imagePath);
}
RenderingElement(Anchor anchor, const std::string_view imagePath, std::uint_fast32_t rotation) requires(Rotating) : Transform(anchor), RotatingBase<Rotating>(rotation) {
RenderingElement(Anchor anchor, const std::string_view imagePath, std::uint_fast32_t rotation) requires(Rotating) : RenderingElementBase(anchor), RotatingBase<Rotating>(rotation) {
LoadImage(imagePath);
}
@ -128,6 +125,20 @@ export namespace Crafter {
}
RenderingElement(Anchor anchor, OpaqueType opaque, Image& image) requires(Scaling && !Owning) : RenderingElementBase(anchor, opaque), ScalingBase<Scaling, Owning>(image.buffer.data(), image.width, image.height) {
}
RenderingElement(Anchor anchor, OpaqueType opaque, Image& image, std::uint_fast32_t rotation) requires(Scaling && !Owning && Rotating) : RenderingElementBase(anchor, opaque), ScalingBase<Scaling, Owning>(image.buffer.data(), image.width, image.height), RotatingBase<Rotating>(rotation) {
}
RenderingElement(Anchor anchor, Image& image) requires(Scaling && !Owning) : RenderingElementBase(anchor, image.opaque), ScalingBase<Scaling, Owning>(image.buffer.data(), image.width, image.height) {
}
RenderingElement(Anchor anchor, Image& image, std::uint_fast32_t rotation) requires(Scaling && !Owning && Rotating) : RenderingElementBase(anchor, image.opaque), ScalingBase<Scaling, Owning>(image.buffer.data(), image.width, image.height), RotatingBase<Rotating>(rotation) {
}
RenderingElement(Anchor anchor, OpaqueType opaque, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight) requires(Owning) : RenderingElementBase(anchor, opaque), ScalingBase<Scaling, Owning>(bufferWidth, bufferHeight) {
}
@ -169,8 +180,8 @@ export namespace Crafter {
scaled.x -= diffX;
scaled.y -= diffY;
ScalingBase<true, Owning>::scalingBuffer.clear();
ScalingBase<true, Owning>::scalingBuffer.resize(scaled.width * scaled.height);
buffer.clear();
buffer.resize(scaled.width * scaled.height);
// Destination center
const double dstCx = (static_cast<double>(scaled.width) - 1.0) * 0.5;
@ -289,6 +300,7 @@ export namespace Crafter {
ScalingBase<true, true>::bufferWidth = xSize;
ScalingBase<true, true>::bufferHeight = ySize;
ScalingBase<true, true>::bufferUpdated = true;
ScalingBase<true, Owning>::scalingBuffer.resize(xSize*ySize);
} else {
buffer.resize(xSize*ySize);
}
@ -302,7 +314,7 @@ export namespace Crafter {
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].r = bgData[idx];
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].g = bgData[idx+1];
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].b = bgData[idx+2];
ScalingBase<true, Owning>::calingBuffer[x*ySize+y].a = bgData[idx+3];
ScalingBase<true, Owning>::scalingBuffer[x*ySize+y].a = bgData[idx+3];
}
}
@ -356,6 +368,7 @@ export namespace Crafter {
ScalingBase<true, true>::bufferWidth = xSize;
ScalingBase<true, true>::bufferHeight = ySize;
ScalingBase<true, true>::bufferUpdated = true;
ScalingBase<true, Owning>::scalingBuffer.resize(xSize*ySize);
} else {
buffer.resize(xSize*ySize);
}

View file

@ -107,6 +107,11 @@ namespace Crafter {
std::chrono::duration<double> delta;
};
export enum class OpaqueType {
FullyOpaque, // All pixels have A of 255
SemiOpaque, // All pixels have A of 0 or 255 (no blending needed)
Transparent // Color blending is used
};
export constexpr std::int_fast32_t BOUND = 9;
export constexpr std::int_fast32_t SCALE = std::numeric_limits<std::int_fast32_t>::max() / BOUND;

View file

@ -28,6 +28,7 @@ export import :TextElement;
export import :GridElement;
export import :Types;
export import :Font;
export import :Image;
export import :Shm;
export import :Animation;