summaryrefslogtreecommitdiff
path: root/Userland/Demos/Cube/Cube.cpp
blob: 0758c719815f7ffd97426eae168df11709fc3757 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@gmx.de>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <LibCore/ArgsParser.h>
#include <LibCore/ElapsedTimer.h>
#include <LibGUI/Application.h>
#include <LibGUI/Icon.h>
#include <LibGUI/Label.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Menubar.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Widget.h>
#include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Matrix4x4.h>
#include <LibGfx/Vector3.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

const int WIDTH = 200;
const int HEIGHT = 200;

static bool flag_hide_window_frame = false;

class Cube final : public GUI::Widget {
    C_OBJECT(Cube)
public:
    virtual ~Cube() override;
    void set_stat_label(RefPtr<GUI::Label> l) { m_stats = l; };
    void set_show_window_frame(bool);
    bool show_window_frame() const { return m_show_window_frame; }

    Function<void(GUI::ContextMenuEvent&)> on_context_menu_request;

protected:
    virtual void context_menu_event(GUI::ContextMenuEvent& event) override
    {
        if (on_context_menu_request)
            on_context_menu_request(event);
    }

private:
    Cube();

    RefPtr<Gfx::Bitmap> m_bitmap;
    RefPtr<GUI::Label> m_stats;

    virtual void paint_event(GUI::PaintEvent&) override;
    virtual void timer_event(Core::TimerEvent&) override;

    int m_accumulated_time;
    int m_cycles;
    int m_phase;
    bool m_show_window_frame { true };
};

Cube::Cube()
{
    m_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, { WIDTH, HEIGHT });

    m_accumulated_time = 0;
    m_cycles = 0;
    m_phase = 0;

    stop_timer();
    start_timer(20);
}

Cube::~Cube()
{
}

void Cube::paint_event(GUI::PaintEvent& event)
{
    GUI::Painter painter(*this);
    painter.add_clip_rect(event.rect());

    /* Blit it! */
    painter.draw_scaled_bitmap(event.rect(), *m_bitmap, m_bitmap->rect());
}

void Cube::timer_event(Core::TimerEvent&)
{
    Core::ElapsedTimer timer;
    timer.start();

    const FloatVector3 vertices[8] {
        { -1, -1, -1 },
        { -1, 1, -1 },
        { 1, 1, -1 },
        { 1, -1, -1 },
        { -1, -1, 1 },
        { -1, 1, 1 },
        { 1, 1, 1 },
        { 1, -1, 1 },
    };

#define QUAD(a, b, c, d) a, b, c, c, d, a

    const int indices[] {
        QUAD(0, 1, 2, 3),
        QUAD(7, 6, 5, 4),
        QUAD(4, 5, 1, 0),
        QUAD(3, 2, 6, 7),
        QUAD(1, 5, 6, 2),
        QUAD(0, 3, 7, 4),
    };

    const Color colors[] {
        Color::Red,
        Color::Red,
        Color::Green,
        Color::Green,
        Color::Blue,
        Color::Blue,
        Color::Magenta,
        Color::Magenta,
        Color::White,
        Color::White,
        Color::Yellow,
        Color::Yellow,
    };

    FloatVector3 transformed_vertices[8];

    static float angle = 0;
    angle += 0.02f;

    auto matrix = FloatMatrix4x4::translate(FloatVector3(0, 0, 1.5f))
        * FloatMatrix4x4::rotate(FloatVector3(1, 0, 0), angle * 1.17356641f)
        * FloatMatrix4x4::rotate(FloatVector3(0, 1, 0), angle * 0.90533273f)
        * FloatMatrix4x4::rotate(FloatVector3(0, 0, 1), angle);

    for (int i = 0; i < 8; i++) {
        transformed_vertices[i] = matrix.transform_point(vertices[i]);
    }

    GUI::Painter painter(*m_bitmap);
    if (m_show_window_frame)
        painter.fill_rect_with_gradient(Gfx::Orientation::Vertical, m_bitmap->rect(), Gfx::Color::White, Gfx::Color::Blue);
    else
        painter.clear_rect(m_bitmap->rect(), Gfx::Color::Transparent);

    auto to_point = [](const FloatVector3& v) {
        return Gfx::IntPoint(v.x(), v.y());
    };

    for (size_t i = 0; i < sizeof(indices) / sizeof(indices[0]) / 3; i++) {
        auto a = transformed_vertices[indices[i * 3]];
        auto b = transformed_vertices[indices[i * 3 + 1]];
        auto c = transformed_vertices[indices[i * 3 + 2]];
        auto normal = (b - a).cross(c - a);
        normal.normalize();

        // Perspective projection
        a.set_x(WIDTH / 2 + a.x() / (1 + a.z() * 0.35f) * WIDTH / 3);
        a.set_y(HEIGHT / 2 - a.y() / (1 + a.z() * 0.35f) * WIDTH / 3);
        b.set_x(WIDTH / 2 + b.x() / (1 + b.z() * 0.35f) * WIDTH / 3);
        b.set_y(HEIGHT / 2 - b.y() / (1 + b.z() * 0.35f) * WIDTH / 3);
        c.set_x(WIDTH / 2 + c.x() / (1 + c.z() * 0.35f) * WIDTH / 3);
        c.set_y(HEIGHT / 2 - c.y() / (1 + c.z() * 0.35f) * WIDTH / 3);

        float winding = (b.x() - a.x()) * (c.y() - a.y()) - (b.y() - a.y()) * (c.x() - a.x());
        if (winding < 0)
            continue;

        float shade = 0.5f + normal.y() * 0.5f;
        auto color = colors[i];
        color.set_red(color.red() * shade);
        color.set_green(color.green() * shade);
        color.set_blue(color.blue() * shade);

        painter.draw_triangle(to_point(a), to_point(b), to_point(c), color);
    }

    if ((m_cycles % 50) == 0) {
        dbgln("{} total cycles. finished 50 in {} ms, avg {} ms", m_cycles, m_accumulated_time, m_accumulated_time / 50);
        m_stats->set_text(String::formatted("{} ms", m_accumulated_time / 50));
        m_accumulated_time = 0;
    }

    update();

    m_accumulated_time += timer.elapsed();
    m_cycles++;
}

void Cube::set_show_window_frame(bool show)
{
    if (show == m_show_window_frame)
        return;
    m_show_window_frame = show;
    m_stats->set_visible(m_show_window_frame);
    auto& w = *window();
    w.set_frameless(!m_show_window_frame);
    w.set_has_alpha_channel(!m_show_window_frame);
    w.set_alpha_hit_threshold(m_show_window_frame ? 0 : 1);
}

int main(int argc, char** argv)
{
    auto app = GUI::Application::construct(argc, argv);

    if (pledge("stdio recvfd sendfd rpath", nullptr) < 0) {
        perror("pledge");
        return 1;
    }

    if (unveil("/res", "r") < 0) {
        perror("unveil");
        return 1;
    }

    if (unveil(nullptr, nullptr) < 0) {
        perror("unveil");
        return 1;
    }

    Core::ArgsParser parser;
    parser.set_general_help("Create a window with a spinning cube.");
    parser.add_option(flag_hide_window_frame, "Hide window frame", "hide-window", 'h');
    parser.parse(argc, argv);

    auto window = GUI::Window::construct();
    window->set_double_buffering_enabled(true);
    window->set_title("Cube");
    window->set_resizable(false);
    window->resize(WIDTH, HEIGHT);
    window->set_has_alpha_channel(true);
    window->set_alpha_hit_threshold(1);

    auto& cube = window->set_main_widget<Cube>();

    auto& time = cube.add<GUI::Label>();
    time.set_relative_rect({ 0, 4, 40, 10 });
    time.move_by({ window->width() - time.width(), 0 });
    cube.set_stat_label(time);

    auto app_icon = GUI::Icon::default_icon("app-cube");
    window->set_icon(app_icon.bitmap_for_size(16));

    auto menubar = GUI::Menubar::construct();
    auto& app_menu = menubar->add_menu("File");
    auto show_window_frame_action = GUI::Action::create_checkable("Show window frame", [&](auto& action) {
        cube.set_show_window_frame(action.is_checked());
    });

    cube.set_show_window_frame(!flag_hide_window_frame);
    show_window_frame_action->set_checked(cube.show_window_frame());
    app_menu.add_action(move(show_window_frame_action));
    app_menu.add_separator();
    app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
    auto& help_menu = menubar->add_menu("Help");
    help_menu.add_action(GUI::CommonActions::make_about_action("Cube Demo", app_icon, window));
    window->set_menubar(move(menubar));

    cube.on_context_menu_request = [&](auto& event) {
        app_menu.popup(event.screen_position());
    };

    window->show();

    return app->exec();
}