-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
Version 1.0RC1 for Godot 3.2
- Loading branch information
There are no files selected for viewing
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_*/ |
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. |
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. | ||
|
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.
Sorry, something went wrong. |
||
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.
Sorry, something went wrong.
dalexeev
|
||
|
||
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.
Sorry, something went wrong.
dalexeev
|
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" |
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") |
1 comment
on commit d0831fe
There was a problem hiding this comment.
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.
This is not critical, but, in theory, the
max(..., 1)
check is optional, since this case is closed by the fact that you didOS.min_window_size = base_resolution
in_ready()
.