add: RenderTarget
This commit is contained in:
@@ -8,6 +8,9 @@ set(src
|
|||||||
src/display/IDisplay.hpp
|
src/display/IDisplay.hpp
|
||||||
src/display/EPD_7in5_V2.hpp
|
src/display/EPD_7in5_V2.hpp
|
||||||
src/display/EPD_7in5_V2.cpp
|
src/display/EPD_7in5_V2.cpp
|
||||||
|
src/render/RenderTarget.hpp
|
||||||
|
src/render/RenderTarget.cpp
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(${target} ${src})
|
add_executable(${target} ${src})
|
||||||
|
|||||||
8
frame/src/Color.hpp
Normal file
8
frame/src/Color.hpp
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace frame
|
||||||
|
{
|
||||||
|
enum Color : uint32_t { WHITE = 0, BLACK = 0xFF };
|
||||||
|
}
|
||||||
@@ -9,4 +9,10 @@ namespace frame
|
|||||||
uint32_t width;
|
uint32_t width;
|
||||||
uint32_t height;
|
uint32_t height;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline Size operator/(Size const& size, int div)
|
||||||
|
{
|
||||||
|
return {size.width / div, size.height / div};
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace frame
|
} // namespace frame
|
||||||
|
|||||||
56
frame/src/Vector.hpp
Normal file
56
frame/src/Vector.hpp
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Size.hpp"
|
||||||
|
|
||||||
|
namespace frame
|
||||||
|
{
|
||||||
|
struct Vector
|
||||||
|
{
|
||||||
|
int32_t x;
|
||||||
|
int32_t y;
|
||||||
|
|
||||||
|
Vector() = default;
|
||||||
|
Vector(int32_t x, int32_t)
|
||||||
|
: x(x)
|
||||||
|
, y(y)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector(Size const& size)
|
||||||
|
: x(size.width)
|
||||||
|
, y(size.width)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator=(Size const& size)
|
||||||
|
{
|
||||||
|
x = size.width;
|
||||||
|
y = size.height;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
inline bool operator==(Vector const& a, Vector const& b)
|
||||||
|
{
|
||||||
|
return a.x == b.x && a.y == b.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool operator!=(Vector const& a, Vector const& b)
|
||||||
|
{
|
||||||
|
return !(a == b);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Vector& operator+=(Vector& a, Vector const& b)
|
||||||
|
{
|
||||||
|
a.x += b.x;
|
||||||
|
a.y += b.y;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Vector& operator-=(Vector& a, Vector const& b)
|
||||||
|
{
|
||||||
|
a.x -= b.x;
|
||||||
|
a.y -= b.y;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace frame
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
#include "display/EPD_7in5_V2.hpp"
|
#include "display/EPD_7in5_V2.hpp"
|
||||||
|
#include "render/RenderTarget.hpp"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -14,34 +15,22 @@ int main()
|
|||||||
|
|
||||||
display.Init();
|
display.Init();
|
||||||
|
|
||||||
display.Clear(frame::display::Color::BLACK);
|
|
||||||
|
|
||||||
std::this_thread::sleep_for(2s);
|
|
||||||
|
|
||||||
display.Clear(frame::display::Color::WHITE);
|
display.Clear(frame::display::Color::WHITE);
|
||||||
|
|
||||||
std::this_thread::sleep_for(2s);
|
frame::render::RenderTarget target(display.getSize());
|
||||||
|
|
||||||
frame::Image image(display.getSize());
|
frame::Vector const center = (display.getSize() / 2);
|
||||||
|
|
||||||
for(int y = 0; y < 1; ++y)
|
for(int i = 0; i < 360; i += 45)
|
||||||
{
|
{
|
||||||
for(int x = 0; x < image.getWidth(); ++x)
|
auto end = center;
|
||||||
{
|
end.x += (int)std::cos(i * 0.01745329);
|
||||||
image.at(x, y) = 0xFF;
|
end.y += (int)std::sin(i * 0.01745329);
|
||||||
}
|
|
||||||
|
target.DrawLine(center, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int y = image.getHeight() - 1; y < image.getHeight(); ++y)
|
display.Display(target.getImage());
|
||||||
{
|
|
||||||
for(int x = 0; x < image.getWidth(); ++x)
|
|
||||||
{
|
|
||||||
image.at(x, y) = 0xFF;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
display.Display(image);
|
|
||||||
|
|
||||||
std::this_thread::sleep_for(10s);
|
std::this_thread::sleep_for(10s);
|
||||||
|
|
||||||
|
|||||||
49
frame/src/render/RenderTarget.cpp
Normal file
49
frame/src/render/RenderTarget.cpp
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#include "RenderTarget.hpp"
|
||||||
|
|
||||||
|
namespace frame::render
|
||||||
|
{
|
||||||
|
|
||||||
|
RenderTarget::RenderTarget(Size size)
|
||||||
|
: image(size)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void RenderTarget::DrawPixel(Vector const& position, Color color)
|
||||||
|
{
|
||||||
|
image.at(position.x, position.y) = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RenderTarget::DrawLine(Vector const& start,
|
||||||
|
Vector const& end,
|
||||||
|
Color color)
|
||||||
|
{
|
||||||
|
auto const deltaX = std::abs(start.x - end.x);
|
||||||
|
auto const deltaY = std::abs(start.y - end.y);
|
||||||
|
|
||||||
|
auto const rX = start.x < end.x ? 1 : -1;
|
||||||
|
auto const rY = start.y < end.y ? 1 : -1;
|
||||||
|
|
||||||
|
auto pos = start;
|
||||||
|
auto E = 2 * deltaY - deltaX;
|
||||||
|
|
||||||
|
DrawPixel(pos, color);
|
||||||
|
|
||||||
|
while(pos != end)
|
||||||
|
{
|
||||||
|
if(E <= 0)
|
||||||
|
{
|
||||||
|
pos.x += rX;
|
||||||
|
|
||||||
|
E += 2 * deltaY;
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
pos.x += rX;
|
||||||
|
pos.y += rY;
|
||||||
|
|
||||||
|
E += 2 * deltaY - 2 * deltaX;
|
||||||
|
}
|
||||||
|
DrawPixel(pos, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace frame::render
|
||||||
29
frame/src/render/RenderTarget.hpp
Normal file
29
frame/src/render/RenderTarget.hpp
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../Color.hpp"
|
||||||
|
#include "../Image.hpp"
|
||||||
|
#include "../Vector.hpp"
|
||||||
|
|
||||||
|
namespace frame::render
|
||||||
|
{
|
||||||
|
|
||||||
|
class RenderTarget
|
||||||
|
{
|
||||||
|
Image image;
|
||||||
|
|
||||||
|
public:
|
||||||
|
RenderTarget(Size size);
|
||||||
|
|
||||||
|
void DrawPixel(Vector const& position, Color color);
|
||||||
|
|
||||||
|
void DrawLine(Vector const& start,
|
||||||
|
Vector const& end,
|
||||||
|
Color color = BLACK);
|
||||||
|
|
||||||
|
Image const& getImage()
|
||||||
|
{
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace frame::render
|
||||||
Reference in New Issue
Block a user