├── icon.png ├── .gitignore ├── addons └── integer_resolution_handler │ ├── plugin.cfg │ ├── plugin.gd │ └── integer_resolution_handler.gd ├── LICENSE └── README.md /icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Yukitty/godot-addon-integer_resolution_handler/HEAD/icon.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Godot-specific ignores 2 | .import/ 3 | export.cfg 4 | export_presets.cfg 5 | 6 | # Imported translations (automatically generated from CSV files) 7 | *.translation 8 | 9 | # Mono-specific ignores 10 | .mono/ 11 | data_*/ 12 | -------------------------------------------------------------------------------- /addons/integer_resolution_handler/plugin.cfg: -------------------------------------------------------------------------------- 1 | [plugin] 2 | 3 | name="IntegerResolutionHandler" 4 | description="Alternative stretch handler for low resolution (pixel art) games in high resolution windows. Restricts the game resolution to integer steps, keeping pixels square." 5 | author="Yukitty" 6 | version="1.1.1" 7 | script="plugin.gd" 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Yukita Mayako 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Integer Resolution Handler 2 | 3 | Alternative stretch handler for low resolution (pixel art) games in high resolution windows. Restricts the game resolution to integer steps, keeping pixels square. 4 | 5 | ## Usage 6 | 7 | 1. Enable the plugin. Close Project Settings. 8 | 2. Navigate Project Settings to the `display/window` category. 9 | 3. In the new section "Integer Resolution Handler", set Base Width and Base Height to your game's native pixel resolution. 10 | 11 | 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 extend dramatically in all directions between scale steps. 12 | 13 | If you set Base Width and Base Height to a 4:3 aspect ratio and use the "Keep Height" or "Expand" aspect handling modes, your game will extend horizontally to support widescreen aspects as well. Just make sure your game is fully playable at its base resolution and GUI elements properly stretch and move, the same as you would for a non-pixel art game. 14 | -------------------------------------------------------------------------------- /addons/integer_resolution_handler/plugin.gd: -------------------------------------------------------------------------------- 1 | tool 2 | extends EditorPlugin 3 | 4 | 5 | const SETTING_BASE_WIDTH := "display/window/integer_resolution_handler/base_width" 6 | const SETTING_BASE_HEIGHT := "display/window/integer_resolution_handler/base_height" 7 | const DEFAULT_BASE_WIDTH: int = 320 8 | const DEFAULT_BASE_HEIGHT: int = 240 9 | 10 | 11 | func _enter_tree(): 12 | add_autoload_singleton("IntegerResolutionHandler", "res://addons/integer_resolution_handler/integer_resolution_handler.gd") 13 | 14 | if not ProjectSettings.has_setting(SETTING_BASE_WIDTH): 15 | ProjectSettings.set_setting(SETTING_BASE_WIDTH, DEFAULT_BASE_WIDTH) 16 | ProjectSettings.set_initial_value(SETTING_BASE_WIDTH, DEFAULT_BASE_WIDTH) 17 | ProjectSettings.add_property_info({ 18 | "name": SETTING_BASE_WIDTH, 19 | "type": TYPE_INT, 20 | "hint": PROPERTY_HINT_RANGE, 21 | "hint_string": "1,1024,1,or_greater" 22 | }) 23 | 24 | if not ProjectSettings.has_setting(SETTING_BASE_HEIGHT): 25 | ProjectSettings.set_setting(SETTING_BASE_HEIGHT, DEFAULT_BASE_HEIGHT) 26 | ProjectSettings.set_initial_value(SETTING_BASE_HEIGHT, DEFAULT_BASE_HEIGHT) 27 | ProjectSettings.add_property_info({ 28 | "name": SETTING_BASE_HEIGHT, 29 | "type": TYPE_INT, 30 | "hint": PROPERTY_HINT_RANGE, 31 | "hint_string": "1,600,1,or_greater" 32 | }) 33 | 34 | var order: int = ProjectSettings.get_order("display/window/size/width") - 2 35 | ProjectSettings.set_order(SETTING_BASE_WIDTH, order) 36 | ProjectSettings.set_order(SETTING_BASE_HEIGHT, order + 1) 37 | ProjectSettings.save() 38 | 39 | 40 | func disable_plugin(): 41 | remove_autoload_singleton("IntegerResolutionHandler") 42 | ProjectSettings.clear("display/window/integer_resolution_handler/base_width") 43 | ProjectSettings.clear("display/window/integer_resolution_handler/base_height") 44 | ProjectSettings.save() 45 | 46 | -------------------------------------------------------------------------------- /addons/integer_resolution_handler/integer_resolution_handler.gd: -------------------------------------------------------------------------------- 1 | extends Node 2 | # IntegerResolutionHandler autoload. 3 | # Watches for window size changes and handles 4 | # game screen scaling with exact integer 5 | # multiples of a base resolution in mind. 6 | 7 | const SETTING_BASE_WIDTH = "display/window/integer_resolution_handler/base_width" 8 | const SETTING_BASE_HEIGHT = "display/window/integer_resolution_handler/base_height" 9 | 10 | var base_resolution := Vector2(320, 240) 11 | var stretch_mode: int 12 | var stretch_aspect: int 13 | onready var stretch_shrink: float = ProjectSettings.get_setting("display/window/stretch/shrink") 14 | 15 | onready var _root: Viewport = get_node("/root") 16 | 17 | 18 | func _ready(): 19 | # Parse project settings 20 | if ProjectSettings.has_setting(SETTING_BASE_WIDTH): 21 | base_resolution.x = ProjectSettings.get_setting(SETTING_BASE_WIDTH) 22 | if ProjectSettings.has_setting(SETTING_BASE_HEIGHT): 23 | base_resolution.y = ProjectSettings.get_setting(SETTING_BASE_HEIGHT) 24 | 25 | match ProjectSettings.get_setting("display/window/stretch/mode"): 26 | "2d": 27 | stretch_mode = SceneTree.STRETCH_MODE_2D 28 | "viewport": 29 | stretch_mode = SceneTree.STRETCH_MODE_VIEWPORT 30 | _: 31 | stretch_mode = SceneTree.STRETCH_MODE_DISABLED 32 | 33 | match ProjectSettings.get_setting("display/window/stretch/aspect"): 34 | "keep": 35 | stretch_aspect = SceneTree.STRETCH_ASPECT_KEEP 36 | "keep_height": 37 | stretch_aspect = SceneTree.STRETCH_ASPECT_KEEP_HEIGHT 38 | "keep_width": 39 | stretch_aspect = SceneTree.STRETCH_ASPECT_KEEP_WIDTH 40 | "expand": 41 | stretch_aspect = SceneTree.STRETCH_ASPECT_EXPAND 42 | _: 43 | stretch_aspect = SceneTree.STRETCH_ASPECT_IGNORE 44 | 45 | # Enforce minimum resolution. 46 | OS.min_window_size = base_resolution 47 | 48 | # Remove default stretch behavior. 49 | var tree: SceneTree = get_tree() 50 | tree.set_screen_stretch(SceneTree.STRETCH_MODE_DISABLED, SceneTree.STRETCH_ASPECT_IGNORE, base_resolution, 1) 51 | 52 | # Start tracking resolution changes and scaling the screen. 53 | update_resolution() 54 | # warning-ignore:return_value_discarded 55 | tree.connect("screen_resized", self, "update_resolution") 56 | 57 | 58 | func update_resolution(): 59 | var video_mode: Vector2 = OS.window_size 60 | if OS.window_fullscreen: 61 | video_mode = OS.get_screen_size() 62 | 63 | var scale := int(max(floor(min(video_mode.x / base_resolution.x, video_mode.y / base_resolution.y)), 1)) 64 | var screen_size: Vector2 = base_resolution 65 | var viewport_size: Vector2 = screen_size * scale 66 | var overscan: Vector2 = ((video_mode - viewport_size) / scale).floor() 67 | var margin: Vector2 68 | var margin2: Vector2 69 | 70 | match stretch_aspect: 71 | SceneTree.STRETCH_ASPECT_KEEP_WIDTH: 72 | screen_size.y += overscan.y 73 | SceneTree.STRETCH_ASPECT_KEEP_HEIGHT: 74 | screen_size.x += overscan.x 75 | SceneTree.STRETCH_ASPECT_EXPAND, SceneTree.STRETCH_ASPECT_IGNORE: 76 | screen_size += overscan 77 | viewport_size = screen_size * scale 78 | margin = (video_mode - viewport_size) / 2 79 | margin2 = margin.ceil() 80 | margin = margin.floor() 81 | 82 | match stretch_mode: 83 | SceneTree.STRETCH_MODE_VIEWPORT: 84 | _root.set_size((screen_size / stretch_shrink).floor()) 85 | _root.set_attach_to_screen_rect(Rect2(margin, viewport_size)) 86 | _root.set_size_override_stretch(false) 87 | _root.set_size_override(false) 88 | SceneTree.STRETCH_MODE_2D, _: 89 | _root.set_size((viewport_size / stretch_shrink).floor()) 90 | _root.set_attach_to_screen_rect(Rect2(margin, viewport_size)) 91 | _root.set_size_override_stretch(true) 92 | _root.set_size_override(true, (screen_size / stretch_shrink).floor()) 93 | 94 | if margin.x < 0: 95 | margin.x = 0 96 | if margin.y < 0: 97 | margin.y = 0 98 | if margin2.x < 0: 99 | margin2.x = 0 100 | if margin2.y < 0: 101 | margin2.y = 0 102 | VisualServer.black_bars_set_margins(int(margin.x), int(margin.y), int(margin2.x), int(margin2.y)) 103 | --------------------------------------------------------------------------------