mirror of
https://gitea.windcorp.ru/Wind-Corporation/Progressia.git
synced 2025-12-15 22:58:36 +03:00
TMP / Made {GLFW,Vulkan}Managers more OO
This commit is contained in:
@@ -8,61 +8,95 @@
|
||||
|
||||
#include "../../main/logging.h"
|
||||
#include "../../main/meta.h"
|
||||
#include "vulkan_mgmt.h"
|
||||
#include "../../main/util.h"
|
||||
|
||||
using namespace progressia::main::logging;
|
||||
|
||||
namespace progressia::desktop {
|
||||
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): global variable required for GLFW callbacks
|
||||
static GLFWwindow *window = nullptr;
|
||||
|
||||
static void onGlfwError(int errorCode, const char *description);
|
||||
static void onWindowGeometryChange(GLFWwindow *window, int width, int height);
|
||||
|
||||
void initializeGlfw() {
|
||||
debug("Beginning GLFW init");
|
||||
class GlfwManagerImpl : public GlfwManager {
|
||||
private:
|
||||
GLFWwindow *window = nullptr;
|
||||
std::function<void()> onScreenResize = nullptr;
|
||||
|
||||
glfwSetErrorCallback(onGlfwError);
|
||||
public:
|
||||
DISABLE_COPYING(GlfwManagerImpl)
|
||||
DISABLE_MOVING(GlfwManagerImpl)
|
||||
|
||||
if (!glfwInit()) {
|
||||
fatal("glfwInit() failed");
|
||||
GlfwManagerImpl() {
|
||||
debug("Beginning GLFW init");
|
||||
|
||||
glfwSetErrorCallback(onGlfwError);
|
||||
|
||||
if (!glfwInit()) {
|
||||
fatal("glfwInit() failed");
|
||||
// REPORT_ERROR
|
||||
exit(1);
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
|
||||
|
||||
std::string title;
|
||||
|
||||
{
|
||||
std::stringstream accumulator;
|
||||
accumulator << progressia::main::meta::NAME << " "
|
||||
<< progressia::main::meta::VERSION << " build "
|
||||
<< progressia::main::meta::BUILD_ID;
|
||||
title = accumulator.str();
|
||||
}
|
||||
|
||||
constexpr auto windowDimensions = 800;
|
||||
window = glfwCreateWindow(windowDimensions, windowDimensions,
|
||||
title.c_str(), nullptr, nullptr);
|
||||
|
||||
glfwSetWindowSizeCallback(window, onWindowGeometryChange);
|
||||
|
||||
debug("GLFW init complete");
|
||||
}
|
||||
|
||||
~GlfwManagerImpl() override { glfwTerminate(); }
|
||||
|
||||
void setOnScreenResize(std::function<void()> hook) override {
|
||||
onScreenResize = hook;
|
||||
}
|
||||
|
||||
void showWindow() override {
|
||||
glfwShowWindow(window);
|
||||
debug("Window now visible");
|
||||
}
|
||||
|
||||
bool shouldRun() override { return !glfwWindowShouldClose(window); }
|
||||
|
||||
void doGlfwRoutine() override { glfwPollEvents(); }
|
||||
|
||||
friend GLFWwindow *getGLFWWindowHandle();
|
||||
friend void onWindowGeometryChange(GLFWwindow *, int, int);
|
||||
};
|
||||
|
||||
namespace {
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables): global variables required by GLFW C callbacks
|
||||
std::weak_ptr<GlfwManagerImpl> theGlfwManager;
|
||||
} // namespace
|
||||
|
||||
std::shared_ptr<GlfwManager> makeGlfwManager() {
|
||||
if (!theGlfwManager.expired()) {
|
||||
fatal("GlfwManager already exists");
|
||||
// REPORT_ERROR
|
||||
exit(1);
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
|
||||
std::shared_ptr<GlfwManagerImpl> aGlfwManager =
|
||||
std::make_shared<GlfwManagerImpl>();
|
||||
|
||||
std::string title;
|
||||
|
||||
{
|
||||
std::stringstream accumulator;
|
||||
accumulator << progressia::main::meta::NAME << " "
|
||||
<< progressia::main::meta::VERSION << " build "
|
||||
<< progressia::main::meta::BUILD_ID;
|
||||
title = accumulator.str();
|
||||
}
|
||||
|
||||
constexpr auto windowDimensions = 800;
|
||||
window = glfwCreateWindow(windowDimensions, windowDimensions, title.c_str(),
|
||||
nullptr, nullptr);
|
||||
|
||||
glfwSetWindowSizeCallback(window, onWindowGeometryChange);
|
||||
|
||||
debug("GLFW init complete");
|
||||
theGlfwManager = aGlfwManager;
|
||||
return aGlfwManager;
|
||||
}
|
||||
|
||||
void showWindow() {
|
||||
glfwShowWindow(window);
|
||||
debug("Window now visible");
|
||||
}
|
||||
|
||||
bool shouldRun() { return !glfwWindowShouldClose(window); }
|
||||
|
||||
void doGlfwRoutine() { glfwPollEvents(); }
|
||||
|
||||
void shutdownGlfw() { glfwTerminate(); }
|
||||
|
||||
void onGlfwError(int errorCode, const char *description) {
|
||||
fatal() << "[GLFW] " << description << " (" << errorCode << ")";
|
||||
// REPORT_ERROR
|
||||
@@ -71,13 +105,25 @@ void onGlfwError(int errorCode, const char *description) {
|
||||
|
||||
void onWindowGeometryChange(GLFWwindow *window, [[maybe_unused]] int width,
|
||||
[[maybe_unused]] int height) {
|
||||
if (window != progressia::desktop::window) {
|
||||
if (auto manager = theGlfwManager.lock()) {
|
||||
if (manager->window != window) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (manager->onScreenResize != nullptr) {
|
||||
manager->onScreenResize();
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
resizeVulkanSurface();
|
||||
}
|
||||
|
||||
GLFWwindow *getGLFWWindowHandle() { return window; }
|
||||
GLFWwindow *getGLFWWindowHandle() {
|
||||
if (auto manager = theGlfwManager.lock()) {
|
||||
return manager->window;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace progressia::desktop
|
||||
|
||||
Reference in New Issue
Block a user