OGLplus  (0.59.0) a C++ wrapper for rendering APIs

context.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_APPLICATION_CONTEXT_HPP
10 #define EAGINE_APPLICATION_CONTEXT_HPP
11 
12 #include "../../oalplus/al_api_fwd.hpp"
13 #include "../../oglplus/gl_api_fwd.hpp"
14 #include "../application_config.hpp"
15 #include "../assert.hpp"
16 #include "../flat_map.hpp"
17 #include "../main_ctx_object.hpp"
18 #include "../memory/buffer.hpp"
19 #include "interface.hpp"
20 #include "options.hpp"
21 #include "state_view.hpp"
22 #include <map>
23 
24 namespace eagine::application {
25 //------------------------------------------------------------------------------
35 public:
38  std::shared_ptr<video_provider> provider) noexcept
39  : _parent{parent}
40  , _provider{std::move(provider)} {}
41 
43  auto parent() const noexcept -> execution_context& {
44  return _parent;
45  }
46 
48  auto frame_number() const noexcept {
49  return _frame_no;
50  }
51 
55  void begin();
56 
60  void end();
61 
65  void commit();
66 
70  auto init_gl_api() noexcept -> bool;
71 
75  auto has_gl_api() const noexcept {
76  return bool(_gl_api);
77  }
78 
82  auto gl_api() const noexcept -> oglp::gl_api& {
83  EAGINE_ASSERT(has_gl_api());
84  return *_gl_api;
85  }
86 
88  auto surface_size() noexcept -> std::tuple<int, int> {
89  if(EAGINE_LIKELY(_provider)) {
90  return extract(_provider).surface_size();
91  }
92  return {1, 1};
93  }
94 
96  auto surface_aspect() noexcept -> float {
97  if(EAGINE_LIKELY(_provider)) {
98  return extract(_provider).surface_aspect();
99  }
100  return 1.F;
101  }
102 
104  void clean_up() noexcept;
105 
106 private:
107  execution_context& _parent;
108  long _frame_no{0};
109  std::shared_ptr<video_provider> _provider{};
110  std::shared_ptr<oglp::gl_api> _gl_api{};
111  std::shared_ptr<video_context_state> _state{};
112 };
113 //------------------------------------------------------------------------------
123 public:
125  execution_context& parent,
126  std::shared_ptr<audio_provider> provider) noexcept
127  : _parent{parent}
128  , _provider{std::move(provider)} {
129  EAGINE_MAYBE_UNUSED(_parent);
130  }
131 
135  auto init_al_api() noexcept -> bool;
136 
140  auto has_al_api() const noexcept {
141  return bool(_al_api);
142  }
143 
147  auto al_api() const noexcept -> auto& {
148  EAGINE_ASSERT(has_al_api());
149  return *_al_api;
150  }
151 
153  void clean_up() noexcept;
154 
155 private:
156  execution_context& _parent;
157  std::shared_ptr<audio_provider> _provider{};
158  std::shared_ptr<oalp::al_api> _al_api{};
159 };
160 //------------------------------------------------------------------------------
167  : public main_ctx_object
168  , private input_sink {
169 public:
170  execution_context(main_ctx_parent parent) noexcept
171  : main_ctx_object(EAGINE_ID(AppExecCtx), parent)
172  , _options{*this} {}
173 
175  auto result() const noexcept -> int {
176  return _exec_result;
177  }
178 
180  auto options() const noexcept -> const launch_options& {
181  return _options;
182  }
183 
185  auto buffer() const noexcept -> memory::buffer&;
186 
188  auto state() const noexcept -> const context_state_view&;
189 
191  auto prepare(std::unique_ptr<launchpad> pad) -> execution_context&;
192 
195  auto is_running() noexcept -> bool;
196 
199  void stop_running() noexcept;
200 
202  void update() noexcept;
203 
205  void clean_up() noexcept;
206 
208  auto run() noexcept -> execution_context& {
209  while(is_running()) {
210  update();
211  }
212  clean_up();
213  return *this;
214  }
215 
217  auto enough_run_time() const noexcept -> bool;
218 
220  auto enough_frames(span_size_t frame_no) const noexcept -> bool;
221 
223  auto video_ctx_count() const noexcept {
224  return span_size(_video_contexts.size());
225  }
226 
228  auto video_ctx(span_size_t index = 0) const noexcept -> video_context* {
229  if((index >= 0) && (index < video_ctx_count())) {
230  return _video_contexts[std_size(index)].get();
231  }
232  return nullptr;
233  }
234 
236  auto audio_ctx_count() const noexcept {
237  return span_size(_audio_contexts.size());
238  }
239 
241  auto audio_ctx(span_size_t index = 0) const noexcept -> audio_context* {
242  if((index >= 0) && (index < audio_ctx_count())) {
243  return _audio_contexts[std_size(index)].get();
244  }
245  return nullptr;
246  }
247 
249  auto connect_input(message_id input_id, input_handler handler)
250  -> execution_context&;
251 
254  auto connect_input(const input_slot& input) -> auto& {
255  return connect_input(input.id(), input.handler());
256  }
257 
260 
262  auto map_input(
263  message_id input_id,
264  identifier mapping_id,
265  message_id signal_id,
266  input_setup setup) -> execution_context&;
267 
269  auto map_input(message_id input_id, message_id signal_id, input_setup setup)
270  -> execution_context& {
271  return map_input(input_id, {}, signal_id, setup);
272  }
273 
275  auto map_inputs(identifier mapping_id) -> execution_context&;
276 
279  return map_inputs({});
280  }
281 
284  return connect_inputs().map_inputs(mapping_id);
285  }
286 
289  return setup_inputs({});
290  }
291 
294 
296  auto switch_input_mapping() -> auto& {
297  return switch_input_mapping({});
298  }
299 
300  auto stop_running_input() noexcept -> input_slot {
301  return {
302  EAGINE_MSG_ID(App, Stop),
303  {this, EAGINE_THIS_MEM_FUNC_C(_handle_stop_running)}};
304  }
305 
307  void random_uniform(span<byte> dest);
308 
310  void random_uniform_01(span<float> dest);
311 
313  void random_normal(span<float> dest);
314 
315 private:
316  int _exec_result{0};
317  launch_options _options;
318  std::shared_ptr<context_state> _state;
319  std::unique_ptr<application> _app;
320  bool _keep_running{true};
321 
322  std::vector<std::shared_ptr<hmi_provider>> _hmi_providers;
323  std::vector<std::shared_ptr<input_provider>> _input_providers;
324  std::vector<std::unique_ptr<video_context>> _video_contexts;
325  std::vector<std::unique_ptr<audio_context>> _audio_contexts;
326 
327  auto _setup_providers() -> bool;
328 
329  identifier _input_mapping{EAGINE_ID(initial)};
330 
331  // input id -> handler function reference
332  flat_map<message_id, input_handler> _connected_inputs;
333 
334  // mapping id -> signal id -> (input id, setup)
335  flat_map<
336  identifier,
337  flat_map<message_id, std::tuple<message_id, input_setup>>>
338  _input_mappings;
339 
340  // signal id -> (setup, handler)
341  flat_map<message_id, std::tuple<input_setup, input_handler>> _mapped_inputs;
342 
343  void _handle_stop_running(const input& engaged) {
344  if(engaged) {
345  stop_running();
346  }
347  }
348 
349  template <typename T>
350  void _forward_input(const input_info&, const input_value<T>&) noexcept;
351 
352  void consume(const input_info&, const input_value<bool>&) noexcept final;
353  void consume(const input_info&, const input_value<int>&) noexcept final;
354  void consume(const input_info&, const input_value<float>&) noexcept final;
355  void consume(const input_info&, const input_value<double>&) noexcept final;
356 };
357 //------------------------------------------------------------------------------
358 auto establish(main_ctx&) -> std::unique_ptr<launchpad>;
359 } // namespace eagine::application
360 
361 #if !EAGINE_LINK_LIBRARY || defined(EAGINE_IMPLEMENTING_LIBRARY)
362 #include <eagine/application/context.inl>
363 #endif
364 
365 #endif
Application harness / wrapper code is placed in this namespace.
Definition: eagine.hpp:72
void clean_up() noexcept
Cleans up and releases this rendering context and APIs.
Helper class used to initialize main context objects.
Definition: main_ctx_object.hpp:45
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
Declaration of class template storing a reference to a callable object.
Definition: callable_ref.hpp:24
Read-only view of application context state values.
Definition: state_view.hpp:34
auto options() const noexcept -> const launch_options &
Returns a reference to the launch options.
Definition: context.hpp:180
#define EAGINE_ID(NAME)
Macro for constructing instances of eagine::identifier.
Definition: identifier.hpp:353
auto has_al_api() const noexcept
Indicates if the AL API in this audio context is initialized.
Definition: context.hpp:140
static constexpr auto span_size(T v) noexcept
Converts argument to span size type.
Definition: types.hpp:59
auto parent() const noexcept -> execution_context &
Returns a reference to the parent application execution context.
Definition: context.hpp:43
Base class for main context objects.
Definition: main_ctx_object.hpp:71
auto audio_ctx(span_size_t index=0) const noexcept -> audio_context *
Returns the audio context at the specified index.
Definition: context.hpp:241
#define EAGINE_MSG_ID(API, NAME)
Macro for instantiating objects of static_message_id.
Definition: message_id.hpp:148
Class representing a user input.
Definition: input.hpp:168
static constexpr auto extract(api_result_value< Result, api_result_validity::never > &) noexcept -> Result &
Overload of extract for api_result_value.
Definition: c_api_wrap.hpp:270
auto init_gl_api() noexcept -> bool
Tries to intialize the GL rendering API in this video context.
auto state() const noexcept -> const context_state_view &
Returns a reference to the context state view.
auto setup_inputs() -> execution_context &
Sets-up (connects handlers and binds to signals) default inputs.
Definition: context.hpp:288
void stop_running() noexcept
Stops the main application loop.
auto frame_number() const noexcept
Returns the current video frame number.
Definition: context.hpp:48
main_ctx_object(identifier obj_id, main_ctx_parent parent) noexcept
Initialization from object id and parent.
Definition: main_ctx_object.hpp:77
auto setup_inputs(identifier mapping_id) -> execution_context &
Sets-up (connects handlers and binds to signals) default inputs.
Definition: context.hpp:283
auto prepare(std::unique_ptr< launchpad > pad) -> execution_context &
Prepares the application launch pad object.
Class that managers user input state.
Definition: input.hpp:73
Class that allows binding of a user input device to a handler callable.
Definition: input.hpp:191
Class holding audio playback and recording-related application support objects.
Definition: context.hpp:122
auto video_ctx(span_size_t index=0) const noexcept -> video_context *
Returns the video context at the specified index.
Definition: context.hpp:228
void commit()
Apply the rendering commands done since the last commit (swap buffers).
void begin()
Start working in this video rendering context (make it current).
static constexpr auto std_size(T v) noexcept
Converts argument to std size type.
Definition: types.hpp:52
auto audio_ctx_count() const noexcept
Returns the count of created audio contexts.
Definition: context.hpp:236
auto surface_size() noexcept -> std::tuple< int, int >
Returns the rendering surface's dimensions (in pixels).
Definition: context.hpp:88
void end()
Stop working with this video rendering context.
void random_uniform_01(span< float > dest)
Generates random uniformly-distributed floats in <0, 1> into dest.
auto buffer() const noexcept -> memory::buffer &
Returns a references to a multi-purpose memory buffer.
auto init_al_api() noexcept -> bool
Tries to intialize the AL sound API in this video context.
#define EAGINE_THIS_MEM_FUNC_C(FUNC)
Macro for creating object of member_function_constant in member functions.
Definition: mem_func_const.hpp:206
auto connect_input(const input_slot &input) -> auto &
Connect the specified input slot.
Definition: context.hpp:254
void update() noexcept
Updates this execution context (once per a single main loop iteration).
auto connect_input(message_id input_id, input_handler handler) -> execution_context &
Connect the specified logical input to a callable handler reference.
auto enough_run_time() const noexcept -> bool
Indicates if the application ran long enough.
auto result() const noexcept -> int
Returns the application execution result.
Definition: context.hpp:175
Class holding video rendering-related application support objects.
Definition: context.hpp:34
auto map_input(message_id input_id, identifier mapping_id, message_id signal_id, input_setup setup) -> execution_context &
Map a specified logical input to a physical input signal.
auto run() noexcept -> execution_context &
Starts the main application loop (will block until stopped).
Definition: context.hpp:208
auto surface_aspect() noexcept -> float
Returns the rendering surface's aspect ratio.
Definition: context.hpp:96
Combined wrapper for the GL API operations and constants.
Definition: basic_gl_api.hpp:27
auto al_api() const noexcept -> auto &
Returns a reference to the AL API in this context.
Definition: context.hpp:147
auto map_input(message_id input_id, message_id signal_id, input_setup setup) -> execution_context &
Map a specified logical input to a physical input signal.
Definition: context.hpp:269
auto video_ctx_count() const noexcept
Returns the count of created video contexts.
Definition: context.hpp:223
Class storing two identifier values representing class/method pair.
Definition: message_id.hpp:25
Class managing options for an application with video / audio rendering.
Definition: options.hpp:431
void clean_up() noexcept
Cleans up and releases this audio context and APIs.
auto has_gl_api() const noexcept
Indicates if the GL rendering API in this video context is initialized.
Definition: context.hpp:75
auto map_inputs() -> execution_context &
Binds generic application inputs to default physical input signals.
Definition: context.hpp:278
Class holding shared video/audio rendering application support objects.
Definition: context.hpp:166
auto connect_inputs() -> execution_context &
Connect generic, reusable application logical input slots.
void clean_up() noexcept
Cleans up this execution context and managed objects.
auto enough_frames(span_size_t frame_no) const noexcept -> bool
Indicates if the application rendered enough frames.
auto gl_api() const noexcept -> oglp::gl_api &
Returns a reference to the GL rendering API in this context.
Definition: context.hpp:82
auto is_running() noexcept -> bool
Indicates if the application is running its main loop.
void random_normal(span< float > dest)
Generates random normally-distributed floats into dest.
void random_uniform(span< byte > dest)
Generates random uniformly-distributed bytes into dest.
basic_identifier< 10, 6, default_identifier_char_set, identifier_t > identifier
Default identifier type used throughout the project.
Definition: identifier.hpp:346
auto switch_input_mapping() -> auto &
Switches to the default input mapping.
Definition: context.hpp:296

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).