Tatooine
first_person_window.h
Go to the documentation of this file.
1#ifndef TATOOINE_RENDERING_FIRST_PERSON_WINDOW_H
2#define TATOOINE_RENDERING_FIRST_PERSON_WINDOW_H
3//==============================================================================
6#include <tatooine/holder.h>
7#include <tatooine/ray.h>
9
10#include <chrono>
11#include <cmath>
12//==============================================================================
13namespace tatooine::rendering {
14//==============================================================================
17 std::size_t m_width, m_height;
19 std::chrono::time_point<std::chrono::system_clock> m_time =
20 std::chrono::system_clock::now();
21 //============================================================================
22 first_person_window(std::size_t width = 800, std::size_t height = 600)
23 : gl::window{"tatooine first person window", width, height},
27 m_time{std::chrono::system_clock::now()} {
30 m_camera_controller.on_resize(static_cast<int>(width), static_cast<int>(height));
31 }
32 virtual ~first_person_window() = default;
33 //============================================================================
34 auto width() const { return m_width; }
35 auto height() const { return m_height; }
36 //----------------------------------------------------------------------------
37 auto camera_controller() -> auto& { return m_camera_controller; }
38 auto camera_controller() const -> auto const& { return m_camera_controller; }
39 //----------------------------------------------------------------------------
40 template <typename Event>
41 auto render_loop(Event&& event) {
42 m_time = std::chrono::system_clock::now();
43 while (!should_close()) {
44 refresh();
45 m_camera_controller.active_camera().set_gl_viewport();
46 auto const before = std::chrono::system_clock::now();
47 update(std::forward<Event>(event),
48 std::chrono::system_clock::now() - m_time);
49 m_time = before;
52 }
53 }
54 //----------------------------------------------------------------------------
55 template <typename F>
56 auto update(F&& f, std::chrono::duration<double> const& dt) {
58 f(dt);
59 }
60 //----------------------------------------------------------------------------
61 auto projection_matrix() const {
63 }
64 //----------------------------------------------------------------------------
65 auto view_matrix() const { return m_camera_controller.view_matrix(); }
66 //----------------------------------------------------------------------------
67 auto on_resize(int w, int h) -> void override {
69 m_width = static_cast<std::size_t>(w);
70 m_height = static_cast<std::size_t>(h);
72 }
73 //----------------------------------------------------------------------------
74 auto on_key_pressed(gl::key k) -> void override {
76 if (k == gl::key::KEY_F2) {
77 camera_controller().use_orthographic_camera();
78 camera_controller().use_orthographic_controller();
79 } else if (k == gl::key::KEY_F3) {
80 camera_controller().use_perspective_camera();
81 camera_controller().use_fps_controller();
82 } else if (k == gl::key::KEY_F4) {
83 camera_controller().look_at({0, 0, -1}, {0, 0, 0});
84 }
85 }
86};
87//==============================================================================
88} // namespace tatooine::rendering
89//==============================================================================
90#endif
Definition: window.h:20
auto should_close() const
Definition: window.h:65
void on_resize(int, int) override
window(const std::string &title, size_t width, size_t height)
void on_key_pressed(key) override
key
Definition: keyboard.h:9
DLL_API auto enable_depth_test() -> void
Definition: camera.h:12
void add_listener(window_listener &l)
Definition: camera_controller.h:64
auto projection_matrix() const
Definition: camera_controller.h:130
void on_resize(int w, int h) override
Definition: camera_controller.h:200
void update(std::chrono::duration< double > const &dt)
Definition: camera_controller.h:209
auto view_matrix() const
Definition: camera_controller.h:134
auto active_camera() const -> auto const &
Definition: camera_controller.h:100
Definition: first_person_window.h:15
auto render_loop(Event &&event)
Definition: first_person_window.h:41
auto view_matrix() const
Definition: first_person_window.h:65
auto width() const
Definition: first_person_window.h:34
auto update(F &&f, std::chrono::duration< double > const &dt)
Definition: first_person_window.h:56
std::chrono::time_point< std::chrono::system_clock > m_time
Definition: first_person_window.h:19
std::size_t m_width
Definition: first_person_window.h:17
auto height() const
Definition: first_person_window.h:35
auto projection_matrix() const
Definition: first_person_window.h:61
first_person_window(std::size_t width=800, std::size_t height=600)
Definition: first_person_window.h:22
std::size_t m_height
Definition: first_person_window.h:17
struct camera_controller< float > m_camera_controller
Definition: first_person_window.h:18
auto camera_controller() -> auto &
Definition: first_person_window.h:37
auto on_resize(int w, int h) -> void override
Definition: first_person_window.h:67
auto camera_controller() const -> auto const &
Definition: first_person_window.h:38
auto on_key_pressed(gl::key k) -> void override
Definition: first_person_window.h:74