Skip to content

Commit 6b08e79

Browse files
committed
Update Multiple Resolutions demo for Godot 4.0
1 parent e77b85f commit 6b08e79

File tree

8 files changed

+273
-246
lines changed

8 files changed

+273
-246
lines changed

gui/multiple_resolutions/README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,25 @@ overscan area to prevent GUI elements from being cut off. This can also improve
3131
the gameplay experience on large monitors by bringing HUD elements closer to the
3232
center of the screen.
3333

34-
A DynamicFont is also used to ensure font rendering remains crisp at high
35-
resolutions, thanks to Godot's built-in support for font oversampling. In other
36-
words, the engine will automatically re-rasterize fonts at different resolutions
37-
than the base window size when the window is resized (or when the window scale
38-
factor is changed).
34+
A DynamicFont with multichannel signed distance field (MSDF) rendering is also used.
35+
This allows for crisp font rendering at any resolution, without having to re-rasterize
36+
the font when the font size changes. This makes changing the various settings in this
37+
demo faster, especially when large font sizes are used as a result of the GUI scale factor
38+
setting being increased.
39+
40+
Note that by default, Godot uses font oversampling for traditional rasterized
41+
DynamicFonts. This means MSDF fonts are *not* required to have crisp fonts at
42+
higher-than-default screen resolutions.
3943

4044
Language: GDScript
4145

42-
Renderer: GLES 2
46+
Renderer: Vulkan Mobile
4347

4448
## Technical notes
4549

4650
The demo works with the following project settings:
4751

48-
- `2d` stretch mode (recommended for most non-pixel art games).
52+
- `canvas_items` stretch mode (formerly `2d`). Recommended for most non-pixel art games.
4953
- `expand` stretch aspect (allows support for multiple aspect ratios without
5054
distortion or black bars).
5155
- Using a base window size with a 1:1 aspect ratio (`648×648` in this demo).
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
[remap]
22

33
importer="texture"
4-
type="StreamTexture"
5-
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
4+
type="CompressedTexture2D"
5+
uid="uid://cx2c0cunh0e1i"
6+
path="res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"
67
metadata={
78
"vram_texture": false
89
}
910

1011
[deps]
1112

1213
source_file="res://icon.png"
13-
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
14+
dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"]
1415

1516
[params]
1617

1718
compress/mode=0
1819
compress/lossy_quality=0.7
19-
compress/hdr_mode=0
20+
compress/hdr_compression=1
2021
compress/bptc_ldr=0
2122
compress/normal_map=0
22-
flags/repeat=0
23-
flags/filter=true
24-
flags/mipmaps=false
25-
flags/anisotropic=false
26-
flags/srgb=2
23+
compress/channel_pack=0
24+
mipmaps/generate=false
25+
mipmaps/limit=-1
26+
roughness/mode=0
27+
roughness/src_normal=""
2728
process/fix_alpha_border=true
2829
process/premult_alpha=false
29-
process/HDR_as_SRGB=false
30-
process/invert_color=false
3130
process/normal_map_invert_y=false
32-
stream=false
33-
size_limit=0
34-
detect_3d=true
35-
svg/scale=1.0
31+
process/hdr_as_srgb=false
32+
process/hdr_clamp_exposure=false
33+
process/size_limit=0
34+
detect_3d/compress_to=1

gui/multiple_resolutions/main.gd

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,52 @@ var base_window_size = Vector2(ProjectSettings.get_setting("display/window/size/
88

99
# These defaults match this demo's project settings. Adjust as needed if adapting this
1010
# in your own project.
11-
var stretch_mode = SceneTree.STRETCH_MODE_2D
12-
var stretch_aspect = SceneTree.STRETCH_ASPECT_EXPAND
11+
var stretch_mode = Window.CONTENT_SCALE_MODE_CANVAS_ITEMS
12+
var stretch_aspect = Window.CONTENT_SCALE_ASPECT_EXPAND
1313

1414
var scale_factor = 1.0
1515
var gui_aspect_ratio = -1.0
1616
var gui_margin = 0.0
1717

18-
onready var panel = $Panel
19-
onready var arc = $Panel/AspectRatioContainer
18+
@onready var panel = $Panel
19+
@onready var arc = $Panel/AspectRatioContainer
2020

2121

2222
func _ready():
2323
# The `resized` signal will be emitted when the window size changes, as the root Control node
2424
# is resized whenever the window size changes. This is because the root Control node
2525
# uses a Full Rect anchor, so its size will always be equal to the window size.
2626
# warning-ignore:return_value_discarded
27-
connect("resized", self, "_on_resized")
27+
connect("resized", self._on_resized)
2828
update_container()
2929

3030

3131
func update_container():
3232
# The code within this function needs to be run twice to work around an issue with containers
3333
# having a 1-frame delay with updates.
34-
# Otherwise, `panel.rect_size` returns a value of the previous frame, which results in incorrect
34+
# Otherwise, `panel.size` returns a value of the previous frame, which results in incorrect
3535
# sizing of the inner AspectRatioContainer when using the Fit to Window setting.
3636
for _i in 2:
3737
if is_equal_approx(gui_aspect_ratio, -1.0):
3838
# Fit to Window. Tell the AspectRatioContainer to use the same aspect ratio as the window,
3939
# making the AspectRatioContainer not have any visible effect.
40-
arc.ratio = panel.rect_size.aspect()
41-
# Apply GUI margin on the AspectRatioContainer's parent (Panel).
42-
# This also makes the GUI margin apply on controls located outside the AspectRatioContainer
40+
arc.ratio = panel.size.aspect()
41+
# Apply GUI offset on the AspectRatioContainer's parent (Panel).
42+
# This also makes the GUI offset apply on controls located outside the AspectRatioContainer
4343
# (such as the inner side label in this demo).
44-
panel.margin_top = gui_margin
45-
panel.margin_bottom = -gui_margin
44+
panel.offset_top = gui_margin
45+
panel.offset_bottom = -gui_margin
4646
else:
4747
# Constrained aspect ratio.
48-
arc.ratio = min(panel.rect_size.aspect(), gui_aspect_ratio)
49-
# Adjust top and bottom margins relative to the aspect ratio when it's constrained.
50-
# This ensures that GUI margin settings behave exactly as if the window had the
48+
arc.ratio = min(panel.size.aspect(), gui_aspect_ratio)
49+
# Adjust top and bottom offsets relative to the aspect ratio when it's constrained.
50+
# This ensures that GUI offset settings behave exactly as if the window had the
5151
# original aspect ratio size.
52-
panel.margin_top = gui_margin / gui_aspect_ratio
53-
panel.margin_bottom = -gui_margin / gui_aspect_ratio
52+
panel.offset_top = gui_margin / gui_aspect_ratio
53+
panel.offset_bottom = -gui_margin / gui_aspect_ratio
5454

55-
panel.margin_left = gui_margin
56-
panel.margin_right = -gui_margin
55+
panel.offset_left = gui_margin
56+
panel.offset_right = -gui_margin
5757

5858

5959
func _on_gui_aspect_ratio_item_selected(index):
@@ -81,8 +81,8 @@ func _on_resized():
8181

8282

8383
func _on_gui_margin_drag_ended(_value_changed):
84-
gui_margin = $"Panel/AspectRatioContainer/ColorRect/CenterContainer/Options/GUIMargin/HSlider".value
85-
$"Panel/AspectRatioContainer/ColorRect/CenterContainer/Options/GUIMargin/Value".text = str(gui_margin)
84+
gui_margin = $"Panel/AspectRatioContainer/Panel/CenterContainer/Options/GUIMargin/HSlider".value
85+
$"Panel/AspectRatioContainer/Panel/CenterContainer/Options/GUIMargin/Value".text = str(gui_margin)
8686
update_container()
8787

8888

@@ -105,25 +105,25 @@ func _on_window_base_size_item_selected(index):
105105
7: # 1680×720 (21:9)
106106
base_window_size = Vector2(1680, 720)
107107

108-
get_tree().set_screen_stretch(stretch_mode, stretch_aspect, base_window_size, scale_factor)
108+
get_viewport().content_scale_size = base_window_size
109109
update_container()
110110

111111

112112
func _on_window_stretch_mode_item_selected(index):
113113
stretch_mode = index
114-
get_tree().set_screen_stretch(stretch_mode, stretch_aspect, base_window_size, scale_factor)
114+
get_viewport().content_scale_mode = stretch_mode
115115

116116
# Disable irrelevant options when the stretch mode is Disabled.
117-
$"Panel/AspectRatioContainer/ColorRect/CenterContainer/Options/WindowBaseSize/OptionButton".disabled = stretch_mode == SceneTree.STRETCH_MODE_DISABLED
118-
$"Panel/AspectRatioContainer/ColorRect/CenterContainer/Options/WindowStretchAspect/OptionButton".disabled = stretch_mode == SceneTree.STRETCH_MODE_DISABLED
117+
$"Panel/AspectRatioContainer/Panel/CenterContainer/Options/WindowBaseSize/OptionButton".disabled = stretch_mode == Window.CONTENT_SCALE_MODE_DISABLED
118+
$"Panel/AspectRatioContainer/Panel/CenterContainer/Options/WindowStretchAspect/OptionButton".disabled = stretch_mode == Window.CONTENT_SCALE_MODE_DISABLED
119119

120120

121121
func _on_window_stretch_aspect_item_selected(index):
122122
stretch_aspect = index
123-
get_tree().set_screen_stretch(stretch_mode, stretch_aspect, base_window_size, scale_factor)
123+
get_viewport().content_scale_aspect = stretch_aspect
124124

125125

126126
func _on_window_scale_factor_drag_ended(_value_changed):
127-
scale_factor = $"Panel/AspectRatioContainer/ColorRect/CenterContainer/Options/WindowScaleFactor/HSlider".value
128-
$"Panel/AspectRatioContainer/ColorRect/CenterContainer/Options/WindowScaleFactor/Value".text = "%d%%" % (scale_factor * 100)
129-
get_tree().set_screen_stretch(stretch_mode, stretch_aspect, base_window_size, scale_factor)
127+
scale_factor = $"Panel/AspectRatioContainer/Panel/CenterContainer/Options/WindowScaleFactor/HSlider".value
128+
$"Panel/AspectRatioContainer/Panel/CenterContainer/Options/WindowScaleFactor/Value".text = "%d%%" % (scale_factor * 100)
129+
get_viewport().content_scale_factor = scale_factor

0 commit comments

Comments
 (0)