serenity/Widgets/Painter.cpp

63 lines
1.5 KiB
C++
Raw Normal View History

2018-10-10 16:49:36 +02:00
#include "Painter.h"
#include "FrameBufferSDL.h"
#include "Widget.h"
#include <AK/Assertions.h>
#include <SDL.h>
#include "Peanut8x8.h"
2018-10-10 16:49:36 +02:00
Painter::Painter(Widget& widget)
: m_widget(widget)
{
}
Painter::~Painter()
{
int rc = SDL_UpdateWindowSurface(FrameBufferSDL::the().window());
ASSERT(rc == 0);
}
static dword* scanline(int y)
2018-10-10 16:49:36 +02:00
{
auto& surface = *FrameBufferSDL::the().surface();
return (dword*)(((byte*)surface.pixels) + (y * surface.pitch));
}
2018-10-10 16:49:36 +02:00
void Painter::fillRect(const Rect& rect, Color color)
{
Rect r = rect;
r.moveBy(m_widget.x(), m_widget.y());
2018-10-10 16:49:36 +02:00
for (int y = r.top(); y < r.bottom(); ++y) {
dword* bits = scanline(y);
for (int x = r.left(); x < r.right(); ++x) {
bits[x] = color.value();
}
}
2018-10-10 16:49:36 +02:00
}
void Painter::drawText(const Point& point, const String& text, const Color& color)
{
Point p = point;
p.moveBy(m_widget.x(), m_widget.y());
byte fontWidth = 8;
2018-10-11 00:10:36 +02:00
byte fontHeight = 8;
for (int row = 0; row < fontHeight; ++row) {
int y = p.y() + row;
dword* bits = scanline(y);
for (unsigned i = 0; i < text.length(); ++i) {
2018-10-11 00:10:36 +02:00
if (text[i] == ' ')
continue;
const char* fontCharacter = Peanut8x8::font[text[i] - Peanut8x8::firstCharacter];
int x = p.x() + i * fontWidth;
for (unsigned j = 0; j < fontWidth; ++j) {
char fc = fontCharacter[row * fontWidth + j];
if (fc == '#')
bits[x + j] = color.value();
}
}
}
}