Allolib  1.0
C++ Components For Interactive Multimedia
al_OpenGLGraphicsDomain.hpp
1 #ifndef GRAPHICSDOMAIN_H
2 #define GRAPHICSDOMAIN_H
3 
4 #include <cassert>
5 #include <functional>
6 #include <iostream>
7 #include <memory>
8 #include <stack>
9 #include <vector>
10 
11 #include "al/app/al_ComputationDomain.hpp"
12 #include "al/app/al_FPS.hpp"
13 #include "al/graphics/al_Graphics.hpp"
14 #include "al/io/al_ControlNav.hpp"
15 #include "al/io/al_Window.hpp"
16 
17 namespace al {
18 
19 class GLFWOpenGLWindowDomain;
20 
22  Window::Cursor cursor = Window::Cursor::POINTER;
23  bool cursorVisible = true;
24  Window::Dim dimensions{50, 50, 640, 480};
26  bool fullScreen = false;
27  std::string title = "Alloapp";
28  bool vsync = true;
29  bool decorated = true;
30 };
31 
39 public:
40  virtual ~OpenGLGraphicsDomain() {}
41 
42  // Domain functions
43  bool init(ComputationDomain *parent = nullptr) override;
44  bool start() override;
45  bool stop() override;
46  bool cleanup(ComputationDomain *parent = nullptr) override;
47 
48  void quit() { mShouldQuitApp = true; }
49  bool shouldQuit() { return mShouldQuitApp || mSubDomainList.size() == 0; }
50 
51  bool running() { return mRunning; }
52 
60  std::shared_ptr<GLFWOpenGLWindowDomain> newWindow();
61 
62  void closeWindow(std::shared_ptr<GLFWOpenGLWindowDomain> windowDomain);
63 
64  // Next window details
65  WindowSetupProperties nextWindowProperties;
66 
67  std::function<void(void)> onCreate = []() {};
68  std::function<void()> onExit = []() {};
69 
70  // Virtual functions to override
71  virtual void preOnCreate() {}
72 
73  virtual void postOnExit() {}
74 
75 private:
76  std::atomic<bool> mShouldQuitApp{false};
77  bool mRunning{false};
78 };
79 
90 public:
92  // Domain functions
93  bool init(ComputationDomain *parent = nullptr) override;
94 
95  bool tick() override;
96 
97  bool cleanup(ComputationDomain *parent = nullptr) override;
98 
102  std::function<void()> onNewFrame = [this]() {
103  mNav.smooth(std::pow(0.0001, mTimeDrift));
104  mNav.step(mTimeDrift * mParent->fps());
105  };
106 
111  std::function<void()> preOnDraw = [this]() {
112  mGraphics->framebuffer(FBO::DEFAULT);
113  mGraphics->viewport(0, 0, mWindow->fbWidth(), mWindow->fbHeight());
114  mGraphics->resetMatrixStack();
115  mGraphics->camera(mView);
116  mGraphics->color(1, 1, 1);
117  };
118 
122  std::function<void(Graphics &)> onDraw = [](Graphics &g) { g.clear(0.3); };
123 
128  std::function<void()> postOnDraw = []() {};
129 
130  Viewpoint &view() { return mView; }
131  const Viewpoint &view() const { return mView; }
132 
133  Nav &nav() { return mNav; }
134  // Nav& nav() { return mNav; }
135  const Nav &nav() const { return mNav; }
136 
137  NavInputControl &navControl() { return mNavControl; }
138 
139  Window &window() { return *mWindow; }
140 
141  Graphics &graphics() { return *mGraphics; }
142 
143 private:
144  std::unique_ptr<Window> mWindow;
145 
146  std::unique_ptr<Graphics> mGraphics;
147 
148  OpenGLGraphicsDomain *mParent;
149 
150  Nav mNav; // is a Pose itself and also handles manipulation of pose
151  Viewpoint mView{mNav.transformed()}; // Pose with Lens and acts as camera
152  NavInputControl mNavControl{mNav}; // interaction with keyboard and mouse
153 };
154 
155 } // namespace al
156 
157 #endif // GRAPHICSDOMAIN_H
ComputationDomain class.
FPS class.
Definition: al_FPS.hpp:15
bool cleanup(ComputationDomain *parent=nullptr) override
cleanup
std::function< void(Graphics &)> onDraw
bool init(ComputationDomain *parent=nullptr) override
initialize
bool tick() override
Execute a pass of the domain.
Interface for loading fonts and rendering text.
Definition: al_Graphics.hpp:63
void step(double dt=1)
Accumulate pose based on velocity.
double smooth() const
Get smoothing amount.
OpenGLGraphicsDomain class.
bool start() override
start the asyncrhonous execution of the domain
bool init(ComputationDomain *parent=nullptr) override
initialize
bool cleanup(ComputationDomain *parent=nullptr) override
cleanup
bool stop() override
stop the asyncrhonous execution of the domain
std::shared_ptr< GLFWOpenGLWindowDomain > newWindow()
Create a new window.
Viewpoint within a scene.
Cursor
Cursor icon types.
Definition: al_Window.hpp:270
DisplayMode
Window display mode bit flags.
Definition: al_Window.hpp:256
Definition: al_App.hpp:23
Window pixel dimensions.
Definition: al_Window.hpp:277