mirror of
https://github.com/glfw/glfw.git
synced 2025-10-05 06:06:36 +00:00
transparent windows for OSX
This commit is contained in:
parent
16ae02ab85
commit
9ab285f440
@ -37,6 +37,7 @@ add_executable(offscreen offscreen.c ${ICON} ${GLAD})
|
|||||||
add_executable(particles WIN32 MACOSX_BUNDLE particles.c ${ICON} ${TINYCTHREAD} ${GETOPT} ${GLAD})
|
add_executable(particles WIN32 MACOSX_BUNDLE particles.c ${ICON} ${TINYCTHREAD} ${GETOPT} ${GLAD})
|
||||||
add_executable(simple WIN32 MACOSX_BUNDLE simple.c ${ICON} ${GLAD})
|
add_executable(simple WIN32 MACOSX_BUNDLE simple.c ${ICON} ${GLAD})
|
||||||
add_executable(splitview WIN32 MACOSX_BUNDLE splitview.c ${ICON} ${GLAD})
|
add_executable(splitview WIN32 MACOSX_BUNDLE splitview.c ${ICON} ${GLAD})
|
||||||
|
add_executable(transparent WIN32 MACOSX_BUNDLE transparent.c ${ICON} ${GLAD})
|
||||||
add_executable(wave WIN32 MACOSX_BUNDLE wave.c ${ICON} ${GLAD})
|
add_executable(wave WIN32 MACOSX_BUNDLE wave.c ${ICON} ${GLAD})
|
||||||
|
|
||||||
target_link_libraries(particles "${CMAKE_THREAD_LIBS_INIT}")
|
target_link_libraries(particles "${CMAKE_THREAD_LIBS_INIT}")
|
||||||
|
170
examples/transparent.c
Normal file
170
examples/transparent.c
Normal file
@ -0,0 +1,170 @@
|
|||||||
|
//========================================================================
|
||||||
|
// Simple GLFW example
|
||||||
|
// Copyright (c) Camilla Berglund <elmindreda@elmindreda.org>
|
||||||
|
//
|
||||||
|
// This software is provided 'as-is', without any express or implied
|
||||||
|
// warranty. In no event will the authors be held liable for any damages
|
||||||
|
// arising from the use of this software.
|
||||||
|
//
|
||||||
|
// Permission is granted to anyone to use this software for any purpose,
|
||||||
|
// including commercial applications, and to alter it and redistribute it
|
||||||
|
// freely, subject to the following restrictions:
|
||||||
|
//
|
||||||
|
// 1. The origin of this software must not be misrepresented; you must not
|
||||||
|
// claim that you wrote the original software. If you use this software
|
||||||
|
// in a product, an acknowledgment in the product documentation would
|
||||||
|
// be appreciated but is not required.
|
||||||
|
//
|
||||||
|
// 2. Altered source versions must be plainly marked as such, and must not
|
||||||
|
// be misrepresented as being the original software.
|
||||||
|
//
|
||||||
|
// 3. This notice may not be removed or altered from any source
|
||||||
|
// distribution.
|
||||||
|
//
|
||||||
|
//========================================================================
|
||||||
|
//! [code]
|
||||||
|
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
#include "linmath.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
static const struct
|
||||||
|
{
|
||||||
|
float x, y;
|
||||||
|
float r, g, b;
|
||||||
|
} vertices[3] =
|
||||||
|
{
|
||||||
|
{ -0.6f, -0.4f, 1.f, 0.f, 0.f },
|
||||||
|
{ 0.6f, -0.4f, 0.f, 1.f, 0.f },
|
||||||
|
{ 0.f, 0.6f, 0.f, 0.f, 1.f }
|
||||||
|
};
|
||||||
|
|
||||||
|
static const char* vertex_shader_text =
|
||||||
|
"uniform mat4 MVP;\n"
|
||||||
|
"attribute vec3 vCol;\n"
|
||||||
|
"attribute vec2 vPos;\n"
|
||||||
|
"varying vec3 color;\n"
|
||||||
|
"void main()\n"
|
||||||
|
"{\n"
|
||||||
|
" gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
|
||||||
|
" color = vCol;\n"
|
||||||
|
"}\n";
|
||||||
|
|
||||||
|
static const char* fragment_shader_text =
|
||||||
|
"varying vec3 color;\n"
|
||||||
|
"void main()\n"
|
||||||
|
"{\n"
|
||||||
|
" gl_FragColor = vec4(color, 1.0);\n"
|
||||||
|
"}\n";
|
||||||
|
|
||||||
|
static void error_callback(int error, const char* description)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Error: %s\n", description);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
||||||
|
{
|
||||||
|
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
||||||
|
glfwSetWindowShouldClose(window, GLFW_TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
GLFWwindow* window;
|
||||||
|
GLuint vertex_buffer, vertex_shader, fragment_shader, program;
|
||||||
|
GLint mvp_location, vpos_location, vcol_location;
|
||||||
|
|
||||||
|
glfwSetErrorCallback(error_callback);
|
||||||
|
|
||||||
|
if (!glfwInit())
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
|
||||||
|
|
||||||
|
glfwWindowHint(GLFW_ALPHA_BITS, 8);
|
||||||
|
glfwWindowHint(GLFW_ALPHA_MASK, GLFW_TRUE);
|
||||||
|
glfwWindowHint(GLFW_DECORATED, GLFW_TRUE);
|
||||||
|
|
||||||
|
window = glfwCreateWindow(256, 256, "Transparent window example", NULL, NULL);
|
||||||
|
if (!window)
|
||||||
|
{
|
||||||
|
glfwTerminate();
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
glfwSetWindowPos(window, 300, 300);
|
||||||
|
|
||||||
|
glfwSetKeyCallback(window, key_callback);
|
||||||
|
|
||||||
|
glfwMakeContextCurrent(window);
|
||||||
|
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
|
||||||
|
glfwSwapInterval(1);
|
||||||
|
|
||||||
|
// NOTE: OpenGL error checks have been omitted for brevity
|
||||||
|
|
||||||
|
glGenBuffers(1, &vertex_buffer);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
|
||||||
|
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
|
||||||
|
glCompileShader(vertex_shader);
|
||||||
|
|
||||||
|
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
|
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
|
||||||
|
glCompileShader(fragment_shader);
|
||||||
|
|
||||||
|
program = glCreateProgram();
|
||||||
|
glAttachShader(program, vertex_shader);
|
||||||
|
glAttachShader(program, fragment_shader);
|
||||||
|
glLinkProgram(program);
|
||||||
|
|
||||||
|
mvp_location = glGetUniformLocation(program, "MVP");
|
||||||
|
vpos_location = glGetAttribLocation(program, "vPos");
|
||||||
|
vcol_location = glGetAttribLocation(program, "vCol");
|
||||||
|
|
||||||
|
glEnableVertexAttribArray(vpos_location);
|
||||||
|
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
|
||||||
|
sizeof(float) * 5, (void*) 0);
|
||||||
|
glEnableVertexAttribArray(vcol_location);
|
||||||
|
glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE,
|
||||||
|
sizeof(float) * 5, (void*) (sizeof(float) * 2));
|
||||||
|
|
||||||
|
while (!glfwWindowShouldClose(window))
|
||||||
|
{
|
||||||
|
float ratio;
|
||||||
|
int width, height;
|
||||||
|
mat4x4 m, p, mvp;
|
||||||
|
|
||||||
|
glfwGetFramebufferSize(window, &width, &height);
|
||||||
|
ratio = width / (float) height;
|
||||||
|
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
glClearColor(0,0,0,0.5);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
|
mat4x4_identity(m);
|
||||||
|
mat4x4_rotate_Z(m, m, (float) glfwGetTime());
|
||||||
|
mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f);
|
||||||
|
mat4x4_mul(mvp, p, m);
|
||||||
|
|
||||||
|
glUseProgram(program);
|
||||||
|
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||||
|
|
||||||
|
glfwSwapBuffers(window);
|
||||||
|
glfwPollEvents();
|
||||||
|
}
|
||||||
|
|
||||||
|
glfwDestroyWindow(window);
|
||||||
|
|
||||||
|
glfwTerminate();
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
//! [code]
|
@ -868,6 +868,12 @@ extern "C" {
|
|||||||
* Framebuffer double buffering [hint](@ref GLFW_DOUBLEBUFFER).
|
* Framebuffer double buffering [hint](@ref GLFW_DOUBLEBUFFER).
|
||||||
*/
|
*/
|
||||||
#define GLFW_DOUBLEBUFFER 0x00021010
|
#define GLFW_DOUBLEBUFFER 0x00021010
|
||||||
|
/*! @brief Context ALPHA Mask hint and attribute.
|
||||||
|
*
|
||||||
|
* Context window alpha [hint](@ref GLFW_ALPHA_MASK_hint) and
|
||||||
|
* [attribute](@ref GLFW_ALPHA_MASK_attrib).
|
||||||
|
*/
|
||||||
|
#define GLFW_ALPHA_MASK 0x00021011
|
||||||
/*! @brief Context client API hint and attribute.
|
/*! @brief Context client API hint and attribute.
|
||||||
*
|
*
|
||||||
* Context client API [hint](@ref GLFW_CLIENT_API_hint) and
|
* Context client API [hint](@ref GLFW_CLIENT_API_hint) and
|
||||||
|
@ -413,7 +413,11 @@ static const NSRange kEmptyRange = { NSNotFound, 0 };
|
|||||||
|
|
||||||
- (BOOL)isOpaque
|
- (BOOL)isOpaque
|
||||||
{
|
{
|
||||||
return YES;
|
// Set to NO even if alphaMask is not used;
|
||||||
|
// The NSView/GLFWContentView does not need to be opaque anyway,
|
||||||
|
// and to avoid keeping track of alphaMask inside the NSView we
|
||||||
|
// just return NO here instead.
|
||||||
|
return NO;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL)canBecomeKeyView
|
- (BOOL)canBecomeKeyView
|
||||||
@ -1081,6 +1085,12 @@ static GLFWbool createNativeWindow(_GLFWwindow* window,
|
|||||||
if (wndconfig->ns.retina)
|
if (wndconfig->ns.retina)
|
||||||
[window->ns.view setWantsBestResolutionOpenGLSurface:YES];
|
[window->ns.view setWantsBestResolutionOpenGLSurface:YES];
|
||||||
|
|
||||||
|
if (wndconfig->alphaMask)
|
||||||
|
{
|
||||||
|
[window->ns.object setOpaque:NO];
|
||||||
|
[window->ns.object setBackgroundColor:[NSColor clearColor]];
|
||||||
|
}
|
||||||
|
|
||||||
[window->ns.object setContentView:window->ns.view];
|
[window->ns.object setContentView:window->ns.view];
|
||||||
[window->ns.object makeFirstResponder:window->ns.view];
|
[window->ns.object makeFirstResponder:window->ns.view];
|
||||||
[window->ns.object setTitle:[NSString stringWithUTF8String:wndconfig->title]];
|
[window->ns.object setTitle:[NSString stringWithUTF8String:wndconfig->title]];
|
||||||
|
@ -182,6 +182,12 @@ const _GLFWfbconfig* _glfwChooseFBConfig(const _GLFWfbconfig* desired,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (desired->alphaMask > 0 && current->alphaMask == 0)
|
||||||
|
{
|
||||||
|
// Alpha mask is a hard constraint
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Count number of missing buffers
|
// Count number of missing buffers
|
||||||
{
|
{
|
||||||
missing = 0;
|
missing = 0;
|
||||||
|
@ -304,6 +304,7 @@ struct _GLFWwndconfig
|
|||||||
GLFWbool floating;
|
GLFWbool floating;
|
||||||
GLFWbool maximized;
|
GLFWbool maximized;
|
||||||
GLFWbool centerCursor;
|
GLFWbool centerCursor;
|
||||||
|
GLFWbool alphaMask;
|
||||||
struct {
|
struct {
|
||||||
GLFWbool retina;
|
GLFWbool retina;
|
||||||
GLFWbool frame;
|
GLFWbool frame;
|
||||||
@ -347,6 +348,7 @@ struct _GLFWfbconfig
|
|||||||
int redBits;
|
int redBits;
|
||||||
int greenBits;
|
int greenBits;
|
||||||
int blueBits;
|
int blueBits;
|
||||||
|
int alphaMask;
|
||||||
int alphaBits;
|
int alphaBits;
|
||||||
int depthBits;
|
int depthBits;
|
||||||
int stencilBits;
|
int stencilBits;
|
||||||
@ -405,6 +407,7 @@ struct _GLFWwindow
|
|||||||
GLFWbool autoIconify;
|
GLFWbool autoIconify;
|
||||||
GLFWbool floating;
|
GLFWbool floating;
|
||||||
GLFWbool shouldClose;
|
GLFWbool shouldClose;
|
||||||
|
GLFWbool transparent;
|
||||||
void* userPointer;
|
void* userPointer;
|
||||||
GLFWvidmode videoMode;
|
GLFWvidmode videoMode;
|
||||||
_GLFWmonitor* monitor;
|
_GLFWmonitor* monitor;
|
||||||
|
@ -296,6 +296,12 @@ GLFWbool _glfwCreateContextNSGL(_GLFWwindow* window,
|
|||||||
return GLFW_FALSE;
|
return GLFW_FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (fbconfig->alphaMask)
|
||||||
|
{
|
||||||
|
GLint opaque = 0;
|
||||||
|
[window->context.nsgl.object setValues:&opaque forParameter:NSOpenGLCPSurfaceOpacity];
|
||||||
|
}
|
||||||
|
|
||||||
[window->context.nsgl.object setView:window->ns.view];
|
[window->context.nsgl.object setView:window->ns.view];
|
||||||
|
|
||||||
window->context.makeCurrent = makeContextCurrentNSGL;
|
window->context.makeCurrent = makeContextCurrentNSGL;
|
||||||
|
@ -201,6 +201,8 @@ static int choosePixelFormat(_GLFWwindow* window,
|
|||||||
}
|
}
|
||||||
|
|
||||||
u->handle = n;
|
u->handle = n;
|
||||||
|
// always able to create alpha mask on win
|
||||||
|
u->alphaMask = desired->alphaMask;
|
||||||
usableCount++;
|
usableCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,11 +147,13 @@ GLFWAPI GLFWwindow* glfwCreateWindow(int width, int height,
|
|||||||
fbconfig = _glfw.hints.framebuffer;
|
fbconfig = _glfw.hints.framebuffer;
|
||||||
ctxconfig = _glfw.hints.context;
|
ctxconfig = _glfw.hints.context;
|
||||||
wndconfig = _glfw.hints.window;
|
wndconfig = _glfw.hints.window;
|
||||||
|
fbconfig.alphaMask = _glfw.hints.framebuffer.alphaMask ? GLFW_TRUE : GLFW_FALSE;
|
||||||
|
|
||||||
wndconfig.width = width;
|
wndconfig.width = width;
|
||||||
wndconfig.height = height;
|
wndconfig.height = height;
|
||||||
wndconfig.title = title;
|
wndconfig.title = title;
|
||||||
ctxconfig.share = (_GLFWwindow*) share;
|
ctxconfig.share = (_GLFWwindow*) share;
|
||||||
|
wndconfig.alphaMask = _glfw.hints.framebuffer.alphaMask ? GLFW_TRUE : GLFW_FALSE;
|
||||||
|
|
||||||
if (ctxconfig.share)
|
if (ctxconfig.share)
|
||||||
{
|
{
|
||||||
@ -182,6 +184,7 @@ GLFWAPI GLFWwindow* glfwCreateWindow(int width, int height,
|
|||||||
window->decorated = wndconfig.decorated;
|
window->decorated = wndconfig.decorated;
|
||||||
window->autoIconify = wndconfig.autoIconify;
|
window->autoIconify = wndconfig.autoIconify;
|
||||||
window->floating = wndconfig.floating;
|
window->floating = wndconfig.floating;
|
||||||
|
window->transparent = wndconfig.alphaMask;
|
||||||
window->cursorMode = GLFW_CURSOR_NORMAL;
|
window->cursorMode = GLFW_CURSOR_NORMAL;
|
||||||
|
|
||||||
window->minwidth = GLFW_DONT_CARE;
|
window->minwidth = GLFW_DONT_CARE;
|
||||||
@ -262,6 +265,7 @@ void glfwDefaultWindowHints(void)
|
|||||||
_glfw.hints.framebuffer.depthBits = 24;
|
_glfw.hints.framebuffer.depthBits = 24;
|
||||||
_glfw.hints.framebuffer.stencilBits = 8;
|
_glfw.hints.framebuffer.stencilBits = 8;
|
||||||
_glfw.hints.framebuffer.doublebuffer = GLFW_TRUE;
|
_glfw.hints.framebuffer.doublebuffer = GLFW_TRUE;
|
||||||
|
_glfw.hints.framebuffer.alphaMask = GLFW_FALSE;
|
||||||
|
|
||||||
// The default is to select the highest available refresh rate
|
// The default is to select the highest available refresh rate
|
||||||
_glfw.hints.refreshRate = GLFW_DONT_CARE;
|
_glfw.hints.refreshRate = GLFW_DONT_CARE;
|
||||||
@ -315,6 +319,9 @@ GLFWAPI void glfwWindowHint(int hint, int value)
|
|||||||
case GLFW_DOUBLEBUFFER:
|
case GLFW_DOUBLEBUFFER:
|
||||||
_glfw.hints.framebuffer.doublebuffer = value ? GLFW_TRUE : GLFW_FALSE;
|
_glfw.hints.framebuffer.doublebuffer = value ? GLFW_TRUE : GLFW_FALSE;
|
||||||
return;
|
return;
|
||||||
|
case GLFW_ALPHA_MASK:
|
||||||
|
_glfw.hints.framebuffer.alphaMask = value;
|
||||||
|
return;
|
||||||
case GLFW_SAMPLES:
|
case GLFW_SAMPLES:
|
||||||
_glfw.hints.framebuffer.samples = value;
|
_glfw.hints.framebuffer.samples = value;
|
||||||
return;
|
return;
|
||||||
|
Loading…
Reference in New Issue
Block a user