mirror of
https://github.com/TheBozos/PS2Recomp.git
synced 2026-12-01 07:38:32 -10:00
Video settings: the window's resolution, windowed / fullscreen / borderless and the render scale, in the settings window (F2, Video tab), saved to video.ini
runtime/ps2_video_settings.h (ps2_video): the window as the player sets it. Loaded when the window opens from video.ini next to the game's ELF (PS2X_VIDEO_CONFIG for another file; main.cpp now sets the I/O paths from the ELF before PS2Runtime::initialize so the folder is known), the environment over it: PS2X_WINDOW_SIZE (as before), PS2X_WINDOW_MODE=windowed|fullscreen|borderless, PS2X_RENDER_SCALE, PS2X_GS_SCALE. Keys: size = <w>x<h>, mode, render_scale (0.25 to 4), gs_scale (1 to 4; put in the environment for the GS native back end when nothing set PS2X_GS_SCALE, so it applies at the next launch). Changes apply live, between two frames (ps2_video::frameUpdate before BeginDrawing in the present loop; the SetTargetFPS block untouched): back to a plain window first (ToggleFullscreen / ToggleBorderlessWindowed undo themselves), SetWindowSize, then fullscreen (exclusive: raylib asks the monitor for the window's size) or borderless (the monitor's size, undecorated). A window comes back to where it was when its size did not change, centred otherwise; a windowed resize with the mouse becomes the saved size. F11 or Alt+Enter switch between the window and the last full-screen mode (borderless by default). The present loop's 4:3 rectangle, centred with bars, is unchanged and holds at every window shape. Render scale (native renderer): Renderer::setRenderScale; present() draws the 3D into a target that many times the window rectangle's size each way, bilinear-filtered to it when the scale is not 1 (point otherwise, as before); clips and the depth read-back follow the target's size. Changes at once. The F2 window is now "Settings (F2)" with two tabs: Controls (the bindings, unchanged) and Video: Mode, Resolution (common 4:3 and 16:9 sizes that fit the monitor, the monitor's own, the current one; disabled for borderless), Render scale (50 % to 200 %), GS renderer scale (1x to 4x, next launch), a note where the environment sets a value, the size now; Apply, Apply and save, Revert. PS2X_VIDEO_SCRIPT=<s>:<w>x<h>:<mode>[:<scale>],...: changes applied at those seconds, as the tab would (unattended checks). Checked on Twisted Metal: Black (TMB_NATIVE=1, 1920x1080 monitor) with PS2X_VIDEO_SCRIPT and window captures: menus through 1280x720 windowed, 1024x768 fullscreen, borderless (1920x1080), 800x600, 1280x720 fullscreen, 640x480 at 2x, 1280x960 at 0.5x, borderless at 1.5x, 960x720; then a match through 0.5x, 2x, fullscreen, borderless at 2x and back to a window. Every capture the requested size, the picture 4:3 with bars where the window is wider (x 160 of 1280, 240 of 1920), 57-60 fps, no crash. video.ini read at startup (fullscreen 1280x720, scale 1.5, GS scale 3), PS2X_WINDOW_MODE / PS2X_WINDOW_SIZE over it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
6bc71c0836
commit
337e4ad789
9 files changed
+555
-16
No files matched your search
@@ -403,6 +403,7 @@ add_library(ps2_runtime STATIC
|
||||
src/lib/ps2_overlay_modules.cpp
|
||||
src/lib/ps2_pad.cpp
|
||||
src/lib/ps2_input_bindings.cpp
|
||||
src/lib/ps2_video_settings.cpp
|
||||
src/lib/ps2_rom_device.cpp
|
||||
src/lib/ps2_vfs.cpp
|
||||
src/lib/ps2_recomp_abi.cpp
|
||||
|
||||
@@ -15,8 +15,11 @@ public:
|
||||
void toggleVisible() { m_visible = !m_visible; }
|
||||
|
||||
private:
|
||||
// The pad bindings window (F2): runtime/ps2_input_bindings.h.
|
||||
void drawControlsWindow();
|
||||
// The settings window (F2): the pad bindings (runtime/ps2_input_bindings.h) and the window and rendering
|
||||
// (runtime/ps2_video_settings.h).
|
||||
void drawSettingsWindow();
|
||||
void drawControlsTab();
|
||||
void drawVideoTab();
|
||||
|
||||
bool m_initialized = false;
|
||||
bool m_visible = true;
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "runtime/native/native_gs_hook.h"
|
||||
#include "runtime/native/native_texture_cache.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
@@ -170,6 +171,10 @@ namespace ps2x::native
|
||||
|
||||
// Present thread: draws the latest frame into the given window rectangle; false when there is none yet.
|
||||
bool present(float x, float y, float width, float height);
|
||||
// The 3D's own resolution against the window rectangle's (the Video settings' render scale, 0.25 to 4): drawn
|
||||
// at that many times its size each way, then filtered to it (2: four samples a window pixel).
|
||||
void setRenderScale(float scale) { m_renderScale.store(std::clamp(scale, 0.25f, 4.0f), std::memory_order_relaxed); }
|
||||
float renderScale() const { return m_renderScale.load(std::memory_order_relaxed); }
|
||||
// The GS frame shown with the native one (a GL texture): PS2X_NATIVE_CAPTURE_DIR saves it with each capture.
|
||||
void setGsFrame(unsigned int textureId, int width, int height, int format)
|
||||
{
|
||||
@@ -236,6 +241,7 @@ namespace ps2x::native
|
||||
bool m_presentsAlone = false;
|
||||
float m_clip[4] = {0, 0, 0, 0};
|
||||
std::atomic<uint32_t> m_clearColour{0};
|
||||
std::atomic<float> m_renderScale{1.0f};
|
||||
std::shared_ptr<Frame> m_backdrop; // under m_mutex: one textured batch, its screen, its time
|
||||
std::mutex m_mutex;
|
||||
std::shared_ptr<Frame> m_published;
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
|
||||
// The window as the player sets it: its size, windowed / fullscreen / borderless, and the render scales. Loaded from
|
||||
// video.ini next to the game's ELF (or PS2X_VIDEO_CONFIG) when the window opens, edited in the Video tab of the
|
||||
// settings window (F2) and saved back there; the environment still overrides the file (PS2X_WINDOW_SIZE,
|
||||
// PS2X_WINDOW_MODE, PS2X_RENDER_SCALE, PS2X_GS_SCALE). F11 or Alt+Enter switch between the window and the last full-screen
|
||||
// mode. Whatever the window's shape, the picture keeps its 4:3 and is centred with bars (the present loop).
|
||||
//
|
||||
// Everything here runs on the window's thread.
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace ps2_video
|
||||
{
|
||||
enum class WindowMode : int
|
||||
{
|
||||
Windowed,
|
||||
Fullscreen, // exclusive: the monitor switches to the chosen size
|
||||
Borderless, // a window without borders over the whole monitor, at the monitor's own size
|
||||
};
|
||||
|
||||
struct Settings
|
||||
{
|
||||
int width = 0, height = 0; // the window's client area (the full-screen video mode's size)
|
||||
WindowMode mode = WindowMode::Windowed;
|
||||
float renderScale = 1.0f; // the native renderer's 3D against the window's size (live)
|
||||
int gsScale = 1; // the GS renderer's internal scale, 1 to 4 (PS2X_GS_SCALE; at the next launch)
|
||||
};
|
||||
|
||||
// Before InitWindow: the file then the environment over the defaults (the runner's window size). The ELF's folder
|
||||
// must be known by then (PS2Runtime::configureIoPathsFromElf).
|
||||
void loadAtStartup(int defaultWidth, int defaultHeight);
|
||||
// After InitWindow: the mode and render scale loaded.
|
||||
void applyAtStartup();
|
||||
// Once a frame, before drawing it (window thread): F11 / Alt+Enter, requestApply()'s change, and a windowed resize kept
|
||||
// as the size.
|
||||
void frameUpdate();
|
||||
|
||||
// What is applied now, and the settings window's copy of it until Apply.
|
||||
const Settings ¤t();
|
||||
Settings &edited();
|
||||
// Applies `edited()` to the window at once (the GS scale waits for the next launch); requestApply() at the next
|
||||
// frameUpdate (from inside a frame: the settings window).
|
||||
void apply();
|
||||
void requestApply();
|
||||
bool save();
|
||||
std::string configPath();
|
||||
|
||||
const char *modeName(WindowMode mode);
|
||||
// Sizes to offer: common 4:3 and 16:9 ones that fit the monitor, the monitor's own, and the current one.
|
||||
std::vector<std::pair<int, int>> resolutions();
|
||||
std::pair<int, int> monitorSize();
|
||||
}
|
||||
@@ -98,6 +98,7 @@ void main()
|
||||
size_t vboBytes = 0;
|
||||
RenderTexture2D target{};
|
||||
int targetWidth = 0, targetHeight = 0;
|
||||
bool targetFiltered = false; // bilinear (render scale other than 1) rather than point
|
||||
struct GpuTexture
|
||||
{
|
||||
unsigned int id = 0;
|
||||
@@ -516,15 +517,23 @@ void main()
|
||||
}
|
||||
}
|
||||
Gpu &gpu = *m_gpu;
|
||||
const int w = std::max(1, static_cast<int>(width)), h = std::max(1, static_cast<int>(height));
|
||||
const float scale = renderScale();
|
||||
const int w = std::max(1, static_cast<int>(std::lround(width * scale))), h = std::max(1, static_cast<int>(std::lround(height * scale)));
|
||||
if (gpu.targetWidth != w || gpu.targetHeight != h)
|
||||
{
|
||||
if (gpu.targetWidth != 0)
|
||||
UnloadRenderTexture(gpu.target);
|
||||
gpu.target = LoadRenderTexture(w, h);
|
||||
gpu.targetFiltered = false;
|
||||
gpu.targetWidth = w;
|
||||
gpu.targetHeight = h;
|
||||
}
|
||||
// Filtered to the window when the render scale makes it another size, as it is (point) otherwise.
|
||||
if (const bool filtered = scale != 1.0f; filtered != gpu.targetFiltered)
|
||||
{
|
||||
SetTextureFilter(gpu.target.texture, filtered ? TEXTURE_FILTER_BILINEAR : TEXTURE_FILTER_POINT);
|
||||
gpu.targetFiltered = filtered;
|
||||
}
|
||||
|
||||
const size_t bytes = frame->vertices.size() * sizeof(Vertex);
|
||||
rlEnableVertexArray(gpu.vao);
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
#if defined(PS2X_ENABLE_DEBUG_UI) && !defined(PLATFORM_VITA)
|
||||
#include "runtime/ps2_input_bindings.h"
|
||||
#include "runtime/ps2_video_settings.h"
|
||||
#include "imgui.h"
|
||||
#include "rlImGui.h"
|
||||
#include "raylib.h"
|
||||
@@ -19,6 +20,8 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <numeric>
|
||||
#include <cstdlib>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
@@ -2209,15 +2212,147 @@ void PS2DebugPanel::shutdown()
|
||||
#endif
|
||||
}
|
||||
|
||||
void PS2DebugPanel::drawControlsWindow()
|
||||
void PS2DebugPanel::drawSettingsWindow()
|
||||
{
|
||||
#if defined(PS2X_ENABLE_DEBUG_UI) && !defined(PLATFORM_VITA)
|
||||
ImGui::SetNextWindowSize(ImVec2(640.0f, 600.0f), ImGuiCond_FirstUseEver);
|
||||
if (!ImGui::Begin("Controls (F2)", &m_controlsVisible))
|
||||
if (!ImGui::Begin("Settings (F2)", &m_controlsVisible))
|
||||
{
|
||||
ImGui::End();
|
||||
return;
|
||||
}
|
||||
if (ImGui::BeginTabBar("settings-tabs"))
|
||||
{
|
||||
if (ImGui::BeginTabItem("Controls"))
|
||||
{
|
||||
drawControlsTab();
|
||||
ImGui::EndTabItem();
|
||||
}
|
||||
else
|
||||
m_bindingAction = -1;
|
||||
if (ImGui::BeginTabItem("Video"))
|
||||
{
|
||||
drawVideoTab();
|
||||
ImGui::EndTabItem();
|
||||
}
|
||||
ImGui::EndTabBar();
|
||||
}
|
||||
ImGui::End();
|
||||
#endif
|
||||
}
|
||||
|
||||
void PS2DebugPanel::drawVideoTab()
|
||||
{
|
||||
#if defined(PS2X_ENABLE_DEBUG_UI) && !defined(PLATFORM_VITA)
|
||||
ps2_video::Settings &edited = ps2_video::edited();
|
||||
const ps2_video::Settings ¤t = ps2_video::current();
|
||||
// What the environment sets wins over video.ini at the next launch too.
|
||||
const auto envNote = [](const char *name)
|
||||
{
|
||||
if (const char *value = std::getenv(name); value && *value)
|
||||
{
|
||||
ImGui::SameLine();
|
||||
ImGui::TextDisabled("(%s=%s at launch)", name, value);
|
||||
}
|
||||
};
|
||||
|
||||
ImGui::SeparatorText("Display");
|
||||
static const char *kModes[] = {"Windowed", "Fullscreen", "Borderless window"};
|
||||
int mode = static_cast<int>(edited.mode);
|
||||
ImGui::SetNextItemWidth(240.0f);
|
||||
if (ImGui::Combo("Mode", &mode, kModes, IM_ARRAYSIZE(kModes)))
|
||||
edited.mode = static_cast<ps2_video::WindowMode>(mode);
|
||||
envNote("PS2X_WINDOW_MODE");
|
||||
|
||||
const std::vector<std::pair<int, int>> sizes = ps2_video::resolutions();
|
||||
const auto [monitorWidth, monitorHeight] = ps2_video::monitorSize();
|
||||
const auto label = [&](int width, int height)
|
||||
{
|
||||
const int divisor = std::gcd(width, height);
|
||||
int ratioW = divisor ? width / divisor : 0, ratioH = divisor ? height / divisor : 0;
|
||||
if (ratioW == 8 && ratioH == 5)
|
||||
ratioW = 16, ratioH = 10;
|
||||
std::string text = std::to_string(width) + " x " + std::to_string(height);
|
||||
if (ratioW != 0 && ratioW <= 21 && ratioH <= 10)
|
||||
text += " (" + std::to_string(ratioW) + ":" + std::to_string(ratioH) + ")";
|
||||
else if (std::abs(width * 9 - height * 16) < width / 50)
|
||||
text += " (16:9)";
|
||||
if (width == monitorWidth && height == monitorHeight)
|
||||
text += " monitor";
|
||||
return text;
|
||||
};
|
||||
const bool borderless = edited.mode == ps2_video::WindowMode::Borderless;
|
||||
ImGui::BeginDisabled(borderless);
|
||||
ImGui::SetNextItemWidth(240.0f);
|
||||
if (ImGui::BeginCombo("Resolution", label(edited.width, edited.height).c_str()))
|
||||
{
|
||||
for (const auto &[width, height] : sizes)
|
||||
{
|
||||
const bool selected = width == edited.width && height == edited.height;
|
||||
if (ImGui::Selectable(label(width, height).c_str(), selected))
|
||||
edited.width = width, edited.height = height;
|
||||
if (selected)
|
||||
ImGui::SetItemDefaultFocus();
|
||||
}
|
||||
ImGui::EndCombo();
|
||||
}
|
||||
ImGui::EndDisabled();
|
||||
envNote("PS2X_WINDOW_SIZE");
|
||||
if (borderless)
|
||||
ImGui::TextDisabled("Borderless covers the monitor (%d x %d); the resolution is the window's when it leaves.",
|
||||
monitorWidth, monitorHeight);
|
||||
else if (edited.mode == ps2_video::WindowMode::Fullscreen)
|
||||
ImGui::TextDisabled("Fullscreen sets the monitor to this resolution.");
|
||||
else
|
||||
ImGui::TextDisabled("The window can also be resized with the mouse.");
|
||||
|
||||
ImGui::SeparatorText("Rendering");
|
||||
static const float kScales[] = {0.5f, 0.75f, 1.0f, 1.5f, 2.0f};
|
||||
static const char *kScaleNames[] = {"50%", "75%", "100% (the window's)", "150%", "200% (supersampled)"};
|
||||
int scaleIndex = 2;
|
||||
for (int i = 0; i < IM_ARRAYSIZE(kScales); ++i)
|
||||
if (std::fabs(edited.renderScale - kScales[i]) < 0.01f)
|
||||
scaleIndex = i;
|
||||
ImGui::SetNextItemWidth(240.0f);
|
||||
if (ImGui::Combo("Render scale", &scaleIndex, kScaleNames, IM_ARRAYSIZE(kScaleNames)))
|
||||
edited.renderScale = kScales[scaleIndex];
|
||||
envNote("PS2X_RENDER_SCALE");
|
||||
ImGui::TextDisabled("The native renderer's 3D against the window's resolution (applies at once).");
|
||||
ImGui::SetNextItemWidth(240.0f);
|
||||
ImGui::SliderInt("GS renderer scale", &edited.gsScale, 1, 4, "%dx");
|
||||
envNote("PS2X_GS_SCALE");
|
||||
ImGui::TextDisabled("The GS renderer's internal resolution, times the PS2's (at the next launch).");
|
||||
|
||||
ImGui::Separator();
|
||||
ImGui::Text("Now: %d x %d, %s, render scale %d%%; window %d x %d", current.width, current.height,
|
||||
ps2_video::modeName(current.mode), static_cast<int>(std::lround(current.renderScale * 100.0f)),
|
||||
GetScreenWidth(), GetScreenHeight());
|
||||
static std::string s_status;
|
||||
if (ImGui::Button("Apply"))
|
||||
{
|
||||
ps2_video::requestApply();
|
||||
s_status = edited.gsScale != current.gsScale ? "applied (the GS scale at the next launch)" : "applied (not saved)";
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Apply and save"))
|
||||
{
|
||||
ps2_video::requestApply();
|
||||
s_status = ps2_video::save() ? "saved to " + ps2_video::configPath() : "could not write " + ps2_video::configPath();
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Revert"))
|
||||
{
|
||||
edited = current;
|
||||
s_status.clear();
|
||||
}
|
||||
ImGui::TextUnformatted(s_status.c_str());
|
||||
ImGui::TextDisabled("F11 or Alt+Enter switches between the window and full screen.");
|
||||
#endif
|
||||
}
|
||||
|
||||
void PS2DebugPanel::drawControlsTab()
|
||||
{
|
||||
#if defined(PS2X_ENABLE_DEBUG_UI) && !defined(PLATFORM_VITA)
|
||||
ps2_input::Config &config = ps2_input::config();
|
||||
std::mutex &mutex = ps2_input::configMutex();
|
||||
|
||||
@@ -2323,7 +2458,6 @@ void PS2DebugPanel::drawControlsWindow()
|
||||
}
|
||||
ImGui::SameLine();
|
||||
ImGui::TextUnformatted(s_status.c_str());
|
||||
ImGui::End();
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -2356,7 +2490,7 @@ void PS2DebugPanel::draw(PS2Runtime &runtime)
|
||||
|
||||
if (m_controlsVisible)
|
||||
{
|
||||
drawControlsWindow();
|
||||
drawSettingsWindow();
|
||||
}
|
||||
if (!m_visible)
|
||||
{
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "ps2_runtime.h"
|
||||
#include "runtime/gs/gs_cpu_backend.h"
|
||||
#include "runtime/ps2_input_bindings.h"
|
||||
#include "runtime/ps2_video_settings.h"
|
||||
#include "ps2_log.h"
|
||||
#include "ps2_stubs.h"
|
||||
#include "ps2_syscalls.h"
|
||||
@@ -1636,15 +1637,11 @@ bool PS2Runtime::initialize(const char *title)
|
||||
InitWindow(HOST_WINDOW_WIDTH, HOST_WINDOW_HEIGHT, title); // raylib vita does not support audio
|
||||
#else
|
||||
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
|
||||
// PS2X_WINDOW_SIZE=<width>x<height>: the window's size when it opens (it can be resized after).
|
||||
int windowWidth = HOST_WINDOW_WIDTH, windowHeight = HOST_WINDOW_HEIGHT;
|
||||
if (const char *size = std::getenv("PS2X_WINDOW_SIZE"); size && *size)
|
||||
{
|
||||
int w = 0, h = 0;
|
||||
if (std::sscanf(size, "%dx%d", &w, &h) == 2 && w >= 160 && h >= 120)
|
||||
windowWidth = w, windowHeight = h;
|
||||
}
|
||||
InitWindow(windowWidth, windowHeight, title);
|
||||
// The window's size and mode from video.ini, PS2X_WINDOW_SIZE=<width>x<height> and PS2X_WINDOW_MODE over it
|
||||
// (runtime/ps2_video_settings.h; the Video tab of the settings window, F2, changes them while playing).
|
||||
ps2_video::loadAtStartup(HOST_WINDOW_WIDTH, HOST_WINDOW_HEIGHT);
|
||||
InitWindow(ps2_video::current().width, ps2_video::current().height, title);
|
||||
ps2_video::applyAtStartup();
|
||||
InitAudioDevice();
|
||||
m_audioBackend.setAudioReady(IsAudioDeviceReady());
|
||||
#endif
|
||||
@@ -3907,6 +3904,9 @@ void PS2Runtime::run()
|
||||
shownTex.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
|
||||
}
|
||||
|
||||
#if !defined(PLATFORM_VITA)
|
||||
ps2_video::frameUpdate(); // F11 / Alt+Enter, the Video tab's changes: between two frames
|
||||
#endif
|
||||
BeginDrawing();
|
||||
ps2_input::frameUpdate(); // the mouse and the wheel, for the pad bindings (input.ini)
|
||||
ClearBackground(BLACK);
|
||||
|
||||
@@ -0,0 +1,329 @@
|
||||
#include "runtime/ps2_video_settings.h"
|
||||
|
||||
#include "ps2_runtime.h"
|
||||
#include "runtime/native/native_renderer.h"
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <string_view>
|
||||
|
||||
namespace ps2_video
|
||||
{
|
||||
namespace
|
||||
{
|
||||
Settings g_current;
|
||||
Settings g_edited;
|
||||
WindowMode g_lastFull = WindowMode::Borderless; // where F11 / Alt+Enter go from the window
|
||||
// The window's position before it last left windowed mode, to come back there (-1: centred).
|
||||
int g_windowX = -1, g_windowY = -1;
|
||||
bool g_applyPending = false; // requestApply(): at the next frameUpdate, between two frames
|
||||
|
||||
std::string trim(const std::string &text)
|
||||
{
|
||||
const size_t first = text.find_first_not_of(" \t\r");
|
||||
if (first == std::string::npos)
|
||||
return {};
|
||||
return text.substr(first, text.find_last_not_of(" \t\r") - first + 1);
|
||||
}
|
||||
|
||||
bool parseMode(std::string_view text, WindowMode &mode)
|
||||
{
|
||||
if (text == "windowed")
|
||||
mode = WindowMode::Windowed;
|
||||
else if (text == "fullscreen")
|
||||
mode = WindowMode::Fullscreen;
|
||||
else if (text == "borderless")
|
||||
mode = WindowMode::Borderless;
|
||||
else
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool parseSize(const char *text, int &width, int &height)
|
||||
{
|
||||
int w = 0, h = 0;
|
||||
if (std::sscanf(text, "%dx%d", &w, &h) != 2 || w < 160 || h < 120)
|
||||
return false;
|
||||
width = w;
|
||||
height = h;
|
||||
return true;
|
||||
}
|
||||
|
||||
void sanitize(Settings &settings)
|
||||
{
|
||||
settings.renderScale = std::clamp(settings.renderScale, 0.25f, 4.0f);
|
||||
settings.gsScale = std::clamp(settings.gsScale, 1, 4);
|
||||
}
|
||||
|
||||
void load(Settings &settings)
|
||||
{
|
||||
std::ifstream file(configPath());
|
||||
if (!file)
|
||||
return;
|
||||
std::string line;
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
line = trim(line.substr(0, line.find('#')));
|
||||
const size_t equal = line.find('=');
|
||||
if (line.empty() || line.front() == '[' || equal == std::string::npos)
|
||||
continue;
|
||||
const std::string name = trim(line.substr(0, equal)), value = trim(line.substr(equal + 1));
|
||||
if (name == "size")
|
||||
parseSize(value.c_str(), settings.width, settings.height);
|
||||
else if (name == "mode")
|
||||
parseMode(value, settings.mode);
|
||||
else if (name == "render_scale")
|
||||
settings.renderScale = static_cast<float>(std::atof(value.c_str()));
|
||||
else if (name == "gs_scale")
|
||||
settings.gsScale = std::atoi(value.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void centreWindow(int width, int height)
|
||||
{
|
||||
const int monitor = GetCurrentMonitor();
|
||||
const Vector2 origin = GetMonitorPosition(monitor);
|
||||
SetWindowPosition(static_cast<int>(origin.x) + std::max(0, (GetMonitorWidth(monitor) - width) / 2),
|
||||
static_cast<int>(origin.y) + std::max(0, (GetMonitorHeight(monitor) - height) / 2));
|
||||
}
|
||||
|
||||
// Back to a plain window from either full-screen mode (raylib restores the size and place it had).
|
||||
void leaveFullModes()
|
||||
{
|
||||
if (IsWindowFullscreen())
|
||||
ToggleFullscreen();
|
||||
if (IsWindowState(FLAG_BORDERLESS_WINDOWED_MODE))
|
||||
ToggleBorderlessWindowed();
|
||||
}
|
||||
|
||||
void applyWindow(const Settings &from, const Settings &to)
|
||||
{
|
||||
if (from.mode == WindowMode::Windowed && to.mode != WindowMode::Windowed)
|
||||
{
|
||||
const Vector2 position = GetWindowPosition();
|
||||
g_windowX = static_cast<int>(position.x);
|
||||
g_windowY = static_cast<int>(position.y);
|
||||
}
|
||||
leaveFullModes();
|
||||
// The size first, as a window: exclusive full screen then asks the monitor for that size (ToggleFullscreen
|
||||
// keeps the window's), borderless covers the monitor whatever it is.
|
||||
if (GetScreenWidth() != to.width || GetScreenHeight() != to.height)
|
||||
SetWindowSize(to.width, to.height);
|
||||
switch (to.mode)
|
||||
{
|
||||
case WindowMode::Windowed:
|
||||
if (from.mode != WindowMode::Windowed || from.width != to.width || from.height != to.height)
|
||||
{
|
||||
if (from.mode != WindowMode::Windowed && g_windowX >= 0 && from.width == to.width && from.height == to.height)
|
||||
SetWindowPosition(g_windowX, g_windowY);
|
||||
else
|
||||
centreWindow(to.width, to.height);
|
||||
}
|
||||
break;
|
||||
case WindowMode::Fullscreen:
|
||||
ToggleFullscreen();
|
||||
break;
|
||||
case WindowMode::Borderless:
|
||||
ToggleBorderlessWindowed();
|
||||
break;
|
||||
}
|
||||
if (to.mode != WindowMode::Windowed)
|
||||
g_lastFull = to.mode;
|
||||
}
|
||||
}
|
||||
|
||||
const char *modeName(WindowMode mode)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
case WindowMode::Fullscreen:
|
||||
return "fullscreen";
|
||||
case WindowMode::Borderless:
|
||||
return "borderless";
|
||||
default:
|
||||
return "windowed";
|
||||
}
|
||||
}
|
||||
|
||||
std::string configPath()
|
||||
{
|
||||
if (const char *path = std::getenv("PS2X_VIDEO_CONFIG"); path && *path)
|
||||
return path;
|
||||
const std::filesystem::path dir = PS2Runtime::getIoPaths().elfDirectory;
|
||||
return (dir.empty() ? std::filesystem::path("video.ini") : dir / "video.ini").string();
|
||||
}
|
||||
|
||||
void loadAtStartup(int defaultWidth, int defaultHeight)
|
||||
{
|
||||
Settings settings;
|
||||
settings.width = defaultWidth;
|
||||
settings.height = defaultHeight;
|
||||
load(settings);
|
||||
// The environment over the file.
|
||||
if (const char *size = std::getenv("PS2X_WINDOW_SIZE"); size && *size)
|
||||
parseSize(size, settings.width, settings.height);
|
||||
if (const char *mode = std::getenv("PS2X_WINDOW_MODE"); mode && *mode)
|
||||
parseMode(mode, settings.mode);
|
||||
if (const char *scale = std::getenv("PS2X_RENDER_SCALE"); scale && *scale)
|
||||
settings.renderScale = static_cast<float>(std::atof(scale));
|
||||
if (const char *scale = std::getenv("PS2X_GS_SCALE"); scale && *scale)
|
||||
settings.gsScale = std::atoi(scale);
|
||||
sanitize(settings);
|
||||
// The GS back end reads its scale from the environment when it starts.
|
||||
if (settings.gsScale != 1 && !std::getenv("PS2X_GS_SCALE"))
|
||||
{
|
||||
const std::string value = std::to_string(settings.gsScale);
|
||||
#if defined(_WIN32)
|
||||
_putenv_s("PS2X_GS_SCALE", value.c_str());
|
||||
#else
|
||||
setenv("PS2X_GS_SCALE", value.c_str(), 0);
|
||||
#endif
|
||||
}
|
||||
g_current = settings;
|
||||
g_current.mode = WindowMode::Windowed; // InitWindow opens a window; applyAtStartup goes on from there
|
||||
g_edited = settings;
|
||||
}
|
||||
|
||||
void applyAtStartup()
|
||||
{
|
||||
Settings target = g_edited;
|
||||
if (target.mode != WindowMode::Windowed)
|
||||
applyWindow(g_current, target);
|
||||
ps2x::native::Renderer::instance().setRenderScale(target.renderScale);
|
||||
g_current = target;
|
||||
std::fprintf(stderr, "[video] %dx%d %s, render scale %.2f, GS scale %d (%s)\n", g_current.width, g_current.height,
|
||||
modeName(g_current.mode), g_current.renderScale, g_current.gsScale, configPath().c_str());
|
||||
}
|
||||
|
||||
void frameUpdate()
|
||||
{
|
||||
// A windowed resize by hand is the window's size from then on (saved with the rest).
|
||||
if (IsWindowResized() && g_current.mode == WindowMode::Windowed && !IsWindowFullscreen() &&
|
||||
!IsWindowState(FLAG_BORDERLESS_WINDOWED_MODE))
|
||||
{
|
||||
g_current.width = GetScreenWidth();
|
||||
g_current.height = GetScreenHeight();
|
||||
g_edited.width = g_current.width;
|
||||
g_edited.height = g_current.height;
|
||||
}
|
||||
if (IsKeyPressed(KEY_F11) || ((IsKeyDown(KEY_LEFT_ALT) || IsKeyDown(KEY_RIGHT_ALT)) && IsKeyPressed(KEY_ENTER)))
|
||||
{
|
||||
g_edited = g_current;
|
||||
g_edited.mode = g_current.mode == WindowMode::Windowed ? g_lastFull : WindowMode::Windowed;
|
||||
g_applyPending = true;
|
||||
}
|
||||
// PS2X_VIDEO_SCRIPT=<s>:<w>x<h>:<mode>[:<render scale>],...: changes applied at those seconds from the first
|
||||
// frame, as the Video tab would (unattended checks of the mode switches).
|
||||
static std::vector<std::pair<double, Settings>> s_script = []
|
||||
{
|
||||
std::vector<std::pair<double, Settings>> steps;
|
||||
const char *text = std::getenv("PS2X_VIDEO_SCRIPT");
|
||||
for (std::string rest = text ? text : ""; !rest.empty();)
|
||||
{
|
||||
const size_t comma = rest.find(',');
|
||||
const std::string step = rest.substr(0, comma);
|
||||
rest = comma == std::string::npos ? std::string() : rest.substr(comma + 1);
|
||||
double at = 0.0;
|
||||
int w = 0, h = 0;
|
||||
char mode[16] = {};
|
||||
float scale = 0.0f;
|
||||
if (std::sscanf(step.c_str(), "%lf:%dx%d:%15[a-z]:%f", &at, &w, &h, mode, &scale) < 4)
|
||||
continue;
|
||||
Settings settings;
|
||||
settings.width = w;
|
||||
settings.height = h;
|
||||
settings.renderScale = scale;
|
||||
if (parseMode(mode, settings.mode))
|
||||
steps.emplace_back(at, settings);
|
||||
}
|
||||
return steps;
|
||||
}();
|
||||
static const double s_start = GetTime();
|
||||
while (!s_script.empty() && GetTime() - s_start >= s_script.front().first)
|
||||
{
|
||||
const Settings &step = s_script.front().second;
|
||||
g_edited = g_current;
|
||||
g_edited.width = step.width;
|
||||
g_edited.height = step.height;
|
||||
g_edited.mode = step.mode;
|
||||
if (step.renderScale > 0.0f)
|
||||
g_edited.renderScale = step.renderScale;
|
||||
s_script.erase(s_script.begin());
|
||||
g_applyPending = true;
|
||||
}
|
||||
if (g_applyPending)
|
||||
{
|
||||
g_applyPending = false;
|
||||
apply();
|
||||
}
|
||||
}
|
||||
|
||||
void requestApply() { g_applyPending = true; }
|
||||
|
||||
const Settings ¤t() { return g_current; }
|
||||
Settings &edited() { return g_edited; }
|
||||
|
||||
void apply()
|
||||
{
|
||||
sanitize(g_edited);
|
||||
if (g_edited.mode != g_current.mode || g_edited.width != g_current.width || g_edited.height != g_current.height)
|
||||
applyWindow(g_current, g_edited);
|
||||
ps2x::native::Renderer::instance().setRenderScale(g_edited.renderScale);
|
||||
g_current = g_edited;
|
||||
std::fprintf(stderr, "[video] applied %dx%d %s, render scale %.2f (window %dx%d)\n", g_current.width, g_current.height,
|
||||
modeName(g_current.mode), g_current.renderScale, GetScreenWidth(), GetScreenHeight());
|
||||
}
|
||||
|
||||
bool save()
|
||||
{
|
||||
std::ofstream file(configPath(), std::ios::trunc);
|
||||
if (!file)
|
||||
return false;
|
||||
const Settings &s = g_edited;
|
||||
char scale[32];
|
||||
std::snprintf(scale, sizeof(scale), "%g", s.renderScale);
|
||||
file << "# The window (the Video tab of the settings window, F2, writes this file; PS2X_WINDOW_SIZE, PS2X_WINDOW_MODE,\n"
|
||||
"# PS2X_RENDER_SCALE and PS2X_GS_SCALE override it). size: the window's, or the full-screen video mode's.\n"
|
||||
"# mode: windowed, fullscreen (exclusive) or borderless (the monitor's size). render_scale: the 3D drawn at\n"
|
||||
"# that many times the window's size each way (0.25 to 4). gs_scale: the GS renderer's internal scale (1 to 4,\n"
|
||||
"# read at launch).\n"
|
||||
"\n[video]\n"
|
||||
<< "size = " << s.width << "x" << s.height << "\n"
|
||||
<< "mode = " << modeName(s.mode) << "\n"
|
||||
<< "render_scale = " << scale << "\n"
|
||||
<< "gs_scale = " << s.gsScale << "\n";
|
||||
return static_cast<bool>(file);
|
||||
}
|
||||
|
||||
std::pair<int, int> monitorSize()
|
||||
{
|
||||
const int monitor = GetCurrentMonitor();
|
||||
return {GetMonitorWidth(monitor), GetMonitorHeight(monitor)};
|
||||
}
|
||||
|
||||
std::vector<std::pair<int, int>> resolutions()
|
||||
{
|
||||
static const std::pair<int, int> kCommon[] = {
|
||||
{640, 480}, {800, 600}, {1024, 768}, {1280, 960}, {1440, 1080}, {1600, 1200}, {1920, 1440}, {2880, 2160}, // 4:3
|
||||
{1280, 720}, {1366, 768}, {1600, 900}, {1920, 1080}, {2560, 1440}, {3200, 1800}, {3840, 2160}, // 16:9
|
||||
};
|
||||
const auto [monitorWidth, monitorHeight] = monitorSize();
|
||||
std::vector<std::pair<int, int>> sizes;
|
||||
for (const auto &size : kCommon)
|
||||
if (monitorWidth <= 0 || (size.first <= monitorWidth && size.second <= monitorHeight))
|
||||
sizes.push_back(size);
|
||||
if (monitorWidth > 0)
|
||||
sizes.emplace_back(monitorWidth, monitorHeight);
|
||||
sizes.emplace_back(g_current.width, g_current.height);
|
||||
sizes.emplace_back(g_edited.width, g_edited.height);
|
||||
std::sort(sizes.begin(), sizes.end());
|
||||
sizes.erase(std::unique(sizes.begin(), sizes.end()), sizes.end());
|
||||
return sizes;
|
||||
}
|
||||
}
|
||||
@@ -214,6 +214,8 @@ int main(int argc, char *argv[])
|
||||
},
|
||||
&debugPanel);
|
||||
#endif
|
||||
// The ELF's folder before the window opens: video.ini is read from there (as input.ini later).
|
||||
PS2Runtime::configureIoPathsFromElf(filePathStr);
|
||||
if (!runtime.initialize(windowTitle.c_str()))
|
||||
{
|
||||
std::cerr << "Failed to initialize PS2 runtime" << std::endl;
|
||||
|
||||
Reference in new issue
Block a user