Skip to content

Commit 5f2e9b1

Browse files
committed
Fixed #139; Add runtime aspect-view cycling via Shift+S and display-state debug hotkey (Shift+X)
1 parent a2c3d26 commit 5f2e9b1

File tree

4 files changed

+83
-12
lines changed

4 files changed

+83
-12
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,11 @@ see all supported options.
232232
- `Shift+R`: Crop right video interactively
233233
- `Shift+B`: Crop both videos to the same area
234234
- `Shift+D`: Decode and advance one frame
235-
- `Shift+M`: Cycle display mode
236235
- `Shift+A`: Seek to the previous frame (best with intra-frame formats)
236+
- `Shift+M`: Cycle display mode
237+
- `Shift+S`: Cycle aspect view mode
237238
- `Shift+F`: Select a region and save cutouts as PNGs
239+
- `Shift+X`: Print display state to console
238240
- `Shift+W`: Restore saved window size
239241
- `Ctrl+W`: Restore startup window size
240242
- `Ctrl+Shift+W`: Save current window size

controls.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ static const std::vector<ControlSection> control_sections{
5656
{"Shift+D", "Decode and advance one frame"},
5757
{"Shift+A", "Seek to the previous frame (best with intra-frame formats)"},
5858
{"Shift+M", "Cycle display mode"},
59+
{"Shift+S", "Cycle aspect view mode"},
5960
{"Shift+F", "Select a region and save cutouts as PNGs"},
61+
{"Shift+X", "Print display state to console"},
6062
{"Shift+W", "Restore saved window size"},
6163
{"Ctrl+W", "Restore startup window size"},
6264
{"Ctrl+Shift+W", "Save current window size"},

display.cpp

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ bool Display::detect_fullscreen_like_state() const {
538538
}
539539

540540
std::array<int, 2> Display::compute_mode_switch_target_window_size() const {
541-
const float target_aspect_ratio = std::max(compute_content_aspect_ratio(), 0.001F);
541+
const float target_aspect_ratio = std::max(compute_active_content_aspect_ratio(), 0.001F);
542542
const double current_area = static_cast<double>(std::max(window_width_, MIN_WINDOW_WIDTH)) * static_cast<double>(std::max(window_height_, MIN_WINDOW_HEIGHT));
543543

544544
int target_w = std::max(MIN_WINDOW_WIDTH, static_cast<int>(std::round(std::sqrt(current_area * target_aspect_ratio))));
@@ -573,7 +573,7 @@ void Display::resize_window_for_mode_switch() {
573573
const auto target_size = compute_mode_switch_target_window_size();
574574
const int target_w = target_size[0];
575575
const int target_h = target_size[1];
576-
const float target_aspect_ratio = std::max(compute_content_aspect_ratio(), 0.001F);
576+
const float target_aspect_ratio = std::max(compute_active_content_aspect_ratio(), 0.001F);
577577

578578
// Keep WINDOW aspect lock consistent with the newly selected display mode.
579579
if (aspect_lock_mode_ == AspectLockMode::Window) {
@@ -635,7 +635,7 @@ void Display::print_verbose_info() {
635635
std::cout << "Main program version: " << VersionInfo::version << std::endl;
636636
std::cout << "Video size: " << video_width_ << "x" << video_height_ << std::endl;
637637
std::cout << "Video duration: " << format_duration(duration_) << std::endl;
638-
std::cout << "Display mode: " << modeToString(mode_) << std::endl;
638+
std::cout << "Display mode: " << mode_to_string(mode_) << std::endl;
639639
std::cout << "Fit to usable bounds: " << std::boolalpha << fit_window_to_usable_bounds_ << std::endl;
640640
std::cout << "High-DPI allowed: " << std::boolalpha << high_dpi_allowed_ << std::endl;
641641
std::string aspect_lock_mode = "off";
@@ -805,14 +805,42 @@ float Display::compute_content_aspect_ratio() const {
805805
return content_w / std::max(content_h, 1.0F);
806806
}
807807

808+
float Display::compute_active_content_aspect_ratio() const {
809+
auto apply_mode_layout_multiplier = [&](const float single_frame_ratio) {
810+
if (mode_ == Mode::HStack) {
811+
return single_frame_ratio * 2.0F;
812+
}
813+
if (mode_ == Mode::VStack) {
814+
return single_frame_ratio * 0.5F;
815+
}
816+
return single_frame_ratio;
817+
};
818+
819+
switch (aspect_view_mode_) {
820+
case AspectViewMode::Stretch:
821+
return static_cast<float>(std::max(window_width_, 1)) / static_cast<float>(std::max(window_height_, 1));
822+
case AspectViewMode::Preset16x9:
823+
return apply_mode_layout_multiplier(16.0F / 9.0F);
824+
case AspectViewMode::Preset4x3:
825+
return apply_mode_layout_multiplier(4.0F / 3.0F);
826+
case AspectViewMode::Preset1x1:
827+
return apply_mode_layout_multiplier(1.0F);
828+
case AspectViewMode::Original:
829+
default:
830+
break;
831+
}
832+
833+
return compute_content_aspect_ratio();
834+
}
835+
808836
void Display::update_content_window_layout() {
809837
const int safe_window_w = std::max(1, window_width_);
810838
const int safe_window_h = std::max(1, window_height_);
811839

812840
content_window_ = SDL_Rect{0, 0, safe_window_w, safe_window_h};
813841

814-
if (is_fullscreen_) {
815-
const float content_aspect_ratio = std::max(compute_content_aspect_ratio(), 0.001F);
842+
if (aspect_view_mode_ != AspectViewMode::Stretch) {
843+
const float content_aspect_ratio = std::max(compute_active_content_aspect_ratio(), 0.001F);
816844
const float window_aspect_ratio = static_cast<float>(safe_window_w) / static_cast<float>(safe_window_h);
817845

818846
if (window_aspect_ratio > content_aspect_ratio) {
@@ -864,7 +892,7 @@ void Display::handle_window_resize(const bool reset_forced_size_guard, const boo
864892

865893
// If aspect-ratio locking is enabled, snap the resize to the selected ratio.
866894
if (!skip_forced_size && !is_fullscreen_ && aspect_lock_mode_ != AspectLockMode::Off) {
867-
const float target_aspect_ratio = (aspect_lock_mode_ == AspectLockMode::Window) ? window_aspect_ratio_ : compute_content_aspect_ratio();
895+
const float target_aspect_ratio = (aspect_lock_mode_ == AspectLockMode::Window) ? window_aspect_ratio_ : compute_active_content_aspect_ratio();
868896
const float safe_target_aspect_ratio = std::max(target_aspect_ratio, 0.001F);
869897
const float current_ratio = static_cast<float>(new_window_w) / static_cast<float>(new_window_h);
870898
if (std::abs(current_ratio - safe_target_aspect_ratio) > 0.001F) {
@@ -3307,8 +3335,23 @@ void Display::handle_event(const SDL_Event& event) {
33073335
notify_user(string_sprintf("Video texture filter set to '%s'", bilinear_texture_filtering_ ? "BILINEAR" : "NEAREST NEIGHBOR"));
33083336
break;
33093337
case SDLK_s: {
3310-
swap_left_right_ = !swap_left_right_;
3311-
refresh_display_side_mapping();
3338+
if (is_shift_down) {
3339+
constexpr int kModeCount = 5;
3340+
const int delta = is_ctrl_down ? -1 : 1;
3341+
aspect_view_mode_ = static_cast<AspectViewMode>((static_cast<int>(aspect_view_mode_) + delta + kModeCount) % kModeCount);
3342+
3343+
if (is_fullscreen_) {
3344+
handle_window_resize(true, true);
3345+
} else {
3346+
const auto target_size = compute_mode_switch_target_window_size();
3347+
apply_window_size_and_relayout(target_size[0], target_size[1], true);
3348+
}
3349+
3350+
notify_user(string_sprintf("Aspect view mode set to '%s'", to_upper_case(aspect_view_mode_to_string(aspect_view_mode_)).c_str()));
3351+
} else {
3352+
swap_left_right_ = !swap_left_right_;
3353+
refresh_display_side_mapping();
3354+
}
33123355
break;
33133356
}
33143357
case SDLK_f:
@@ -3345,7 +3388,7 @@ void Display::handle_event(const SDL_Event& event) {
33453388
recreate_video_textures_for_current_mode();
33463389
resize_window_for_mode_switch();
33473390

3348-
const std::string mode_name = modeToString(mode_);
3391+
const std::string mode_name = mode_to_string(mode_);
33493392
notify_user(string_sprintf("Display mode set to '%s'", to_upper_case(mode_name).c_str()));
33503393
} else {
33513394
print_image_similarity_metrics_ = true;
@@ -3430,7 +3473,11 @@ void Display::handle_event(const SDL_Event& event) {
34303473
}
34313474
break;
34323475
case SDLK_x:
3433-
show_fps_ = true;
3476+
if (is_shift_down) {
3477+
notify_user(string_sprintf("Display state: window=%dx%d aspect=%s", window_width_, window_height_, aspect_view_mode_to_string(aspect_view_mode_).c_str()));
3478+
} else {
3479+
show_fps_ = true;
3480+
}
34343481
break;
34353482
case SDLK_PLUS:
34363483
case SDLK_KP_PLUS:

display.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ class Display {
107107
enum class Mode { Split, VStack, HStack };
108108
enum class Loop { Off, ForwardOnly, PingPong };
109109
enum class AspectLockMode { Off, Window, Content };
110+
enum class AspectViewMode { Original, Stretch, Preset16x9, Preset4x3, Preset1x1 };
110111
enum class DiffMode { LegacyAbs, AbsLinear, AbsSqrt, SignedDiverging };
111112

112-
std::string modeToString(const Mode& mode) {
113+
std::string mode_to_string(const Mode& mode) {
113114
switch (mode) {
114115
case Mode::Split:
115116
return "split";
@@ -122,6 +123,23 @@ class Display {
122123
}
123124
}
124125

126+
std::string aspect_view_mode_to_string(const Display::AspectViewMode& mode) {
127+
switch (mode) {
128+
case Display::AspectViewMode::Original:
129+
return "original";
130+
case Display::AspectViewMode::Stretch:
131+
return "stretch";
132+
case Display::AspectViewMode::Preset16x9:
133+
return "16:9";
134+
case Display::AspectViewMode::Preset4x3:
135+
return "4:3";
136+
case Display::AspectViewMode::Preset1x1:
137+
return "1:1";
138+
default:
139+
return "unknown";
140+
}
141+
}
142+
125143
private:
126144
const int display_number_;
127145
Mode mode_;
@@ -159,6 +177,7 @@ class Display {
159177
float font_scale_;
160178
// Stored window aspect ratio used by AspectLockMode::Window.
161179
float window_aspect_ratio_{1.0F};
180+
AspectViewMode aspect_view_mode_{AspectViewMode::Original};
162181

163182
// Last size we programmatically forced (used to avoid resize feedback loops).
164183
std::array<int, 2> last_forced_window_size_{{-1, -1}};
@@ -309,6 +328,7 @@ class Display {
309328
void clamp_overlay_offsets();
310329
bool detect_fullscreen_like_state() const;
311330
float compute_content_aspect_ratio() const;
331+
float compute_active_content_aspect_ratio() const;
312332
std::array<int, 2> compute_mode_switch_target_window_size() const;
313333
void update_content_window_layout();
314334
void apply_window_size_and_relayout(int target_w, int target_h, bool force_layout_refresh);

0 commit comments

Comments
 (0)