diff options
author | Pedro Pereira <pmh.pereira@gmail.com> | 2021-11-21 10:38:56 +0000 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-11-21 16:40:14 +0000 |
commit | 6ac97d439724854c16c315c7b293509377ffbc53 (patch) | |
tree | b0f043bc66418d045042bf8863c8a6d8f0752e2b /Userland/Demos/Starfield | |
parent | d5c687b50f9eca74079ce49314e3da96963b57c6 (diff) | |
download | serenity-6ac97d439724854c16c315c7b293509377ffbc53.zip |
Starfield: Support variable speed
This change allows us to modify the speed in which the Starfield is
generated. Increasing the speed also increases the length of each trail.
Diffstat (limited to 'Userland/Demos/Starfield')
-rw-r--r-- | Userland/Demos/Starfield/Starfield.cpp | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/Userland/Demos/Starfield/Starfield.cpp b/Userland/Demos/Starfield/Starfield.cpp index 5941d634f6..2a93c03427 100644 --- a/Userland/Demos/Starfield/Starfield.cpp +++ b/Userland/Demos/Starfield/Starfield.cpp @@ -47,6 +47,7 @@ private: Vector<Coordinate> m_stars; int m_sweep_plane = 2000; + unsigned m_speed = 1; }; Starfield::Starfield(int interval) @@ -102,18 +103,26 @@ void Starfield::timer_event(Core::TimerEvent&) auto half_x = width() / 2; auto half_y = height() / 2; + GUI::Painter painter(*m_bitmap); + for (auto star : m_stars) { auto z = ((star.z + m_sweep_plane) % 2000) * 0.0005; computed_point.set_x(half_x + star.x / z); computed_point.set_y(half_y + star.y / z); + auto computed_end_point = Gfx::IntPoint(); + + auto z_end = ((star.z + m_sweep_plane - m_speed) % 2000) * 0.0005; + computed_end_point.set_x(half_x + star.x / z_end); + computed_end_point.set_y(half_y + star.y / z_end); + if (computed_point.x() < 0 || computed_point.x() >= width() || computed_point.y() < 0 || computed_point.y() >= height()) continue; u8 falloff = (1 - z * z) * 255; - m_bitmap->set_pixel(computed_point, Color(falloff, falloff, falloff)); + painter.draw_line(computed_point, computed_end_point, Color(falloff, falloff, falloff)); } - m_sweep_plane -= 1; + m_sweep_plane -= m_speed; if (m_sweep_plane < 0) m_sweep_plane = 2000; update(); |