diff --git a/Autoloads/pixel_screen.gd b/Autoloads/pixel_screen.gd new file mode 100755 index 0000000..61a0411 --- /dev/null +++ b/Autoloads/pixel_screen.gd @@ -0,0 +1,89 @@ +extends Node + +# Autoloaded as "PixelScreen". The game draws into a fixed 640x480 screen (the +# viewport size in Project Settings), which is scaled to fit the window as big +# as it can go while keeping its shape, with black bars filling the rest. +# It scales smoothly to any size, and the sharp_bilinear shader keeps the pixels +# crisp and all the same size. +# +# The scene being played, and the Transition overlay, live inside the screen. +# Change scenes with change_scene() / reload_scene() (Transition does this), +# not get_tree().change_scene_to_file(), which would put the new scene outside it. +# get_tree().current_scene is null once the game is running: Godot only allows +# it to be a direct child of the window. Use PixelScreen.scene instead. + +var screen := SubViewport.new() +var scene: Node # The scene being played. +var _container := SubViewportContainer.new() +var _bars := ColorRect.new() + + +func _ready() -> void: + # Keeps passing input to the screen while the game is paused, so the pause + # menu works. The game itself pauses as normal. + process_mode = Node.PROCESS_MODE_ALWAYS + + _bars.color = Color.BLACK + _bars.mouse_filter = Control.MOUSE_FILTER_IGNORE + add_child(_bars) + + screen.size = Vector2i( + ProjectSettings.get_setting("display/window/size/viewport_width"), + ProjectSettings.get_setting("display/window/size/viewport_height")) + screen.process_mode = Node.PROCESS_MODE_PAUSABLE + screen.render_target_update_mode = SubViewport.UPDATE_ALWAYS + screen.handle_input_locally = false + screen.canvas_item_default_texture_filter = Viewport.DEFAULT_CANVAS_ITEM_TEXTURE_FILTER_NEAREST + screen.snap_2d_transforms_to_pixel = true + screen.audio_listener_enable_2d = true + + _container.stretch = false + _container.size = screen.size + _container.texture_filter = CanvasItem.TEXTURE_FILTER_LINEAR # The shader needs it. + var material := ShaderMaterial.new() + material.shader = preload("res://Autoloads/sharp_bilinear.gdshader") + _container.material = material + _container.add_child(screen) + add_child(_container) + + get_window().size_changed.connect(_fit_to_window) + _fit_to_window() + # The main scene is added after the autoloads, so move it in once it's there. + _adopt_scenes.call_deferred() + + +# Swaps the scene being played for the one at `path`. +func change_scene(path: String) -> void: + _swap_to.call_deferred(load(path)) + + +func reload_scene() -> void: + if scene != null and not scene.scene_file_path.is_empty(): + change_scene(scene.scene_file_path) + + +func _adopt_scenes() -> void: + Transition.reparent(screen, false) + scene = get_tree().current_scene + if scene != null: + scene.reparent(screen, false) + screen.move_child(scene, 0) # Under the Transition overlay. + + +func _swap_to(packed: PackedScene) -> void: + if is_instance_valid(scene): + scene.get_parent().remove_child(scene) + scene.queue_free() + scene = packed.instantiate() + screen.add_child(scene) + screen.move_child(scene, 0) # Under the Transition overlay. + + +# Scales the screen as big as fits in the window, keeping its shape, centred. +func _fit_to_window() -> void: + var windowSize := Vector2(get_window().size) + var screenSize := Vector2(screen.size) + var scale := minf(windowSize.x / screenSize.x, windowSize.y / screenSize.y) + _bars.size = windowSize + _container.scale = Vector2.ONE * scale + _container.position = ((windowSize - screenSize * scale) / 2.0).round() diff --git a/Autoloads/pixel_screen.gd.uid b/Autoloads/pixel_screen.gd.uid new file mode 100755 index 0000000..4ac791d --- /dev/null +++ b/Autoloads/pixel_screen.gd.uid @@ -0,0 +1 @@ +uid://btqewdah127eg diff --git a/Autoloads/sharp_bilinear.gdshader b/Autoloads/sharp_bilinear.gdshader new file mode 100755 index 0000000..d5cc0e0 --- /dev/null +++ b/Autoloads/sharp_bilinear.gdshader @@ -0,0 +1,20 @@ +shader_type canvas_item; + +// "Sharp bilinear" upscaling for pixel art, used by PixelScreen. +// Each game pixel is drawn as a solid block, and only the single screen pixel +// where two game pixels meet is blended. So the game can be scaled by any +// amount (2.25x, 3.7x...) with every pixel looking the same size, instead of +// some being drawn a screen pixel wider than others. +// Needs the texture to be sampled with linear filtering. + +void fragment() { + vec2 textureSize = 1.0 / TEXTURE_PIXEL_SIZE; + vec2 texel = UV * textureSize; + // How many screen pixels one game pixel covers. + vec2 scale = 1.0 / max(fwidth(texel), vec2(1e-5)); + // Inside this distance of a game pixel's centre, show its colour as is. + vec2 solid = max(0.5 - 0.5 / scale, vec2(0.0)); + vec2 fromCentre = fract(texel) - 0.5; + vec2 blend = (fromCentre - clamp(fromCentre, -solid, solid)) * scale + 0.5; + COLOR = texture(TEXTURE, (floor(texel) + blend) / textureSize); +} diff --git a/Autoloads/sharp_bilinear.gdshader.uid b/Autoloads/sharp_bilinear.gdshader.uid new file mode 100755 index 0000000..87e356f --- /dev/null +++ b/Autoloads/sharp_bilinear.gdshader.uid @@ -0,0 +1 @@ +uid://doxlp8ex7ruwr diff --git a/Autoloads/transition.gd b/Autoloads/transition.gd index 1b529ee..b9ba55a 100755 --- a/Autoloads/transition.gd +++ b/Autoloads/transition.gd @@ -38,11 +38,12 @@ func _unhandled_input(event: InputEvent) -> void: and not event.echo and event.keycode == KEY_F9: reload_scene() +# Scenes are swapped by PixelScreen, so they're drawn inside its 640x480 screen. func change_scene(path: String) -> void: - _dissolve(func(): get_tree().change_scene_to_file(path)) + _dissolve(func(): PixelScreen.change_scene(path)) func reload_scene() -> void: - _dissolve(func(): get_tree().reload_current_scene()) + _dissolve(func(): PixelScreen.reload_scene()) func _dissolve(swap_scene: Callable) -> void: if busy: return diff --git a/project.godot b/project.godot index 93363f3..7eb6edf 100755 --- a/project.godot +++ b/project.godot @@ -17,6 +17,7 @@ config/icon="res://icon.svg" [autoload] +PixelScreen="*res://Autoloads/pixel_screen.gd" Settings="*res://Autoloads/settings.gd" Transition="*res://Autoloads/transition.gd" @@ -27,9 +28,6 @@ window/size/viewport_height=480 window/size/mode=2 window/size/window_width_override=1280 window/size/window_height_override=960 -window/stretch/mode="viewport" -window/stretch/aspect="keep" -window/stretch/scale_mode="integer" [dotnet]