Files
cardboy/Firmware/sdk/library/include_public/Surface.hpp
2025-07-28 09:39:13 +02:00

76 lines
2.1 KiB
C++

//
// Created by Stepan Usatiuk on 26.07.2025.
//
#ifndef SURFACE_HPP
#define SURFACE_HPP
#include <memory>
#include <type_traits>
#include "Pixel.hpp"
#include "StandardEvents.hpp"
#include "Window.hpp"
#include "utils.hpp"
template<typename Derived, typename PixelType>
requires std::is_base_of_v<Pixel, PixelType>
class Surface : public StandardEventHandler<Derived> {
public:
Surface() = default;
void draw_pixel(unsigned x, unsigned y, const BwPixel& pixel) {
static_cast<Derived*>(this)->draw_pixel_impl(x, y, pixel);
}
void draw_rect(unsigned x, unsigned y, unsigned width, unsigned height, const BwPixel& pixel) {
for (unsigned i = 0; i < width; ++i) {
draw_pixel(x + i, y, pixel);
draw_pixel(x + i, y + height - 1, pixel);
}
for (unsigned i = 0; i < height; ++i) {
draw_pixel(x, y + i, pixel);
draw_pixel(x + width - 1, y + i, pixel);
}
}
void clear() { static_cast<Derived*>(this)->clear_impl(); }
int get_width() const { return static_cast<const Derived*>(this)->get_width_impl(); }
int get_height() const { return static_cast<const Derived*>(this)->get_height_impl(); }
template<typename T>
EventHandlingResult handle(const T& event) {
if (_window.get())
return _window->handle(event);
return EventHandlingResult::CONTINUE;
}
template<typename WindowType, typename... Args>
void set_window(Args&&... args) {
_window = std::make_unique<WindowType>(static_cast<Derived*>(this), std::forward<Args>(args)...);
_window->refresh();
}
Surface(const Surface& other) = delete;
Surface(Surface&& other) noexcept = delete;
Surface& operator=(const Surface& other) = delete;
Surface& operator=(Surface&& other) noexcept = delete;
bool has_window() const { return _window != nullptr; }
Window<Derived, PixelType>* get_window() {
assert(has_window());
return _window.get();
}
protected:
std::unique_ptr<Window<Derived, PixelType>> _window = nullptr;
};
#endif // SURFACE_HPP