89 lines
3.3 KiB
GDScript
Executable file
89 lines
3.3 KiB
GDScript
Executable file
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()
|