Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6876fa2

Browse files
committed
display mode
1 parent 390c052 commit 6876fa2

3 files changed

Lines changed: 166 additions & 9 deletions

File tree

src/vkgs/engine/engine.cc

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ struct RenderPassKey {
7979
}
8080
};
8181

82+
struct Resolution {
83+
int width;
84+
int height;
85+
const char* tag;
86+
};
87+
88+
std::vector<Resolution> preset_resolutions = {
89+
// clang-format off
90+
{640, 480, "640 x 480 (480p)"},
91+
{800, 600, "800 x 600"},
92+
{1280, 720, "1280 x 720 (720p, HD)"},
93+
{1600, 900, "1600 x 900"},
94+
{1920, 1080, "1920 x 1080 (1080p, FHD)"},
95+
{2560, 1440, "2560 x 1440 (1440p, QHD)"},
96+
// clang-format on
97+
};
98+
8299
} // namespace
83100

84101
class Engine::Impl {
@@ -88,6 +105,11 @@ class Engine::Impl {
88105
GeometryShader,
89106
};
90107

108+
enum class DisplayMode {
109+
Windowed,
110+
WindowedFullscreen,
111+
};
112+
91113
public:
92114
Impl() {
93115
context_ = vk::Context(0);
@@ -757,8 +779,7 @@ class Engine::Impl {
757779

758780
// draw ui
759781
{
760-
viewer_.NewUiFrame();
761-
ImGui::NewFrame();
782+
viewer_.BeginUi();
762783

763784
const auto& io = ImGui::GetIO();
764785

@@ -804,10 +825,68 @@ class Engine::Impl {
804825
if (ImGui::IsKeyDown(ImGuiKey_Space)) {
805826
camera_.Translate(0.f, speed * dt);
806827
}
828+
829+
if (ImGui::IsKeyDown(ImGuiKey::ImGuiMod_Alt) && ImGui::IsKeyPressed(ImGuiKey_Enter, false)) {
830+
ToggleDisplayMode();
831+
}
807832
}
808833

809834
if (ImGui::Begin("vkgs")) {
810835
ImGui::Text("%s", context_.device_name().c_str());
836+
837+
// Windowed or Windows Fullscreen
838+
std::vector<const char*> display_modes = {"Windowed", "Windowed Fullscreen"};
839+
int display_mode_index = static_cast<int>(display_mode_);
840+
if (ImGui::Combo("Display Mode", &display_mode_index, display_modes.data(), display_modes.size())) {
841+
switch (display_mode_index) {
842+
case 0:
843+
SetWindowed();
844+
break;
845+
case 1:
846+
SetWindowedFullscreen();
847+
break;
848+
}
849+
}
850+
851+
// Resolutions
852+
const auto [width, height] = viewer_.window_size();
853+
std::string current_resolution;
854+
int resolution_index = 0;
855+
std::vector<const char*> resolutions;
856+
switch (display_mode_) {
857+
case DisplayMode::Windowed: {
858+
bool is_preset = false;
859+
for (int i = 0; i < preset_resolutions.size(); ++i) {
860+
const auto& resolution = preset_resolutions[i];
861+
if (width == resolution.width && height == resolution.height) {
862+
is_preset = true;
863+
resolution_index = i;
864+
}
865+
resolutions.push_back(resolution.tag);
866+
}
867+
868+
if (!is_preset) {
869+
current_resolution = std::to_string(width) + " x " + std::to_string(height) + " (custom)";
870+
resolution_index = resolutions.size();
871+
resolutions.push_back(current_resolution.c_str());
872+
}
873+
} break;
874+
875+
case DisplayMode::WindowedFullscreen:
876+
current_resolution = std::to_string(width) + " x " + std::to_string(height) + " (fullscreen)";
877+
resolution_index = resolutions.size();
878+
resolutions.push_back(current_resolution.c_str());
879+
break;
880+
}
881+
ImGui::BeginDisabled(display_mode_index == 1);
882+
if (ImGui::Combo("Resolution", &resolution_index, resolutions.data(), resolutions.size())) {
883+
if (resolution_index < preset_resolutions.size()) {
884+
const auto& resolution = preset_resolutions[resolution_index];
885+
viewer_.SetWindowSize(resolution.width, resolution.height);
886+
}
887+
}
888+
ImGui::EndDisabled();
889+
811890
ImGui::Text("%d total splats", frame_info.total_point_count);
812891
ImGui::Text("%d loaded splats", frame_info.loaded_point_count);
813892

@@ -953,7 +1032,8 @@ class Engine::Impl {
9531032
ImGui::PopID();
9541033
}
9551034
ImGui::End();
956-
ImGui::Render();
1035+
1036+
viewer_.EndUi();
9571037
}
9581038

9591039
model = ToScaleMatrix4(scale_ * scale) * glm::toMat4(gq) * ToTranslationMatrix4(translation_ + gt) *
@@ -1437,6 +1517,33 @@ class Engine::Impl {
14371517
framebuffer_ = vk::Framebuffer(context_, framebuffer_info);
14381518
}
14391519

1520+
void ToggleDisplayMode() {
1521+
switch (display_mode_) {
1522+
case DisplayMode::Windowed:
1523+
SetWindowedFullscreen();
1524+
break;
1525+
case DisplayMode::WindowedFullscreen:
1526+
SetWindowed();
1527+
break;
1528+
}
1529+
}
1530+
1531+
void SetWindowed() {
1532+
if (display_mode_ == DisplayMode::WindowedFullscreen) {
1533+
display_mode_ = DisplayMode::Windowed;
1534+
viewer_.SetWindowed();
1535+
}
1536+
}
1537+
1538+
void SetWindowedFullscreen() {
1539+
if (display_mode_ == DisplayMode::Windowed) {
1540+
display_mode_ = DisplayMode::WindowedFullscreen;
1541+
viewer_.SetWindowedFullscreen();
1542+
}
1543+
}
1544+
1545+
DisplayMode display_mode_ = DisplayMode::Windowed;
1546+
14401547
std::atomic_bool terminate_ = false;
14411548

14421549
std::mutex mutex_;

src/vkgs/viewer/viewer.cc

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ namespace vkgs {
2323
namespace viewer {
2424

2525
class Viewer::Impl {
26+
private:
27+
enum class DisplayMode {
28+
Windowed,
29+
WindowedFullscreen,
30+
};
31+
2632
public:
2733
static void DropCallback(GLFWwindow* window, int count, const char** paths) {
2834
std::vector<std::string> filepaths(paths, paths + count);
@@ -56,11 +62,9 @@ class Viewer::Impl {
5662

5763
void CreateWindow(const WindowCreateInfo& create_info) {
5864
// create window
59-
width_ = 1600;
60-
height_ = 900;
6165
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
6266
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
63-
window_ = glfwCreateWindow(width_, height_, "vkgs", NULL, NULL);
67+
window_ = glfwCreateWindow(1600, 900, "vkgs", NULL, NULL);
6468

6569
// Vulkan surface
6670
glfwCreateWindowSurface(create_info.instance, window_, NULL, &surface_);
@@ -89,15 +93,46 @@ class Viewer::Impl {
8993

9094
void Show() { glfwShowWindow(window_); }
9195

96+
void SetWindowed() {
97+
if (display_mode == DisplayMode::WindowedFullscreen) {
98+
glfwSetWindowMonitor(window_, NULL, xpos_, ypos_, width_, height_, 0);
99+
display_mode = DisplayMode::Windowed;
100+
}
101+
}
102+
103+
void SetWindowedFullscreen() {
104+
if (display_mode == DisplayMode::Windowed) {
105+
glfwGetWindowPos(window_, &xpos_, &ypos_);
106+
glfwGetWindowSize(window_, &width_, &height_);
107+
108+
// TODO: multi monitor
109+
GLFWmonitor* primary = glfwGetPrimaryMonitor();
110+
const GLFWvidmode* mode = glfwGetVideoMode(primary);
111+
glfwSetWindowMonitor(window_, primary, 0, 0, mode->width, mode->height, mode->refreshRate);
112+
display_mode = DisplayMode::WindowedFullscreen;
113+
}
114+
}
115+
116+
void SetWindowSize(int width, int height) {
117+
if (glfwGetWindowAttrib(window_, GLFW_MAXIMIZED)) {
118+
glfwRestoreWindow(window_);
119+
}
120+
121+
glfwSetWindowSize(window_, width, height);
122+
}
123+
92124
void PollEvents() { glfwPollEvents(); }
93125

94126
bool ShouldClose() const { return glfwWindowShouldClose(window_); }
95127

96-
void NewUiFrame() {
128+
void BeginUi() {
97129
ImGui_ImplVulkan_NewFrame();
98130
ImGui_ImplGlfw_NewFrame();
131+
ImGui::NewFrame();
99132
}
100133

134+
void EndUi() { ImGui::Render(); }
135+
101136
void DrawUi(VkCommandBuffer command_buffer) {
102137
ImDrawData* draw_data = ImGui::GetDrawData();
103138
ImGui_ImplVulkan_RenderDrawData(draw_data, command_buffer);
@@ -130,8 +165,11 @@ class Viewer::Impl {
130165
std::vector<std::string> dropped_filepaths_;
131166

132167
GLFWwindow* window_ = nullptr;
168+
int xpos_ = 0;
169+
int ypos_ = 0;
133170
int width_ = 0;
134171
int height_ = 0;
172+
DisplayMode display_mode = DisplayMode::Windowed;
135173

136174
VkSurfaceKHR surface_ = VK_NULL_HANDLE;
137175
};
@@ -148,13 +186,21 @@ void Viewer::RecreateUi(const WindowCreateInfo& create_info) { impl_->RecreateUi
148186

149187
void Viewer::Show() { impl_->Show(); }
150188

189+
void Viewer::SetWindowed() { impl_->SetWindowed(); }
190+
191+
void Viewer::SetWindowedFullscreen() { impl_->SetWindowedFullscreen(); }
192+
193+
void Viewer::SetWindowSize(int width, int height) { impl_->SetWindowSize(width, height); }
194+
151195
void Viewer::PollEvents() { impl_->PollEvents(); }
152196

153197
std::vector<std::string> Viewer::ConsumeDroppedFilepaths() { return impl_->ConsumeDroppedFilepaths(); }
154198

155199
bool Viewer::ShouldClose() const { return impl_->ShouldClose(); }
156200

157-
void Viewer::NewUiFrame() { impl_->NewUiFrame(); }
201+
void Viewer::BeginUi() { impl_->BeginUi(); }
202+
203+
void Viewer::EndUi() { impl_->EndUi(); }
158204

159205
void Viewer::DrawUi(VkCommandBuffer command_buffer) { impl_->DrawUi(command_buffer); }
160206

src/vkgs/viewer/viewer.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ class Viewer {
3636
void DestroyWindow();
3737
void RecreateUi(const WindowCreateInfo& create_info);
3838
void Show();
39+
void SetWindowed();
40+
void SetWindowedFullscreen();
41+
void SetWindowSize(int width, int height);
3942
void PollEvents();
4043
std::vector<std::string> ConsumeDroppedFilepaths();
4144
bool ShouldClose() const;
42-
void NewUiFrame();
45+
void BeginUi();
46+
void EndUi();
4347
void DrawUi(VkCommandBuffer command_buffer);
4448

4549
VkSurfaceKHR surface() const;

0 commit comments

Comments
 (0)