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()) {
|
2018-10-12 10:06:50 +02:00
|
|
|
m_translation = window->position();
|
2018-10-12 14:15:14 +02:00
|
|
|
m_translation.moveBy(widget.relativePosition());
|
2018-10-12 02:41:27 +02:00
|
|
|
} 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-12 14:15:14 +02:00
|
|
|
void Painter::drawBitmap(const Point& p, const CBitmap& bitmap, Color color)
|
2018-10-12 12:29:58 +02:00
|
|
|
{
|
2018-10-12 14:15:14 +02:00
|
|
|
Point point = p;
|
|
|
|
point.moveBy(m_translation);
|
2018-10-12 12:49:45 +02:00
|
|
|
for (unsigned row = 0; row < bitmap.height(); ++row) {
|
2018-10-12 12:29:58 +02:00
|
|
|
int y = point.y() + row;
|
|
|
|
int x = point.x();
|
|
|
|
dword* bits = scanline(y);
|
2018-10-12 12:49:45 +02:00
|
|
|
for (unsigned j = 0; j < bitmap.width(); ++j) {
|
|
|
|
char fc = bitmap.bits()[row * bitmap.width() + j];
|
2018-10-12 12:29:58 +02:00
|
|
|
if (fc == '#')
|
|
|
|
bits[x + j] = color.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Painter::drawText(const Rect& rect, const String& text, TextAlignment alignment, 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();
|
|
|
|
} 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-12 16:57:43 +02:00
|
|
|
point.moveBy(-(textWidth / 2), -(m_font.glyphHeight() / 2));
|
2018-10-11 01:48:09 +02:00
|
|
|
} else {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2018-10-10 20:06:58 +02:00
|
|
|
|
2018-10-12 12:29:58 +02:00
|
|
|
for (unsigned i = 0; i < text.length(); ++i) {
|
|
|
|
byte ch = text[i];
|
|
|
|
if (ch == ' ')
|
|
|
|
continue;
|
2018-10-12 12:49:45 +02:00
|
|
|
auto* bitmap = m_font.glyphBitmap(ch);
|
|
|
|
if (!bitmap) {
|
2018-10-12 12:29:58 +02:00
|
|
|
printf("Font doesn't have 0x%02x ('%c')\n", ch, ch);
|
|
|
|
ASSERT_NOT_REACHED();
|
2018-10-10 20:06:58 +02:00
|
|
|
}
|
2018-10-12 12:29:58 +02:00
|
|
|
int x = point.x() + i * m_font.glyphWidth();
|
|
|
|
int y = point.y();
|
2018-10-12 12:49:45 +02:00
|
|
|
drawBitmap({ x, y }, *bitmap, color);
|
2018-10-10 20:06:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 14:58:16 +02:00
|
|
|
void Painter::drawPixel(const Point& p, Color color)
|
|
|
|
{
|
|
|
|
auto point = p;
|
|
|
|
point.moveBy(m_translation);
|
|
|
|
scanline(point.y())[point.x()] = color.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Painter::drawLine(const Point& p1, const Point& p2, Color color)
|
|
|
|
{
|
|
|
|
auto point1 = p1;
|
|
|
|
point1.moveBy(m_translation);
|
|
|
|
|
|
|
|
auto point2 = p2;
|
|
|
|
point2.moveBy(m_translation);
|
|
|
|
|
|
|
|
// Special case: vertical line.
|
|
|
|
if (point1.x() == point2.x()) {
|
|
|
|
if (point1.y() > point2.y())
|
|
|
|
std::swap(point1, point2);
|
|
|
|
for (int y = point1.y(); y <= point2.y(); ++y)
|
|
|
|
scanline(y)[point1.x()] = color.value();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (point1.x() > point2.x())
|
|
|
|
std::swap(point1, point2);
|
|
|
|
|
|
|
|
// Special case: horizontal line.
|
|
|
|
if (point1.y() == point2.y()) {
|
|
|
|
if (point1.y() > point2.y())
|
|
|
|
std::swap(point1, point2);
|
|
|
|
auto* pixels = scanline(point1.y());
|
|
|
|
for (int x = point1.x(); x <= point2.x(); ++x)
|
|
|
|
pixels[x] = color.value();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const double dx = point2.x() - point1.x();
|
|
|
|
const double dy = point2.y() - point1.y();
|
|
|
|
const double deltaError = fabs(dy / dx);
|
|
|
|
double error = 0;
|
|
|
|
const double yStep = dy == 0 ? 0 : (dy > 0 ? 1 : -1);
|
|
|
|
|
|
|
|
int y = point1.y();
|
|
|
|
for (int x = point1.x(); x <= point2.x(); ++x) {
|
|
|
|
scanline(y)[x] = color.value();
|
|
|
|
error += deltaError;
|
|
|
|
if (error >= 0.5) {
|
|
|
|
y = (double)y + yStep;
|
|
|
|
error -= 1.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|