mirror of
https://github.com/RGBCube/serenity
synced 2025-05-27 23:45:07 +00:00
Start separating out the SDL-related stuff in Widgets.
This commit is contained in:
parent
077f1007eb
commit
3e908abfca
9 changed files with 54 additions and 34 deletions
92
Widgets/FrameBuffer.cpp
Normal file
92
Widgets/FrameBuffer.cpp
Normal file
|
@ -0,0 +1,92 @@
|
|||
#include "FrameBuffer.h"
|
||||
#include "GraphicsBitmap.h"
|
||||
#include <AK/Assertions.h>
|
||||
|
||||
FrameBuffer* 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()
|
||||
{
|
||||
#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
|
||||
|
||||
dword* FrameBuffer::scanline(int y)
|
||||
{
|
||||
#ifdef USE_SDL
|
||||
return (dword*)(((byte*)m_surface->pixels) + (y * m_surface->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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue