Skip to content

Commit

Permalink
Remove duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
floppyhammer committed Jun 21, 2024
1 parent 12248c0 commit b6ccc89
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 130 deletions.
70 changes: 9 additions & 61 deletions pathfinder/gpu/gl/window_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,75 +88,23 @@ WindowBuilderGl::WindowBuilderGl(const Vec2I &size) {

WindowBuilderGl::~WindowBuilderGl() {
// Destroy windows.
{
for (auto &w : sub_windows_) {
if (!w.expired()) {
// We need to destroy a window explicitly in case its smart pointer is held elsewhere.
w.lock()->destroy();
}
for (auto &w : sub_windows_) {
if (!w.expired()) {
// We need to destroy a window explicitly in case its smart pointer is held elsewhere.
w.lock()->destroy();
}
sub_windows_.clear();

primary_window_->destroy();
primary_window_.reset();
}
sub_windows_.clear();

primary_window_->destroy();
primary_window_.reset();

// Cleanup GLFW.
#ifndef __ANDROID__
glfwTerminate();
#endif
}

#ifndef __ANDROID__
GLFWwindow *WindowBuilderGl::glfw_window_init(const Vec2I &logical_size,
const std::string &title,
float &dpi_scaling_factor,
GLFWwindow *shared_window) {
#ifndef __EMSCRIPTEN__
// Enable window resizing.
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

// Hide window upon creation as we need to center the window before showing it.
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);

// Get monitor position (used to correctly center the window in a multi-monitor scenario).
int monitor_count, monitor_x, monitor_y;
GLFWmonitor **monitors = glfwGetMonitors(&monitor_count);

const GLFWvidmode *video_mode = glfwGetVideoMode(monitors[0]);
glfwGetMonitorPos(monitors[0], &monitor_x, &monitor_y);

// Get DPI scale.
float dpi_scale_x, dpi_scale_y;
glfwGetMonitorContentScale(monitors[0], &dpi_scale_x, &dpi_scale_y);
assert(dpi_scale_x == dpi_scale_y);
dpi_scaling_factor = dpi_scale_x;
#endif

#if defined(__linux__) || defined(_WIN32)
auto physical_size = (logical_size.to_f32() * dpi_scaling_factor).to_i32();
#else ifdef __APPLE__
auto physical_size = logical_size;
#endif

auto glfw_window = glfwCreateWindow(physical_size.x, physical_size.y, title.c_str(), nullptr, shared_window);
if (glfw_window == nullptr) {
throw std::runtime_error("Failed to create GLFW window!");
}

#ifndef __EMSCRIPTEN__
// Center the window.
glfwSetWindowPos(glfw_window,
monitor_x + (video_mode->width - physical_size.x) / 2,
monitor_y + (video_mode->height - physical_size.y) / 2);

// Show the window.
glfwShowWindow(glfw_window);
#endif

return glfw_window;
}
#endif

std::shared_ptr<Window> WindowBuilderGl::create_window(const Vec2I &size, const std::string &title) {
#ifndef __ANDROID__
auto window_gl = (WindowGl *)primary_window_.get();
Expand Down
8 changes: 0 additions & 8 deletions pathfinder/gpu/gl/window_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ class WindowBuilderGl : public WindowBuilder {
std::shared_ptr<Device> request_device() override;

std::shared_ptr<Queue> create_queue() override;

private:
#ifndef __ANDROID__
static GLFWwindow *glfw_window_init(const Vec2I &logical_size,
const std::string &title,
float &dpi_scaling_factor,
GLFWwindow *shared_window = nullptr);
#endif
};

} // namespace Pathfinder
Expand Down
57 changes: 2 additions & 55 deletions pathfinder/gpu/vk/window_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,57 +125,6 @@ void WindowBuilderVk::stop_and_destroy_swapchains() {
primary_window_->swapchain_->destroy();
}

#ifndef __ANDROID__
GLFWwindow *WindowBuilderVk::glfw_window_init(const Vec2I &logical_size,
const std::string &title,
float &dpi_scaling_factor,
GLFWwindow *shared_window) {
#ifndef __EMSCRIPTEN__
// Enable window resizing.
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

// Hide window upon creation as we need to center the window before showing it.
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);

// Get monitor position (used to correctly center the window in a multi-monitor scenario).
int monitor_count, monitor_x, monitor_y;
GLFWmonitor **monitors = glfwGetMonitors(&monitor_count);

const GLFWvidmode *video_mode = glfwGetVideoMode(monitors[0]);
glfwGetMonitorPos(monitors[0], &monitor_x, &monitor_y);

// Get DPI scale.
float dpi_scale_x, dpi_scale_y;
glfwGetMonitorContentScale(monitors[0], &dpi_scale_x, &dpi_scale_y);
assert(dpi_scale_x == dpi_scale_y);
dpi_scaling_factor = dpi_scale_x;
#endif

#if defined(__linux__) || defined(_WIN32)
auto physical_size = (logical_size.to_f32() * dpi_scaling_factor).to_i32();
#else ifdef __APPLE__
auto physical_size = logical_size;
#endif

auto glfw_window = glfwCreateWindow(physical_size.x, physical_size.y, title.c_str(), nullptr, shared_window);
if (glfw_window == nullptr) {
throw std::runtime_error("Failed to create a GLFW window!");
}

#ifndef __EMSCRIPTEN__
// Center the window.
glfwSetWindowPos(glfw_window,
monitor_x + (video_mode->width - physical_size.x) / 2,
monitor_y + (video_mode->height - physical_size.y) / 2);

// Show the window.
glfwShowWindow(glfw_window);
#endif

return glfw_window;
}
#endif

std::shared_ptr<Window> WindowBuilderVk::create_window(const Vec2I &size, const std::string &title) {
#ifndef __ANDROID__
float dpi_scaling_factor;
Expand Down Expand Up @@ -339,10 +288,8 @@ SwapchainSupportDetails query_swapchain_support(VkPhysicalDevice _physical_devic

if (present_mode_count != 0) {
details.present_modes.resize(present_mode_count);
vkGetPhysicalDeviceSurfacePresentModesKHR(_physical_device,
surface,
&present_mode_count,
details.present_modes.data());
vkGetPhysicalDeviceSurfacePresentModesKHR(
_physical_device, surface, &present_mode_count, details.present_modes.data());
}

return details;
Expand Down
5 changes: 0 additions & 5 deletions pathfinder/gpu/vk/window_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ class WindowBuilderVk : public WindowBuilder {

#ifdef __ANDROID__
ANativeWindow *native_window_{};
#else
static GLFWwindow *glfw_window_init(const Vec2I &logical_size,
const std::string &title,
float &dpi_scaling_factor,
GLFWwindow *shared_window = nullptr);
#endif

static VKAPI_ATTR VkBool32 VKAPI_CALL debug_callback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
Expand Down
51 changes: 51 additions & 0 deletions pathfinder/gpu/window_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,55 @@ void WindowBuilder::poll_events() {
#endif
}

#ifndef __ANDROID__
GLFWwindow *WindowBuilder::glfw_window_init(const Vec2I &logical_size,
const std::string &title,
float &dpi_scaling_factor,
GLFWwindow *shared_window) {
#ifndef __EMSCRIPTEN__
// Enable window resizing.
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

// Hide window upon creation as we need to center the window before showing it.
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);

// Get monitor position (used to correctly center the window in a multi-monitor scenario).
int monitor_count, monitor_x, monitor_y;
GLFWmonitor **monitors = glfwGetMonitors(&monitor_count);

const GLFWvidmode *video_mode = glfwGetVideoMode(monitors[0]);
glfwGetMonitorPos(monitors[0], &monitor_x, &monitor_y);

// Get DPI scale.
float dpi_scale_x, dpi_scale_y;
glfwGetMonitorContentScale(monitors[0], &dpi_scale_x, &dpi_scale_y);
assert(dpi_scale_x == dpi_scale_y);
dpi_scaling_factor = dpi_scale_x;
#endif

#if defined(__linux__) || defined(_WIN32)
auto physical_size = (logical_size.to_f32() * dpi_scaling_factor).to_i32();
#else ifdef __APPLE__
auto physical_size = logical_size;
#endif

auto glfw_window = glfwCreateWindow(physical_size.x, physical_size.y, title.c_str(), nullptr, shared_window);
if (glfw_window == nullptr) {
throw std::runtime_error("Failed to create GLFW window!");
}

#ifndef __EMSCRIPTEN__
// Center the window.
glfwSetWindowPos(glfw_window,
monitor_x + (video_mode->width - physical_size.x) / 2,
monitor_y + (video_mode->height - physical_size.y) / 2);

// Show the window.
glfwShowWindow(glfw_window);
#endif

return glfw_window;
}
#endif

} // namespace Pathfinder
12 changes: 11 additions & 1 deletion pathfinder/gpu/window_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "device.h"
#include "queue.h"

struct GLFWwindow;

namespace Pathfinder {

class Window;
Expand All @@ -19,7 +21,8 @@ class WindowBuilder {

/// Wait for the swapchains to finish the current frame, then destroy them.
/// Call this right after the render loop is stopped.
virtual void stop_and_destroy_swapchains() {}
virtual void stop_and_destroy_swapchains() {
}

/// Create a new sub-window.
virtual std::shared_ptr<Window> create_window(const Vec2I &size, const std::string &title) = 0;
Expand All @@ -33,6 +36,13 @@ class WindowBuilder {
void poll_events();

protected:
#ifndef __ANDROID__
static GLFWwindow *glfw_window_init(const Vec2I &logical_size,
const std::string &title,
float &dpi_scaling_factor,
GLFWwindow *shared_window = nullptr);
#endif

std::shared_ptr<Window> primary_window_;
std::vector<std::weak_ptr<Window>> sub_windows_;
};
Expand Down

0 comments on commit b6ccc89

Please sign in to comment.