1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 09:07:35 +00:00

Fire: Make the main widget a GUI::Frame

This commit is contained in:
Andreas Kling 2021-07-05 00:16:31 +02:00
parent 9f0aef6051
commit 36cc011ae4

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -25,12 +25,12 @@
#include <LibCore/ElapsedTimer.h> #include <LibCore/ElapsedTimer.h>
#include <LibGUI/Action.h> #include <LibGUI/Action.h>
#include <LibGUI/Application.h> #include <LibGUI/Application.h>
#include <LibGUI/Frame.h>
#include <LibGUI/Icon.h> #include <LibGUI/Icon.h>
#include <LibGUI/Label.h> #include <LibGUI/Label.h>
#include <LibGUI/Menu.h> #include <LibGUI/Menu.h>
#include <LibGUI/Menubar.h> #include <LibGUI/Menubar.h>
#include <LibGUI/Painter.h> #include <LibGUI/Painter.h>
#include <LibGUI/Widget.h>
#include <LibGUI/Window.h> #include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h> #include <LibGfx/Bitmap.h>
#include <stdio.h> #include <stdio.h>
@ -39,7 +39,7 @@
#include <unistd.h> #include <unistd.h>
#define FIRE_WIDTH 320 #define FIRE_WIDTH 320
#define FIRE_HEIGHT 168 #define FIRE_HEIGHT 200
#define FIRE_MAX 29 #define FIRE_MAX 29
static const Color s_palette[] = { static const Color s_palette[] = {
@ -55,8 +55,9 @@ static const Color s_palette[] = {
Color(0xCF, 0xCF, 0x6F), Color(0xEF, 0xEF, 0xC7), Color(0xFF, 0xFF, 0xFF) Color(0xCF, 0xCF, 0x6F), Color(0xEF, 0xEF, 0xC7), Color(0xFF, 0xFF, 0xFF)
}; };
class Fire : public GUI::Widget { class Fire : public GUI::Frame {
C_OBJECT(Fire) C_OBJECT(Fire);
public: public:
virtual ~Fire() override; virtual ~Fire() override;
void set_stat_label(RefPtr<GUI::Label> l) { stats = l; }; void set_stat_label(RefPtr<GUI::Label> l) { stats = l; };
@ -80,7 +81,7 @@ private:
Fire::Fire() Fire::Fire()
{ {
bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::Indexed8, { 320, 200 }); bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::Indexed8, { FIRE_WIDTH, FIRE_HEIGHT });
/* Initialize fire palette */ /* Initialize fire palette */
for (int i = 0; i < 30; i++) for (int i = 0; i < 30; i++)
@ -113,12 +114,13 @@ Fire::~Fire()
void Fire::paint_event(GUI::PaintEvent& event) void Fire::paint_event(GUI::PaintEvent& event)
{ {
GUI::Frame::paint_event(event);
Core::ElapsedTimer timer; Core::ElapsedTimer timer;
timer.start(); timer.start();
GUI::Painter painter(*this); GUI::Painter painter(*this);
painter.add_clip_rect(event.rect()); painter.add_clip_rect(event.rect());
painter.draw_scaled_bitmap(rect(), *bitmap, bitmap->rect()); painter.draw_scaled_bitmap(frame_inner_rect(), *bitmap, bitmap->rect());
timeAvg += timer.elapsed(); timeAvg += timer.elapsed();
cycles++; cycles++;
@ -133,7 +135,7 @@ void Fire::timer_event(Core::TimerEvent&)
/* Paint our palettized buffer to screen */ /* Paint our palettized buffer to screen */
for (int px = 0 + phase; px < FIRE_WIDTH; px += 2) { for (int px = 0 + phase; px < FIRE_WIDTH; px += 2) {
for (int py = 1; py < 200; py++) { for (int py = 1; py < FIRE_HEIGHT; py++) {
int rnd = rand() % 3; int rnd = rand() % 3;
/* Calculate new pixel value, don't go below 0 */ /* Calculate new pixel value, don't go below 0 */
@ -217,7 +219,7 @@ int main(int argc, char** argv)
window->set_double_buffering_enabled(false); window->set_double_buffering_enabled(false);
window->set_title("Fire"); window->set_title("Fire");
window->set_resizable(false); window->set_resizable(false);
window->resize(640, 400); window->resize(FIRE_WIDTH * 2 + 4, FIRE_HEIGHT * 2 + 4);
auto menubar = GUI::Menubar::construct(); auto menubar = GUI::Menubar::construct();
auto& file_menu = menubar->add_menu("&File"); auto& file_menu = menubar->add_menu("&File");