This repository was archived by the owner on Dec 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_distributedapp.cpp
More file actions
108 lines (87 loc) · 2.98 KB
/
10_distributedapp.cpp
File metadata and controls
108 lines (87 loc) · 2.98 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
#include <string>
#include "al/core/app/al_DistributedApp.hpp"
#include "al/core/graphics/al_Shapes.hpp"
#include "al/util/ui/al_Parameter.hpp"
#include "al/util/ui/al_ControlGUI.hpp"
#include "al/util/al_FontModule.hpp"
using namespace al;
/* Distributed App provides a simple way to share states and
* parameters between a simulator and renderer apps using a
* single source file.
* You must define a state to be broadcast to all listeners
* This state is synchronous but unreliable information
* i.e. missed data should not affect the overall state of the
* application
*/
struct SharedState {
int frameCount;
};
// Inherit from DistributedApp and template it on the shared
// state data struct
class MyApp : public DistributedApp<SharedState>
{
public:
virtual void onCreate() override {
// Set the camera to view the scene
nav().pos(Vec3d(0,0,8));
// Prepare mesh to draw a cone
addCone(mesh);
mesh.primitive(Mesh::LINES);
// Register the parameters with the GUI
gui << X << Y << Size;
gui.init(); // Initialize GUI. Don't forget this!
// DistributedApp provides a parameter server.
// This links the parameters between "simulator" and "renderers" automatically
parameterServer() << X << Y << Size;
font.loadDefault(24);
navControl().active(false);
}
// The simulate function is only called on the "simulator" instance
virtual void simulate(double dt) override {
// You should write to the state on this function
// Then the state will be available anywhere else in the application
// (even for instances that are running on a different node on the
// network)
state().frameCount++;
}
virtual void onAnimate(double dt) override {
}
virtual void onDraw(Graphics &g) override
{
g.clear();
g.pushMatrix();
// You can get a parameter's value using the get() member function
g.translate(X.get(), Y.get(), 0);
g.scale(Size.get());
g.draw(mesh); // Draw the mesh
g.color(1);
g.blendAdd();
g.blending(true);
font.render(g, std::to_string(state().frameCount));
g.popMatrix();
// Draw th GUI on the simulator only
if (role() == ROLE_SIMULATOR || role() == ROLE_DESKTOP) {
gui.draw(g);
}
}
private:
Mesh mesh;
FontModule font;
Parameter X {"X", "Position", 0.0, "", -1.0f, 1.0f};
Parameter Y {"Y", "Position", 0.0, "", -1.0f, 1.0f};
Parameter Size {"Scale", "Size", 1.0, "", 0.1f, 3.0f};
/* DistributedApp provides a parameter server. In fact it will
* crash if you have a parameter server with the same port,
* as it will raise an exception when it is unable to acquire
* the port
*/
// ParameterServer paramServer {"127.0.0.1", 9010};
ControlGUI gui;
};
int main(int argc, char *argv[])
{
MyApp app;
app.dimensions(800, 600);
app.start();
return 0;
}