1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:37:34 +00:00

Demos/Eyes: Render eyes antialiased

This looks good, and is a good way to test ellipse rendering is
working properly.
This commit is contained in:
MacDue 2022-03-21 01:52:19 +00:00 committed by Linus Groh
parent 89445b967d
commit cf5b6c5c8d
2 changed files with 9 additions and 7 deletions

View file

@ -20,18 +20,19 @@ void EyesWidget::track_mouse_move(Gfx::IntPoint const& point)
void EyesWidget::paint_event(GUI::PaintEvent& event) void EyesWidget::paint_event(GUI::PaintEvent& event)
{ {
GUI::Painter painter(*this); GUI::Painter painter(*this);
Gfx::AntiAliasingPainter aa_painter { painter };
painter.clear_rect(event.rect(), Gfx::Color()); painter.clear_rect(event.rect(), Gfx::Color());
for (int i = 0; i < m_full_rows; i++) { for (int i = 0; i < m_full_rows; i++) {
for (int j = 0; j < m_eyes_in_row; j++) for (int j = 0; j < m_eyes_in_row; j++)
render_eyeball(i, j, painter); render_eyeball(i, j, aa_painter);
} }
for (int i = 0; i < m_extra_columns; ++i) for (int i = 0; i < m_extra_columns; ++i)
render_eyeball(m_full_rows, i, painter); render_eyeball(m_full_rows, i, aa_painter);
} }
void EyesWidget::render_eyeball(int row, int column, GUI::Painter& painter) const void EyesWidget::render_eyeball(int row, int column, Gfx::AntiAliasingPainter& painter) const
{ {
auto eye_width = width() / m_eyes_in_row; auto eye_width = width() / m_eyes_in_row;
auto eye_height = height() / m_num_rows; auto eye_height = height() / m_num_rows;
@ -40,9 +41,9 @@ void EyesWidget::render_eyeball(int row, int column, GUI::Painter& painter) cons
auto height_thickness = max(int(eye_height / 5.5), 1); auto height_thickness = max(int(eye_height / 5.5), 1);
bounds.shrink(int(eye_width / 12.5), 0); bounds.shrink(int(eye_width / 12.5), 0);
painter.fill_ellipse(bounds, palette().base_text()); painter.draw_ellipse(bounds, palette().base_text());
bounds.shrink(width_thickness, height_thickness); bounds.shrink(width_thickness, height_thickness);
painter.fill_ellipse(bounds, palette().base()); painter.draw_ellipse(bounds, palette().base());
Gfx::IntPoint pupil_center = this->pupil_center(bounds); Gfx::IntPoint pupil_center = this->pupil_center(bounds);
Gfx::IntSize pupil_size { Gfx::IntSize pupil_size {
@ -56,7 +57,7 @@ void EyesWidget::render_eyeball(int row, int column, GUI::Painter& painter) cons
pupil_size.height() pupil_size.height()
}; };
painter.fill_ellipse(pupil, palette().base_text()); painter.draw_ellipse(pupil, palette().base_text());
} }
Gfx::IntPoint EyesWidget::pupil_center(Gfx::IntRect& eyeball_bounds) const Gfx::IntPoint EyesWidget::pupil_center(Gfx::IntRect& eyeball_bounds) const

View file

@ -9,6 +9,7 @@
#include <LibGUI/MouseTracker.h> #include <LibGUI/MouseTracker.h>
#include <LibGUI/Widget.h> #include <LibGUI/Widget.h>
#include <LibGfx/AntiAliasingPainter.h>
class EyesWidget final : public GUI::Widget class EyesWidget final : public GUI::Widget
, GUI::MouseTracker { , GUI::MouseTracker {
@ -29,7 +30,7 @@ private:
virtual void paint_event(GUI::PaintEvent&) override; virtual void paint_event(GUI::PaintEvent&) override;
virtual void track_mouse_move(Gfx::IntPoint const&) override; virtual void track_mouse_move(Gfx::IntPoint const&) override;
void render_eyeball(int row, int column, GUI::Painter&) const; void render_eyeball(int row, int column, Gfx::AntiAliasingPainter& aa_painter) const;
Gfx::IntPoint pupil_center(Gfx::IntRect& eyeball_bounds) const; Gfx::IntPoint pupil_center(Gfx::IntRect& eyeball_bounds) const;
Gfx::IntPoint m_mouse_position; Gfx::IntPoint m_mouse_position;