86 lines
2.7 KiB
C++

#pragma once
#if !defined(BAD_APPLE_OS_DRAW_HPP_INCLUDED)
#define BAD_APPLE_OS_DRAW_HPP_INCLUDED
#include <stdint.h>
#include <array>
#include <vector>
namespace psf
{
class Font;
}
namespace draw
{
const unsigned SIZE_FULL = static_cast<unsigned>(-1);
struct Pixel
{
uint8_t blue = 0;
uint8_t green = 0;
uint8_t red = 0;
uint8_t reserved_ = 0;
};
inline Pixel PIXEL_WHITE = {255, 255, 255};
inline Pixel PIXEL_BLACK = {0, 0, 0};
extern const std::array<Pixel, 16> VGA_PIXELS;
class Framebuffer
{
protected:
Pixel* mBase = nullptr;
unsigned mWidth = 0;
unsigned mHeight = 0;
unsigned mPitch = 0;
public:
Framebuffer() = default;
Framebuffer(const Framebuffer&) = default;
Framebuffer(Pixel* base, unsigned width, unsigned height, unsigned pitch) noexcept
: mBase(base), mWidth(width), mHeight(height), mPitch(pitch) {}
[[nodiscard]] Pixel* getBase() const noexcept { return mBase; }
[[nodiscard]] unsigned getWidth() const noexcept { return mWidth; }
[[nodiscard]] unsigned getHeight() const noexcept { return mHeight; }
[[nodiscard]] unsigned getPitch() const noexcept { return mPitch; }
[[nodiscard]] unsigned getBufferSize() const noexcept { return mPitch * mHeight * sizeof(Pixel); }
const Pixel& getPixel(unsigned posX, unsigned posY) noexcept;
void setPixel(unsigned posX, unsigned posY, Pixel pixel) noexcept;
};
class OwningFramebuffer : public Framebuffer
{
private:
std::vector<Pixel> mPixels;
public:
OwningFramebuffer() = default;
OwningFramebuffer(const OwningFramebuffer&) noexcept = default;
OwningFramebuffer(OwningFramebuffer&&) noexcept = default;
OwningFramebuffer(unsigned width, unsigned height, unsigned pitch) noexcept;
OwningFramebuffer& operator=(const OwningFramebuffer&) noexcept = default;
OwningFramebuffer& operator=(OwningFramebuffer&&) noexcept = default;
static OwningFramebuffer makeFrom(const Framebuffer& other) noexcept
{
return OwningFramebuffer(other.getWidth(), other.getHeight(), other.getPitch());
}
};
void initializeDefaultFramebuffer(const Framebuffer& framebuffer) noexcept;
[[nodiscard]] const Framebuffer& getPrimaryFramebuffer() noexcept;
void setPixelDirect(unsigned posX, unsigned posY, Pixel pixel) noexcept;
void setPixel(unsigned posX, unsigned posY, Pixel pixel) noexcept;
const Pixel& getPixel(unsigned posX, unsigned posY) noexcept;
void character(unsigned posX, unsigned posY, const psf::Font& font, char chr, const Pixel& fgColor = PIXEL_WHITE, const Pixel& bgColor = PIXEL_BLACK) noexcept;
void scrollBy(int scrollX, int scrollY, const Pixel& fillColor = PIXEL_BLACK) noexcept;
}
#endif // !defined(BAD_APPLE_OS_FRAMEBUFFER_HPP_INCLUDED)