1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
#ifndef _CONTEXT_H
#define _CONTEXT_H
extern "C" {
#include <nnwt/time.h>
#include <stl/window.h>
#include <stl/gl.h>
}
#include <string>
namespace Mauri
{
static bool pointer_pos_callback(void *userdata, f64 x, f64 y);
static void should_close_callback(void *userdata);
class Context
{
public:
Context() : start(nn_get_timestamp()) {}
~Context()
{
if (this->window) this->window->free(&this->window);
}
f64 pointer_x;
f64 pointer_y;
bool should_close = false;
auto create_window(s32 width, s32 height, const std::string &name, const std::string monitor) -> bool
{
this->window = stl_window_create();
this->window->pointer_pos_callback = pointer_pos_callback;
this->window->should_close_callback = should_close_callback;
this->window->userdata = this;
s32 flags = STELA_WINDOW_VSYNC;
if (!monitor.empty()) flags |= STELA_WINDOW_WALLPAPER;
if (!this->window->create_window(this->window, width, height, flags, monitor.c_str(), name.c_str(), "mauri")) {
return false;
}
this->window->gl_make_current(this->window);
if (!this->window->gl_loader_load(this->window)) {
return false;
}
glEnable(GL_BLEND);
return true;
}
auto width() -> s32
{
return this->window->width;
}
auto height() -> s32
{
return this->window->height;
}
auto resize_window(s32 width, s32 height) -> void
{
this->window->resize(this->window, width, height);
}
auto should_close_window() const -> bool
{
return this->should_close;
}
auto close_window() -> void
{
this->should_close = true;
}
auto is_draw_enabled() const -> bool
{
return this->draw_enabled;
}
auto set_draw_enabled(bool enabled) -> void
{
this->draw_enabled = enabled;
}
auto poll() -> void
{
this->window->poll(this->window, false);
this->window->process_events(this->window);
}
auto swap_buffers() -> void
{
this->window->gl_swap_buffers(this->window);
};
auto current_time() const -> f64
{
return (nn_get_timestamp() - this->start) / 1000000.f;
}
auto check_error() -> void
{
stl_gl_check_error();
}
private:
u64 start;
bool draw_enabled = true;
struct stl_window *window = nullptr;
};
bool pointer_pos_callback(void *userdata, f64 x, f64 y)
{
Context *c = (Context *)userdata;
c->pointer_x = x;
c->pointer_y = y;
return true;
}
void should_close_callback(void *userdata)
{
Context *c = (Context *)userdata;
c->should_close = true;
}
extern auto context() -> Context *;
extern auto set_context(Context *c) -> void;
} // namespace Mauri
#endif // _CONTEXT_H
|