-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.cpp
159 lines (133 loc) · 5.31 KB
/
main.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
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
////////////////////////////////////////////////////////////////////////////////
// Distributed under the Boost Software License, Version 1.0. //
// (See accompanying file LICENSE or copy at //
// https://www.boost.org/LICENSE_1_0.txt) //
////////////////////////////////////////////////////////////////////////////////
#include <chrono>
#include <cstdint>
#include <memory>
#include <string>
#include <tuple>
#include <core/context.h>
#include <core/exception.h>
#include <core/looper.h>
#include <core/start.h>
#include <graphics/mesh_manager.h>
#include <graphics/post_processing_description.h>
#include <graphics/render_graph/render_graph.h>
#include <graphics/render_graph/texture_node.h>
#include <graphics/render_pipeline.h>
#include <graphics/render_target.h>
#include <graphics/render_target_manager.h>
#include <graphics/scene.h>
#include <graphics/single_entity.h>
#include <graphics/text_factory.h>
#include <graphics/texture_manager.h>
#include <graphics/window.h>
#include <graphics/window_manager.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
#include "samples/animation_sample.h"
#include "samples/physics_sample.h"
#include "samples/render_graph_sample.h"
#include "samples/sample.h"
#include "samples/water_sample.h"
static constexpr std::size_t sample_count = 4u;
using namespace std::chrono_literals;
struct RenderState
{
std::unique_ptr<iris::RenderPipeline> render_pipeline;
std::unique_ptr<Sample> sample;
};
std::unique_ptr<Sample> create_sample(
iris::Context &context,
iris::Window *window,
iris::RenderPipeline &render_pipeline,
std::size_t index)
{
switch (index)
{
case 0: return std::make_unique<AnimationSample>(window, render_pipeline, context); break;
case 1: return std::make_unique<RenderGraphSample>(window, render_pipeline, context); break;
case 2: return std::make_unique<PhysicsSample>(window, render_pipeline, context); break;
case 3: return std::make_unique<WaterSample>(window, render_pipeline, context); break;
default: throw iris::Exception("unknown sample index");
}
}
RenderState create_render_state(iris::Context &context, iris::Window *window, iris::Camera *camera, std::size_t index)
{
auto render_pipeline = std::make_unique<iris::RenderPipeline>(
context.material_manager(),
context.mesh_manager(),
context.render_target_manager(),
window->width(),
window->height());
auto sample = create_sample(context, window, *render_pipeline, index % sample_count);
const auto *sample_target = sample->target();
auto *scene = render_pipeline->create_scene();
auto *rg = render_pipeline->create_render_graph();
rg->render_node()->set_colour_input(rg->create<iris::TextureNode>(sample_target->colour_texture()));
scene->create_entity<iris::SingleEntity>(
rg, context.mesh_manager().sprite({1.0f, 1.0f, 1.0f}), iris::Transform{{0.0f}, {}, {1920.0f, 1080.0f, 1.0f}});
iris::PostProcessingDescription post_processing_description{
.bloom = {iris::BloomDescription{6.0f}},
.colour_adjust = {iris::ColourAdjustDescription{}},
.anti_aliasing = {iris::AntiAliasingDescription{}}};
auto *pass = render_pipeline->create_render_pass(scene);
pass->camera = camera;
pass->post_processing_description = post_processing_description;
return {std::move(render_pipeline), std::move(sample)};
}
void go(iris::Context context)
{
context.resource_manager().set_root_directory("assets");
auto &rtm = context.render_target_manager();
auto window = context.window_manager().create_window(1920, 1080);
std::size_t sample_number = 0u;
iris::Camera camera{iris::CameraType::ORTHOGRAPHIC, window->width(), window->height()};
auto [render_pipeline, sample] = create_render_state(context, window, &camera, sample_number);
window->set_render_pipeline(std::move(render_pipeline));
std::size_t frame_counter = 0u;
std::size_t next_update = 1u;
iris::Looper looper{
0ms,
16ms,
[&sample = sample](auto, auto)
{
sample->fixed_update();
return true;
},
[&, &sample = sample](std::chrono::microseconds elapsed, auto)
{
auto running = true;
auto event = window->pump_event();
while (event)
{
if (event->is_quit() || event->is_key(iris::Key::ESCAPE))
{
running = false;
}
else if (event->is_key(iris::Key::TAB, iris::KeyState::UP))
{
++sample_number;
auto [new_render_pipeline, new_sample] =
create_render_state(context, window, &camera, sample_number);
sample = std::move(new_sample);
window->set_render_pipeline(std::move(new_render_pipeline));
}
else
{
sample->handle_input(*event);
}
event = window->pump_event();
}
sample->variable_update();
window->render();
return running;
}};
looper.run();
}
int main(int argc, char **argv)
{
iris::start(argc, argv, go, true);
}