Added a hint to mark a foreign window as usable with OpenGL Fixes https://github.com/libsdl-org/SDL/issues/2942
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
diff --git a/include/SDL_hints.h b/include/SDL_hints.h
index ec228fb..ec324f9 100644
--- a/include/SDL_hints.h
+++ b/include/SDL_hints.h
@@ -1454,6 +1454,17 @@ extern "C" {
#define SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT "SDL_VIDEO_WINDOW_SHARE_PIXEL_FORMAT"
/**
+ * \brief When calling SDL_CreateWindowFrom(), make the window compatible with OpenGL.
+ *
+ * This variable can be set to the following values:
+ * "0" - Don't add any graphics flags to the SDL_WindowFlags
+ * "1" - Add SDL_WINDOW_OPENGL to the SDL_WindowFlags
+ *
+ * By default SDL will not make the foreign window compatible with OpenGL.
+ */
+#define SDL_HINT_VIDEO_FOREIGN_WINDOW_OPENGL "SDL_VIDEO_FOREIGN_WINDOW_OPENGL"
+
+/**
* \brief When calling SDL_CreateWindowFrom(), make the window compatible with Vulkan.
*
* This variable can be set to the following values:
diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 6fd668c..da8c25d 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -1676,6 +1676,7 @@ SDL_Window *
SDL_CreateWindowFrom(const void *data)
{
SDL_Window *window;
+ Uint32 flags = SDL_WINDOW_FOREIGN;
if (!_this) {
SDL_UninitializedVideo();
@@ -1685,6 +1686,37 @@ SDL_CreateWindowFrom(const void *data)
SDL_Unsupported();
return NULL;
}
+
+ if (SDL_GetHintBoolean(SDL_HINT_VIDEO_FOREIGN_WINDOW_OPENGL, SDL_FALSE)) {
+ if (!_this->GL_CreateContext) {
+ SDL_SetError("OpenGL support is either not configured in SDL "
+ "or not available in current SDL video driver "
+ "(%s) or platform", _this->name);
+ return NULL;
+ }
+ if (SDL_GL_LoadLibrary(NULL) < 0) {
+ return NULL;
+ }
+ flags |= SDL_WINDOW_OPENGL;
+ }
+
+ if (SDL_GetHintBoolean(SDL_HINT_VIDEO_FOREIGN_WINDOW_VULKAN, SDL_FALSE)) {
+ if (!_this->Vulkan_CreateSurface) {
+ SDL_SetError("Vulkan support is either not configured in SDL "
+ "or not available in current SDL video driver "
+ "(%s) or platform", _this->name);
+ return NULL;
+ }
+ if (flags & SDL_WINDOW_OPENGL) {
+ SDL_SetError("Vulkan and OpenGL not supported on same window");
+ return NULL;
+ }
+ if (SDL_Vulkan_LoadLibrary(NULL) < 0) {
+ return NULL;
+ }
+ flags |= SDL_WINDOW_VULKAN;
+ }
+
window = (SDL_Window *)SDL_calloc(1, sizeof(*window));
if (!window) {
SDL_OutOfMemory();
@@ -1692,7 +1724,7 @@ SDL_CreateWindowFrom(const void *data)
}
window->magic = &_this->window_magic;
window->id = _this->next_object_id++;
- window->flags = SDL_WINDOW_FOREIGN;
+ window->flags = flags;
window->last_fullscreen_flags = window->flags;
window->is_destroying = SDL_FALSE;
window->opacity = 1.0f;
@@ -1703,16 +1735,6 @@ SDL_CreateWindowFrom(const void *data)
}
_this->windows = window;
-#if SDL_VIDEO_VULKAN
- if (SDL_GetHintBoolean(SDL_HINT_VIDEO_FOREIGN_WINDOW_VULKAN, SDL_FALSE)) {
- window->flags |= SDL_WINDOW_VULKAN;
- if (SDL_Vulkan_LoadLibrary(NULL) < 0) {
- SDL_DestroyWindow(window);
- return NULL;
- }
- }
-#endif
-
if (_this->CreateSDLWindowFrom(_this, window, data) < 0) {
SDL_DestroyWindow(window);
return NULL;
diff --git a/src/video/windows/SDL_windowswindow.c b/src/video/windows/SDL_windowswindow.c
index 4eb279a..9ec9089 100644
--- a/src/video/windows/SDL_windowswindow.c
+++ b/src/video/windows/SDL_windowswindow.c
@@ -422,6 +422,9 @@ WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
}
}
}
+ } else if (window->flags & SDL_WINDOW_OPENGL) {
+ /* Try to set up the pixel format, if it hasn't been set by the application */
+ WIN_GL_SetupWindow(_this, window);
}
}
#endif