summaryrefslogtreecommitdiff
path: root/Widgets/FrameBuffer.cpp
blob: 89e2a570901f39850e6eea029eddf2b2e9764da5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "FrameBuffer.h"
#include "GraphicsBitmap.h"
#include <AK/Assertions.h>

FrameBuffer* s_the;

void FrameBuffer::initialize()
{
    s_the = nullptr;
}

FrameBuffer& FrameBuffer::the()
{
    ASSERT(s_the);
    return *s_the;
}

FrameBuffer::FrameBuffer(unsigned width, unsigned height)
    : AbstractScreen(width, height)
{
    ASSERT(!s_the);
    s_the = this;
#ifdef USE_SDL
    initializeSDL();
#endif
}

FrameBuffer::FrameBuffer(RGBA32* data, unsigned width, unsigned height)
    : AbstractScreen(width, height)
#ifdef SERENITY
    , m_data(data)
#endif
{
    ASSERT(!s_the);
    s_the = this;
}


FrameBuffer::~FrameBuffer()
{
#ifdef USE_SDL
    SDL_DestroyWindow(m_window);
    m_surface = nullptr;
    m_window = nullptr;
    SDL_Quit();
#endif
}

void FrameBuffer::show()
{
}

#ifdef USE_SDL
void FrameBuffer::initializeSDL()
{
    if (m_window)
        return;

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        ASSERT_NOT_REACHED();
    }

    m_window = SDL_CreateWindow(
        "FrameBuffer",
        SDL_WINDOWPOS_UNDEFINED, 
        SDL_WINDOWPOS_UNDEFINED, 
        width(),
        height(),
        SDL_WINDOW_SHOWN);

    ASSERT(m_window);

    m_surface = SDL_GetWindowSurface(m_window);
    ASSERT(m_surface);

    SDL_FillRect(m_surface, nullptr, SDL_MapRGB(m_surface->format, 0xff, 0xff, 0xff));

    SDL_UpdateWindowSurface(m_window);
}
#endif

RGBA32* FrameBuffer::scanline(int y)
{
#ifdef USE_SDL
    return reinterpret_cast<RGBA32*>(((byte*)m_surface->pixels) + (y * m_surface->pitch));
#endif
#ifdef SERENITY
    unsigned pitch = sizeof(RGBA32) * width();
    return reinterpret_cast<RGBA32*>(((byte*)m_data) + (y * pitch));
#endif
}

void FrameBuffer::blit(const Point& position, GraphicsBitmap& bitmap)
{
    Rect dst_rect(position, bitmap.size());
    //printf("blit at %d,%d %dx%d\n", dst_rect.x(), dst_rect.y(), dst_rect.width(), dst_rect.height());
    dst_rect.intersect(rect());
    //printf("    -> intersection %d,%d %dx%d\n", dst_rect.x(), dst_rect.y(), dst_rect.width(), dst_rect.height());

    for (int y = 0; y < dst_rect.height(); ++y) {
        auto* framebuffer_scanline = scanline(position.y() + y);
        auto* bitmap_scanline = bitmap.scanline(y);
        memcpy(framebuffer_scanline + dst_rect.x(), bitmap_scanline + (dst_rect.x() - position.x()), dst_rect.width() * 4);
    }
}

void FrameBuffer::flush()
{
#ifdef USE_SDL
    SDL_UpdateWindowSurface(m_window);
#endif
}