2d-example.cpp

 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
#include <planet/vk/engine.hpp>


namespace {
    felspar::coro::task<int> render_loop(
            planet::vk::engine::app &app,
            planet::vk::engine::renderer &renderer) {
        planet::vk::engine::pipeline::mesh mesh{
                renderer, "planet-vk-engine/mesh.world.vert.spirv"};

        constexpr std::array vertices{
                planet::vk::engine::pipeline::mesh::vertex{
                        {-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}},
                planet::vk::engine::pipeline::mesh::vertex{
                        {0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}},
                planet::vk::engine::pipeline::mesh::vertex{
                        {0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}},
                planet::vk::engine::pipeline::mesh::vertex{
                        {-0.5f, 0.5f, 0.0f}, {1.0f, 0.0f, 1.0f}}};
        constexpr std::array<std::uint32_t, 6> indices{0, 1, 2, 2, 3, 0};

        for (bool done = false; not done;) {
            SDL_Event event;
            while (SDL_PollEvent(&event)) {
                if (event.type == SDL_QUIT) { done = true; }
                if (event.type == SDL_KEYDOWN
                    && event.key.keysym.sym == SDLK_ESCAPE) {
                    done = true;
                }
                if (event.type == SDL_WINDOWEVENT
                    && event.window.event == SDL_WINDOWEVENT_CLOSE
                    && event.window.windowID
                            == SDL_GetWindowID(app.window.get())) {
                    done = true;
                }
            }

            co_await renderer.start({{{0.f, 0.f, 0.f, 1.f}}});

            mesh.this_frame.draw(vertices, indices);
            mesh.this_frame.draw(vertices, indices, {0.75f, 0.75f, 0.0f});

            renderer.submit_and_present();
        }
        co_return 1;
    }
}


int main(int const argc, char const *argv[]) {
    felspar::posix::promise_to_never_use_select();
    felspar::io::poll_warden warden{};
    planet::sdl::init sdl{warden, "blue5alamander/planet-vk/2d-example"};
    return planet::vk::engine::app{
            argc, argv, sdl, "blue5alamander/planet-vk/2d-example"}
            .run(render_loop);
}