First iteration. Window create successfull

This commit is contained in:
Stepanov Igor 2025-10-04 14:52:26 +03:00
parent d6bf8b25f9
commit 26114b9a59
6 changed files with 355 additions and 393 deletions

1
.gitignore vendored
View file

@ -9,6 +9,7 @@ Debug/voxel_engine
/export /export
/config /config
/out /out
/compile_commands.json
/misc /misc
/world /world

View file

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.11)
project(VoxelEngine) project(VoxelEngine)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)

View file

@ -18,7 +18,7 @@ endif()
find_package(ZLIB REQUIRED) find_package(ZLIB REQUIRED)
find_package(PNG REQUIRED) find_package(PNG REQUIRED)
find_package(CURL REQUIRED) find_package(CURL REQUIRED)
find_package(glfw3 REQUIRED) find_package(SDL3 REQUIRED)
if(NOT APPLE) if(NOT APPLE)
find_package(EnTT REQUIRED) find_package(EnTT REQUIRED)
endif() endif()
@ -65,7 +65,7 @@ target_include_directories(VoxelEngineSrc PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries( target_link_libraries(
VoxelEngineSrc VoxelEngineSrc
PRIVATE glfw PRIVATE SDL3::SDL3
OpenGL::GL OpenGL::GL
GLEW::GLEW GLEW::GLEW
ZLIB::ZLIB ZLIB::ZLIB

View file

@ -1,23 +1,13 @@
#include "Shader.hpp" #include "Shader.hpp"
#include <exception>
#include <fstream>
#include <iostream>
#include <sstream>
#include <filesystem>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "coders/GLSLExtension.hpp" #include "coders/GLSLExtension.hpp"
#include "debug/Logger.hpp" #include "debug/Logger.hpp"
static debug::Logger logger("gl-shader"); static debug::Logger logger("gl-shader");
namespace fs = std::filesystem;
GLSLExtension* Shader::preprocessor = new GLSLExtension(); GLSLExtension* Shader::preprocessor = new GLSLExtension();
Shader* Shader::used = nullptr; Shader* Shader::used = nullptr;

View file

@ -1,25 +1,29 @@
#include "window/Window.hpp" #include "window/Window.hpp"
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <SDL3/SDL_events.h>
#include <unordered_set> #include <SDL3/SDL_init.h>
#include <SDL3/SDL_mouse.h>
#include <SDL3/SDL_oldnames.h>
#include <SDL3/SDL_timer.h>
#include <SDL3/SDL_version.h>
#include <SDL3/SDL_video.h>
#include <SDL3/SDL_clipboard.h>
#include <chrono> #include <unordered_set>
#include <stack>
#include <vector> #include <vector>
#include <stack>
#include "debug/Logger.hpp" #include "debug/Logger.hpp"
#include "graphics/core/ImageData.hpp" #include "graphics/core/ImageData.hpp"
#include "graphics/core/Texture.hpp" #include "graphics/core/Texture.hpp"
#include "settings.hpp" #include "settings.hpp"
#include "util/ObjectsKeeper.hpp"
#include "util/platform.hpp" #include "util/platform.hpp"
#include "window/input.hpp" #include "window/input.hpp"
static debug::Logger logger("window"); static debug::Logger logger("window");
static std::unordered_set<std::string> supported_gl_extensions; static std::unordered_set<std::string> supported_gl_extensions;
static void window_size_callback(GLFWwindow* window, int width, int height);
static void init_gl_extensions_list() { static void init_gl_extensions_list() {
GLint numExtensions = 0; GLint numExtensions = 0;
@ -118,54 +122,15 @@ static bool initialize_gl(int width, int height) {
const GLubyte* renderer = glGetString(GL_RENDERER); const GLubyte* renderer = glGetString(GL_RENDERER);
logger.info() << "GL Vendor: " << reinterpret_cast<const char*>(vendor); logger.info() << "GL Vendor: " << reinterpret_cast<const char*>(vendor);
logger.info() << "GL Renderer: " << reinterpret_cast<const char*>(renderer); logger.info() << "GL Renderer: " << reinterpret_cast<const char*>(renderer);
logger.info() << "GLFW: " << glfwGetVersionString(); logger.info() << "SDL: " << SDL_GetVersion();
return false; return false;
} }
static const char* glfw_error_name(int error) {
switch (error) {
case GLFW_NO_ERROR:
return "no error";
case GLFW_NOT_INITIALIZED:
return "not initialized";
case GLFW_NO_CURRENT_CONTEXT:
return "no current context";
case GLFW_INVALID_ENUM:
return "invalid enum";
case GLFW_INVALID_VALUE:
return "invalid value";
case GLFW_OUT_OF_MEMORY:
return "out of memory";
case GLFW_API_UNAVAILABLE:
return "api unavailable";
case GLFW_VERSION_UNAVAILABLE:
return "version unavailable";
case GLFW_PLATFORM_ERROR:
return "platform error";
case GLFW_FORMAT_UNAVAILABLE:
return "format unavailable";
case GLFW_NO_WINDOW_CONTEXT:
return "no window context";
default:
return "unknown error";
}
}
static void glfw_error_callback(int error, const char* description) {
auto logline = logger.error();
logline << "GLFW error [0x" << std::hex << error << " - "
<< glfw_error_name(error) << "]";
if (description) {
logline << ": " << 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] = {}; class SDLInput : public Input {
class GLFWInput : public Input {
public: public:
int scroll = 0; int scroll = 0;
uint currentFrame = 0; uint currentFrame = 0;
@ -176,7 +141,7 @@ public:
bool keys[KEYS_BUFFER_SIZE] {}; bool keys[KEYS_BUFFER_SIZE] {};
std::unordered_map<Keycode, util::HandlersList<>> keyCallbacks; std::unordered_map<Keycode, util::HandlersList<>> keyCallbacks;
GLFWInput(GLFWwindow* window) SDLInput(SDL_Window* window)
: window(window) { : window(window) {
} }
@ -184,40 +149,55 @@ public:
delta.x = 0.0f; delta.x = 0.0f;
delta.y = 0.0f; delta.y = 0.0f;
scroll = 0; scroll = 0;
currentFrame++;
codepoints.clear(); codepoints.clear();
pressedKeys.clear(); pressedKeys.clear();
glfwPollEvents();
for (auto& [_, binding] : bindings.getAll()) { SDL_Event event;
if (!binding.enabled) {
binding.state = false; while(SDL_PollEvent(&event))
continue; {
} switch (event.type) {
binding.justChanged = false; case SDL_EVENT_QUIT:
// window->setShouldClose(true);
bool newstate = false;
switch (binding.type) {
case InputType::KEYBOARD:
newstate = pressed(static_cast<Keycode>(binding.code));
break; break;
case InputType::MOUSE: case SDL_EVENT_KEY_DOWN:
newstate = clicked(static_cast<Mousecode>(binding.code)); break;
case SDL_EVENT_KEY_UP:
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
break;
case SDL_EVENT_MOUSE_MOTION:
break; break;
} }
// for (auto& [_, binding] : bindings.getAll()) {
// if (!binding.enabled) {
// binding.state = false;
// continue;
// }
// binding.justChanged = false;
if (newstate) { // bool newstate = false;
if (!binding.state) { // switch (binding.type) {
binding.state = true; // case InputType::KEYBOARD:
binding.justChanged = true; // newstate = pressed(static_cast<Keycode>(binding.code));
binding.onactived.notify(); // break;
} // case InputType::MOUSE:
} else { // newstate = clicked(static_cast<Mousecode>(binding.code));
if (binding.state) { // break;
binding.state = false; // }
binding.justChanged = true;
} // if (newstate) {
} // if (!binding.state) {
// binding.state = true;
// binding.justChanged = true;
// binding.onactived.notify();
// }
// } else {
// if (binding.state) {
// binding.state = false;
// binding.justChanged = true;
// }
// }
} }
} }
@ -242,11 +222,12 @@ public:
} }
const char* getClipboardText() const override { const char* getClipboardText() const override {
return glfwGetClipboardString(window); /*todo free*/
return SDL_GetClipboardText();
} }
void setClipboardText(const char* text) override { void setClipboardText(const char* text) override {
glfwSetClipboardString(window, text); SDL_SetClipboardText(text);
} }
int getScroll() override { int getScroll() override {
@ -286,11 +267,7 @@ public:
void toggleCursor() override { void toggleCursor() override {
cursorDrag = false; cursorDrag = false;
if (cursorLocked) { SDL_SetWindowMouseGrab(window, cursorLocked);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
} else {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
}
cursorLocked = !cursorLocked; cursorLocked = !cursorLocked;
} }
@ -325,22 +302,24 @@ public:
return codepoints; return codepoints;
} }
private: private:
GLFWwindow* window; SDL_Window* window;
bool cursorLocked = false; bool cursorLocked = false;
bool cursorDrag = false; bool cursorDrag = false;
glm::vec2 delta; glm::vec2 delta;
glm::vec2 cursor; glm::vec2 cursor;
}; };
static_assert(!std::is_abstract<GLFWInput>()); static_assert(!std::is_abstract<SDLInput>());
class GLFWWindow : public Window { class WindowSdlImpl final : public Window {
public: public:
GLFWInput& input; SDLInput& input;
DisplaySettings* settings; DisplaySettings* settings;
GLFWWindow( WindowSdlImpl(
GLFWInput& glfwInput, SDLInput& glfwInput,
GLFWwindow* window, SDL_Window* window,
SDL_GLContext context,
DisplaySettings* settings, DisplaySettings* settings,
int width, int width,
int height int height
@ -348,22 +327,20 @@ public:
: Window({width, height}), : Window({width, height}),
input(glfwInput), input(glfwInput),
settings(settings), settings(settings),
window(window) { window(window),
glcontext(context) {
} }
~GLFWWindow() { ~WindowSdlImpl() override {
for (int i = 0; i <= static_cast<int>(CursorShape::LAST); i++) { SDL_GL_DestroyContext(glcontext);
glfwDestroyCursor(standard_cursors[i]);
}
glfwTerminate();
} }
double time() override { double time() override {
return glfwGetTime(); return SDL_GetTicks();
} }
void swapBuffers() override { void swapBuffers() override {
glfwSwapBuffers(window); SDL_GL_SwapWindow(window);
resetScissor(); resetScissor();
if (framerate > 0) { if (framerate > 0) {
auto elapsedTime = time() - prevSwap; auto elapsedTime = time() - prevSwap;
@ -378,65 +355,44 @@ public:
} }
bool isMaximized() const override { bool isMaximized() const override {
return glfwGetWindowAttrib(window, GLFW_MAXIMIZED); return (SDL_GetWindowFlags(window) & SDL_WINDOW_MAXIMIZED) != 0;
} }
bool isFocused() const override { bool isFocused() const override {
return glfwGetWindowAttrib(window, GLFW_FOCUSED); Uint32 flags = SDL_GetWindowFlags(window);
return (flags & SDL_WINDOW_INPUT_FOCUS) != 0 || (flags & SDL_WINDOW_MOUSE_FOCUS) != 0;
} }
bool isIconified() const override { bool isIconified() const override {
return glfwGetWindowAttrib(window, GLFW_ICONIFIED); return (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0;
} }
bool isShouldClose() const override {
return glfwWindowShouldClose(window); bool isShouldClose() const override
{
return toClose;
} }
void setShouldClose(bool flag) override { void setShouldClose(bool flag) override
glfwSetWindowShouldClose(window, flag); {
toClose = flag;
} }
void setCursor(CursorShape shape) override { void setCursor(CursorShape shape) override {
if (cursor == shape) { // if (cursor == shape) {
return; // return;
} // }
cursor = shape; // cursor = shape;
// NULL cursor is valid for GLFW // // NULL cursor is valid for GLFW
glfwSetCursor(window, standard_cursors[static_cast<int>(shape)]); // glfwSetCursor(window, standard_cursors[static_cast<int>(shape)]);
} }
void toggleFullscreen() override { void toggleFullscreen() override {
fullscreen = !fullscreen; fullscreen = !fullscreen;
GLFWmonitor* monitor = glfwGetPrimaryMonitor(); if (!SDL_SetWindowFullscreen(window, fullscreen)) {
const GLFWvidmode* mode = glfwGetVideoMode(monitor); // SDL_Log("Failed to toggle fullscreen: %s", SDL_GetError());
if (input.isCursorLocked()){
input.toggleCursor();
} }
if (fullscreen) {
glfwGetWindowPos(window, &posX, &posY);
glfwSetWindowMonitor(
window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate
);
} else {
glfwSetWindowMonitor(
window,
nullptr,
posX,
posY,
settings->width.get(),
settings->height.get(),
GLFW_DONT_CARE
);
window_size_callback(window, settings->width.get(), settings->height.get());
}
double xPos, yPos;
glfwGetCursorPos(window, &xPos, &yPos);
input.setCursorPosition(xPos, yPos);
} }
bool isFullscreen() const override { bool isFullscreen() const override {
@ -445,16 +401,33 @@ public:
void setIcon(const ImageData* image) override { void setIcon(const ImageData* image) override {
if (image == nullptr) { if (image == nullptr) {
glfwSetWindowIcon(window, 0, nullptr); SDL_SetWindowIcon(window, nullptr);
return; return;
} }
GLFWimage icon {
static_cast<int>(image->getWidth()), // Create SDL_Surface from ImageData
static_cast<int>(image->getHeight()), SDL_Surface* iconSurface = SDL_CreateSurface(
image->getData()}; image->getWidth(),
glfwSetWindowIcon(window, 1, &icon); image->getHeight(),
SDL_PIXELFORMAT_RGBA32 // Adjust format based on your ImageData
);
if (!iconSurface) {
// SDL_Log("Failed to create surface for window icon: %s", SDL_GetError());
return;
}
memcpy(iconSurface->pixels, image->getData(),
image->getWidth() * image->getHeight() * 4);
if (!SDL_SetWindowIcon(window, iconSurface)) {
// SDL_Log("Failed to set window icon: %s", SDL_GetError());
}
SDL_DestroySurface(iconSurface);
} }
void setSize(int width, int height) { void setSize(int width, int height) {
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
size = {width, height}; size = {width, height};
@ -534,15 +507,17 @@ public:
void setFramerate(int framerate) override { void setFramerate(int framerate) override {
if ((framerate != -1) != (this->framerate != -1)) { if ((framerate != -1) != (this->framerate != -1)) {
glfwSwapInterval(framerate == -1); SDL_GL_SetSwapInterval(framerate == -1);
} }
this->framerate = framerate; this->framerate = framerate;
} }
private: private:
GLFWwindow* window; SDL_Window* window;
SDL_GLContext glcontext;
CursorShape cursor = CursorShape::ARROW; CursorShape cursor = CursorShape::ARROW;
bool fullscreen = false; bool fullscreen = false;
bool toClose = false;
int framerate = -1; int framerate = -1;
std::stack<glm::vec4> scissorStack; std::stack<glm::vec4> scissorStack;
glm::vec4 scissorArea; glm::vec4 scissorArea;
@ -550,86 +525,85 @@ private:
int posX = 0; int posX = 0;
int posY = 0; int posY = 0;
}; };
static_assert(!std::is_abstract<GLFWWindow>());
static void mouse_button_callback(GLFWwindow* window, int button, int action, int) { // static void mouse_button_callback(GLFWwindow* window, int button, int action, int) {
auto handler = static_cast<GLFWWindow*>(glfwGetWindowUserPointer(window)); // auto handler = static_cast<GLFWWindow*>(glfwGetWindowUserPointer(window));
handler->input.onMouseCallback(button, action == GLFW_PRESS); // handler->input.onMouseCallback(button, action == GLFW_PRESS);
} // }
static void character_callback(GLFWwindow* window, unsigned int codepoint) { // static void character_callback(GLFWwindow* window, unsigned int codepoint) {
auto handler = static_cast<GLFWWindow*>(glfwGetWindowUserPointer(window)); // auto handler = static_cast<GLFWWindow*>(glfwGetWindowUserPointer(window));
handler->input.codepoints.push_back(codepoint); // handler->input.codepoints.push_back(codepoint);
} // }
static void key_callback( // static void key_callback(
GLFWwindow* window, int key, int /*scancode*/, int action, int /*mode*/ // GLFWwindow* window, int key, int /*scancode*/, int action, int /*mode*/
) { // ) {
auto handler = static_cast<GLFWWindow*>(glfwGetWindowUserPointer(window)); // auto handler = static_cast<GLFWWindow*>(glfwGetWindowUserPointer(window));
auto& input = handler->input; // auto& input = handler->input;
if (key == GLFW_KEY_UNKNOWN) { // if (key == GLFW_KEY_UNKNOWN) {
return; // return;
} // }
if (action == GLFW_PRESS) { // if (action == GLFW_PRESS) {
input.onKeyCallback(key, true); // input.onKeyCallback(key, true);
} else if (action == GLFW_RELEASE) { // } else if (action == GLFW_RELEASE) {
input.onKeyCallback(key, false); // input.onKeyCallback(key, false);
} else if (action == GLFW_REPEAT) { // } else if (action == GLFW_REPEAT) {
input.onKeyCallback(key, true); // input.onKeyCallback(key, true);
} // }
} // }
static void window_size_callback(GLFWwindow* window, int width, int height) { // static void window_size_callback(GLFWwindow* window, int width, int height) {
auto handler = static_cast<GLFWWindow*>(glfwGetWindowUserPointer(window)); // auto handler = static_cast<GLFWWindow*>(glfwGetWindowUserPointer(window));
if (width && height) { // if (width && height) {
handler->setSize(width, height); // handler->setSize(width, height);
} // }
handler->resetScissor(); // handler->resetScissor();
} // }
static void scroll_callback(GLFWwindow* window, double, double yoffset) { // static void scroll_callback(GLFWwindow* window, double, double yoffset) {
auto handler = static_cast<GLFWWindow*>(glfwGetWindowUserPointer(window)); // auto handler = static_cast<GLFWWindow*>(glfwGetWindowUserPointer(window));
handler->input.scroll += yoffset; // handler->input.scroll += yoffset;
} // }
static void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos) { // static void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos) {
auto handler = static_cast<GLFWWindow*>(glfwGetWindowUserPointer(window)); // auto handler = static_cast<GLFWWindow*>(glfwGetWindowUserPointer(window));
handler->input.setCursorPosition(xpos, ypos); // handler->input.setCursorPosition(xpos, ypos);
} // }
static void iconify_callback(GLFWwindow* window, int iconified) { // static void iconify_callback(GLFWwindow* window, int iconified) {
auto handler = static_cast<GLFWWindow*>(glfwGetWindowUserPointer(window)); // auto handler = static_cast<GLFWWindow*>(glfwGetWindowUserPointer(window));
if (handler->isFullscreen() && iconified == 0) { // if (handler->isFullscreen() && iconified == 0) {
GLFWmonitor* monitor = glfwGetPrimaryMonitor(); // GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor); // const GLFWvidmode* mode = glfwGetVideoMode(monitor);
glfwSetWindowMonitor( // glfwSetWindowMonitor(
window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate // window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate
); // );
} // }
} // }
static void create_standard_cursors() { // static void create_standard_cursors() {
for (int i = 0; i <= static_cast<int>(CursorShape::LAST); i++) { // for (int i = 0; i <= static_cast<int>(CursorShape::LAST); i++) {
int cursor = GLFW_ARROW_CURSOR + i; // int cursor = GLFW_ARROW_CURSOR + i;
// GLFW 3.3 does not support some cursors // // GLFW 3.3 does not support some cursors
if (GLFW_VERSION_MAJOR <= 3 && GLFW_VERSION_MINOR <= 3 && // if (GLFW_VERSION_MAJOR <= 3 && GLFW_VERSION_MINOR <= 3 &&
cursor > GLFW_VRESIZE_CURSOR) { // cursor > GLFW_VRESIZE_CURSOR) {
break; // break;
} // }
standard_cursors[i] = glfwCreateStandardCursor(cursor); // standard_cursors[i] = glfwCreateStandardCursor(cursor);
} // }
} // }
static void setup_callbacks(GLFWwindow* window) { // static void setup_callbacks(GLFWwindow* window) {
glfwSetKeyCallback(window, key_callback); // glfwSetKeyCallback(window, key_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback); // glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetCursorPosCallback(window, cursor_pos_callback); // glfwSetCursorPosCallback(window, cursor_pos_callback);
glfwSetWindowSizeCallback(window, window_size_callback); // glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetCharCallback(window, character_callback); // glfwSetCharCallback(window, character_callback);
glfwSetScrollCallback(window, scroll_callback); // glfwSetScrollCallback(window, scroll_callback);
glfwSetWindowIconifyCallback(window, iconify_callback); // glfwSetWindowIconifyCallback(window, iconify_callback);
} // }
std::tuple< std::tuple<
std::unique_ptr<Window>, std::unique_ptr<Window>,
@ -638,35 +612,34 @@ std::tuple<
int width = settings->width.get(); int width = settings->width.get();
int height = settings->height.get(); int height = settings->height.get();
glfwSetErrorCallback(glfw_error_callback); if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_EVENTS)) {
if (glfwInit() == GLFW_FALSE) { logger.error() << "failed to initialize SDL";
logger.error() << "failed to initialize GLFW";
return {nullptr, nullptr}; return {nullptr, nullptr};
} }
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
#if GLFW_VERSION_MAJOR >= 3 && GLFW_VERSION_MINOR >= 4 // #if GLFW_VERSION_MAJOR >= 3 && GLFW_VERSION_MINOR >= 4
// see issue #465 // // see issue #465
glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GL_FALSE); // // glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GL_FALSE);
#endif // #endif
#ifdef __APPLE__ #ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_FALSE); // glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_FALSE);
#else #else
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE); // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
#endif #endif
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
glfwWindowHint(GLFW_SAMPLES, settings->samples.get()); // glfwWindowHint(GLFW_SAMPLES, settings->samples.get());
auto window = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr); auto window = SDL_CreateWindow(title.c_str(), width, height, SDL_WINDOW_OPENGL);
if (window == nullptr) { if (window == nullptr) {
logger.error() << "failed to create GLFW window"; logger.error() << "failed to create GLFW window";
glfwTerminate();
return {nullptr, nullptr}; return {nullptr, nullptr};
} }
glfwMakeContextCurrent(window); SDL_GLContext glcontext = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, glcontext);
glewExperimental = GL_TRUE; glewExperimental = GL_TRUE;
@ -679,7 +652,7 @@ std::tuple<
} else { } else {
logger.error() << "failed to initialize GLEW:\n" logger.error() << "failed to initialize GLEW:\n"
<< glewGetErrorString(glewErr); << glewGetErrorString(glewErr);
glfwTerminate(); SDL_DestroyWindow(window);
return {nullptr, nullptr}; return {nullptr, nullptr};
} }
} }
@ -704,26 +677,25 @@ 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); // setup_callbacks(window);
glfwSwapInterval(1); SDL_GL_SetSwapInterval(1);
input_util::initialize(); input_util::initialize();
create_standard_cursors(); // create_standard_cursors();
glm::vec2 scale; glm::vec2 scale;
glfwGetMonitorContentScale(glfwGetPrimaryMonitor(), &scale.x, &scale.y); // SDL_SetDisplayContentScale(SDL_GetPrimaryDisplay(), &scale.x, &scale.y);
logger.info() << "monitor content scale: " << scale.x << "x" << scale.y; logger.info() << "monitor content scale: " << scale.x << "x" << scale.y;
if (initialize_gl(width, height)) { if (initialize_gl(width, height)) {
glfwTerminate();
return {nullptr, nullptr}; return {nullptr, nullptr};
} }
auto inputPtr = std::make_unique<GLFWInput>(window); auto inputPtr = std::make_unique<SDLInput>(window);
auto windowPtr = std::make_unique<GLFWWindow>( auto windowPtr = std::make_unique<WindowSdlImpl>(
*inputPtr, window, settings, width, height *inputPtr, window, glcontext, settings, width, height
); );
glfwSetWindowUserPointer(window, windowPtr.get()); // glfwSetWindowUserPointer(window, windowPtr.get());
return {std::move(windowPtr), std::move(inputPtr)}; return {std::move(windowPtr), std::move(inputPtr)};
} }

View file

@ -1,56 +1,60 @@
#include "input.hpp" #include "input.hpp"
#include "debug/Logger.hpp"
#include "util/stringutil.hpp"
#include "data/dv.hpp"
#include <GLFW/glfw3.h>
#include <unordered_map> #include <unordered_map>
#include <SDL3/SDL.h>
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_keycode.h>
#include <SDL3/SDL_mouse.h>
#include <SDL3/SDL_oldnames.h>
#include "debug/Logger.hpp"
#include "data/dv.hpp"
#include "util/stringutil.hpp"
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
#endif // _WIN32 #endif // _WIN32
static debug::Logger logger("input"); static debug::Logger logger("input");
static std::unordered_map<std::string, int> keycodes { static std::unordered_map<std::string, std::uint64_t> keycodes {
{"enter", GLFW_KEY_ENTER}, {"enter", SDLK_RETURN },
{"space", GLFW_KEY_SPACE}, {"space", SDLK_SPACE },
{"backspace", GLFW_KEY_BACKSPACE}, {"backspace", SDLK_BACKSPACE },
{"caps-lock", GLFW_KEY_CAPS_LOCK}, {"caps-lock", SDLK_CAPSLOCK },
{"escape", GLFW_KEY_ESCAPE}, {"escape", SDLK_ESCAPE },
{"delete", GLFW_KEY_DELETE}, {"delete", SDLK_DELETE },
{"home", GLFW_KEY_HOME}, {"home", SDLK_HOME },
{"end", GLFW_KEY_END}, {"end", SDLK_END },
{"tab", GLFW_KEY_TAB}, {"tab", SDLK_TAB },
{"insert", GLFW_KEY_INSERT}, {"insert", SDLK_INSERT },
{"page-down", GLFW_KEY_PAGE_DOWN}, {"page-down", SDLK_PAGEDOWN },
{"page-up", GLFW_KEY_PAGE_UP}, {"page-up", SDLK_PAGEUP },
{"left-shift", GLFW_KEY_LEFT_SHIFT}, {"left-shift", SDLK_LSHIFT },
{"right-shift", GLFW_KEY_RIGHT_SHIFT}, {"right-shift", SDLK_RSHIFT },
{"left-ctrl", GLFW_KEY_LEFT_CONTROL}, {"left-ctrl", SDLK_LCTRL },
{"right-ctrl", GLFW_KEY_RIGHT_CONTROL}, {"right-ctrl", SDLK_RCTRL},
{"left-alt", GLFW_KEY_LEFT_ALT}, {"left-alt", SDLK_LALT },
{"right-alt", GLFW_KEY_RIGHT_ALT}, {"right-alt", SDLK_RALT },
{"left-super", GLFW_KEY_LEFT_SUPER}, {"left-super", 0 /*todo*/ },
{"right-super", GLFW_KEY_RIGHT_SUPER}, {"right-super", 0 /*todo*/ },
{"grave-accent", GLFW_KEY_GRAVE_ACCENT}, {"grave-accent", SDLK_GRAVE },
{"left", GLFW_KEY_LEFT}, {"left", SDLK_LEFT },
{"right", GLFW_KEY_RIGHT}, {"right", SDLK_RIGHT },
{"down", GLFW_KEY_DOWN}, {"down", SDLK_DOWN },
{"up", GLFW_KEY_UP}, {"up", SDLK_UP },
}; };
SDL_Event s;
static std::unordered_map<std::string, int> mousecodes { static std::unordered_map<std::string, std::uint32_t> mousecodes {
{"left", GLFW_MOUSE_BUTTON_1}, {"left", SDL_BUTTON_LEFT },
{"right", GLFW_MOUSE_BUTTON_2}, {"right", SDL_BUTTON_RIGHT },
{"middle", GLFW_MOUSE_BUTTON_3}, {"middle", SDL_BUTTON_MIDDLE },
{"side1", GLFW_MOUSE_BUTTON_4}, {"side1", SDL_BUTTON_MASK(4) /*todo*/},
{"side2", GLFW_MOUSE_BUTTON_5}, {"side2", SDL_BUTTON_MASK(5) /*todo*/},
{"side3", GLFW_MOUSE_BUTTON_6}, {"side3", SDL_BUTTON_MASK(6) /*todo*/},
{"side4", GLFW_MOUSE_BUTTON_7}, {"side4", SDL_BUTTON_MASK(7) /*todo*/},
{"side5", GLFW_MOUSE_BUTTON_8}, {"side5", SDL_BUTTON_MASK(8) /*todo*/},
}; };
static std::unordered_map<int, std::string> keynames {}; static std::unordered_map<int, std::string> keynames {};
@ -86,14 +90,14 @@ void Binding::reset(Mousecode code) {
} }
void input_util::initialize() { void input_util::initialize() {
for (int i = 0; i <= 9; i++) { for (std::uint32_t i = 0; i <= 9; i++) {
keycodes[std::to_string(i)] = GLFW_KEY_0 + i; keycodes[std::to_string(i)] = SDLK_0 + i;
} }
for (int i = 0; i < 25; i++) { for (std::uint32_t i = 0; i < 25; i++) {
keycodes["f" + std::to_string(i + 1)] = GLFW_KEY_F1 + i; keycodes["f" + std::to_string(i + 1)] = SDLK_F1 + i;
} }
for (char i = 'a'; i <= 'z'; i++) { for (char i = 'a'; i <= 'z'; i++) {
keycodes[std::string({i})] = GLFW_KEY_A - 'a' + i; keycodes[std::string({i})] = SDLK_A - 'a' + i;
} }
for (const auto& entry : keycodes) { for (const auto& entry : keycodes) {
keynames[entry.second] = entry.first; keynames[entry.second] = entry.first;
@ -120,96 +124,91 @@ Mousecode input_util::mousecode_from(const std::string& name) {
} }
std::string input_util::to_string(Keycode code) { std::string input_util::to_string(Keycode code) {
int icode_repr = static_cast<int>(code); SDL_Scancode scancode = static_cast<SDL_Scancode>(code);
const char* name = switch (scancode) {
glfwGetKeyName(icode_repr, glfwGetKeyScancode(icode_repr)); case SDLK_TAB:
if (name == nullptr) { return "Tab";
switch (icode_repr) { case SDL_SCANCODE_LCTRL:
case GLFW_KEY_TAB: return "Left Ctrl";
return "Tab"; case SDL_SCANCODE_RCTRL:
case GLFW_KEY_LEFT_CONTROL: return "Right Ctrl";
return "Left Ctrl"; case SDL_SCANCODE_LALT:
case GLFW_KEY_RIGHT_CONTROL: return "Left Alt";
return "Right Ctrl"; case SDL_SCANCODE_RALT:
case GLFW_KEY_LEFT_ALT: return "Right Alt";
return "Left Alt"; case SDL_SCANCODE_LSHIFT:
case GLFW_KEY_RIGHT_ALT: return "Left Shift";
return "Right Alt"; case SDL_SCANCODE_RSHIFT:
case GLFW_KEY_LEFT_SHIFT: return "Right Shift";
return "Left Shift"; case SDL_SCANCODE_CAPSLOCK:
case GLFW_KEY_RIGHT_SHIFT: return "Caps-Lock";
return "Right Shift"; case SDL_SCANCODE_SPACE:
case GLFW_KEY_CAPS_LOCK: return "Space";
return "Caps-Lock"; case SDL_SCANCODE_ESCAPE:
case GLFW_KEY_SPACE: return "Esc";
return "Space"; case SDL_SCANCODE_RETURN:
case GLFW_KEY_ESCAPE: return "Enter";
return "Esc"; case SDL_SCANCODE_UP:
case GLFW_KEY_ENTER: return "Up";
return "Enter"; case SDL_SCANCODE_DOWN:
case GLFW_KEY_UP: return "Down";
return "Up"; case SDL_SCANCODE_LEFT:
case GLFW_KEY_DOWN: return "Left";
return "Down"; case SDL_SCANCODE_RIGHT:
case GLFW_KEY_LEFT: return "Right";
return "Left"; case SDL_SCANCODE_BACKSPACE:
case GLFW_KEY_RIGHT: return "Backspace";
return "Right"; case SDL_SCANCODE_F1:
case GLFW_KEY_BACKSPACE: return "F1";
return "Backspace"; case SDL_SCANCODE_F2:
case GLFW_KEY_F1: return "F2";
return "F1"; case SDL_SCANCODE_F3:
case GLFW_KEY_F2: return "F3";
return "F2"; case SDL_SCANCODE_F4:
case GLFW_KEY_F3: return "F4";
return "F3"; case SDL_SCANCODE_F5:
case GLFW_KEY_F4: return "F5";
return "F4"; case SDL_SCANCODE_F6:
case GLFW_KEY_F5: return "F6";
return "F5"; case SDL_SCANCODE_F7:
case GLFW_KEY_F6: return "F7";
return "F6"; case SDL_SCANCODE_F8:
case GLFW_KEY_F7: return "F8";
return "F7"; case SDL_SCANCODE_F9:
case GLFW_KEY_F8: return "F9";
return "F8"; case SDL_SCANCODE_F10:
case GLFW_KEY_F9: return "F10";
return "F9"; case SDL_SCANCODE_F11:
case GLFW_KEY_F10: return "F11";
return "F10"; case SDL_SCANCODE_F12:
case GLFW_KEY_F11: return "F12";
return "F11"; case SDL_SCANCODE_DELETE:
case GLFW_KEY_F12: return "Delete";
return "F12"; case SDL_SCANCODE_HOME:
case GLFW_KEY_DELETE: return "Home";
return "Delete"; case SDL_SCANCODE_END:
case GLFW_KEY_HOME: return "End";
return "Home"; case SDL_SCANCODE_LGUI:
case GLFW_KEY_END: return "Left Super";
return "End"; case SDL_SCANCODE_RGUI:
case GLFW_KEY_LEFT_SUPER: return "Right Super";
return "Left Super"; case SDL_SCANCODE_PAGEUP:
case GLFW_KEY_RIGHT_SUPER: return "Page Up";
return "Right Super"; case SDL_SCANCODE_PAGEDOWN:
case GLFW_KEY_PAGE_UP: return "Page Down";
return "Page Up"; case SDL_SCANCODE_INSERT:
case GLFW_KEY_PAGE_DOWN: return "Insert";
return "Page Down"; case SDL_SCANCODE_PRINTSCREEN:
case GLFW_KEY_INSERT: return "Print Screen";
return "Insert"; case SDL_SCANCODE_NUMLOCKCLEAR:
case GLFW_KEY_PRINT_SCREEN: return "Num Lock";
return "Print Screen"; case SDL_SCANCODE_APPLICATION:
case GLFW_KEY_NUM_LOCK: return "Menu";
return "Num Lock"; case SDL_SCANCODE_PAUSE:
case GLFW_KEY_MENU: return "Pause";
return "Menu"; default:
case GLFW_KEY_PAUSE: return "Unknown";
return "Pause";
default:
return "Unknown";
}
} }
return std::string(name);
} }
std::string input_util::to_string(Mousecode code) { std::string input_util::to_string(Mousecode code) {