Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
Version 1.0RC1 for Godot 3.2
  • Loading branch information
Yukitty committed Oct 15, 2020
0 parents commit d0831fe
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Godot-specific ignores
.import/
export.cfg
export_presets.cfg

# Imported translations (automatically generated from CSV files)
*.translation

# Mono-specific ignores
.mono/
data_*/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Yukita Mayako

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Integer Resolution Handler

This addon is a complete runtime script for handling integer resolutions for pixel-perfect 2D games.

## Usage

1. Enable the plugin. Close Project Settings.
2. Navigate Project Settings to the `display/window` category and scroll to the bottom.
3. In the new section "Integer Resolution Handler", set Base Width and Base Height to your game's native pixel resolution.

The IntegerResolutionHandler also works with all of the existing `stretch` settings, so fiddle there if you don't like how it behaves. Notably, setting `stretch/aspect` to "Keep" will enforce strict screen resolutions, while "Expand" will allow the viewable area to nearly double between scale steps.

90 changes: 90 additions & 0 deletions addons/integer_resolution_handler/integer_resolution_handler.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
extends Node
# IntegerResolutionHandler autoload.
# Watches for window size changes and handles
# game screen scaling with exact integer
# multiples of a base resolution in mind.


onready var base_resolution := Vector2(
ProjectSettings.get_setting("display/window/integer_resolution_handler/base_width"),
ProjectSettings.get_setting("display/window/integer_resolution_handler/base_height"))
var stretch_mode: int
var stretch_aspect: int
onready var stretch_shrink: float = ProjectSettings.get_setting("display/window/stretch/shrink")

onready var _root: Viewport = get_node("/root")


func _ready():
# Parse project settings
match ProjectSettings.get_setting("display/window/stretch/mode"):
"2d":
stretch_mode = SceneTree.STRETCH_MODE_2D
"viewport":
stretch_mode = SceneTree.STRETCH_MODE_VIEWPORT
_:
stretch_mode = SceneTree.STRETCH_MODE_DISABLED

match ProjectSettings.get_setting("display/window/stretch/aspect"):
"keep":
stretch_aspect = SceneTree.STRETCH_ASPECT_KEEP
"keep_height":
stretch_aspect = SceneTree.STRETCH_ASPECT_KEEP_HEIGHT
"keep_width":
stretch_aspect = SceneTree.STRETCH_ASPECT_KEEP_WIDTH
"expand":
stretch_aspect = SceneTree.STRETCH_ASPECT_EXPAND
_:
stretch_aspect = SceneTree.STRETCH_ASPECT_IGNORE

# Enforce minimum resolution.
OS.min_window_size = base_resolution

# Remove default stretch behavior.
var tree: SceneTree = get_tree()
tree.set_screen_stretch(SceneTree.STRETCH_MODE_DISABLED, SceneTree.STRETCH_ASPECT_IGNORE, base_resolution, 1)

# Start tracking resolution changes and scaling the screen.
update_resolution()
# warning-ignore:return_value_discarded
tree.connect("screen_resized", self, "update_resolution")


func update_resolution():
var video_mode: Vector2 = OS.window_size
if OS.window_fullscreen:
video_mode = OS.get_screen_size()

var scale := int(max(floor(min(video_mode.x / base_resolution.x, video_mode.y / base_resolution.y)), 1))

This comment has been minimized.

Copy link
@dalexeev

dalexeev Oct 15, 2020

This is not critical, but, in theory, the max(..., 1) check is optional, since this case is closed by the fact that you did OS.min_window_size = base_resolution in _ready().

var screen_size: Vector2 = base_resolution
var viewport_size: Vector2 = screen_size * scale
var overscan: Vector2 = ((video_mode - viewport_size) / scale).floor()
var margin: Vector2

match stretch_aspect:
SceneTree.STRETCH_ASPECT_KEEP_WIDTH:
screen_size.y += overscan.y
SceneTree.STRETCH_ASPECT_KEEP_HEIGHT:
screen_size.x += overscan.x
SceneTree.STRETCH_ASPECT_EXPAND, SceneTree.STRETCH_ASPECT_IGNORE:
screen_size += overscan
viewport_size = screen_size * scale
margin = ((video_mode - viewport_size) / 2).round()

match stretch_mode:
SceneTree.STRETCH_MODE_VIEWPORT:
_root.set_size((screen_size / stretch_shrink).floor())
_root.set_attach_to_screen_rect(Rect2(margin, viewport_size))
_root.set_size_override_stretch(false)
_root.set_size_override(false)
SceneTree.STRETCH_MODE_2D, _:
_root.set_size((viewport_size / stretch_shrink).floor())
_root.set_attach_to_screen_rect(Rect2(margin, viewport_size))
_root.set_size_override_stretch(true)
_root.set_size_override(true, (screen_size / stretch_shrink).floor(), margin)

This comment has been minimized.

Copy link
@dalexeev

dalexeev Oct 15, 2020

_root.set_size_override_stretch(...)
_root.set_size_override(...)

Perhaps I don't understand something, then these 2 lines are useless, they are reset at every screen_resized signal, if you delete or comment them out, nothing visually changes. It might have some performance impact, but I haven't tested it.

Likewise on lines 78-79.

This comment has been minimized.

Copy link
@Yukitty

Yukitty Oct 15, 2020

Author Owner

Those commands ARE the functionality of STRETCH_MODE_2D.

Most of this code is a direct port from what Godot Engine does internally, just prettier.


if margin.x < 0:
margin.x = 0
if margin.y < 0:
margin.y = 0
VisualServer.black_bars_set_margins(int(margin.x), int(margin.y), int(margin.x), int(margin.y))

This comment has been minimized.

Copy link
@dalexeev

dalexeev Oct 15, 2020

You are not considering that the left margin may not be equal to the right margin. For example, if the window width is an odd number and the base resolution is even. Likewise for vertical dimensions. Check out the last two lines from my example in my comment.

Also checks for negative values are a little strange to me. This shouldn't happen. Or is it just in case the user specified incorrect settings?

This comment has been minimized.

Copy link
@Yukitty

Yukitty Oct 15, 2020

Author Owner

The negative checks here and the max(..., 1) above are both for when OS.min_window_size isn't set (or if it somehow fails). This allows the margin to be negative to keep the screen centered during too-small window sizes, but the black bars flip the value to positive when used.

The reason the margins are the same on both sides is another port directly from the base engine code, which indeed MAY cause some screen edge issues here (partially covered pixels), but I'm not 100% certain that simply flooring one side and ceiling the other would adequately resolve it. 🤔 (Might have to do something scale-aware?)

This comment has been minimized.

Copy link
@dalexeev

dalexeev Oct 15, 2020

I meant that if the base resolution, for example 320 pixels, and the window size, for example 323 pixels, then the margin on the left should be 1, and on the right 2 (or vice versa). I tested this, my code handles it correctly. And you have a strip of 1 screen pixel on the right that runs over the content if you slowly stretch the window.

9 changes: 9 additions & 0 deletions addons/integer_resolution_handler/plugin.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[plugin]

name="IntegerResolutionHandler"
description="Alternative resolution stretch mode which locks to exact multiples of the base game resolution.
In other words, square pixels."
author="Yukitty"
version="1.0"
script="plugin.gd"
36 changes: 36 additions & 0 deletions addons/integer_resolution_handler/plugin.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
tool
extends EditorPlugin


func _enter_tree():
add_autoload_singleton("IntegerResolutionHandler", "res://addons/integer_resolution_handler/integer_resolution_handler.gd")

ProjectSettings.set_setting(
"display/window/integer_resolution_handler/base_width",
max(floor(ProjectSettings.get_setting("display/window/size/width") / 3), 8))
ProjectSettings.add_property_info({
"name": "display/window/integer_resolution_handler/base_width",
"type": TYPE_INT,
"hint": PROPERTY_HINT_RANGE,
"hint_string": "1,1024,1,or_greater"
})
ProjectSettings.set_initial_value(
"display/window/integer_resolution_handler/base_width", 320)

ProjectSettings.set_setting(
"display/window/integer_resolution_handler/base_height",
max(floor(ProjectSettings.get_setting("display/window/size/height") / 3), 8))
ProjectSettings.add_property_info({
"name": "display/window/integer_resolution_handler/base_height",
"type": TYPE_INT,
"hint": PROPERTY_HINT_RANGE,
"hint_string": "1,600,1,or_greater"
})
ProjectSettings.set_initial_value(
"display/window/integer_resolution_handler/base_height", 240)


func _exit_tree():
remove_autoload_singleton("IntegerResolutionHandler")
ProjectSettings.clear("display/window/integer_resolution_handler/base_width")
ProjectSettings.clear("display/window/integer_resolution_handler/base_height")
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

1 comment on commit d0831fe

@dalexeev
Copy link

@dalexeev dalexeev commented on d0831fe Oct 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for leaving the review in the oldest commit, it was just the easiest and most convenient way.

You can also try adding a background color/texture setting using the VisualServer.black_bars_set_margins() function, but it's a little buggy.

Also, when it comes to pixel perfect games, one cannot fail to mention the rendering/quality/2d/use_pixel_snap and rendering/quality/dynamic_fonts/use_oversampling settings. At least it would be nice to add it to the addon instructions.

Otherwise, this is a very good and competent job! I didn't even think about the fact that we can implement support for aspects other than KEEP for this mode.

Please sign in to comment.