#ifndef _CONTEXT_H #define _CONTEXT_H extern "C" { #include #include #include } #include 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