Game window respects Windows theme setting

This commit is contained in:
Kae 2024-03-14 21:17:05 +11:00
parent 3534067801
commit 8164e5ae6f
2 changed files with 29 additions and 5 deletions

View File

@ -10,6 +10,11 @@
#include "SDL2/SDL.h" #include "SDL2/SDL.h"
#include "StarPlatformServices_pc.hpp" #include "StarPlatformServices_pc.hpp"
#ifdef STAR_SYSTEM_WINDOWS
#include "SDL2/SDL_syswm.h"
#include <dwmapi.h>
#endif
namespace Star { namespace Star {
Maybe<Key> keyFromSdlKeyCode(SDL_Keycode sym) { Maybe<Key> keyFromSdlKeyCode(SDL_Keycode sym) {
@ -268,11 +273,30 @@ public:
m_sdlWindow = SDL_CreateWindow(m_windowTitle.utf8Ptr(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, m_sdlWindow = SDL_CreateWindow(m_windowTitle.utf8Ptr(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
m_windowSize[0], m_windowSize[1], SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); m_windowSize[0], m_windowSize[1], SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (!m_sdlWindow) if (!m_sdlWindow)
throw ApplicationException::format("Application: Could not create SDL window: {}", SDL_GetError()); throw ApplicationException::format("Application: Could not create SDL Window: {}", SDL_GetError());
SDL_ShowWindow(m_sdlWindow); SDL_ShowWindow(m_sdlWindow);
SDL_RaiseWindow(m_sdlWindow); SDL_RaiseWindow(m_sdlWindow);
// Makes the window border black. From https://github.com/libsdl-org/SDL/commit/89948787#diff-f2ae5c36a8afc0a9a343a6664ab306da2963213e180af8cd97b12397dcbb9ae7R1478
#ifdef STAR_SYSTEM_WINDOWS
if (void* handle = SDL_LoadObject("dwmapi.dll")) {
if (auto DwmSetWindowAttributeFunc = (decltype(&DwmSetWindowAttribute))SDL_LoadFunction(handle, "DwmSetWindowAttribute")) {
SDL_SysWMinfo wmInfo{};
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(m_sdlWindow, &wmInfo);
DWORD type{}, value{}, count = sizeof(value);
LSTATUS status = RegGetValue(HKEY_CURRENT_USER,
TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"),
TEXT("AppsUseLightTheme"),
RRF_RT_REG_DWORD, &type, &value, &count);
BOOL enabled = status == ERROR_SUCCESS && type == REG_DWORD && value == 0;
DwmSetWindowAttributeFunc(wmInfo.info.win.window, DWMWA_USE_IMMERSIVE_DARK_MODE, &enabled, sizeof(enabled));
}
SDL_UnloadObject(handle);
}
#endif
int width; int width;
int height; int height;
SDL_GetWindowSize(m_sdlWindow, &width, &height); SDL_GetWindowSize(m_sdlWindow, &width, &height);

View File

@ -88,15 +88,15 @@ inline Vec2F WorldCamera::centerWorldPosition() const {
inline Vec2F WorldCamera::worldToScreen(Vec2F const& worldCoord) const { inline Vec2F WorldCamera::worldToScreen(Vec2F const& worldCoord) const {
Vec2F wrappedCoord = m_worldGeometry.nearestTo(Vec2F(m_worldCenter), worldCoord); Vec2F wrappedCoord = m_worldGeometry.nearestTo(Vec2F(m_worldCenter), worldCoord);
return Vec2F( return Vec2F(
(wrappedCoord[0] - m_worldCenter[0]) * (TilePixels * m_pixelRatio) + m_screenSize[0] / 2.0, (wrappedCoord[0] - m_worldCenter[0]) * (TilePixels * m_pixelRatio) + (float)m_screenSize[0] / 2.0,
(wrappedCoord[1] - m_worldCenter[1]) * (TilePixels * m_pixelRatio) + m_screenSize[1] / 2.0 (wrappedCoord[1] - m_worldCenter[1]) * (TilePixels * m_pixelRatio) + (float)m_screenSize[1] / 2.0
); );
} }
inline Vec2F WorldCamera::screenToWorld(Vec2F const& screen) const { inline Vec2F WorldCamera::screenToWorld(Vec2F const& screen) const {
return Vec2F( return Vec2F(
(screen[0] - m_screenSize[0] / 2.0) / (TilePixels * m_pixelRatio) + m_worldCenter[0], (screen[0] - (float)m_screenSize[0] / 2.0) / (TilePixels * m_pixelRatio) + m_worldCenter[0],
(screen[1] - m_screenSize[1] / 2.0) / (TilePixels * m_pixelRatio) + m_worldCenter[1] (screen[1] - (float)m_screenSize[1] / 2.0) / (TilePixels * m_pixelRatio) + m_worldCenter[1]
); );
} }