SFML community forums

General => SFML projects => Topic started by: texus on June 07, 2015, 01:12:35 am

Title: Non-rectangular semi-transparent window
Post by: texus on June 07, 2015, 01:12:35 am
This is just some code that I wanted to share.
It loads an image from a file and will give the window the shape of that image. You can also set the opacity of the window.

I only wrote code for Windows and Linux. I will try to have a look at OS X next month. In case I update the code you can find it on github (https://github.com/texus/TransparentWindows).

Edit: The code now works on Windows, Linux and Mac.
On mac you have to compile both files, on windows and linux just Transparent.cpp.
On windows you only need the sfml libraries, on linux you also have to add "-lX11 -lXext" to the linker flags and on mac you should add "-framework Cocoa".

Transparent.cpp:
(click to show/hide)

Transparent.mm (only needed on mac)
(click to show/hide)

Result:
(http://i.imgur.com/9RwGge9.png)
Title: Re: Non-rectangular semi-transparent window
Post by: zmertens on June 07, 2015, 01:18:49 am
That's pretty neat!
Title: Re: Non-rectangular semi-transparent window
Post by: Mario on June 07, 2015, 10:17:24 am
You should split that code, one for the non-rectangular shape, one for alpha transparency.
Title: Re: Non-rectangular semi-transparent window
Post by: texus on June 07, 2015, 12:30:00 pm
Good idea. While developing I needed them together on linux while I was trying various methods to get it working, but in this finished version they have indeed became independent.
Title: Re: Non-rectangular semi-transparent window
Post by: texus on June 07, 2015, 10:51:43 pm
I found some time already to add support for Mac OS X. Check github (https://github.com/texus/TransparentWindows) for the updated code.

There is one problem though. In order to make the non-rectangular window work I also had to make a change inside SFML. I had to add the following function in the SFOpenGLView class:
-(BOOL)isOpaque {
    return NO;
}

Without that change the window can only be semi-transparent and will not get the custom shape on mac.
Title: Re: Non-rectangular semi-transparent window
Post by: Hiura on June 07, 2015, 11:09:23 pm
Can you try adding in your .mm file something like that:

@implementation SFOpenGLView (Opaque)
-(BOOL)isOpaque { return NO; }
@end

Does it work?

Override a method via ObjC Category (http://stackoverflow.com/questions/1085479/override-a-method-via-objc-category-and-call-the-default-implementation)
Title: Re: Non-rectangular semi-transparent window
Post by: texus on June 07, 2015, 11:32:35 pm
No, it just gives me the error that no interface declaration can be found for 'SFOpenGLView'.
Title: Re: Non-rectangular semi-transparent window
Post by: texus on June 07, 2015, 11:51:32 pm
I got it working. Instead of overriding the function of SFOpenGLView like that (for which the declaration isn't public), I could just override the one in NSOpenGLView. Thanks for the advice.
Title: Re: Non-rectangular semi-transparent window
Post by: Hiura on June 08, 2015, 07:51:38 am
Brilliant!  :)
Title: Re: Non-rectangular semi-transparent window
Post by: dwarfman78 on June 08, 2015, 10:35:40 am
Is it possible to add it to SFML as a standard feature in the next release ?