1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 19:18:11 +00:00

Start separating out the SDL-related stuff in Widgets.

This commit is contained in:
Andreas Kling 2019-01-10 05:21:19 +01:00
parent 077f1007eb
commit 3e908abfca
9 changed files with 54 additions and 34 deletions

View file

@ -1,9 +1,13 @@
#include "Color.h" #include "Color.h"
#include "FrameBufferSDL.h" #include "FrameBuffer.h"
Color::Color(byte r, byte g, byte b) Color::Color(byte r, byte g, byte b)
{ {
m_value = SDL_MapRGB(FrameBufferSDL::the().surface()->format, r, g, b); #ifdef USE_SDL
m_value = SDL_MapRGB(FrameBuffer::the().surface()->format, r, g, b);
#else
#error FIXME: Implement
#endif
} }
Color::Color(NamedColor named) Color::Color(NamedColor named)
@ -26,6 +30,9 @@ Color::Color(NamedColor named)
default: ASSERT_NOT_REACHED(); break; default: ASSERT_NOT_REACHED(); break;
} }
m_value = SDL_MapRGB(FrameBufferSDL::the().surface()->format, rgb.r, rgb.g, rgb.g); #ifdef USE_SDL
m_value = SDL_MapRGB(FrameBuffer::the().surface()->format, rgb.r, rgb.g, rgb.g);
#else
#error FIXME: Implement
#endif
} }

View file

@ -1,37 +1,41 @@
#include "FrameBufferSDL.h" #include "FrameBuffer.h"
#include "GraphicsBitmap.h" #include "GraphicsBitmap.h"
#include <AK/Assertions.h> #include <AK/Assertions.h>
FrameBufferSDL* s_the = nullptr; FrameBuffer* s_the = nullptr;
FrameBufferSDL& FrameBufferSDL::the() FrameBuffer& FrameBuffer::the()
{ {
ASSERT(s_the); ASSERT(s_the);
return *s_the; return *s_the;
} }
FrameBufferSDL::FrameBufferSDL(unsigned width, unsigned height) FrameBuffer::FrameBuffer(unsigned width, unsigned height)
: AbstractScreen(width, height) : AbstractScreen(width, height)
{ {
ASSERT(!s_the); ASSERT(!s_the);
s_the = this; s_the = this;
#ifdef USE_SDL
initializeSDL(); initializeSDL();
#endif
} }
FrameBufferSDL::~FrameBufferSDL() FrameBuffer::~FrameBuffer()
{ {
#ifdef USE_SDL
SDL_DestroyWindow(m_window); SDL_DestroyWindow(m_window);
m_surface = nullptr; m_surface = nullptr;
m_window = nullptr; m_window = nullptr;
SDL_Quit(); SDL_Quit();
#endif
} }
void FrameBufferSDL::show() void FrameBuffer::show()
{ {
} }
void FrameBufferSDL::initializeSDL() #ifdef USE_SDL
void FrameBuffer::initializeSDL()
{ {
if (m_window) if (m_window)
return; return;
@ -57,13 +61,16 @@ void FrameBufferSDL::initializeSDL()
SDL_UpdateWindowSurface(m_window); SDL_UpdateWindowSurface(m_window);
} }
#endif
dword* FrameBufferSDL::scanline(int y) dword* FrameBuffer::scanline(int y)
{ {
#ifdef USE_SDL
return (dword*)(((byte*)m_surface->pixels) + (y * m_surface->pitch)); return (dword*)(((byte*)m_surface->pixels) + (y * m_surface->pitch));
#endif
} }
void FrameBufferSDL::blit(const Point& position, GraphicsBitmap& bitmap) void FrameBuffer::blit(const Point& position, GraphicsBitmap& bitmap)
{ {
Rect dst_rect(position, bitmap.size()); 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()); //printf("blit at %d,%d %dx%d\n", dst_rect.x(), dst_rect.y(), dst_rect.width(), dst_rect.height());
@ -77,7 +84,9 @@ void FrameBufferSDL::blit(const Point& position, GraphicsBitmap& bitmap)
} }
} }
void FrameBufferSDL::flush() void FrameBuffer::flush()
{ {
#ifdef USE_SDL
SDL_UpdateWindowSurface(m_window); SDL_UpdateWindowSurface(m_window);
#endif
} }

View file

@ -1,21 +1,25 @@
#pragma once #pragma once
#include "AbstractScreen.h" #include "AbstractScreen.h"
#ifdef USE_SDL
#include <SDL.h> #include <SDL.h>
#endif
class GraphicsBitmap; class GraphicsBitmap;
class FrameBufferSDL final : public AbstractScreen { class FrameBuffer final : public AbstractScreen {
public: public:
FrameBufferSDL(unsigned width, unsigned height); FrameBuffer(unsigned width, unsigned height);
virtual ~FrameBufferSDL() override; virtual ~FrameBuffer() override;
void show(); void show();
#ifdef USE_SDL
SDL_Surface* surface() { return m_surface; } SDL_Surface* surface() { return m_surface; }
SDL_Window* window() { return m_window; } #endif
static FrameBufferSDL& the(); static FrameBuffer& the();
dword* scanline(int y); dword* scanline(int y);
@ -23,9 +27,10 @@ public:
void flush(); void flush();
private: private:
#ifdef USE_SDL
void initializeSDL(); void initializeSDL();
SDL_Window* m_window { nullptr }; SDL_Window* m_window { nullptr };
SDL_Surface* m_surface { nullptr }; SDL_Surface* m_surface { nullptr };
#endif
}; };

View file

@ -8,7 +8,7 @@ AK_OBJS = \
VFS_OBJS = \ VFS_OBJS = \
AbstractScreen.o \ AbstractScreen.o \
FrameBufferSDL.o \ FrameBuffer.o \
EventLoop.o \ EventLoop.o \
EventLoopSDL.o \ EventLoopSDL.o \
Rect.o \ Rect.o \

View file

@ -72,7 +72,7 @@ void Object::startTimer(int ms)
printf("Object{%p} already has a timer!\n", this); printf("Object{%p} already has a timer!\n", this);
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
#if USE_SDL #ifdef USE_SDL
m_timerID = SDL_AddTimer(ms, sdlTimerCallback, this); m_timerID = SDL_AddTimer(ms, sdlTimerCallback, this);
#endif #endif
} }

View file

@ -1,11 +1,10 @@
#include "Painter.h" #include "Painter.h"
#include "FrameBufferSDL.h" #include "FrameBuffer.h"
#include "Widget.h" #include "Widget.h"
#include "Font.h" #include "Font.h"
#include "Window.h" #include "Window.h"
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/StdLibExtras.h> #include <AK/StdLibExtras.h>
#include <SDL.h>
Painter::Painter(Widget& widget) Painter::Painter(Widget& widget)
: m_widget(widget) : m_widget(widget)

View file

@ -3,13 +3,13 @@
#include "RootWidget.h" #include "RootWidget.h"
#include "Painter.h" #include "Painter.h"
#include "WindowManager.h" #include "WindowManager.h"
#include "FrameBufferSDL.h" #include "FrameBuffer.h"
#include <cstdio> #include <cstdio>
RootWidget::RootWidget() RootWidget::RootWidget()
{ {
setWindowRelativeRect(FrameBufferSDL::the().rect()); setWindowRelativeRect(FrameBuffer::the().rect());
m_backing = GraphicsBitmap::create_wrapper(size(), (byte*)FrameBufferSDL::the().scanline(0)); m_backing = GraphicsBitmap::create_wrapper(size(), (byte*)FrameBuffer::the().scanline(0));
} }
RootWidget::~RootWidget() RootWidget::~RootWidget()

View file

@ -4,7 +4,7 @@
#include "Window.h" #include "Window.h"
#include "AbstractScreen.h" #include "AbstractScreen.h"
#include "EventLoop.h" #include "EventLoop.h"
#include "FrameBufferSDL.h" #include "FrameBuffer.h"
static const int windowFrameWidth = 2; static const int windowFrameWidth = 2;
static const int windowTitleBarHeight = 16; static const int windowTitleBarHeight = 16;
@ -144,7 +144,7 @@ void WindowManager::repaint()
void WindowManager::did_paint(Window& window) void WindowManager::did_paint(Window& window)
{ {
auto& framebuffer = FrameBufferSDL::the(); auto& framebuffer = FrameBuffer::the();
if (m_windows_in_order.tail() == &window) { if (m_windows_in_order.tail() == &window) {
ASSERT(window.backing()); ASSERT(window.backing());
framebuffer.blit(window.position(), *window.backing()); framebuffer.blit(window.position(), *window.backing());
@ -214,7 +214,7 @@ void WindowManager::processMouseEvent(MouseEvent& event)
pos.moveBy(event.x() - m_dragOrigin.x(), event.y() - m_dragOrigin.y()); pos.moveBy(event.x() - m_dragOrigin.x(), event.y() - m_dragOrigin.y());
m_dragWindow->setPositionWithoutRepaint(pos); m_dragWindow->setPositionWithoutRepaint(pos);
paintWindowFrame(*m_dragWindow); paintWindowFrame(*m_dragWindow);
FrameBufferSDL::the().flush(); FrameBuffer::the().flush();
return; return;
} }
} }
@ -261,7 +261,7 @@ void WindowManager::handlePaintEvent(PaintEvent& event)
void WindowManager::recompose() void WindowManager::recompose()
{ {
printf("[WM] recompose_count: %u\n", ++m_recompose_count); printf("[WM] recompose_count: %u\n", ++m_recompose_count);
auto& framebuffer = FrameBufferSDL::the(); auto& framebuffer = FrameBuffer::the();
PaintEvent dummy_event(m_rootWidget->rect()); PaintEvent dummy_event(m_rootWidget->rect());
m_rootWidget->paintEvent(dummy_event); m_rootWidget->paintEvent(dummy_event);
for (auto* window = m_windows_in_order.head(); window; window = window->next()) { for (auto* window = m_windows_in_order.head(); window; window = window->next()) {

View file

@ -1,4 +1,4 @@
#include "FrameBufferSDL.h" #include "FrameBuffer.h"
#include "EventLoopSDL.h" #include "EventLoopSDL.h"
#include "RootWidget.h" #include "RootWidget.h"
#include "Label.h" #include "Label.h"
@ -14,7 +14,7 @@
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
FrameBufferSDL fb(800, 600); FrameBuffer fb(800, 600);
fb.show(); fb.show();
EventLoopSDL loop; EventLoopSDL loop;