2018-10-10 16:49:36 +02:00
|
|
|
#include "Painter.h"
|
|
|
|
#include "FrameBufferSDL.h"
|
|
|
|
#include "Widget.h"
|
2018-10-11 23:45:57 +02:00
|
|
|
#include "Font.h"
|
2018-10-12 02:41:27 +02:00
|
|
|
#include "Window.h"
|
2018-10-10 16:49:36 +02:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <SDL.h>
|
2018-10-11 14:44:27 +02:00
|
|
|
|
2018-10-10 16:49:36 +02:00
|
|
|
Painter::Painter(Widget& widget)
|
|
|
|
: m_widget(widget)
|
2018-10-11 23:45:57 +02:00
|
|
|
, m_font(Font::defaultFont())
|
2018-10-10 16:49:36 +02:00
|
|
|
{
|
2018-10-12 02:41:27 +02:00
|
|
|
if (auto* window = widget.window()) {
|
|
|
|
printf("window :: %s\n", window->title().characters());
|
|
|
|
m_translation.setX(window->x());
|
|
|
|
m_translation.setY(window->y());
|
|
|
|
} else {
|
|
|
|
m_translation.setX(widget.x());
|
|
|
|
m_translation.setY(widget.y());
|
|
|
|
}
|
2018-10-10 16:49:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-10-12 02:41:27 +02:00
|
|
|
r.moveBy(m_translation);
|
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
|
|
|
|
2018-10-11 23:14:51 +02:00
|
|
|
void Painter::drawRect(const Rect& rect, Color color)
|
|
|
|
{
|
|
|
|
Rect r = rect;
|
2018-10-12 02:41:27 +02:00
|
|
|
r.moveBy(m_translation);
|
2018-10-11 23:14:51 +02:00
|
|
|
|
|
|
|
for (int y = r.top(); y < r.bottom(); ++y) {
|
|
|
|
dword* bits = scanline(y);
|
|
|
|
if (y == r.top() || y == (r.bottom() - 1)) {
|
|
|
|
for (int x = r.left(); x < r.right(); ++x) {
|
|
|
|
bits[x] = color.value();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bits[r.left()] = color.value();
|
|
|
|
bits[r.right() - 1] = color.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 02:24:05 +02:00
|
|
|
void Painter::xorRect(const Rect& rect, Color color)
|
|
|
|
{
|
|
|
|
Rect r = rect;
|
2018-10-12 02:41:27 +02:00
|
|
|
r.moveBy(m_translation);
|
2018-10-12 02:24:05 +02:00
|
|
|
|
|
|
|
for (int y = r.top(); y < r.bottom(); ++y) {
|
|
|
|
dword* bits = scanline(y);
|
|
|
|
if (y == r.top() || y == (r.bottom() - 1)) {
|
|
|
|
for (int x = r.left(); x < r.right(); ++x) {
|
|
|
|
bits[x] ^= color.value();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bits[r.left()] ^= color.value();
|
|
|
|
bits[r.right() - 1] ^= color.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-11 01:48:09 +02:00
|
|
|
void Painter::drawText(const Rect& rect, const String& text, TextAlignment alignment, const Color& color)
|
2018-10-10 20:06:58 +02:00
|
|
|
{
|
2018-10-11 01:48:09 +02:00
|
|
|
Point point;
|
|
|
|
|
|
|
|
if (alignment == TextAlignment::TopLeft) {
|
|
|
|
point = rect.location();
|
2018-10-12 02:41:27 +02:00
|
|
|
point.moveBy(m_translation);
|
2018-10-11 01:48:09 +02:00
|
|
|
} else if (alignment == TextAlignment::Center) {
|
2018-10-11 23:45:57 +02:00
|
|
|
int textWidth = text.length() * m_font.glyphWidth();
|
2018-10-11 01:48:09 +02:00
|
|
|
point = rect.center();
|
2018-10-11 23:45:57 +02:00
|
|
|
point.moveBy(-(textWidth / 2), -(m_font.glyphWidth() / 2));
|
2018-10-12 02:41:27 +02:00
|
|
|
point.moveBy(m_translation);
|
2018-10-11 01:48:09 +02:00
|
|
|
} else {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2018-10-10 20:06:58 +02:00
|
|
|
|
2018-10-11 23:45:57 +02:00
|
|
|
for (int row = 0; row < m_font.glyphHeight(); ++row) {
|
2018-10-11 01:48:09 +02:00
|
|
|
int y = point.y() + row;
|
2018-10-10 20:06:58 +02:00
|
|
|
dword* bits = scanline(y);
|
|
|
|
for (unsigned i = 0; i < text.length(); ++i) {
|
2018-10-11 12:33:03 +02:00
|
|
|
byte ch = text[i];
|
|
|
|
if (ch == ' ')
|
2018-10-11 00:10:36 +02:00
|
|
|
continue;
|
2018-10-11 23:45:57 +02:00
|
|
|
const char* glyph = m_font.glyph(ch);
|
2018-10-11 23:54:34 +02:00
|
|
|
if (!glyph) {
|
2018-10-11 12:33:03 +02:00
|
|
|
printf("Font doesn't have 0x%02x ('%c')\n", ch, ch);
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2018-10-11 23:45:57 +02:00
|
|
|
int x = point.x() + i * m_font.glyphWidth();
|
2018-10-11 23:54:34 +02:00
|
|
|
for (int j = 0; j < m_font.glyphWidth(); ++j) {
|
2018-10-11 23:45:57 +02:00
|
|
|
char fc = glyph[row * m_font.glyphWidth() + j];
|
2018-10-10 20:06:58 +02:00
|
|
|
if (fc == '#')
|
|
|
|
bits[x + j] = color.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|