2018-10-10 16:49:36 +02:00
|
|
|
#include "Painter.h"
|
|
|
|
#include "FrameBufferSDL.h"
|
|
|
|
#include "Widget.h"
|
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <SDL.h>
|
2018-10-11 00:56:28 +02:00
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2018-10-10 20:06:58 +02:00
|
|
|
static dword* scanline(int y)
|
2018-10-10 16:49:36 +02:00
|
|
|
{
|
2018-10-10 20:06:58 +02:00
|
|
|
auto& surface = *FrameBufferSDL::the().surface();
|
|
|
|
return (dword*)(((byte*)surface.pixels) + (y * surface.pitch));
|
|
|
|
}
|
2018-10-10 16:49:36 +02:00
|
|
|
|
2018-10-10 20:06:58 +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
|
|
|
|
2018-10-10 20:06:58 +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
|
|
|
}
|
2018-10-10 20:06:58 +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;
|
2018-10-10 20:06:58 +02:00
|
|
|
|
|
|
|
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;
|
2018-10-11 00:56:28 +02:00
|
|
|
const char* fontCharacter = Peanut8x8::font[text[i] - Peanut8x8::firstCharacter];
|
2018-10-10 20:06:58 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|