Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
263 lines (208 loc) · 8.62 KB
/
Copy pathmain.cpp
File metadata and controls
263 lines (208 loc) · 8.62 KB
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
/*
* main.cpp
*/
// BulletRender
#include "app/Loop.h"
#include "app/Window.h"
#include "render/passes/Canvas.h"
#include "render/passes/Grid.h"
#include "render/passes/Lines.h"
#include "render/passes/SkyBox.h"
#include "render/passes/WorldAxis.h"
#include "render/Renderer.h"
#include "render/Shader.h"
#include "scene/Camera.h"
#include "scene/Scene.h"
// BulletEngine
#include "app/Application.h"
#include "app/TaskExecutor.h"
#include "assets/Loaders.h"
#include "ecs/Ecs.h"
#include "ecs/Reflection.h"
#include "ecs/systems/CanvasSystem.h"
#include "ecs/systems/DebugDrawSystem.h"
#include "ecs/systems/HierarchySystem.h"
#include "ecs/systems/InputSystem.h"
#include "ecs/systems/PhysicsSystem.h"
#include "ecs/systems/PickSystem.h"
#include "ecs/systems/ReloadSystem.h"
#include "ecs/systems/RenderSystem.h"
#include "ecs/systems/ScriptSystem.h"
#include "interface/Editor.h"
#include "interface/Launcher.h"
#include "io/Log.h"
#include "project/Project.h"
#include "script/Api.h"
#include <memory>
using namespace BulletEngine;
namespace br = BulletRender;
static const std::string VERTEX_SHADER_PATH = "assets/shaders/normal.vert.glsl";
static const std::string FRAGMENT_SHADER_PATH = "assets/shaders/normal.frag.glsl";
int main()
{
// streams reach editor from here on, terminal still gets them
io::Log::instance().capture();
// window
br::app::Loop::setDocking(true);
br::app::Loop::setDrawScene(false); // editor draws scene into its panels
br::app::WindowConfig windowCfg{1600, 900, "BulletEngine", true, true};
if (!br::app::Window::init(windowCfg))
{
return -1;
}
// gl objects must die before context does
{
// renderer
br::render::Renderer::init();
auto lines = std::make_shared<br::render::Lines>(1.5f);
lines->setDepthTest(false); // gizmos stay visible through geometry
auto grid = std::make_shared<br::render::Grid>();
auto worldAxis = std::make_shared<br::render::WorldAxis>();
auto canvas = std::make_shared<br::render::Canvas>();
auto skybox = std::make_shared<br::render::SkyBox>(nullptr);
skybox->setEnabled(false);
grid->setFade(interface::GROUND_FADE_START, interface::GROUND_FADE_END);
worldAxis->setFade(interface::GROUND_FADE_START, interface::GROUND_FADE_END);
br::render::Renderer::registerPrePass(skybox);
br::render::Renderer::registerPrePass(grid);
br::render::Renderer::registerPrePass(worldAxis);
br::render::Renderer::registerOverlayPass(lines);
br::render::Renderer::registerOverlayPass(canvas);
// scene
br::scene::Scene scene;
br::render::Renderer::setDefaultShader(
std::make_shared<br::render::GraphicsShader>(VERTEX_SHADER_PATH, FRAGMENT_SHADER_PATH));
// world
app::TaskExecutor::instance().start();
assets::registerLoaders();
ecs::registerTypes();
ecs::World world;
// systems
ecs::systems::PhysicsSystem physicsSystem;
physicsSystem.watch(world);
ecs::systems::HierarchySystem hierarchySystem;
ecs::systems::RenderSystem renderSystem(scene, skybox);
ecs::systems::DebugDrawSystem debugDrawSystem(lines);
ecs::systems::ReloadSystem reloadSystem;
// editor
interface::Editor editor(world, physicsSystem, debugDrawSystem);
ecs::systems::ScriptSystem scriptSystem(physicsSystem, editor);
ecs::systems::CanvasSystem canvasSystem(scriptSystem);
scriptSystem.observe(world);
// its own tools, game view goes without them
editor.addEditorPass(grid, "Grid");
editor.addEditorPass(worldAxis, "Axes");
editor.addEditorPass(lines, nullptr);
editor.addGamePass(canvas);
// picking casts through whichever camera scene panel shows
ecs::systems::PickSystem pickSystem(physicsSystem);
// phases
app::Application app;
app.setWorld(&world);
app::Scheduler& scheduler = app.getScheduler();
scheduler.add(app::Phase::PreUpdate, [](const app::FrameContext&) {
app::TaskExecutor::instance().drain();
}, -10, "assets");
scheduler.add(app::Phase::PreUpdate, [&editor](const app::FrameContext& frame) {
if (editor.isSceneFocused())
{
editor.getCamera().update(frame.deltaTime);
}
br::utils::Input::instance().update();
}, 0, "input");
scheduler.add(app::Phase::PreUpdate, [&reloadSystem](const app::FrameContext& frame) {
reloadSystem.update(*frame.world, frame.deltaTime);
}, 10, "reload");
editor.addModeListener([&scriptSystem, &world](interface::Mode mode) {
if (mode == interface::Mode::Play)
{
scriptSystem.start(world);
}
else
{
scriptSystem.stop();
}
});
scheduler.add(app::Phase::Update, [&scriptSystem](const app::FrameContext& frame) {
scriptSystem.update(*frame.world, frame.deltaTime);
}, 0, "scripts");
// forces land before step that reads them
scheduler.add(app::Phase::FixedUpdate, [&scriptSystem](const app::FrameContext& frame) {
scriptSystem.fixedUpdate(*frame.world, frame.fixedDeltaTime);
}, -10, "scripts fixed");
scheduler.add(app::Phase::PostUpdate, [&scriptSystem](const app::FrameContext& frame) {
scriptSystem.lateUpdate(*frame.world, frame.deltaTime);
}, 0, "scripts late");
scheduler.add(app::Phase::FixedUpdate, [&physicsSystem, &editor](const app::FrameContext& frame) {
// idle world still keeps physics in step, picking casts rays into it
if (editor.isPlaying())
{
physicsSystem.step(*frame.world, frame.fixedDeltaTime);
}
else
{
physicsSystem.sync(*frame.world);
}
}, 0, "physics");
// contacts reach scripts once step is over, so they may spawn and destroy freely
scheduler.add(app::Phase::FixedUpdate, [&scriptSystem, &physicsSystem](const app::FrameContext& frame) {
scriptSystem.deliver(*frame.world, physicsSystem.getEvents());
physicsSystem.clearEvents();
}, 10, "contacts");
scheduler.add(app::Phase::PostUpdate, [&pickSystem, &editor](const app::FrameContext& frame) {
if (!editor.hasScenePick())
{
return;
}
editor.setSelection(pickSystem.pick(*frame.world, editor.getCamera(), editor.getScenePick(), editor.getSceneSize()));
editor.clearScenePick();
}, 0, "pick");
scheduler.add(app::Phase::PostUpdate, [&hierarchySystem](const app::FrameContext& frame) {
hierarchySystem.update(*frame.world);
}, 10, "hierarchy");
scheduler.add(app::Phase::Render, [&renderSystem](const app::FrameContext& frame) {
renderSystem.render(*frame.world);
}, 0, "scene");
scheduler.add(app::Phase::Render, [&debugDrawSystem, &editor, &physicsSystem](const app::FrameContext& frame) {
debugDrawSystem.draw(*frame.world, editor.getSelection(), physicsSystem.getContacts());
}, 10, "debug draw");
scheduler.add(app::Phase::RenderUi, [&canvasSystem, &canvas](const app::FrameContext& frame) {
canvasSystem.draw(*frame.world, *canvas);
}, 0, "canvas");
// input
ecs::systems::InputSystem inputSystem;
inputSystem.bind(br::utils::InputKey::ESCAPE, []() {
br::app::Window::setShouldClose(true);
});
// loop
br::app::Loop loop(scene);
loop.setBeforeFrame([&editor]() { editor.beforeFrame(); });
interface::Launcher launcher;
bool opened = false;
loop.run([&](float dt) {
// editor waits until there is project under it
if (!project::Project::instance().isOpen())
{
launcher.draw();
return;
}
if (!opened)
{
opened = true;
editor.openStartScene();
}
app.tick(dt);
editor.renderViews(scene);
editor.draw();
world.flush();
canvas->endFrame();
br::utils::Input::instance().endFrame();
});
}
// workers hold nothing of gl, but what they feed does, so they go first
app::TaskExecutor::instance().stop();
br::render::Renderer::shutdown();
br::app::Window::shutdown();
io::Log::instance().release();
return 0;
}