Skip to content

bindings/cpp/framebuffer_window.hpp

C++98 API source. Preserve the bindings/cpp/ layout when downloading multiple files.

Source files · Download framebuffer_window.hpp

#ifndef GASPARDOS_FRAMEBUFFER_WINDOW_HPP
#define GASPARDOS_FRAMEBUFFER_WINDOW_HPP
#include "framebuffer.hpp"
#include "gaspardos.hpp"

namespace gaspardos
{
    class FramebufferPresenter
    {
        Window&            window_;
        const Framebuffer* source_;
        const uint32_t*    pixels_;
        unsigned           width_, height_, column_, row_;
        int                x_, y_;
        FramebufferPresenter(const FramebufferPresenter&);
        FramebufferPresenter& operator=(const FramebufferPresenter&);

    public:
        explicit FramebufferPresenter(Window& window)
            : window_(window)
            , source_(0)
            , pixels_(0)
            , width_(0)
            , height_(0)
            , column_(0)
            , row_(0)
            , x_(0)
            , y_(0)
        {
        }

        bool pending() const
        {
            return source_ != 0;
        }

        void cancel()
        {
            source_ = 0;
            pixels_ = 0;
            column_ = row_ = 0;
        }

        // Keep source pixels unchanged while this returns -11. Accepted tiles
        // have already been copied. Completion means queued, not VSync.
        int64_t present(const Framebuffer& source, int x = 0, int y = 0)
        {
            if (!source.width() || !source.height() || x < -4096 || y < -4096
                || x > 4096 - static_cast<int>(source.width())
                || y > 4096 - static_cast<int>(source.height()))
                return -22;
            if (source_
                && (source_ != &source || pixels_ != source.pixels() || width_ != source.width()
                    || height_ != source.height() || x != x_ || y != y_))
                return -16;
            if (!source_)
            {
                source_ = &source;
                pixels_ = source.pixels();
                width_  = source.width();
                height_ = source.height();
                x_      = x;
                y_      = y;
            }
            while (row_ < height_)
            {
                GO64_Blit tile;
                tile.x         = x_ + static_cast<int>(column_);
                tile.y         = y_ + static_cast<int>(row_);
                tile.width     = std::min<unsigned>(GO64_BLIT_TILE, width_ - column_);
                tile.height    = std::min<unsigned>(GO64_BLIT_TILE, height_ - row_);
                tile.stride    = width_;
                tile.format    = GO64_BLIT_RGBX8888;
                tile.pixels    = pixels_ + static_cast<std::size_t>(row_) * width_ + column_;
                int64_t result = window_.blit(tile);
                if (result < 0)
                {
                    if (result != -11)
                        cancel();
                    return result;
                }
                column_ += tile.width;
                if (column_ == width_)
                {
                    column_ = 0;
                    row_ += tile.height;
                }
            }
            cancel();
            return 0;
        }
    };
}  // namespace gaspardos
#endif