Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions include/game_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,43 @@ enum class GraphicsApi {
OPENGL,
OPENGL_ES2
};

enum class GraphicsClientApi {
UNKNOWN,
OPENGL,
OPENGL_ES
};

enum class GraphicsWindowSystem {
UNKNOWN,
GLFW,
SDL3,
EGLUT
};

enum class GraphicsContextProfile {
UNKNOWN,
CORE,
COMPATIBILITY,
ES
};

enum class GraphicsContextCreationApi {
UNKNOWN,
NATIVE,
EGL,
OSMESA
};

struct GraphicsContextInfo {
GraphicsWindowSystem windowSystem = GraphicsWindowSystem::UNKNOWN;
GraphicsClientApi clientApi = GraphicsClientApi::UNKNOWN;
int versionMajor = -1;
int versionMinor = -1;
int versionRevision = -1;
GraphicsContextProfile profile = GraphicsContextProfile::UNKNOWN;
GraphicsContextCreationApi creationApi = GraphicsContextCreationApi::UNKNOWN;
};
enum class KeyAction {
PRESS,
REPEAT,
Expand Down Expand Up @@ -94,6 +131,12 @@ class GameWindow {

virtual void makeCurrent(bool) = 0;

// Returns properties of the successfully created context. Call this while
// the context is current; implementations must not return request hints.
virtual GraphicsContextInfo getGraphicsContextInfo() const {
return {};
}

virtual void setIcon(std::string const& iconPath) = 0;

virtual void show() = 0;
Expand Down
9 changes: 7 additions & 2 deletions include/game_window_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ class GameWindowManager {
static std::shared_ptr<GameWindowManager> getManager();


using AnyFunc = void* (*)();
using AnyFunc = void (*)();
using ProcAddrFunc = AnyFunc (*)(const char*);


virtual ProcAddrFunc getProcAddrFunc() = 0;

// Returns a resolver for host EGL entry points when the active window
// implementation uses EGL. This is separate from the guest GL resolver
// exposed to Minecraft and may be unavailable for native contexts.
virtual ProcAddrFunc getEglProcAddrFunc() { return nullptr; }

virtual std::shared_ptr<GameWindow>
createWindow(const std::string& title, int width, int height, GraphicsApi api) = 0;

Expand All @@ -41,4 +46,4 @@ class GameWindowManager {
}

const std::shared_ptr<GameWindowErrorHandler>& getErrorHandler() { return errorhandler; }
};
};
9 changes: 8 additions & 1 deletion src/window_eglut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ void EGLUTWindow::makeCurrent(bool active) {
eglutMakeCurrent(active ? winId : -1);
}

GraphicsContextInfo EGLUTWindow::getGraphicsContextInfo() const {
GraphicsContextInfo info;
info.windowSystem = GraphicsWindowSystem::EGLUT;
info.creationApi = GraphicsContextCreationApi::EGL;
return info;
}

void EGLUTWindow::show() {
#ifdef GAMEWINDOW_X11_LOCK
std::lock_guard<std::recursive_mutex> lock(x11_sync);
Expand Down Expand Up @@ -451,4 +458,4 @@ int EGLUTWindow::translateMeta(unsigned int meta) {
mods |= KEY_MOD_NUMLOCK;
}
return mods;
}
}
2 changes: 2 additions & 0 deletions src/window_eglut.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class EGLUTWindow : public WindowWithLinuxJoystick {

void makeCurrent(bool active) override;

GraphicsContextInfo getGraphicsContextInfo() const override;

void show() override;

void close() override;
Expand Down
53 changes: 52 additions & 1 deletion src/window_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,57 @@ void GLFWGameWindow::makeCurrent(bool c) {
glfwMakeContextCurrent(c ? window : nullptr);
}

GraphicsContextInfo GLFWGameWindow::getGraphicsContextInfo() const {
GraphicsContextInfo info;
info.windowSystem = GraphicsWindowSystem::GLFW;

switch(glfwGetWindowAttrib(window, GLFW_CLIENT_API)) {
case GLFW_OPENGL_API:
info.clientApi = GraphicsClientApi::OPENGL;
break;
case GLFW_OPENGL_ES_API:
info.clientApi = GraphicsClientApi::OPENGL_ES;
break;
default:
break;
}

info.versionMajor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
info.versionMinor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
info.versionRevision = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);

if(info.clientApi == GraphicsClientApi::OPENGL_ES) {
info.profile = GraphicsContextProfile::ES;
} else {
switch(glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE)) {
case GLFW_OPENGL_CORE_PROFILE:
info.profile = GraphicsContextProfile::CORE;
break;
case GLFW_OPENGL_COMPAT_PROFILE:
info.profile = GraphicsContextProfile::COMPATIBILITY;
break;
default:
break;
}
}

switch(glfwGetWindowAttrib(window, GLFW_CONTEXT_CREATION_API)) {
case GLFW_NATIVE_CONTEXT_API:
info.creationApi = GraphicsContextCreationApi::NATIVE;
break;
case GLFW_EGL_CONTEXT_API:
info.creationApi = GraphicsContextCreationApi::EGL;
break;
case GLFW_OSMESA_CONTEXT_API:
info.creationApi = GraphicsContextCreationApi::OSMESA;
break;
default:
break;
}

return info;
}

GLFWGameWindow::~GLFWGameWindow() {
#ifdef GAMEWINDOW_X11_LOCK
std::lock_guard<std::recursive_mutex> lock(x11_sync);
Expand Down Expand Up @@ -508,4 +559,4 @@ int GLFWGameWindow::translateMeta(unsigned int meta) {
mods |= KEY_MOD_NUMLOCK;
}
return mods;
}
}
2 changes: 2 additions & 0 deletions src/window_glfw.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class GLFWGameWindow : public GameWindow {

void makeCurrent(bool active) override;

GraphicsContextInfo getGraphicsContextInfo() const override;

double getRelativeScale() const;

void setRelativeScale();
Expand Down
15 changes: 12 additions & 3 deletions src/window_manager_eglut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
#endif
#include <libgen.h>
#include <cstring>
#include <EGL/egl.h>

extern "C" void eglGetProcAddress();
namespace {
GameWindowManager::AnyFunc resolveEglProcAddress(const char* name) {
return reinterpret_cast<GameWindowManager::AnyFunc>(eglGetProcAddress(name));
}
}

EGLUTWindowManager::EGLUTWindowManager() {
char buf[PATH_MAX];
Expand All @@ -22,7 +27,11 @@ EGLUTWindowManager::EGLUTWindowManager() {
}

GameWindowManager::ProcAddrFunc EGLUTWindowManager::getProcAddrFunc() {
return (GameWindowManager::ProcAddrFunc) eglGetProcAddress;
return resolveEglProcAddress;
}

GameWindowManager::ProcAddrFunc EGLUTWindowManager::getEglProcAddrFunc() {
return resolveEglProcAddress;
}

std::shared_ptr<GameWindow> EGLUTWindowManager::createWindow(const std::string& title, int width, int height,
Expand All @@ -43,4 +52,4 @@ void EGLUTWindowManager::addGamePadMapping(const std::string &content) {
std::shared_ptr<GameWindowManager> GameWindowManager::createManager() {
return std::shared_ptr<GameWindowManager>(new EGLUTWindowManager());
}
#endif
#endif
4 changes: 3 additions & 1 deletion src/window_manager_eglut.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ class EGLUTWindowManager : public GameWindowManager {

ProcAddrFunc getProcAddrFunc() override;

ProcAddrFunc getEglProcAddrFunc() override;

std::shared_ptr<GameWindow> createWindow(const std::string& title, int width, int height, GraphicsApi api) override;

void addGamepadMappingFile(const std::string& path) override;

void addGamePadMapping(const std::string &content) override;
};
};
14 changes: 12 additions & 2 deletions src/window_manager_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
#include "joystick_manager_glfw.h"
#include <stdexcept>

namespace {
GameWindowManager::AnyFunc resolveGlfwProcAddress(const char* name) {
return reinterpret_cast<GameWindowManager::AnyFunc>(glfwGetProcAddress(name));
}
}

GLFWWindowManager::GLFWWindowManager() {
// To create a default mapping for not mapped Gamepads
// to avoid subtracting heads from buttons again
Expand All @@ -13,7 +19,11 @@ GLFWWindowManager::GLFWWindowManager() {
}

GameWindowManager::ProcAddrFunc GLFWWindowManager::getProcAddrFunc() {
return (GameWindowManager::ProcAddrFunc) glfwGetProcAddress;
return resolveGlfwProcAddress;
}

GameWindowManager::ProcAddrFunc GLFWWindowManager::getEglProcAddrFunc() {
return resolveGlfwProcAddress;
}

std::shared_ptr<GameWindow> GLFWWindowManager::createWindow(const std::string& title, int width, int height,
Expand All @@ -34,4 +44,4 @@ void GLFWWindowManager::addGamePadMapping(const std::string &content) {
std::shared_ptr<GameWindowManager> GameWindowManager::createManager() {
return std::shared_ptr<GameWindowManager>(new GLFWWindowManager());
}
#endif
#endif
4 changes: 3 additions & 1 deletion src/window_manager_glfw.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ class GLFWWindowManager : public GameWindowManager {

ProcAddrFunc getProcAddrFunc() override;

ProcAddrFunc getEglProcAddrFunc() override;

std::shared_ptr<GameWindow> createWindow(const std::string& title, int width, int height, GraphicsApi api) override;

void addGamepadMappingFile(const std::string& path) override;

void addGamePadMapping(const std::string &content) override;
};
};
4 changes: 4 additions & 0 deletions src/window_manager_glfw_fallback_eglut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ GameWindowManager::ProcAddrFunc GLFWFallbackEGLUTWindowManager::getProcAddrFunc(
return manager->getProcAddrFunc();
}

GameWindowManager::ProcAddrFunc GLFWFallbackEGLUTWindowManager::getEglProcAddrFunc() {
return manager->getEglProcAddrFunc();
}

std::shared_ptr<GameWindow> GLFWFallbackEGLUTWindowManager::createWindow(const std::string& title, int width, int height,
GraphicsApi api) {
return manager->createWindow(title, width, height, api);
Expand Down
4 changes: 3 additions & 1 deletion src/window_manager_glfw_fallback_eglut.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ class GLFWFallbackEGLUTWindowManager : public GameWindowManager {

ProcAddrFunc getProcAddrFunc() override;

ProcAddrFunc getEglProcAddrFunc() override;

std::shared_ptr<GameWindow> createWindow(const std::string& title, int width, int height, GraphicsApi api) override;

void addGamepadMappingFile(const std::string& path) override;

void addGamePadMapping(const std::string &content) override;
};
};
18 changes: 16 additions & 2 deletions src/window_manager_sdl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@

#include <SDL3/SDL.h>

namespace {
GameWindowManager::AnyFunc resolveSdlGlProcAddress(const char* name) {
return reinterpret_cast<GameWindowManager::AnyFunc>(SDL_GL_GetProcAddress(name));
}

GameWindowManager::AnyFunc resolveSdlEglProcAddress(const char* name) {
return reinterpret_cast<GameWindowManager::AnyFunc>(SDL_EGL_GetProcAddress(name));
}
}

SDL3WindowManager::SDL3WindowManager() {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_GAMEPAD);
}

GameWindowManager::ProcAddrFunc SDL3WindowManager::getProcAddrFunc() {
return (GameWindowManager::ProcAddrFunc) SDL_GL_GetProcAddress;
return resolveSdlGlProcAddress;
}

GameWindowManager::ProcAddrFunc SDL3WindowManager::getEglProcAddrFunc() {
return resolveSdlEglProcAddress;
}

std::shared_ptr<GameWindow> SDL3WindowManager::createWindow(const std::string& title, int width, int height,
Expand Down Expand Up @@ -45,4 +59,4 @@ void SDL3WindowManager::addGamePadMapping(const std::string &content) {
// Define this window manager as the used one
std::shared_ptr<GameWindowManager> GameWindowManager::createManager() {
return std::shared_ptr<GameWindowManager>(new SDL3WindowManager());
}
}
4 changes: 3 additions & 1 deletion src/window_manager_sdl3.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ class SDL3WindowManager : public GameWindowManager {

ProcAddrFunc getProcAddrFunc() override;

ProcAddrFunc getEglProcAddrFunc() override;

std::shared_ptr<GameWindow> createWindow(const std::string& title, int width, int height, GraphicsApi api) override;

void addGamepadMappingFile(const std::string& path) override;

void addGamePadMapping(const std::string &content) override;
};
};
33 changes: 33 additions & 0 deletions src/window_sdl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,39 @@ void SDL3GameWindow::makeCurrent(bool c) {
SDL_GL_MakeCurrent(window, c ? context : nullptr);
}

GraphicsContextInfo SDL3GameWindow::getGraphicsContextInfo() const {
GraphicsContextInfo info;
info.windowSystem = GraphicsWindowSystem::SDL3;
int value = 0;

if(SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &value)) {
info.versionMajor = value;
}
if(SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &value)) {
info.versionMinor = value;
}
if(SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &value)) {
switch(value) {
case SDL_GL_CONTEXT_PROFILE_ES:
info.clientApi = GraphicsClientApi::OPENGL_ES;
info.profile = GraphicsContextProfile::ES;
break;
case SDL_GL_CONTEXT_PROFILE_CORE:
info.clientApi = GraphicsClientApi::OPENGL;
info.profile = GraphicsContextProfile::CORE;
break;
case SDL_GL_CONTEXT_PROFILE_COMPATIBILITY:
info.clientApi = GraphicsClientApi::OPENGL;
info.profile = GraphicsContextProfile::COMPATIBILITY;
break;
default:
break;
}
}

return info;
}

SDL3GameWindow::~SDL3GameWindow() {
if(window) {
SDL_DestroyWindow(window);
Expand Down
Loading