1

I wrote a game in C++, but it looks very different on different screens. I would like to have an in-game lightness modifier slider to let the user pick the right level of darkness.

I'm not very experienced with the Win32 API, but I tried the following:

window = CreateWindowExA(WS_EX_LAYERED, name, "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
         20, 20, 1366, 768, NULL, NULL, hInstance, NULL);
 
SetLayeredWindowAttributes(window, 0, 127,  LWA_ALPHA);

This sets the whole window to be semi-transparent, and I can see my desktop behind the app. If my desktop would be completely black this would be the desired effect.

Is there a way to get solid black or white instead of my desktop "behind" the game?

Or is there a better way to go about this?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Jompa
  • 59
  • 1
  • 7
  • I have not done Win32 UI stuff in a long time but couldn't you make this window the child of a window with a black background? – Jim Rhodes Aug 09 '21 at 19:47
  • @JimRhodes that would only work in Windows 8+, where `WS_EX_LAYERED` is supported on child windows. – Remy Lebeau Aug 09 '21 at 19:48
  • Light/dark is more a setting of the display monitor rather not the UI. That being said, you choose the colors you draw your window with, so what is stopping you from simply drawing with lighter/darker colors? If you really wanted to control "brightness" with a slider, you could have your drawing code simply apply a brightness algorithm to every RGB color it draws, and then have the slider invalidate the UI so the drawing code applies the latest brightness value on the next repaint. See [Algorithm to modify brightness for RGB image?](https://stackoverflow.com/questions/11163578/) – Remy Lebeau Aug 09 '21 at 19:51
  • @RemyLebeau I've tried postprocessing the whole screen to darken each pixel individually, but it decreases my FPS considerably, I was hoping this transparency method would work since it appears to be able to do essentially the same thing at almost zero fps cost – Jompa Aug 09 '21 at 20:15
  • 1
    @Jompa I didn't say to post-process the pixels after they are drawn. I said to apply the brightness at the time the pixels are being drawn. But, if you really want to go the transparency route, then I suggest creating a pure-black window and *overlay* it on top of your main window, and then you can set the transparency of the overlay. Rather than using a pure-black background and setting the transparency of the main window. – Remy Lebeau Aug 09 '21 at 20:27
  • Now doing that assumes that the RGB color space were linear. Turns out that the RGB color space is actually *non-linear*, meaning that a semi-transparent all-black overlay doesn't change the perceived brightness uniformly. The result will look as bad as from where you started, except worse. Unless you subscribe to fixing the core issue **in your application**, you will just wind up with workarounds that look even worse. – IInspectable Aug 10 '21 at 07:52

0 Answers0