2018-10-10 17:49:36 +03:00
|
|
|
#include "Painter.h"
|
|
|
|
#include "FrameBufferSDL.h"
|
|
|
|
#include "Widget.h"
|
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <SDL.h>
|
2018-10-11 15:44:27 +03:00
|
|
|
|
|
|
|
#if 0
|
2018-10-11 01:56:28 +03:00
|
|
|
#include "Peanut8x8.h"
|
2018-10-11 15:44:27 +03:00
|
|
|
#define FONT_NAMESPACE Peanut8x8
|
|
|
|
#else
|
|
|
|
#include "Peanut8x10.h"
|
|
|
|
#define FONT_NAMESPACE Peanut8x10
|
|
|
|
#endif
|
2018-10-10 17:49:36 +03:00
|
|
|
|
|
|
|
Painter::Painter(Widget& widget)
|
|
|
|
: m_widget(widget)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Painter::~Painter()
|
|
|
|
{
|
|
|
|
int rc = SDL_UpdateWindowSurface(FrameBufferSDL::the().window());
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
}
|
|
|
|
|
2018-10-10 21:06:58 +03:00
|
|
|
static dword* scanline(int y)
|
2018-10-10 17:49:36 +03:00
|
|
|
{
|
2018-10-10 21:06:58 +03:00
|
|
|
auto& surface = *FrameBufferSDL::the().surface();
|
|
|
|
return (dword*)(((byte*)surface.pixels) + (y * surface.pitch));
|
|
|
|
}
|
2018-10-10 17:49:36 +03:00
|
|
|
|
2018-10-10 21:06:58 +03:00
|
|
|
void Painter::fillRect(const Rect& rect, Color color)
|
|
|
|
{
|
|
|
|
Rect r = rect;
|
|
|
|
r.moveBy(m_widget.x(), m_widget.y());
|
2018-10-10 17:49:36 +03:00
|
|
|
|
2018-10-10 21:06:58 +03: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 17:49:36 +03:00
|
|
|
}
|
2018-10-10 21:06:58 +03:00
|
|
|
|
2018-10-12 00:14:51 +03:00
|
|
|
void Painter::drawRect(const Rect& rect, Color color)
|
|
|
|
{
|
|
|
|
Rect r = rect;
|
|
|
|
r.moveBy(m_widget.x(), m_widget.y());
|
|
|
|
|
|
|
|
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 02:48:09 +03:00
|
|
|
void Painter::drawText(const Rect& rect, const String& text, TextAlignment alignment, const Color& color)
|
2018-10-10 21:06:58 +03:00
|
|
|
{
|
2018-10-11 02:48:09 +03:00
|
|
|
Point point;
|
|
|
|
|
|
|
|
if (alignment == TextAlignment::TopLeft) {
|
|
|
|
point = rect.location();
|
|
|
|
point.moveBy(m_widget.x(), m_widget.y());;
|
|
|
|
} else if (alignment == TextAlignment::Center) {
|
2018-10-11 15:44:27 +03:00
|
|
|
int textWidth = text.length() * FONT_NAMESPACE::fontWidth;
|
2018-10-11 02:48:09 +03:00
|
|
|
point = rect.center();
|
2018-10-11 15:44:27 +03:00
|
|
|
point.moveBy(-(textWidth / 2), -(FONT_NAMESPACE::fontWidth / 2));
|
2018-10-11 02:48:09 +03:00
|
|
|
point.moveBy(m_widget.x(), m_widget.y());
|
|
|
|
} else {
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2018-10-10 21:06:58 +03:00
|
|
|
|
2018-10-11 15:44:27 +03:00
|
|
|
for (int row = 0; row < FONT_NAMESPACE::fontHeight; ++row) {
|
2018-10-11 02:48:09 +03:00
|
|
|
int y = point.y() + row;
|
2018-10-10 21:06:58 +03:00
|
|
|
dword* bits = scanline(y);
|
|
|
|
for (unsigned i = 0; i < text.length(); ++i) {
|
2018-10-11 13:33:03 +03:00
|
|
|
byte ch = text[i];
|
|
|
|
if (ch == ' ')
|
2018-10-11 01:10:36 +03:00
|
|
|
continue;
|
2018-10-11 15:44:27 +03:00
|
|
|
if (ch < FONT_NAMESPACE::firstCharacter || ch > FONT_NAMESPACE::lastCharacter) {
|
2018-10-11 13:33:03 +03:00
|
|
|
printf("Font doesn't have 0x%02x ('%c')\n", ch, ch);
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
2018-10-11 15:44:27 +03:00
|
|
|
const char* fontCharacter = FONT_NAMESPACE::font[ch - FONT_NAMESPACE::firstCharacter];
|
|
|
|
int x = point.x() + i * FONT_NAMESPACE::fontWidth;
|
|
|
|
for (unsigned j = 0; j < FONT_NAMESPACE::fontWidth; ++j) {
|
|
|
|
char fc = fontCharacter[row * FONT_NAMESPACE::fontWidth + j];
|
2018-10-10 21:06:58 +03:00
|
|
|
if (fc == '#')
|
|
|
|
bits[x + j] = color.value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|