Skip to content

Commit

Permalink
Adds "pixelperfect" window aspect.
Browse files Browse the repository at this point in the history
This mode makes sure that only integers are used as multiplier or divisor
for the viewport size. This works best with viewport stretch mode.
As opposed to the other aspects this mode will allow for black borders
on all sides to satisfy the requirement.
  • Loading branch information
bfloch committed Feb 15, 2021
1 parent a59286f commit 50ee2f0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
4 changes: 3 additions & 1 deletion main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2167,6 +2167,8 @@ bool Main::start() {
cs_aspect = Window::CONTENT_SCALE_ASPECT_KEEP_HEIGHT;
} else if (stretch_aspect == "expand") {
cs_aspect = Window::CONTENT_SCALE_ASPECT_EXPAND;
} else if (stretch_aspect == "pixelperfect") {
cs_aspect = Window::CONTENT_SCALE_ASPECT_PIXELPERFECT;
}

sml->get_root()->set_content_scale_mode(cs_sm);
Expand Down Expand Up @@ -2211,7 +2213,7 @@ bool Main::start() {
PropertyInfo(Variant::STRING,
"display/window/stretch/aspect",
PROPERTY_HINT_ENUM,
"ignore,keep,keep_width,keep_height,expand"));
"ignore,keep,keep_width,keep_height,expand,pixelperfect"));
GLOBAL_DEF("display/window/stretch/shrink", 1.0);
ProjectSettings::get_singleton()->set_custom_property_info("display/window/stretch/shrink",
PropertyInfo(Variant::FLOAT,
Expand Down
23 changes: 21 additions & 2 deletions scene/main/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,20 @@ void Window::_update_viewport_size() {
float viewport_aspect = desired_res.aspect();
float video_mode_aspect = video_mode.aspect();

if (content_scale_aspect == CONTENT_SCALE_ASPECT_IGNORE || Math::is_equal_approx(viewport_aspect, video_mode_aspect)) {
if (content_scale_aspect == CONTENT_SCALE_ASPECT_PIXELPERFECT) {
// only multiply or divide by integer
viewport_size = desired_res;

if (video_mode >= desired_res) {
Size2 scale_size = video_mode / desired_res;
int scale_factor = Math::floor(std::min(scale_size.x, scale_size.y));
screen_size = desired_res * scale_factor;
} else {
Size2 scale_size = desired_res / video_mode;
int scale_factor = Math::ceil(std::max(scale_size.x, scale_size.y));
screen_size = desired_res / scale_factor;
}
} else if (content_scale_aspect == CONTENT_SCALE_ASPECT_IGNORE || Math::is_equal_approx(viewport_aspect, video_mode_aspect)) {
//same aspect or ignore aspect
viewport_size = desired_res;
screen_size = video_mode;
Expand Down Expand Up @@ -612,7 +625,13 @@ void Window::_update_viewport_size() {
Size2 margin;
Size2 offset;
//black bars and margin
if (content_scale_aspect != CONTENT_SCALE_ASPECT_EXPAND && screen_size.x < video_mode.x) {
if (content_scale_aspect == CONTENT_SCALE_ASPECT_PIXELPERFECT) {
margin.y = Math::round((video_mode.y - screen_size.y) / 2.0);
margin.x = Math::round((video_mode.x - screen_size.x) / 2.0);
//VisualServer::get_singleton()->black_bars_set_margins(margin.x, margin.y, margin.x, margin.y);
offset.x = Math::round(margin.x * viewport_size.y / screen_size.y);
offset.y = Math::round(margin.y * viewport_size.x / screen_size.x);
} else if (content_scale_aspect != CONTENT_SCALE_ASPECT_EXPAND && screen_size.x < video_mode.x) {
margin.x = Math::round((video_mode.x - screen_size.x) / 2.0);
//RenderingServer::get_singleton()->black_bars_set_margins(margin.x, 0, margin.x, 0);
offset.x = Math::round(margin.x * viewport_size.y / screen_size.y);
Expand Down
1 change: 1 addition & 0 deletions scene/main/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Window : public Viewport {
CONTENT_SCALE_ASPECT_KEEP_WIDTH,
CONTENT_SCALE_ASPECT_KEEP_HEIGHT,
CONTENT_SCALE_ASPECT_EXPAND,
CONTENT_SCALE_ASPECT_PIXELPERFECT,
};

enum LayoutDirection {
Expand Down

0 comments on commit 50ee2f0

Please sign in to comment.