-
Notifications
You must be signed in to change notification settings - Fork 43
/
MinimalApp.cpp
125 lines (103 loc) · 4.59 KB
/
MinimalApp.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
/* Copyright 2012-2020 Matthew Reid
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <ExamplesCommon/HelpDisplayRenderOperation.h>
#include <ExamplesCommon/HelpDisplayToggleEventListener.h>
#include <ExamplesCommon/VisRootUtil.h>
#include <ExamplesCommon/WindowUtil.h>
#include <SkyboltEngine/CameraInputSystem.h>
#include <SkyboltEngine/EngineCommandLineParser.h>
#include <SkyboltEngine/EngineRoot.h>
#include <SkyboltEngine/EngineRootFactory.h>
#include <SkyboltEngine/EntityFactory.h>
#include <SkyboltEngine/WindowUtil.h>
#include <SkyboltEngine/Diagnostics/StatsDisplaySystem.h>
#include <SkyboltEngine/Input/InputPlatformOsg.h>
#include <SkyboltEngine/Input/InputSystem.h>
#include <SkyboltEngine/Input/LogicalAxis.h>
#include <SkyboltEngine/SimVisBinding/CameraSimVisBinding.h>
#include <SkyboltEngine/SimVisBinding/SimVisSystem.h>
#include <SkyboltEngine/UpdateLoop/UpdateLoopUtility.h>
#include <SkyboltSim/World.h>
#include <SkyboltSim/Components/CameraComponent.h>
#include <SkyboltSim/Components/CameraControllerComponent.h>
#include <SkyboltSim/System/System.h>
#include <SkyboltVis/Camera.h>
#include <SkyboltVis/Scene.h>
#include <SkyboltVis/VisRoot.h>
#include <SkyboltVis/RenderOperation/RenderCameraViewport.h>
#include <SkyboltVis/RenderOperation/RenderOperationOrder.h>
#include <SkyboltVis/RenderOperation/RenderTarget.h>
#include <SkyboltVis/Window/StandaloneWindow.h>
#include <SkyboltCommon/Exception.h>
#include <SkyboltCommon/Json/ReadJsonFile.h>
using namespace skybolt;
using namespace skybolt::sim;
using namespace skybolt::vis;
static void createEntities(const EntityFactory& entityFactory, World& world, CameraController& cameraController)
{
world.addEntity(entityFactory.createEntity("Stars"));
world.addEntity(entityFactory.createEntity("SunBillboard"));
world.addEntity(entityFactory.createEntity("MoonBillboard"));
EntityPtr planet = entityFactory.createEntity("PlanetEarth");
world.addEntity(planet);
cameraController.setTarget(planet.get());
}
static osg::ref_ptr<HelpDisplayRenderOperation> createHelpDisplay()
{
std::string helpMessage =
R"(= Controls =
W: Zoom in
S: Zoom out
H: Toggle help message
Shift (hold): Camera tilt modifier
Esc: Exit
Mouse: Rotate camera
)";
osg::ref_ptr<HelpDisplayRenderOperation> helpDisplay = new HelpDisplayRenderOperation();
helpDisplay->setMessage(helpMessage);
return helpDisplay;
}
int main(int argc, char *argv[])
{
try
{
// Create engine
auto params = EngineCommandLineParser::parse(argc, argv);
std::unique_ptr<EngineRoot> engineRoot = EngineRootFactory::create(params);
// Create camera
EntityPtr simCamera = engineRoot->entityFactory->createEntity("Camera");
engineRoot->simWorld->addEntity(simCamera);
auto visRoot = createExampleVisRoot();
// Attach camera to window
vis::WindowPtr window = createExampleWindow();
osg::ref_ptr<vis::RenderCameraViewport> viewport = createAndAddViewportToWindowWithEngine(*window, *engineRoot);
viewport->setCamera(getVisCamera(*simCamera));
visRoot->addWindow(window);
// Create input
auto inputPlatform = std::make_shared<InputPlatformOsg>(window->getView());
std::vector<LogicalAxisPtr> axes = CameraInputSystem::createDefaultAxes(*inputPlatform);
// Create systems
engineRoot->systemRegistry->push_back(std::make_shared<InputSystem>(inputPlatform, window.get(), axes));
engineRoot->systemRegistry->push_back(std::make_shared<CameraInputSystem>(simCamera, inputPlatform, axes));
osg::ref_ptr<HelpDisplayRenderOperation> helpDisplay = createHelpDisplay();
window->getRenderOperationSequence().addOperation(helpDisplay, (int)RenderOperationOrder::Hud);
auto helpDisplayToggleEventListener = std::make_shared<HelpDisplayToggleEventListener>(helpDisplay);
inputPlatform->getEventEmitter()->addEventListener<KeyEvent>(helpDisplayToggleEventListener.get());
//#define SHOW_STATS
#ifdef SHOW_STATS
engineRoot->systemRegistry->push_back(std::make_shared<StatsDisplaySystem>(&visRoot->getViewer(), window->getView(), viewport->getFinalRenderTarget()->getOsgCamera()));
#endif
// Create entities
createEntities(*engineRoot->entityFactory, *engineRoot->simWorld, *simCamera->getFirstComponentRequired<CameraControllerComponent>()->cameraController);
// Run loop
runMainLoop(*visRoot, *engineRoot, UpdateLoop::neverExit);
}
catch (const std::exception& e)
{
printf("%s\n", e.what());
}
return 0;
}