Skip to content

Commit

Permalink
RenderWindow: Add a function to set the fullscreen gamma
Browse files Browse the repository at this point in the history
  • Loading branch information
dscharrer committed Oct 26, 2017
1 parent 94ba099 commit 41f1d3d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/window/RenderWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ class RenderWindow : public Window {
*/
virtual bool setVSync(int vsync) = 0;

/*!
* Set the monitor gamma when in fullscreen mode.
*/
virtual bool setGamma(float gamma = 1.f) = 0;

Renderer * getRenderer() { return m_renderer; }

//! Get a sorted list of supported fullscreen display modes.
Expand Down
34 changes: 34 additions & 0 deletions src/window/SDL1Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ SDL1Window::SDL1Window()
: m_initialized(false)
, m_desktopMode(Vec2i(640, 480))
, m_input(NULL)
, m_gamma(1.f)
, m_gammaOverridden(false)
{
m_renderer = new OpenGLRenderer;
}
Expand All @@ -53,6 +55,7 @@ SDL1Window::~SDL1Window() {
}

if(s_mainWindow) {
restoreGamma();
SDL_Quit(), s_mainWindow = NULL;
}

Expand Down Expand Up @@ -242,6 +245,8 @@ bool SDL1Window::initialize() {

SDL_ShowCursor(SDL_DISABLE);

setGamma(m_gamma);

m_renderer->initialize();

onCreate();
Expand Down Expand Up @@ -270,6 +275,27 @@ bool SDL1Window::setVSync(int vsync) {
return true;
}

void SDL1Window::restoreGamma() {
if(m_gammaOverridden) {
SDL_SetGamma(1.f, 1.f, 1.f);
SDL_SetGammaRamp(m_gammaRed, m_gammaGreen, m_gammaBlue);
m_gammaOverridden = false;
}
}

bool SDL1Window::setGamma(float gamma) {
if(m_initialized && m_fullscreen) {
if(!m_gammaOverridden) {
m_gammaOverridden = (SDL_GetGammaRamp(m_gammaRed, m_gammaGreen, m_gammaBlue) == 0);
}
if(SDL_SetGamma(gamma, gamma, gamma) != 0) {
return false;
}
}
m_gamma = gamma;
return true;
}

bool SDL1Window::setMode(DisplayMode mode, bool fullscreen) {

if(fullscreen && mode.resolution == Vec2i_ZERO) {
Expand Down Expand Up @@ -305,10 +331,18 @@ void SDL1Window::changeMode(DisplayMode mode, bool makeFullscreen) {
return;
}

if(!makeFullscreen && wasFullscreen) {
restoreGamma();
}

if(wasFullscreen != makeFullscreen) {
onToggleFullscreen(makeFullscreen);
}

if(makeFullscreen) {
setGamma(m_gamma);
}

updateSize();

tick();
Expand Down
9 changes: 9 additions & 0 deletions src/window/SDL1Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class SDL1Window : public RenderWindow {
bool setVSync(int vsync);
void setFullscreenMode(const DisplayMode & mode);
void setWindowSize(const Vec2i & size);
bool setGamma(float gamma = 1.f);
bool initialize();
void tick();

Expand All @@ -58,13 +59,21 @@ class SDL1Window : public RenderWindow {
void changeMode(DisplayMode mode, bool fullscreen);
void updateSize(bool force = false);

void restoreGamma();

static int SDLCALL eventFilter(const SDL_Event * event);

bool m_initialized;
DisplayMode m_desktopMode;

SDL1InputBackend * m_input;

float m_gamma;
bool m_gammaOverridden;
u16 m_gammaRed[256];
u16 m_gammaGreen[256];
u16 m_gammaBlue[256];

static SDL1Window * s_mainWindow;

};
Expand Down
29 changes: 28 additions & 1 deletion src/window/SDL2Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ SDL2Window::SDL2Window()
, m_glcontext(NULL)
, m_input(NULL)
, m_minimizeOnFocusLost(AlwaysEnabled)
{
, m_gamma(1.f)
, m_gammaOverridden(false)
{
m_renderer = new OpenGLRenderer;
}

Expand All @@ -100,6 +102,7 @@ SDL2Window::~SDL2Window() {
}

if(m_window) {
restoreGamma();
SDL_DestroyWindow(m_window);
}

Expand Down Expand Up @@ -434,6 +437,8 @@ bool SDL2Window::initialize() {
SDL_ShowWindow(m_window);
SDL_ShowCursor(SDL_DISABLE);

setGamma(m_gamma);

m_renderer->initialize();

onCreate();
Expand Down Expand Up @@ -464,6 +469,26 @@ bool SDL2Window::setVSync(int vsync) {
return true;
}

void SDL2Window::restoreGamma() {
if(m_gammaOverridden) {
SDL_SetWindowGammaRamp(m_window, m_gammaRed, m_gammaGreen, m_gammaBlue);
m_gammaOverridden = false;
}
}

bool SDL2Window::setGamma(float gamma) {
if(m_window && m_fullscreen) {
if(!m_gammaOverridden) {
m_gammaOverridden = (SDL_GetWindowGammaRamp(m_window, m_gammaRed, m_gammaGreen, m_gammaBlue) == 0);
}
if(SDL_SetWindowBrightness(m_window, gamma) != 0) {
return false;
}
}
m_gamma = gamma;
return true;
}

void SDL2Window::changeMode(DisplayMode mode, bool makeFullscreen) {

if(!m_window) {
Expand Down Expand Up @@ -512,6 +537,7 @@ void SDL2Window::changeMode(DisplayMode mode, bool makeFullscreen) {

if(!makeFullscreen) {
if(wasFullscreen) {
restoreGamma();
SDL_RestoreWindow(m_window);
}
SDL_SetWindowSize(m_window, mode.resolution.x, mode.resolution.y);
Expand All @@ -522,6 +548,7 @@ void SDL2Window::changeMode(DisplayMode mode, bool makeFullscreen) {
}

if(makeFullscreen) {
setGamma(m_gamma);
// SDL regrettably sends resize events when a fullscreen window is minimized.
// Because of that we ignore all size change events when fullscreen.
// Instead, handle the size change here.
Expand Down
9 changes: 9 additions & 0 deletions src/window/SDL2Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class SDL2Window : public RenderWindow {
bool setVSync(int vsync);
void setFullscreenMode(const DisplayMode & mode);
void setWindowSize(const Vec2i & size);
bool setGamma(float gamma = 1.f);
bool initialize();
void tick();

Expand All @@ -59,6 +60,8 @@ class SDL2Window : public RenderWindow {
void changeMode(DisplayMode mode, bool fullscreen);
void updateSize(bool force = false);

void restoreGamma();

static int SDLCALL eventFilter(void * userdata, SDL_Event * event);

SDL_Window * m_window;
Expand All @@ -68,6 +71,12 @@ class SDL2Window : public RenderWindow {

MinimizeSetting m_minimizeOnFocusLost;

float m_gamma;
bool m_gammaOverridden;
u16 m_gammaRed[256];
u16 m_gammaGreen[256];
u16 m_gammaBlue[256];

static SDL2Window * s_mainWindow;

friend class SDL2InputBackend;
Expand Down

0 comments on commit 41f1d3d

Please sign in to comment.