Tatooine
window.h
Go to the documentation of this file.
1#ifndef TATOOINE_GL_WINDOW_H
2#define TATOOINE_GL_WINDOW_H
3//==============================================================================
7#include <tatooine/gl/imgui.h>
10
11#include <array>
12#include <iostream>
13#include <list>
14#include <memory>
15#include <string>
16#include <thread>
17//==============================================================================
18namespace tatooine::gl {
19//==============================================================================
20class window : public window_notifier, public window_listener {
21 public:
22 //============================================================================
23 // members
24 //============================================================================
25 std::unique_ptr<glfw::window> m_glfw_window;
26 std::unique_ptr<struct imgui_render_backend> m_imgui_render_backend;
27 std::list<std::thread> m_async_tasks;
28 std::vector<std::list<std::thread>::iterator> m_joinable_async_tasks;
30 //============================================================================
31 auto imgui_render_backend() const -> auto const & {
33 }
34 auto imgui_render_backend() -> auto & { return *m_imgui_render_backend; }
35 //============================================================================
36 // ctors / dtor
37 //============================================================================
38 public:
39 window(const std::string &title, size_t width, size_t height);
41
42 //============================================================================
43 // methods
44 //============================================================================
45 public:
47 void release();
48 void refresh();
52 void on_key_pressed(key /*k*/) override;
53 void on_key_released(key /*k*/) override;
54 void on_button_pressed(button /*b*/) override;
55 void on_button_released(button /*b*/) override;
56 void on_wheel_up() override;
57 void on_wheel_down() override;
58 void on_wheel_left() override;
59 void on_wheel_right() override;
60 void on_cursor_moved(double /*x*/, double /*y*/) override;
61 void on_resize(int /*width*/, int /*height*/) override;
62 auto get() -> auto& {return *m_glfw_window;}
63 auto get() const -> auto const& {return *m_glfw_window;}
64 //----------------------------------------------------------------------------
65 auto should_close() const { return m_glfw_window->should_close(); }
67 auto monitor = glfwGetPrimaryMonitor();
68 std::pair<int, int> res;
69 glfwGetMonitorPhysicalSize(monitor, &res.first, &res.second);
70 return res;
71 }
72 //----------------------------------------------------------------------------
73 template <typename F>
74 void do_async(F &&f) {
75 auto it = [this] {
76 auto lock = std::lock_guard{m_async_tasks_mutex};
77 m_async_tasks.emplace_back();
78 return prev(end(m_async_tasks));
79 }();
80
81 *it = std::thread{[this, it, f = std::forward<F>(f)] {
82 auto ctx = context{*this};
83 ctx.make_current();
84 f();
85 std::lock_guard task_lock{m_async_tasks_mutex};
86 m_joinable_async_tasks.push_back(it);
87 }};
88 }
89
90 private:
91 void setup(const std::string &title, size_t width, size_t height);
92 void init_imgui(size_t width, size_t height);
94};
95//==============================================================================
96} // namespace tatooine::gl
97//==============================================================================
98#endif
Definition: context.h:16
Definition: window.h:20
auto should_close() const
Definition: window.h:65
void on_resize(int, int) override
auto get() -> auto &
Definition: window.h:62
std::unique_ptr< struct imgui_render_backend > m_imgui_render_backend
Definition: window.h:26
void setup(const std::string &title, size_t width, size_t height)
void on_wheel_right() override
void on_button_pressed(button) override
void on_wheel_up() override
auto imgui_render_backend() -> auto &
Definition: window.h:34
window(const std::string &title, size_t width, size_t height)
void on_key_pressed(key) override
std::vector< std::list< std::thread >::iterator > m_joinable_async_tasks
Definition: window.h:28
std::unique_ptr< glfw::window > m_glfw_window
Definition: window.h:25
std::mutex m_async_tasks_mutex
Definition: window.h:29
void on_key_released(key) override
auto imgui_render_backend() const -> auto const &
Definition: window.h:31
void do_async(F &&f)
Definition: window.h:74
auto primary_screen_resolution() const
Definition: window.h:66
auto get() const -> auto const &
Definition: window.h:63
void on_wheel_down() override
void on_cursor_moved(double, double) override
void on_wheel_left() override
std::list< std::thread > m_async_tasks
Definition: window.h:27
void on_button_released(button) override
void init_imgui(size_t width, size_t height)
Definition: ansiformat.h:6
button
Definition: mouse.h:8
key
Definition: keyboard.h:9
auto end(buffer_map< ArrayType, T, Access > &map)
Definition: buffer.h:104
auto prev(Iter iter)
Definition: iterator_facade.h:343
Definition: window_listener.h:10
Definition: window_notifier.h:122