104 lines
2.7 KiB
C++
104 lines
2.7 KiB
C++
/*
|
|
* Copyright 2016 - 2022 Gary Talent (gary@drinkingtea.net). All rights reserved.
|
|
*/
|
|
|
|
#include <GLFW/glfw3.h>
|
|
#include <imgui_impl_opengl3.h>
|
|
#include <imgui_impl_glfw.h>
|
|
|
|
#include <nostalgia/core/config.hpp>
|
|
#include <nostalgia/core/userland/gfx.hpp>
|
|
|
|
#include "core.hpp"
|
|
|
|
namespace nostalgia::core {
|
|
|
|
constexpr auto Scale = 5;
|
|
|
|
static void handleGlfwError(int err, const char *desc) noexcept {
|
|
oxErrf("GLFW error ({}): {}\n", err, desc);
|
|
}
|
|
|
|
void ImGui_Impl_NewFrame() noexcept {
|
|
ImGui_ImplGlfw_NewFrame();
|
|
}
|
|
|
|
static void handleKeyPress(Context *ctx, int key) noexcept {
|
|
switch (key) {
|
|
case GLFW_KEY_ESCAPE:
|
|
case GLFW_KEY_Q:
|
|
shutdown(ctx);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void handleGlfwKeyEvent(GLFWwindow *window, int key, int, int action, int) noexcept {
|
|
const auto ctx = static_cast<Context *>(glfwGetWindowUserPointer(window));
|
|
if (action == GLFW_PRESS) {
|
|
handleKeyPress(ctx, key);
|
|
}
|
|
}
|
|
|
|
ox::Error initGfx(Context *ctx) noexcept {
|
|
auto id = ctx->windowerData<GlfwImplData>();
|
|
glfwSetErrorCallback(handleGlfwError);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
if constexpr(ox::defines::OS == ox::defines::OS::Darwin) {
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
|
|
}
|
|
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
|
|
id->window = glfwCreateWindow(240 * Scale, 160 * Scale, "nostalgia", nullptr, nullptr);
|
|
if (id->window == nullptr) {
|
|
return OxError(1, "Could not open GLFW window");
|
|
}
|
|
glfwSetKeyCallback(id->window, handleGlfwKeyEvent);
|
|
glfwSetWindowUserPointer(id->window, ctx);
|
|
glfwMakeContextCurrent(id->window);
|
|
if constexpr(config::ImGuiEnabled) {
|
|
IMGUI_CHECKVERSION();
|
|
ImGui::CreateContext();
|
|
auto &io = ImGui::GetIO();
|
|
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
|
|
//io.MouseDrawCursor = true;
|
|
ImGui_ImplGlfw_InitForOpenGL(id->window, true);
|
|
}
|
|
return renderer::init(ctx);
|
|
}
|
|
|
|
void setWindowTitle(Context *ctx, const char *title) noexcept {
|
|
const auto id = ctx->windowerData<GlfwImplData>();
|
|
glfwSetWindowTitle(id->window, title);
|
|
}
|
|
|
|
void focusWindow(Context *ctx) noexcept {
|
|
const auto id = ctx->windowerData<GlfwImplData>();
|
|
glfwFocusWindow(id->window);
|
|
}
|
|
|
|
int getScreenWidth(Context *ctx) noexcept {
|
|
auto id = ctx->windowerData<GlfwImplData>();
|
|
int w = 0, h = 0;
|
|
glfwGetFramebufferSize(id->window, &w, &h);
|
|
return w;
|
|
}
|
|
|
|
int getScreenHeight(Context *ctx) noexcept {
|
|
auto id = ctx->windowerData<GlfwImplData>();
|
|
int w = 0, h = 0;
|
|
glfwGetFramebufferSize(id->window, &w, &h);
|
|
return h;
|
|
}
|
|
|
|
common::Size getScreenSize(Context *ctx) noexcept {
|
|
auto id = ctx->windowerData<GlfwImplData>();
|
|
int w = 0, h = 0;
|
|
glfwGetFramebufferSize(id->window, &w, &h);
|
|
return {w, h};
|
|
}
|
|
|
|
}
|