From 5777b6e058c60e433c261c48df38673752a9359f Mon Sep 17 00:00:00 2001 From: sardap <22311832+sardap@users.noreply.github.com> Date: Wed, 24 Aug 2022 09:13:26 +1000 Subject: [PATCH] Added screenshot hotkey (#1776) * Added screenshot hotkey and added a toggle to debug GUI to disable screenshot hotkey (#1765) * Renamed gfx_dumps folder to screenshots and screenshots taken with hotkey now use game render options not debug gui screenshot render options (#1765) * Updated get current timestamp to use strftime * fixed clang formating errors for screenshot hotkey Co-authored-by: sardap --- .gitignore | 1 + common/versions.cpp | 11 ++++ common/versions.h | 1 + game/graphics/opengl_renderer/debug_gui.cpp | 1 + game/graphics/opengl_renderer/debug_gui.h | 1 + game/graphics/pipelines/opengl.cpp | 62 ++++++++++++++++----- 6 files changed, 62 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 7bba4e3ac9..686a0a9a97 100644 --- a/.gitignore +++ b/.gitignore @@ -31,6 +31,7 @@ ee-results.json # graphics debug debug_out/* gfx_dumps/* +screenshots/* # game stuff game_config/* diff --git a/common/versions.cpp b/common/versions.cpp index 1608a535ce..f0d0ead033 100644 --- a/common/versions.cpp +++ b/common/versions.cpp @@ -17,3 +17,14 @@ GameVersion game_name_to_version(const std::string& name) { bool valid_game_version(const std::string& name) { return name == "jak1" || name == "jak2"; } + +std::string version_to_game_name(GameVersion v) { + switch (v) { + case GameVersion::Jak1: + return "jak1"; + case GameVersion::Jak2: + return "jak2"; + default: + ASSERT_MSG(false, fmt::format("no game_name for version: {} found", v)); + } +} diff --git a/common/versions.h b/common/versions.h index 381816d208..f7121a4be2 100644 --- a/common/versions.h +++ b/common/versions.h @@ -54,3 +54,4 @@ constexpr PerGameVersion game_version_names = {"jak1", "jak2"}; GameVersion game_name_to_version(const std::string& name); bool valid_game_version(const std::string& name); +std::string version_to_game_name(GameVersion v); diff --git a/game/graphics/opengl_renderer/debug_gui.cpp b/game/graphics/opengl_renderer/debug_gui.cpp index 170ba86ad5..ee78630469 100644 --- a/game/graphics/opengl_renderer/debug_gui.cpp +++ b/game/graphics/opengl_renderer/debug_gui.cpp @@ -113,6 +113,7 @@ void OpenGlDebugGui::draw(const DmaStats& dma_stats) { ImGui::InputInt("Width", &screenshot_width); ImGui::InputInt("Height", &screenshot_height); ImGui::InputInt("MSAA", &screenshot_samples); + ImGui::Checkbox("Screenshot on f2", &screenshot_hotkey_enabled); ImGui::EndMenu(); } diff --git a/game/graphics/opengl_renderer/debug_gui.h b/game/graphics/opengl_renderer/debug_gui.h index 4e6282737d..21e38a7619 100644 --- a/game/graphics/opengl_renderer/debug_gui.h +++ b/game/graphics/opengl_renderer/debug_gui.h @@ -66,6 +66,7 @@ class OpenGlDebugGui { int screenshot_width = 1920; int screenshot_height = 1080; int screenshot_samples = 16; + bool screenshot_hotkey_enabled = true; private: FrameTimeRecorder m_frame_timer; diff --git a/game/graphics/pipelines/opengl.cpp b/game/graphics/pipelines/opengl.cpp index b8527286d1..eea0a27f56 100644 --- a/game/graphics/pipelines/opengl.cpp +++ b/game/graphics/pipelines/opengl.cpp @@ -24,8 +24,10 @@ #include "game/graphics/opengl_renderer/debug_gui.h" #include "game/graphics/texture/TexturePool.h" #include "game/runtime.h" +#include "game/sce/libscf.h" #include "game/system/newpad.h" +#include "third-party/fmt/core.h" #include "third-party/imgui/imgui.h" #include "third-party/imgui/imgui_impl_glfw.h" #include "third-party/imgui/imgui_impl_opengl3.h" @@ -78,6 +80,8 @@ struct GraphicsData { std::unique_ptr g_gfx_data; +static bool want_hotkey_screenshot = false; + bool is_cursor_position_valid = false; double last_cursor_x_position = 0; double last_cursor_y_position = 0; @@ -319,10 +323,18 @@ void GLDisplay::on_key(GLFWwindow* window, int key, int /*scancode*/, int action } else if (action == GlfwKeyAction::Release) { // lg::debug("KEY RELEASE: key: {} scancode: {} mods: {:X}", key, scancode, mods); Pad::OnKeyRelease(key); - if ((key == GLFW_KEY_LEFT_ALT || key == GLFW_KEY_RIGHT_ALT) && - glfwGetWindowAttrib(window, GLFW_FOCUSED)) { - set_imgui_visible(!is_imgui_visible()); - update_cursor_visibility(window, is_imgui_visible()); + // Debug keys input mapping TODO add remapping + switch (key) { + case GLFW_KEY_LEFT_ALT: + case GLFW_KEY_RIGHT_ALT: + if (glfwGetWindowAttrib(window, GLFW_FOCUSED)) { + set_imgui_visible(!is_imgui_visible()); + update_cursor_visibility(window, is_imgui_visible()); + } + break; + case GLFW_KEY_F2: + want_hotkey_screenshot = true; + break; } } } @@ -395,12 +407,25 @@ void GLDisplay::on_iconify(GLFWwindow* /*window*/, int iconified) { } namespace { -std::string make_output_file_name(const std::string& file_name) { - file_util::create_dir_if_needed(file_util::get_file_path({"gfx_dumps"})); - return file_util::get_file_path({"gfx_dumps", file_name}); +std::string make_full_screenshot_output_file_path(const std::string& file_name) { + file_util::create_dir_if_needed(file_util::get_file_path({"screenshots"})); + return file_util::get_file_path({"screenshots", file_name}); } } // namespace +static std::string get_current_timestamp() { + auto current_time = std::time(0); + auto local_current_time = *std::localtime(¤t_time); + // Remember to increase size of result if the date format is changed + char result[20]; + std::strftime(result, sizeof(result), "%Y_%m_%d_%H_%M_%S", &local_current_time); + return std::string(result); +} + +static std::string make_hotkey_screenshot_file_name() { + return version_to_game_name(g_game_version) + "_" + get_current_timestamp() + ".png"; +} + static bool endsWith(std::string_view str, std::string_view suffix) { return str.size() >= suffix.size() && 0 == str.compare(str.size() - suffix.size(), suffix.size(), suffix); @@ -441,6 +466,15 @@ void render_game_frame(int game_width, options.save_screenshot = false; options.gpu_sync = g_gfx_data->debug_gui.should_gl_finish(); options.borderless_windows_hacks = windows_borderless_hack; + + want_hotkey_screenshot = + want_hotkey_screenshot && g_gfx_data->debug_gui.screenshot_hotkey_enabled; + if (want_hotkey_screenshot) { + want_hotkey_screenshot = false; + options.save_screenshot = true; + std::string screenshot_file_name = make_hotkey_screenshot_file_name(); + options.screenshot_path = make_full_screenshot_output_file_path(screenshot_file_name); + } if (g_gfx_data->debug_gui.get_screenshot_flag()) { options.save_screenshot = true; options.game_res_w = g_gfx_data->debug_gui.screenshot_width; @@ -448,7 +482,13 @@ void render_game_frame(int game_width, options.draw_region_width = options.game_res_w; options.draw_region_height = options.game_res_h; options.msaa_samples = g_gfx_data->debug_gui.screenshot_samples; + std::string screenshot_file_name = g_gfx_data->debug_gui.screenshot_name(); + if (!endsWith(screenshot_file_name, ".png")) { + screenshot_file_name += ".png"; + } + options.screenshot_path = make_full_screenshot_output_file_path(screenshot_file_name); } + options.draw_small_profiler_window = g_gfx_data->debug_gui.small_profiler; options.pmode_alp_register = g_gfx_data->pmode_alp; @@ -458,14 +498,6 @@ void render_game_frame(int game_width, options.msaa_samples = msaa_max; } - if (options.save_screenshot) { - // ensure the screenshot has an extension - std::string temp_path = g_gfx_data->debug_gui.screenshot_name(); - if (!endsWith(temp_path, ".png")) { - temp_path += ".png"; - } - options.screenshot_path = make_output_file_name(temp_path); - } if constexpr (run_dma_copy) { auto& chain = g_gfx_data->dma_copier.get_last_result(); g_gfx_data->ogl_renderer.render(DmaFollower(chain.data.data(), chain.start_offset), options);