CPP-TOOLBOX
Loading...
Searching...
No Matches
window.hpp
Go to the documentation of this file.
1#ifndef WINDOW_HPP
2#define WINDOW_HPP
3
4#include <glad/glad.h>
5#include <GLFW/glfw3.h>
6
7#include <numeric>
8#include <optional>
9#include <ostream>
10#include <vector>
11
13
14struct VideoMode {
15 int width;
16 int height;
18
19 friend std::ostream &operator<<(std::ostream &os, const VideoMode &vm) {
20 os << vm.width << "x" << vm.height << " @ " << vm.refresh_rate << "Hz";
21 return os;
22 }
23};
24
25std::vector<std::string> get_available_resolutions(const std::optional<std::string> &aspect_ratio = std::nullopt);
26
27class Window {
28 private:
29 Logger logger{"window"};
30
31 public:
32 unsigned int width_px, height_px;
34
35 Window(unsigned int width_px = 700, unsigned int height_px = 700, const std::string &window_name = "my program",
36 bool start_in_fullscreen = false, bool start_with_mouse_captured = false, bool vsync = false,
37 bool print_out_opengl_data = false);
38 ~Window();
39 GLFWwindow *glfw_window;
40 void print_opengl_info();
41 void toggle_mouse_mode();
42 void disable_cursor();
43 void enable_cursor();
44
45 bool window_should_close() { return glfwWindowShouldClose(glfw_window); }
46
47 // these functions only exist so I don't have to rember the opengl api for this
50
51 void set_resolution(const std::string &resolution);
52
53 void toggle_fullscreen();
54 void enable_fullscreen();
55 void disable_fullscreen();
56 void set_fullscreen_by_on_off(const std::string &on_off_string);
57
58 std::tuple<unsigned int, unsigned int> reduce_ratio(std::tuple<unsigned int, unsigned int> ratio) {
59 auto [num, den] = ratio;
60 if (den == 0) {
61 // Handle division by zero gracefully (you can also throw an exception)
62 return {0, 0};
63 }
64
65 unsigned int g = std::gcd(num, den);
66 return {num / g, den / g};
67 }
68
69 std::tuple<unsigned int, unsigned int> get_aspect_ratio_in_simplest_terms() {
70 return reduce_ratio({this->width_px, this->height_px});
71 }
72
73 std::function<void(double)> wrap_tick_with_required_glfw_calls(std::function<void(double)> tick) {
74 return [tick, this](double dt) {
75 // clear buffers before tick
76 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
77
78 tick(dt);
79
80 // swap and poll after tick
81 glfwSwapBuffers(glfw_window);
82 glfwPollEvents();
83 };
84 }
85
86 bool cursor_is_disabled = false;
88};
89
90#endif // WINDOW_HPP
Definition logger.hpp:22
unsigned int width_px
Definition window.hpp:32
void toggle_fullscreen()
Definition window.cpp:248
void enable_cursor()
Definition window.cpp:240
unsigned int height_px
Definition window.hpp:32
std::tuple< unsigned int, unsigned int > reduce_ratio(std::tuple< unsigned int, unsigned int > ratio)
Definition window.hpp:58
~Window()
Definition window.cpp:88
void disable_wireframe_mode()
Definition window.cpp:246
void enable_fullscreen()
Definition window.cpp:269
bool cursor_is_disabled
Definition window.hpp:86
std::function< void(double)> wrap_tick_with_required_glfw_calls(std::function< void(double)> tick)
Definition window.hpp:73
bool window_should_close()
Definition window.hpp:45
bool window_in_fullscreen
Definition window.hpp:87
void toggle_mouse_mode()
Definition window.cpp:227
Window(unsigned int width_px=700, unsigned int height_px=700, const std::string &window_name="my program", bool start_in_fullscreen=false, bool start_with_mouse_captured=false, bool vsync=false, bool print_out_opengl_data=false)
Definition window.cpp:11
int top_left_corner_of_window_x
Definition window.hpp:33
std::tuple< unsigned int, unsigned int > get_aspect_ratio_in_simplest_terms()
Definition window.hpp:69
void enable_wireframe_mode()
Definition window.cpp:245
void print_opengl_info()
make a glfw window
Definition window.cpp:108
GLFWwindow * glfw_window
Definition window.hpp:39
void set_resolution(const std::string &resolution)
Definition window.cpp:368
void set_fullscreen_by_on_off(const std::string &on_off_string)
Definition window.cpp:382
void disable_fullscreen()
Definition window.cpp:282
int top_left_corner_of_window_y
Definition window.hpp:33
void disable_cursor()
Definition window.cpp:236
@ g
Definition input_state.hpp:33
Definition window.hpp:14
int width
Definition window.hpp:15
int height
Definition window.hpp:16
int refresh_rate
Definition window.hpp:17
friend std::ostream & operator<<(std::ostream &os, const VideoMode &vm)
Definition window.hpp:19
std::vector< std::string > get_available_resolutions(const std::optional< std::string > &aspect_ratio=std::nullopt)
Definition window.cpp:357