refactor GLFWWindow
This commit is contained in:
parent
0b8d56afe1
commit
7262119f5b
@ -35,7 +35,7 @@
|
|||||||
#include "util/platform.hpp"
|
#include "util/platform.hpp"
|
||||||
#include "window/Camera.hpp"
|
#include "window/Camera.hpp"
|
||||||
#include "window/input.hpp"
|
#include "window/input.hpp"
|
||||||
#include "window/display.hpp"
|
#include "window/Window.hpp"
|
||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
#include "Mainloop.hpp"
|
#include "Mainloop.hpp"
|
||||||
#include "ServerMainloop.hpp"
|
#include "ServerMainloop.hpp"
|
||||||
@ -95,7 +95,13 @@ void Engine::initialize(CoreParameters coreParameters) {
|
|||||||
|
|
||||||
controller = std::make_unique<EngineController>(*this);
|
controller = std::make_unique<EngineController>(*this);
|
||||||
if (!params.headless) {
|
if (!params.headless) {
|
||||||
auto [window, input] = display::initialize(&settings.display);
|
std::string title = "VoxelCore v" +
|
||||||
|
std::to_string(ENGINE_VERSION_MAJOR) + "." +
|
||||||
|
std::to_string(ENGINE_VERSION_MINOR);
|
||||||
|
if (ENGINE_DEBUG_BUILD) {
|
||||||
|
title += " [debug]";
|
||||||
|
}
|
||||||
|
auto [window, input] = Window::initialize(&settings.display, title);
|
||||||
if (!window || !input){
|
if (!window || !input){
|
||||||
throw initialize_error("could not initialize window");
|
throw initialize_error("could not initialize window");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
#include "debug/Logger.hpp"
|
#include "debug/Logger.hpp"
|
||||||
#include "frontend/screens/MenuScreen.hpp"
|
#include "frontend/screens/MenuScreen.hpp"
|
||||||
#include "frontend/screens/LevelScreen.hpp"
|
#include "frontend/screens/LevelScreen.hpp"
|
||||||
#include "window/display.hpp"
|
#include "window/Window.hpp"
|
||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
|
|
||||||
static debug::Logger logger("mainloop");
|
static debug::Logger logger("mainloop");
|
||||||
|
|||||||
@ -41,7 +41,7 @@
|
|||||||
#include "voxels/GlobalChunks.hpp"
|
#include "voxels/GlobalChunks.hpp"
|
||||||
#include "window/Camera.hpp"
|
#include "window/Camera.hpp"
|
||||||
#include "window/input.hpp"
|
#include "window/input.hpp"
|
||||||
#include "window/display.hpp"
|
#include "window/Window.hpp"
|
||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
#include "world/World.hpp"
|
#include "world/World.hpp"
|
||||||
#include "debug/Logger.hpp"
|
#include "debug/Logger.hpp"
|
||||||
|
|||||||
@ -30,7 +30,7 @@
|
|||||||
#include "util/stringutil.hpp"
|
#include "util/stringutil.hpp"
|
||||||
#include "voxels/Chunks.hpp"
|
#include "voxels/Chunks.hpp"
|
||||||
#include "window/Camera.hpp"
|
#include "window/Camera.hpp"
|
||||||
#include "window/display.hpp"
|
#include "window/Window.hpp"
|
||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
#include "world/World.hpp"
|
#include "world/World.hpp"
|
||||||
|
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
#include "graphics/core/Texture.hpp"
|
#include "graphics/core/Texture.hpp"
|
||||||
#include "assets/Assets.hpp"
|
#include "assets/Assets.hpp"
|
||||||
#include "maths/UVRegion.hpp"
|
#include "maths/UVRegion.hpp"
|
||||||
#include "window/display.hpp"
|
#include "window/Window.hpp"
|
||||||
#include "window/Camera.hpp"
|
#include "window/Camera.hpp"
|
||||||
#include "engine/Engine.hpp"
|
#include "engine/Engine.hpp"
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
#include "Batch2D.hpp"
|
#include "Batch2D.hpp"
|
||||||
#include "Framebuffer.hpp"
|
#include "Framebuffer.hpp"
|
||||||
#include "window/display.hpp"
|
#include "window/Window.hpp"
|
||||||
|
|
||||||
static void set_blend_mode(BlendMode mode) {
|
static void set_blend_mode(BlendMode mode) {
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
#include "frontend/ContentGfxCache.hpp"
|
#include "frontend/ContentGfxCache.hpp"
|
||||||
#include "voxels/Block.hpp"
|
#include "voxels/Block.hpp"
|
||||||
#include "window/Camera.hpp"
|
#include "window/Camera.hpp"
|
||||||
#include "window/display.hpp"
|
#include "window/Window.hpp"
|
||||||
#include "graphics/core/Atlas.hpp"
|
#include "graphics/core/Atlas.hpp"
|
||||||
#include "graphics/core/Batch3D.hpp"
|
#include "graphics/core/Batch3D.hpp"
|
||||||
#include "graphics/core/Framebuffer.hpp"
|
#include "graphics/core/Framebuffer.hpp"
|
||||||
|
|||||||
@ -7,7 +7,7 @@
|
|||||||
#include "graphics/core/Cubemap.hpp"
|
#include "graphics/core/Cubemap.hpp"
|
||||||
#include "graphics/core/Framebuffer.hpp"
|
#include "graphics/core/Framebuffer.hpp"
|
||||||
#include "graphics/core/DrawContext.hpp"
|
#include "graphics/core/DrawContext.hpp"
|
||||||
#include "window/display.hpp"
|
#include "window/Window.hpp"
|
||||||
#include "window/Camera.hpp"
|
#include "window/Camera.hpp"
|
||||||
#include "maths/UVRegion.hpp"
|
#include "maths/UVRegion.hpp"
|
||||||
|
|
||||||
|
|||||||
@ -28,7 +28,7 @@
|
|||||||
#include "voxels/Chunk.hpp"
|
#include "voxels/Chunk.hpp"
|
||||||
#include "voxels/Chunks.hpp"
|
#include "voxels/Chunks.hpp"
|
||||||
#include "window/Camera.hpp"
|
#include "window/Camera.hpp"
|
||||||
#include "window/display.hpp"
|
#include "window/Window.hpp"
|
||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
#include "world/LevelEvents.hpp"
|
#include "world/LevelEvents.hpp"
|
||||||
#include "world/World.hpp"
|
#include "world/World.hpp"
|
||||||
|
|||||||
@ -20,7 +20,7 @@
|
|||||||
#include "graphics/core/Shader.hpp"
|
#include "graphics/core/Shader.hpp"
|
||||||
#include "gui_util.hpp"
|
#include "gui_util.hpp"
|
||||||
#include "window/Camera.hpp"
|
#include "window/Camera.hpp"
|
||||||
#include "window/display.hpp"
|
#include "window/Window.hpp"
|
||||||
#include "window/input.hpp"
|
#include "window/input.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|||||||
@ -15,7 +15,7 @@
|
|||||||
#include "graphics/core/Font.hpp"
|
#include "graphics/core/Font.hpp"
|
||||||
#include "graphics/ui/markdown.hpp"
|
#include "graphics/ui/markdown.hpp"
|
||||||
#include "util/stringutil.hpp"
|
#include "util/stringutil.hpp"
|
||||||
#include "window/display.hpp"
|
#include "window/Window.hpp"
|
||||||
#include "devtools/actions.hpp"
|
#include "devtools/actions.hpp"
|
||||||
#include "../markdown.hpp"
|
#include "../markdown.hpp"
|
||||||
|
|
||||||
|
|||||||
@ -23,7 +23,7 @@
|
|||||||
#include "voxels/Chunks.hpp"
|
#include "voxels/Chunks.hpp"
|
||||||
#include "voxels/voxel.hpp"
|
#include "voxels/voxel.hpp"
|
||||||
#include "window/Camera.hpp"
|
#include "window/Camera.hpp"
|
||||||
#include "window/display.hpp"
|
#include "window/Window.hpp"
|
||||||
#include "window/input.hpp"
|
#include "window/input.hpp"
|
||||||
#include "world/Level.hpp"
|
#include "world/Level.hpp"
|
||||||
|
|
||||||
|
|||||||
@ -44,16 +44,16 @@ public:
|
|||||||
const glm::ivec2& getSize() const {
|
const glm::ivec2& getSize() const {
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static std::tuple<
|
||||||
|
std::unique_ptr<Window>,
|
||||||
|
std::unique_ptr<Input>
|
||||||
|
> initialize(DisplaySettings* settings, std::string title);
|
||||||
protected:
|
protected:
|
||||||
glm::ivec2 size;
|
glm::ivec2 size;
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace display {
|
namespace display {
|
||||||
std::tuple<
|
|
||||||
std::unique_ptr<Window>,
|
|
||||||
std::unique_ptr<Input>
|
|
||||||
> initialize(DisplaySettings* settings);
|
|
||||||
|
|
||||||
void clear();
|
void clear();
|
||||||
void clearDepth();
|
void clearDepth();
|
||||||
void setBgColor(glm::vec3 color);
|
void setBgColor(glm::vec3 color);
|
||||||
@ -1,4 +1,4 @@
|
|||||||
#include "window/display.hpp"
|
#include "window/Window.hpp"
|
||||||
|
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
@ -85,6 +85,45 @@ static void GLAPIENTRY gl_message_callback(
|
|||||||
<< gl_severity_name(severity) << ": " << message;
|
<< gl_severity_name(severity) << ": " << message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool initialize_gl(int width, int height) {
|
||||||
|
glewExperimental = GL_TRUE;
|
||||||
|
|
||||||
|
GLenum glewErr = glewInit();
|
||||||
|
if (glewErr != GLEW_OK) {
|
||||||
|
if (glewErr == GLEW_ERROR_NO_GLX_DISPLAY) {
|
||||||
|
// see issue #240
|
||||||
|
logger.warning()
|
||||||
|
<< "glewInit() returned GLEW_ERROR_NO_GLX_DISPLAY; ignored";
|
||||||
|
} else {
|
||||||
|
logger.error() << "failed to initialize GLEW:\n"
|
||||||
|
<< glewGetErrorString(glewErr);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
glEnable(GL_DEBUG_OUTPUT);
|
||||||
|
glDebugMessageCallback(gl_message_callback, 0);
|
||||||
|
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
glClearColor(0.0f, 0.0f, 0.0f, 1);
|
||||||
|
glEnable(GL_BLEND);
|
||||||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
|
||||||
|
GLint maxTextureSize[1] {static_cast<GLint>(Texture::MAX_RESOLUTION)};
|
||||||
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE, maxTextureSize);
|
||||||
|
if (maxTextureSize[0] > 0) {
|
||||||
|
Texture::MAX_RESOLUTION = maxTextureSize[0];
|
||||||
|
logger.info() << "max texture size is " << Texture::MAX_RESOLUTION;
|
||||||
|
}
|
||||||
|
|
||||||
|
const GLubyte* vendor = glGetString(GL_VENDOR);
|
||||||
|
const GLubyte* renderer = glGetString(GL_RENDERER);
|
||||||
|
logger.info() << "GL Vendor: " << reinterpret_cast<const char*>(vendor);
|
||||||
|
logger.info() << "GL Renderer: " << reinterpret_cast<const char*>(renderer);
|
||||||
|
logger.info() << "GLFW: " << glfwGetVersionString();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
static const char* glfw_error_name(int error) {
|
static const char* glfw_error_name(int error) {
|
||||||
switch (error) {
|
switch (error) {
|
||||||
case GLFW_NO_ERROR:
|
case GLFW_NO_ERROR:
|
||||||
@ -124,7 +163,7 @@ static void glfw_error_callback(int error, const char* description) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline constexpr short KEYS_BUFFER_SIZE = 1036;
|
inline constexpr short KEYS_BUFFER_SIZE = 1036;
|
||||||
inline constexpr short _MOUSE_KEYS_OFFSET = 1024;
|
inline constexpr short MOUSE_KEYS_OFFSET = 1024;
|
||||||
|
|
||||||
static GLFWcursor* standard_cursors[static_cast<int>(CursorShape::LAST) + 1] = {};
|
static GLFWcursor* standard_cursors[static_cast<int>(CursorShape::LAST) + 1] = {};
|
||||||
|
|
||||||
@ -201,7 +240,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void onMouseCallback(int button, bool pressed) {
|
void onMouseCallback(int button, bool pressed) {
|
||||||
int key = button + _MOUSE_KEYS_OFFSET;
|
int key = button + MOUSE_KEYS_OFFSET;
|
||||||
onKeyCallback(key, pressed);
|
onKeyCallback(key, pressed);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -231,12 +270,12 @@ public:
|
|||||||
|
|
||||||
bool clicked(mousecode code) const override {
|
bool clicked(mousecode code) const override {
|
||||||
return pressed(
|
return pressed(
|
||||||
static_cast<keycode>(_MOUSE_KEYS_OFFSET + static_cast<int>(code))
|
static_cast<keycode>(MOUSE_KEYS_OFFSET + static_cast<int>(code))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
bool jclicked(mousecode code) const override {
|
bool jclicked(mousecode code) const override {
|
||||||
return clicked(code) &&
|
return clicked(code) &&
|
||||||
frames[static_cast<int>(code) + _MOUSE_KEYS_OFFSET] ==
|
frames[static_cast<int>(code) + MOUSE_KEYS_OFFSET] ==
|
||||||
currentFrame;
|
currentFrame;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -560,20 +599,34 @@ static void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos) {
|
|||||||
handler->input.setCursorPosition(xpos, ypos);
|
handler->input.setCursorPosition(xpos, ypos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void create_standard_cursors() {
|
||||||
|
for (int i = 0; i <= static_cast<int>(CursorShape::LAST); i++) {
|
||||||
|
int cursor = GLFW_ARROW_CURSOR + i;
|
||||||
|
// GLFW 3.3 does not support some cursors
|
||||||
|
if (GLFW_VERSION_MAJOR <= 3 && GLFW_VERSION_MINOR <= 3 &&
|
||||||
|
cursor > GLFW_VRESIZE_CURSOR) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
standard_cursors[i] = glfwCreateStandardCursor(cursor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void setup_callbacks(GLFWwindow* window) {
|
||||||
|
glfwSetKeyCallback(window, key_callback);
|
||||||
|
glfwSetMouseButtonCallback(window, mouse_button_callback);
|
||||||
|
glfwSetCursorPosCallback(window, cursor_pos_callback);
|
||||||
|
glfwSetWindowSizeCallback(window, window_size_callback);
|
||||||
|
glfwSetCharCallback(window, character_callback);
|
||||||
|
glfwSetScrollCallback(window, scroll_callback);
|
||||||
|
}
|
||||||
|
|
||||||
std::tuple<
|
std::tuple<
|
||||||
std::unique_ptr<Window>,
|
std::unique_ptr<Window>,
|
||||||
std::unique_ptr<Input>
|
std::unique_ptr<Input>
|
||||||
> display::initialize(DisplaySettings* settings) {
|
> Window::initialize(DisplaySettings* settings, std::string title) {
|
||||||
int width = settings->width.get();
|
int width = settings->width.get();
|
||||||
int height = settings->height.get();
|
int height = settings->height.get();
|
||||||
|
|
||||||
std::string title = "VoxelCore v" +
|
|
||||||
std::to_string(ENGINE_VERSION_MAJOR) + "." +
|
|
||||||
std::to_string(ENGINE_VERSION_MINOR);
|
|
||||||
if (ENGINE_DEBUG_BUILD) {
|
|
||||||
title += " [debug]";
|
|
||||||
}
|
|
||||||
|
|
||||||
glfwSetErrorCallback(glfw_error_callback);
|
glfwSetErrorCallback(glfw_error_callback);
|
||||||
if (glfwInit() == GLFW_FALSE) {
|
if (glfwInit() == GLFW_FALSE) {
|
||||||
logger.error() << "failed to initialize GLFW";
|
logger.error() << "failed to initialize GLFW";
|
||||||
@ -632,34 +685,21 @@ std::tuple<
|
|||||||
Texture::MAX_RESOLUTION = maxTextureSize[0];
|
Texture::MAX_RESOLUTION = maxTextureSize[0];
|
||||||
logger.info() << "max texture size is " << Texture::MAX_RESOLUTION;
|
logger.info() << "max texture size is " << Texture::MAX_RESOLUTION;
|
||||||
}
|
}
|
||||||
|
setup_callbacks(window);
|
||||||
glfwSetKeyCallback(window, key_callback);
|
|
||||||
glfwSetMouseButtonCallback(window, mouse_button_callback);
|
|
||||||
glfwSetCursorPosCallback(window, cursor_pos_callback);
|
|
||||||
glfwSetWindowSizeCallback(window, window_size_callback);
|
|
||||||
glfwSetCharCallback(window, character_callback);
|
|
||||||
glfwSetScrollCallback(window, scroll_callback);
|
|
||||||
|
|
||||||
glfwSwapInterval(1);
|
glfwSwapInterval(1);
|
||||||
const GLubyte* vendor = glGetString(GL_VENDOR);
|
input_util::initialize();
|
||||||
const GLubyte* renderer = glGetString(GL_RENDERER);
|
create_standard_cursors();
|
||||||
logger.info() << "GL Vendor: " << reinterpret_cast<const char*>(vendor);
|
|
||||||
logger.info() << "GL Renderer: " << reinterpret_cast<const char*>(renderer);
|
|
||||||
logger.info() << "GLFW: " << glfwGetVersionString();
|
|
||||||
glm::vec2 scale;
|
glm::vec2 scale;
|
||||||
glfwGetMonitorContentScale(glfwGetPrimaryMonitor(), &scale.x, &scale.y);
|
glfwGetMonitorContentScale(glfwGetPrimaryMonitor(), &scale.x, &scale.y);
|
||||||
logger.info() << "monitor content scale: " << scale.x << "x" << scale.y;
|
logger.info() << "monitor content scale: " << scale.x << "x" << scale.y;
|
||||||
|
|
||||||
input_util::initialize();
|
if (initialize_gl(width, height)) {
|
||||||
|
glfwTerminate();
|
||||||
for (int i = 0; i <= static_cast<int>(CursorShape::LAST); i++) {
|
return {nullptr, nullptr};
|
||||||
int cursor = GLFW_ARROW_CURSOR + i;
|
|
||||||
// GLFW 3.3 does not support some cursors
|
|
||||||
if (GLFW_VERSION_MAJOR <= 3 && GLFW_VERSION_MINOR <= 3 && cursor > GLFW_VRESIZE_CURSOR) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
standard_cursors[i] = glfwCreateStandardCursor(cursor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto inputPtr = std::make_unique<GLFWInput>(window);
|
auto inputPtr = std::make_unique<GLFWInput>(window);
|
||||||
auto windowPtr = std::make_unique<GLFWWindow>(
|
auto windowPtr = std::make_unique<GLFWWindow>(
|
||||||
*inputPtr, window, settings, width, height
|
*inputPtr, window, settings, width, height
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user