Skip to content

Commit

Permalink
Add IWindow interface to allow for updating window video mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jameskr97 committed Oct 17, 2021
1 parent 85b9cbf commit eaef76b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/Core/Platform/DisplayMode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef CORE_PLATFORM_DISPLAYMODE
#define CORE_PLATFORM_DISPLAYMODE

namespace Core::Platform {

/**
* Each DisplayMode struct represents the settings which the users monitors is capable of displaying.
* Exists as a compatibility layer between `Core/` and `RageUtil/`. Without this, includes to `RageUtil`
* would be necessary in `Core`.
*/
struct DisplayMode {
DisplayMode() = default;
int width{0};
int height{0};
int refreshRate{0};
};

}

#endif //CORE_PLATFORM_DISPLAYMODE
25 changes: 25 additions & 0 deletions src/Core/Platform/Window/GLFWWindowBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,31 @@ namespace Core::Platform::Window {
return videoMode->refreshRate;
}

std::vector<DisplayMode> GLFWWindowBackend::getDisplayModes() const {
std::vector<DisplayMode> res;
int modeCount = 0;
auto *modes = glfwGetVideoModes(glfwGetPrimaryMonitor(), &modeCount);
for(int i = 0; i < modeCount; i++){
GLFWvidmode mode = modes[i];
res.push_back(DisplayMode{mode.width, mode.height, mode.refreshRate});
}
return res;
}

DisplayMode GLFWWindowBackend::getCurrentDisplayMode() const {
auto mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
return DisplayMode{mode->width, mode->height, mode->refreshRate};
}

bool GLFWWindowBackend::setVideoMode(const VideoMode& p) {
this->videoMode = p;
int px, py;
glfwGetWindowPos(this->windowHandle, &px, &py);
glfwSetWindowMonitor(this->windowHandle, p.isFullscreen ? glfwGetPrimaryMonitor() : nullptr, px, py, p.width, p.height, p.refreshRate);
glfwSetInputMode(this->windowHandle, GLFW_CURSOR, p.isFullscreen ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);
return true;
}

DeviceButton GLFWWindowBackend::convertKeyToLegacy(int keycode, int mods){
// GLFW keycodes are all uppercase ascii. If we're in that ascii, determine if uppercase
// or lowercase, and convert if necessary.
Expand Down
3 changes: 3 additions & 0 deletions src/Core/Platform/Window/GLFWWindowBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class GLFWWindowBackend : public IWindowBackend {
void setTitle(const std::string &title) override;
Dimensions getFrameBufferSize() const override;
int getRefreshRate() const override;
std::vector<DisplayMode> getDisplayModes() const override;
DisplayMode getCurrentDisplayMode() const override;
bool setVideoMode(const VideoMode& p) override;

static void setWindowHint(int hint, int value);

Expand Down
5 changes: 5 additions & 0 deletions src/Core/Platform/Window/IWindowBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

#include "Core/Utility/ActionDelegate.hpp"
#include "Core/Platform/Window/VideoMode.hpp"
#include "Core/Platform/DisplayMode.hpp"

#include <string>
#include <vector>

namespace Core::Platform::Window {
struct Dimensions {
Expand All @@ -30,7 +32,10 @@ namespace Core::Platform::Window {
virtual void setTitle(const std::string& title) = 0;
virtual Dimensions getFrameBufferSize() const = 0;
virtual int getRefreshRate() const = 0;
virtual std::vector<DisplayMode> getDisplayModes() const = 0;
virtual DisplayMode getCurrentDisplayMode() const = 0;

virtual bool setVideoMode(const VideoMode& p) = 0;
const VideoMode& getVideoMode() const;

// Callback Registration
Expand Down
9 changes: 5 additions & 4 deletions src/Etterna/Globals/StepMania.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ namespace StepMania {
paramsOut.width = iWidth;
paramsOut.height = PREFSMAN->m_iDisplayHeight;
paramsOut.refreshRate = PREFSMAN->m_iRefreshRate;
paramsOut.isFullscreen = PREFSMAN->m_bFullscreenIsBorderlessWindow;
paramsOut.isFullscreen = !PREFSMAN->m_bWindowed;
paramsOut.isBorderless = PREFSMAN->m_bFullscreenIsBorderlessWindow;
paramsOut.isVsyncEnabled = PREFSMAN->m_bVsync;
}
Expand All @@ -933,9 +933,10 @@ namespace StepMania {
void ApplyGraphicOptions(){
bool bNeedReload = false;

// VideoModeParams params;
// GetPreferredVideoModeParams(params);
std::string sError = "";//DISPLAY->SetVideoMode(params, bNeedReload);
VideoMode params;
GetPreferredVideoModeParams(params);
DISPLAY->SetVideoMode(params, bNeedReload);
std::string sError;
if (!sError.empty())
RageException::Throw("%s", sError.c_str());

Expand Down
15 changes: 15 additions & 0 deletions src/RageUtil/Graphics/RageDisplay_OGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,20 @@ void
RageDisplay_Legacy::GetDisplaySpecs(DisplaySpecs& out) const
{
out.clear();
// Prepare DisplayMode for following DisplaySpec
std::set<DisplayMode> available;
for(auto mode : this->window->getDisplayModes()){
available.insert(DisplayMode{
static_cast<unsigned int>(mode.width),
static_cast<unsigned int>(mode.height),
static_cast<double>(mode.refreshRate)});
}

// Current Mode
auto current = this->window->getCurrentDisplayMode();
DisplayMode m{static_cast<unsigned int>(current.width), static_cast<unsigned int>(current.height), static_cast<double>(current.refreshRate)};
RectI bounds = {0, 0, static_cast<int>(current.width), static_cast<int>(current.height)};
out.insert(DisplaySpec("", "Fullscreen", available, m, bounds));
}

static void
Expand Down Expand Up @@ -747,6 +761,7 @@ RageDisplay_Legacy::TryVideoMode(const VideoMode& p, bool& bNewDeviceOut)
// p.windowed, p.width, p.height, p.bpp, p.rate, p.vsync );

std::string err;
window->setVideoMode(p);
if (!err.empty())
return err; // failed to set video mode

Expand Down

0 comments on commit eaef76b

Please sign in to comment.