1 #include "video_memory_holder.h" 2 #include <whycpp/color.h> 3 #include <whycpp/types.h> 5 #include "../int_utils.h" 18 uint32_t FlattenRGBA(
const RGBA& rgba) {
22 VideoMemoryHolder::VideoMemoryHolder(i32 width, i32 height)
23 : width(width), height(height), buffer(std::make_unique<uint32_t[]>(AsSize(width * height))) {
24 LOG_DEBUG(
"VideoMemoryHolder [%d, %d] created", width, height);
26 VideoMemoryHolder::~VideoMemoryHolder() {
27 LOG_DEBUG(
"VideoMemoryHolder [%d, %d] destroyed", width, height);
29 void VideoMemoryHolder::Set(i32 x, i32 y,
const RGBA& color) {
30 auto index = CheckY(y) * AsSize(width) + CheckX(x);
33 const RGBA VideoMemoryHolder::Get(i32 x, i32 y)
const {
34 auto index = CheckY(y) * AsSize(width) + CheckX(x);
37 i32 VideoMemoryHolder::GetHeight()
const {
40 i32 VideoMemoryHolder::GetWidth()
const {
43 size_t VideoMemoryHolder::CheckX(i32 x)
const {
44 if (x >= width || x < 0) {
45 LOG_VERBOSE(
"VideoMemoryHolder: X is out of bound, should be [0, %d) but %d", width, x);
46 return AsSize(clamp(x, 0, width - 1));
50 size_t VideoMemoryHolder::CheckY(i32 y)
const {
51 if (y >= height || y < 0) {
52 LOG_VERBOSE(
"VideoMemoryHolder: Y is out of bound, should be [0, %d) but %d", height, y);
53 return AsSize(clamp(y, 0, height - 1));
57 i32 VideoMemoryHolder::GetScreenWidth()
const {
60 void VideoMemoryHolder::SetScreenWidth(i32 screen_width_) {
61 if (screen_width_ < 1)
return;
62 screen_width = screen_width_;
64 i32 VideoMemoryHolder::GetScreenHeight()
const {
67 void VideoMemoryHolder::SetScreenHeight(i32 screen_height_) {
68 if (screen_height_ < 1)
return;
69 screen_height = screen_height_;
71 const uint8_t* VideoMemoryHolder::GetBuffer()
const {
72 return reinterpret_cast<uint8_t*
>(buffer.get());
75 void VideoMemoryHolder::Fill(
const RGBA& color) {
76 auto len = AsSize(width * height);
78 uint64_t col2 = (col << 32u) | col;
79 auto buf =
reinterpret_cast<uint64_t*
>(buffer.get());
80 for (
size_t i = 0; i < len / 2; i++) {