1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Add Stretch Modes for Splash Screen

Co-authored-by: Samuel Pedrajas <samuelpedrajaspz@gmail.com>
This commit is contained in:
Justin Sasso
2025-08-13 20:59:57 -04:00
parent c7b1767560
commit b6b3e1ef9e
16 changed files with 178 additions and 46 deletions

View File

@@ -76,6 +76,7 @@ public:
RendererCanvasRender *get_canvas() override { return &canvas; }
RendererSceneRender *get_scene() override { return &scene; }
void set_boot_image_with_stretch(const Ref<Image> &p_image, const Color &p_color, RenderingServer::SplashStretchMode p_stretch_mode, bool p_use_filter = true) override {}
void set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter = true) override {}
void initialize() override {}

View File

@@ -45,6 +45,11 @@ RendererCompositor *RendererCompositor::create() {
return _create_func();
}
void RendererCompositor::set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter) {
RenderingServer::SplashStretchMode stretch_mode = RenderingServer::map_scaling_option_to_stretch_mode(p_scale);
set_boot_image_with_stretch(p_image, p_color, stretch_mode, p_use_filter);
}
bool RendererCompositor::is_xr_enabled() const {
return xr_enabled;
}

View File

@@ -87,7 +87,8 @@ public:
virtual RendererCanvasRender *get_canvas() = 0;
virtual RendererSceneRender *get_scene() = 0;
virtual void set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter = true) = 0;
virtual void set_boot_image_with_stretch(const Ref<Image> &p_image, const Color &p_color, RenderingServer::SplashStretchMode p_stretch_mode, bool p_use_filter = true) = 0;
virtual void set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter = true);
virtual void initialize() = 0;
virtual void begin_frame(double frame_step) = 0;

View File

@@ -180,7 +180,7 @@ void RendererCompositorRD::finalize() {
RD::get_singleton()->free_rid(blit.sampler);
}
void RendererCompositorRD::set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter) {
void RendererCompositorRD::set_boot_image_with_stretch(const Ref<Image> &p_image, const Color &p_color, RenderingServer::SplashStretchMode p_stretch_mode, bool p_use_filter) {
if (p_image.is_null() || p_image->is_empty()) {
return;
}
@@ -215,15 +215,7 @@ void RendererCompositorRD::set_boot_image(const Ref<Image> &p_image, const Color
Size2 window_size = DisplayServer::get_singleton()->window_get_size();
Rect2 imgrect(0, 0, p_image->get_width(), p_image->get_height());
Rect2 screenrect;
if (p_scale) {
screenrect = OS::get_singleton()->calculate_boot_screen_rect(window_size, imgrect.size);
} else {
screenrect = imgrect;
screenrect.position += ((window_size - screenrect.size) / 2.0).floor();
}
Rect2 screenrect = RenderingServer::get_splash_stretched_screen_rect(p_image->get_size(), window_size, p_stretch_mode);
screenrect.position /= window_size;
screenrect.size /= window_size;

View File

@@ -119,7 +119,7 @@ public:
RendererCanvasRender *get_canvas() { return canvas; }
RendererSceneRender *get_scene() { return scene; }
void set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter);
void set_boot_image_with_stretch(const Ref<Image> &p_image, const Color &p_color, RenderingServer::SplashStretchMode p_stretch_mode, bool p_use_filter);
void initialize();
void begin_frame(double frame_step);

View File

@@ -1886,6 +1886,63 @@ int RenderingServer::global_shader_uniform_type_get_shader_datatype(GlobalShader
}
}
Rect2 RenderingServer::get_splash_stretched_screen_rect(const Size2 &p_image_size, const Size2 &p_window_size, SplashStretchMode p_stretch_mode) {
Size2 imgsize = p_image_size;
Rect2 screenrect;
switch (p_stretch_mode) {
case SplashStretchMode::SPLASH_STRETCH_MODE_DISABLED: {
screenrect.size = imgsize;
screenrect.position = ((p_window_size - screenrect.size) / 2.0).floor();
} break;
case SplashStretchMode::SPLASH_STRETCH_MODE_KEEP: {
if (p_window_size.width > p_window_size.height) {
// Scale horizontally.
screenrect.size.y = p_window_size.height;
screenrect.size.x = imgsize.width * p_window_size.height / imgsize.height;
screenrect.position.x = (p_window_size.width - screenrect.size.x) / 2;
} else {
// Scale vertically.
screenrect.size.x = p_window_size.width;
screenrect.size.y = imgsize.height * p_window_size.width / imgsize.width;
screenrect.position.y = (p_window_size.height - screenrect.size.y) / 2;
}
} break;
case SplashStretchMode::SPLASH_STRETCH_MODE_KEEP_WIDTH: {
// Scale vertically.
screenrect.size.x = p_window_size.width;
screenrect.size.y = imgsize.height * p_window_size.width / imgsize.width;
screenrect.position.y = (p_window_size.height - screenrect.size.y) / 2;
} break;
case SplashStretchMode::SPLASH_STRETCH_MODE_KEEP_HEIGHT: {
// Scale horizontally.
screenrect.size.y = p_window_size.height;
screenrect.size.x = imgsize.width * p_window_size.height / imgsize.height;
screenrect.position.x = (p_window_size.width - screenrect.size.x) / 2;
} break;
case SplashStretchMode::SPLASH_STRETCH_MODE_COVER: {
double window_aspect = (double)p_window_size.width / p_window_size.height;
double img_aspect = imgsize.width / imgsize.height;
if (window_aspect > img_aspect) {
// Scale vertically.
screenrect.size.x = p_window_size.width;
screenrect.size.y = imgsize.height * p_window_size.width / imgsize.width;
screenrect.position.y = (p_window_size.height - screenrect.size.y) / 2;
} else {
// Scale horizontally.
screenrect.size.y = p_window_size.height;
screenrect.size.x = imgsize.width * p_window_size.height / imgsize.height;
screenrect.position.x = (p_window_size.width - screenrect.size.x) / 2;
}
} break;
case SplashStretchMode::SPLASH_STRETCH_MODE_IGNORE: {
screenrect.size.x = p_window_size.width;
screenrect.size.y = p_window_size.height;
} break;
}
return screenrect;
}
RenderingDevice *RenderingServer::get_rendering_device() const {
// Return the rendering device we're using globally.
return RenderingDevice::get_singleton();
@@ -3495,7 +3552,10 @@ void RenderingServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_test_texture"), &RenderingServer::get_test_texture);
ClassDB::bind_method(D_METHOD("get_white_texture"), &RenderingServer::get_white_texture);
ClassDB::bind_method(D_METHOD("set_boot_image_with_stretch", "image", "color", "stretch_mode", "use_filter"), &RenderingServer::set_boot_image_with_stretch, DEFVAL(true));
#ifndef DISABLE_DEPRECATED
ClassDB::bind_method(D_METHOD("set_boot_image", "image", "color", "scale", "use_filter"), &RenderingServer::set_boot_image, DEFVAL(true));
#endif
ClassDB::bind_method(D_METHOD("get_default_clear_color"), &RenderingServer::get_default_clear_color);
ClassDB::bind_method(D_METHOD("set_default_clear_color", "color"), &RenderingServer::set_default_clear_color);
@@ -3528,6 +3588,13 @@ void RenderingServer::_bind_methods() {
BIND_ENUM_CONSTANT(PIPELINE_SOURCE_SPECIALIZATION);
BIND_ENUM_CONSTANT(PIPELINE_SOURCE_MAX);
BIND_ENUM_CONSTANT(SPLASH_STRETCH_MODE_DISABLED);
BIND_ENUM_CONSTANT(SPLASH_STRETCH_MODE_KEEP);
BIND_ENUM_CONSTANT(SPLASH_STRETCH_MODE_KEEP_WIDTH);
BIND_ENUM_CONSTANT(SPLASH_STRETCH_MODE_KEEP_HEIGHT);
BIND_ENUM_CONSTANT(SPLASH_STRETCH_MODE_COVER);
BIND_ENUM_CONSTANT(SPLASH_STRETCH_MODE_IGNORE);
ADD_SIGNAL(MethodInfo("frame_pre_draw"));
ADD_SIGNAL(MethodInfo("frame_post_draw"));
@@ -3576,6 +3643,13 @@ void RenderingServer::mesh_add_surface_from_planes(RID p_mesh, const Vector<Plan
mesh_add_surface_from_mesh_data(p_mesh, mdata);
}
#ifndef DISABLE_DEPRECATED
void RenderingServer::set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter) {
SplashStretchMode stretch_mode = map_scaling_option_to_stretch_mode(p_scale);
set_boot_image_with_stretch(p_image, p_color, stretch_mode, p_use_filter);
}
#endif
RID RenderingServer::instance_create2(RID p_base, RID p_scenario) {
RID instance = instance_create();
instance_set_base(instance, p_base);

View File

@@ -1849,7 +1849,23 @@ public:
virtual void mesh_add_surface_from_mesh_data(RID p_mesh, const Geometry3D::MeshData &p_mesh_data);
virtual void mesh_add_surface_from_planes(RID p_mesh, const Vector<Plane> &p_planes);
virtual void set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter = true) = 0;
enum SplashStretchMode {
SPLASH_STRETCH_MODE_DISABLED,
SPLASH_STRETCH_MODE_KEEP,
SPLASH_STRETCH_MODE_KEEP_WIDTH,
SPLASH_STRETCH_MODE_KEEP_HEIGHT,
SPLASH_STRETCH_MODE_COVER,
SPLASH_STRETCH_MODE_IGNORE,
};
virtual void set_boot_image_with_stretch(const Ref<Image> &p_image, const Color &p_color, SplashStretchMode p_stretch_mode, bool p_use_filter = true) = 0;
static Rect2 get_splash_stretched_screen_rect(const Size2 &p_image_size, const Size2 &p_window_size, SplashStretchMode p_stretch_mode); // Helper for splash screen
#ifndef DISABLE_DEPRECATED
void set_boot_image(const Ref<Image> &p_image, const Color &p_color, bool p_scale, bool p_use_filter = true); // Superseded, but left to preserve compat.
#endif
_ALWAYS_INLINE_ static SplashStretchMode map_scaling_option_to_stretch_mode(bool p_scale) {
return p_scale ? SplashStretchMode::SPLASH_STRETCH_MODE_KEEP : SplashStretchMode::SPLASH_STRETCH_MODE_DISABLED;
}
virtual Color get_default_clear_color() = 0;
virtual void set_default_clear_color(const Color &p_color) = 0;
@@ -2003,6 +2019,7 @@ VARIANT_ENUM_CAST(RenderingServer::CanvasLightShadowFilter);
VARIANT_ENUM_CAST(RenderingServer::CanvasOccluderPolygonCullMode);
VARIANT_ENUM_CAST(RenderingServer::GlobalShaderParameterType);
VARIANT_ENUM_CAST(RenderingServer::RenderingInfo);
VARIANT_ENUM_CAST(RenderingServer::SplashStretchMode);
VARIANT_ENUM_CAST(RenderingServer::CanvasTextureChannel);
VARIANT_ENUM_CAST(RenderingServer::BakeChannels);

View File

@@ -1110,7 +1110,7 @@ public:
#define ServerName RendererCompositor
#define server_name RSG::rasterizer
FUNC4S(set_boot_image, const Ref<Image> &, const Color &, bool, bool)
FUNC4S(set_boot_image_with_stretch, const Ref<Image> &, const Color &, RenderingServer::SplashStretchMode, bool)
/* STATUS INFORMATION */