Skip to content

Commit

Permalink
[settings] Add configurable window modes
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Allenby committed Jun 11, 2021
1 parent 99d1a5d commit 317a7e7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
1 change: 1 addition & 0 deletions configuration/renderer.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ minor = 0
patch = 0

[application.window]
mode = "windowed"
width = 800
height = 800
name = "Inexor-Vulkan-Renderer"
Expand Down
1 change: 1 addition & 0 deletions include/inexor/vulkan-renderer/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class VulkanRenderer {

std::uint32_t m_window_width{0};
std::uint32_t m_window_height{0};
wrapper::Window::Mode m_window_mode{wrapper::Window::Mode::WINDOWED};

std::string m_window_title;

Expand Down
12 changes: 11 additions & 1 deletion include/inexor/vulkan-renderer/wrapper/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ namespace inexor::vulkan_renderer::wrapper {

/// @brief RAII wrapper class for GLFW windows.
class Window {
public:
enum class Mode { WINDOWED, FULLSCREEN, WINDOWED_FULLSCREEN };

private:
GLFWwindow *m_window{nullptr};
std::uint32_t m_width{0};
std::uint32_t m_height{0};
Mode m_mode{Mode::WINDOWED};

public:
/// @brief Default constructor.
Expand All @@ -21,7 +26,8 @@ class Window {
/// @param height The height of the window.
/// @param visible True if the window is visible after creation, false otherwise.
/// @param resizable True if the window should be resizable, false otherwise.
Window(const std::string &title, std::uint32_t width, std::uint32_t height, bool visible, bool resizable);
Window(const std::string &title, std::uint32_t width, std::uint32_t height, bool visible, bool resizable,
Mode mode);
Window(const Window &) = delete;
Window(Window &&) noexcept;
~Window();
Expand Down Expand Up @@ -82,6 +88,10 @@ class Window {
[[nodiscard]] std::uint32_t height() const {
return m_height;
}

[[nodiscard]] Mode mode() const {
return m_mode;
}
};

} // namespace inexor::vulkan_renderer::wrapper
16 changes: 15 additions & 1 deletion src/vulkan-renderer/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ void Application::load_toml_configuration_file(const std::string &file_name) {
const auto &configuration_title = toml::find<std::string>(renderer_configuration, "title");
spdlog::debug("Title: '{}'", configuration_title);

using WindowMode = ::inexor::vulkan_renderer::wrapper::Window::Mode;
const auto &wmodestr = toml::find<std::string>(renderer_configuration, "application", "window", "mode");
if (wmodestr == "windowed") {
m_window_mode = WindowMode::WINDOWED;
} else if (wmodestr == "windowed_fullscreen") {
m_window_mode = WindowMode::WINDOWED_FULLSCREEN;
} else if (wmodestr == "fullscreen") {
m_window_mode = WindowMode::FULLSCREEN;
} else {
spdlog::warn("Invalid application window mode: {}", wmodestr);
m_window_mode = WindowMode::WINDOWED;
}

m_window_width = toml::find<int>(renderer_configuration, "application", "window", "width");
m_window_height = toml::find<int>(renderer_configuration, "application", "window", "height");
m_window_title = toml::find<std::string>(renderer_configuration, "application", "window", "name");
Expand Down Expand Up @@ -397,7 +410,8 @@ Application::Application(int argc, char **argv) {
m_engine_version, VK_API_VERSION_1_1, m_enable_validation_layers,
enable_renderdoc_instance_layer);

m_window = std::make_unique<wrapper::Window>(m_window_title, m_window_width, m_window_height, true, true);
m_window =
std::make_unique<wrapper::Window>(m_window_title, m_window_width, m_window_height, true, true, m_window_mode);

m_input_data = std::make_unique<input::KeyboardMouseInputData>();

Expand Down
22 changes: 16 additions & 6 deletions src/vulkan-renderer/wrapper/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
namespace inexor::vulkan_renderer::wrapper {

Window::Window(const std::string &title, const std::uint32_t width, const std::uint32_t height, const bool visible,
const bool resizable)
: m_width(width), m_height(height) {
const bool resizable, const Mode mode)
: m_width(width), m_height(height), m_mode(mode) {
assert(!title.empty());

glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
Expand All @@ -17,16 +17,26 @@ Window::Window(const std::string &title, const std::uint32_t width, const std::u

spdlog::debug("Creating window.");

m_window = glfwCreateWindow(static_cast<int>(width), static_cast<int>(height), title.c_str(), nullptr, nullptr);
GLFWmonitor *monitor = nullptr;
if (m_mode != Mode::WINDOWED) {
monitor = glfwGetPrimaryMonitor();
if (m_mode == Mode::WINDOWED_FULLSCREEN) {
auto mode = glfwGetVideoMode(monitor);
m_width = mode->width;
m_height = mode->height;
}
}

m_window = glfwCreateWindow(static_cast<int>(width), static_cast<int>(height), title.c_str(), monitor, nullptr);

if (m_window == nullptr) {
throw std::runtime_error("Error: glfwCreateWindow failed for window " + title + " !");
}
}

Window::Window(Window &&other) noexcept {
m_window = std::exchange(other.m_window, nullptr);
}
Window::Window(Window &&other) noexcept
: m_window(std::exchange(other.m_window, nullptr)), m_width(other.m_width), m_height(other.m_height),
m_mode(other.m_mode) {}

void Window::wait_for_focus() {
int current_width = 0;
Expand Down

0 comments on commit 317a7e7

Please sign in to comment.