From 580f7d88926cf05f5b581f1691c44f5ff3604432 Mon Sep 17 00:00:00 2001 From: Samuel Pedrajas Date: Mon, 25 Oct 2021 19:27:12 +0200 Subject: [PATCH 1/2] new scaling modes for splash screen --- doc/classes/ProjectSettings.xml | 5 ++- doc/classes/RenderingServer.xml | 22 +++++++++++-- main/main.cpp | 26 +++++++++++++-- servers/rendering/rasterizer_dummy.h | 2 +- servers/rendering/renderer_compositor.h | 2 +- .../renderer_rd/renderer_compositor_rd.cpp | 32 +++++++++++++++++-- .../renderer_rd/renderer_compositor_rd.h | 2 +- .../rendering/rendering_server_default.cpp | 4 +-- servers/rendering/rendering_server_default.h | 2 +- servers/rendering_server.cpp | 9 +++++- servers/rendering_server.h | 12 ++++++- 11 files changed, 103 insertions(+), 15 deletions(-) diff --git a/doc/classes/ProjectSettings.xml b/doc/classes/ProjectSettings.xml index c646380bee..3e6d500d15 100644 --- a/doc/classes/ProjectSettings.xml +++ b/doc/classes/ProjectSettings.xml @@ -196,7 +196,10 @@ Background color for the boot splash. - If [code]true[/code], scale the boot splash image to the full window length when engine starts. If [code]false[/code], the engine will leave it at the default pixel size. + If [code]true[/code], scale the boot splash image according to the mode selected in the stretch_mode property. If [code]false[/code], the engine will leave it at the default pixel size. + + + Specifies how the splash image will be stretched. Takes no effect if fullsize is false. For more information about available stretch modes look for SPLASH_STRETCH_MODE* constants in the documentation. Path to an image used as the boot splash. diff --git a/doc/classes/RenderingServer.xml b/doc/classes/RenderingServer.xml index 973f8cdb1e..0e1aa8f633 100644 --- a/doc/classes/RenderingServer.xml +++ b/doc/classes/RenderingServer.xml @@ -2575,10 +2575,10 @@ - + - Sets a boot image. The color defines the background color. If [code]scale[/code] is [code]true[/code], the image will be scaled to fit the screen size. If [code]use_filter[/code] is [code]true[/code], the image will be scaled with linear interpolation. If [code]use_filter[/code] is [code]false[/code], the image will be scaled with nearest-neighbor interpolation. + Sets a boot image. The color defines the background color. The value of [code]stretch_mode[/code] indicates how the image will be stretched (for more information about stretch modes see SPLASH_STRETCH_MODE* constants). If [code]use_filter[/code] is [code]true[/code], the image will be scaled with linear interpolation. If [code]use_filter[/code] is [code]false[/code], the image will be scaled with nearest-neighbor interpolation. @@ -4356,5 +4356,23 @@ Hardware supports multithreading. This enum is currently unused in Godot 3.x. + + If the window width is greater than its height, the splash image will be stretched to have the same height as the window. Otherwise, the image will be stretched to have the same width as the window. Both cases keep the original image's aspect ratio. + + + The splash image is stretched to have the same width as the window. It keeps the image's aspect ratio. + + + The splash image is stretched to have the same height as the window. It keeps the image's aspect ratio. + + + The splash image covers the window while keeping the aspect ratio. + + + The splash image covers the window witohut keeping the aspect ratio. + + + The splash image uses its default pixel size. + diff --git a/main/main.cpp b/main/main.cpp index d4a6216e35..998f20045e 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1634,12 +1634,22 @@ Error Main::setup2(Thread::ID p_main_tid_override) { if (show_logo) { //boot logo! String boot_logo_path = GLOBAL_DEF("application/boot_splash/image", String()); bool boot_logo_scale = GLOBAL_DEF("application/boot_splash/fullsize", true); + String boot_stretch_mode = GLOBAL_DEF("application/boot_splash/stretch_mode", "keep"); bool boot_logo_filter = GLOBAL_DEF("application/boot_splash/use_filter", true); + + ProjectSettings::get_singleton()->set_custom_property_info("application/boot_splash/stretch_mode", + PropertyInfo(Variant::STRING, + "application/boot_splash/stretch_mode", + PROPERTY_HINT_ENUM, "keep,keep_width,keep_height,cover,expand")); ProjectSettings::get_singleton()->set_custom_property_info("application/boot_splash/image", PropertyInfo(Variant::STRING, "application/boot_splash/image", PROPERTY_HINT_FILE, "*.png")); + if (!boot_logo_scale) { // ignore stretch mode if logo scaling is disabled + boot_stretch_mode = "disabled"; + } + Ref boot_logo; boot_logo_path = boot_logo_path.strip_edges(); @@ -1660,7 +1670,19 @@ Error Main::setup2(Thread::ID p_main_tid_override) { const Color boot_bg_color = GLOBAL_DEF("application/boot_splash/bg_color", boot_splash_bg_color); #endif if (boot_logo.is_valid()) { - RenderingServer::get_singleton()->set_boot_image(boot_logo, boot_bg_color, boot_logo_scale, + RenderingServer::SplashStretchMode vs_boot_stretch_mode = RenderingServer::SPLASH_STRETCH_MODE_DISABLED; + if (boot_stretch_mode == "keep") + vs_boot_stretch_mode = RenderingServer::SPLASH_STRETCH_MODE_KEEP; + else if (boot_stretch_mode == "keep_width") + vs_boot_stretch_mode = RenderingServer::SPLASH_STRETCH_MODE_KEEP_WIDTH; + else if (boot_stretch_mode == "keep_height") + vs_boot_stretch_mode = RenderingServer::SPLASH_STRETCH_MODE_KEEP_HEIGHT; + else if (boot_stretch_mode == "cover") + vs_boot_stretch_mode = RenderingServer::SPLASH_STRETCH_MODE_COVER; + else if (boot_stretch_mode == "expand") + vs_boot_stretch_mode = RenderingServer::SPLASH_STRETCH_MODE_EXPAND; + + RenderingServer::get_singleton()->set_boot_image(boot_logo, boot_bg_color, vs_boot_stretch_mode, boot_logo_filter); } else { @@ -1675,7 +1697,7 @@ Error Main::setup2(Thread::ID p_main_tid_override) { MAIN_PRINT("Main: ClearColor"); RenderingServer::get_singleton()->set_default_clear_color(boot_bg_color); MAIN_PRINT("Main: Image"); - RenderingServer::get_singleton()->set_boot_image(splash, boot_bg_color, false); + RenderingServer::get_singleton()->set_boot_image(splash, boot_bg_color, RenderingServer::SPLASH_STRETCH_MODE_DISABLED); #endif } diff --git a/servers/rendering/rasterizer_dummy.h b/servers/rendering/rasterizer_dummy.h index 7821210284..f91538f09e 100644 --- a/servers/rendering/rasterizer_dummy.h +++ b/servers/rendering/rasterizer_dummy.h @@ -737,7 +737,7 @@ public: RendererCanvasRender *get_canvas() override { return &canvas; } RendererSceneRender *get_scene() override { return &scene; } - void set_boot_image(const Ref &p_image, const Color &p_color, bool p_scale, bool p_use_filter = true) override {} + void set_boot_image(const Ref &p_image, const Color &p_color, RenderingServer::SplashStretchMode p_stretch_mode, bool p_use_filter = true) override {} void initialize() override {} void begin_frame(double frame_step) override { diff --git a/servers/rendering/renderer_compositor.h b/servers/rendering/renderer_compositor.h index 1971c3e781..461f430ee4 100644 --- a/servers/rendering/renderer_compositor.h +++ b/servers/rendering/renderer_compositor.h @@ -75,7 +75,7 @@ public: virtual RendererCanvasRender *get_canvas() = 0; virtual RendererSceneRender *get_scene() = 0; - virtual void set_boot_image(const Ref &p_image, const Color &p_color, bool p_scale, bool p_use_filter = true) = 0; + virtual void set_boot_image(const Ref &p_image, const Color &p_color, RenderingServer::SplashStretchMode p_stretch_mode, bool p_use_filter = true) = 0; virtual void initialize() = 0; virtual void begin_frame(double frame_step) = 0; diff --git a/servers/rendering/renderer_rd/renderer_compositor_rd.cpp b/servers/rendering/renderer_rd/renderer_compositor_rd.cpp index 559e6d5ad7..1c5d6ebc65 100644 --- a/servers/rendering/renderer_rd/renderer_compositor_rd.cpp +++ b/servers/rendering/renderer_rd/renderer_compositor_rd.cpp @@ -159,7 +159,7 @@ void RendererCompositorRD::finalize() { RD::get_singleton()->free(blit.sampler); } -void RendererCompositorRD::set_boot_image(const Ref &p_image, const Color &p_color, bool p_scale, bool p_use_filter) { +void RendererCompositorRD::set_boot_image(const Ref &p_image, const Color &p_color, RenderingServer::SplashStretchMode p_stretch_mode, bool p_use_filter) { RD::get_singleton()->prepare_screen_for_drawing(); RID texture = storage->texture_allocate(); @@ -182,7 +182,35 @@ void RendererCompositorRD::set_boot_image(const Ref &p_image, const Color Rect2 imgrect(0, 0, p_image->get_width(), p_image->get_height()); Rect2 screenrect; - if (p_scale) { + if (p_stretch_mode == RenderingServer::SPLASH_STRETCH_MODE_KEEP_HEIGHT) { + //scale horizontally + screenrect.size.y = window_size.height; + screenrect.size.x = imgrect.size.x * window_size.height / imgrect.size.y; + screenrect.position.x = (window_size.width - screenrect.size.x) / 2; + } else if (p_stretch_mode == RenderingServer::SPLASH_STRETCH_MODE_KEEP_WIDTH) { + //scale vertically + screenrect.size.x = window_size.width; + screenrect.size.y = imgrect.size.y * window_size.width / imgrect.size.x; + screenrect.position.y = (window_size.height - screenrect.size.y) / 2; + } else if (p_stretch_mode == RenderingServer::SPLASH_STRETCH_MODE_COVER) { + double window_aspect = (double)window_size.width / window_size.height; + double img_aspect = imgrect.size.x / imgrect.size.y; + + if (window_aspect > img_aspect) { + //scale vertically + screenrect.size.x = window_size.width; + screenrect.size.y = imgrect.size.y * window_size.width / imgrect.size.x; + screenrect.position.y = (window_size.height - screenrect.size.y) / 2; + } else { + //scale horizontally + screenrect.size.y = window_size.height; + screenrect.size.x = imgrect.size.x * window_size.height / imgrect.size.y; + screenrect.position.x = (window_size.width - screenrect.size.x) / 2; + } + } else if (p_stretch_mode == RenderingServer::SPLASH_STRETCH_MODE_EXPAND) { + screenrect.size.x = window_size.width; + screenrect.size.y = window_size.height; + } else if (p_stretch_mode == RenderingServer::SPLASH_STRETCH_MODE_KEEP) { if (window_size.width > window_size.height) { //scale horizontally screenrect.size.y = window_size.height; diff --git a/servers/rendering/renderer_rd/renderer_compositor_rd.h b/servers/rendering/renderer_rd/renderer_compositor_rd.h index 0230c46800..2094f0e235 100644 --- a/servers/rendering/renderer_rd/renderer_compositor_rd.h +++ b/servers/rendering/renderer_rd/renderer_compositor_rd.h @@ -90,7 +90,7 @@ public: RendererCanvasRender *get_canvas() { return canvas; } RendererSceneRender *get_scene() { return scene; } - void set_boot_image(const Ref &p_image, const Color &p_color, bool p_scale, bool p_use_filter); + void set_boot_image(const Ref &p_image, const Color &p_color, RenderingServer::SplashStretchMode p_stretch_mode, bool p_use_filter); void initialize(); void begin_frame(double frame_step); diff --git a/servers/rendering/rendering_server_default.cpp b/servers/rendering/rendering_server_default.cpp index 107c9f8040..3ba81a2528 100644 --- a/servers/rendering/rendering_server_default.cpp +++ b/servers/rendering/rendering_server_default.cpp @@ -277,9 +277,9 @@ Vector RenderingServerDefault::get_frame_prof /* TESTING */ -void RenderingServerDefault::set_boot_image(const Ref &p_image, const Color &p_color, bool p_scale, bool p_use_filter) { +void RenderingServerDefault::set_boot_image(const Ref &p_image, const Color &p_color, RenderingServer::SplashStretchMode p_stretch_mode, bool p_use_filter) { redraw_request(); - RSG::rasterizer->set_boot_image(p_image, p_color, p_scale, p_use_filter); + RSG::rasterizer->set_boot_image(p_image, p_color, p_stretch_mode, p_use_filter); } void RenderingServerDefault::set_default_clear_color(const Color &p_color) { diff --git a/servers/rendering/rendering_server_default.h b/servers/rendering/rendering_server_default.h index 911d4c463b..ea89c5a072 100644 --- a/servers/rendering/rendering_server_default.h +++ b/servers/rendering/rendering_server_default.h @@ -894,7 +894,7 @@ public: virtual double get_frame_setup_time_cpu() const override; - virtual void set_boot_image(const Ref &p_image, const Color &p_color, bool p_scale, bool p_use_filter = true) override; + virtual void set_boot_image(const Ref &p_image, const Color &p_color, RenderingServer::SplashStretchMode p_stretch_mode, bool p_use_filter = true) override; virtual void set_default_clear_color(const Color &p_color) override; virtual bool has_feature(Features p_feature) const override; diff --git a/servers/rendering_server.cpp b/servers/rendering_server.cpp index 039dbc71e3..6e8c5d6d26 100644 --- a/servers/rendering_server.cpp +++ b/servers/rendering_server.cpp @@ -2694,7 +2694,7 @@ 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", "image", "color", "scale", "use_filter"), &RenderingServer::set_boot_image, DEFVAL(true)); + ClassDB::bind_method(D_METHOD("set_boot_image", "image", "color", "stretch_mode", "use_filter"), &RenderingServer::set_boot_image, DEFVAL(true)); ClassDB::bind_method(D_METHOD("set_default_clear_color", "color"), &RenderingServer::set_default_clear_color); ClassDB::bind_method(D_METHOD("has_feature", "feature"), &RenderingServer::has_feature); @@ -2718,6 +2718,13 @@ void RenderingServer::_bind_methods() { BIND_ENUM_CONSTANT(FEATURE_SHADERS); BIND_ENUM_CONSTANT(FEATURE_MULTITHREADED); + 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_EXPAND); + BIND_ENUM_CONSTANT(SPLASH_STRETCH_MODE_DISABLED); + ADD_SIGNAL(MethodInfo("frame_pre_draw")); ADD_SIGNAL(MethodInfo("frame_post_draw")); diff --git a/servers/rendering_server.h b/servers/rendering_server.h index b6068afcf1..6dd599a14c 100644 --- a/servers/rendering_server.h +++ b/servers/rendering_server.h @@ -1464,7 +1464,16 @@ 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 &p_planes); - virtual void set_boot_image(const Ref &p_image, const Color &p_color, bool p_scale, bool p_use_filter = true) = 0; + enum SplashStretchMode { + SPLASH_STRETCH_MODE_KEEP, + SPLASH_STRETCH_MODE_KEEP_WIDTH, + SPLASH_STRETCH_MODE_KEEP_HEIGHT, + SPLASH_STRETCH_MODE_COVER, + SPLASH_STRETCH_MODE_EXPAND, + SPLASH_STRETCH_MODE_DISABLED, + }; + + virtual void set_boot_image(const Ref &p_image, const Color &p_color, RenderingServer::SplashStretchMode p_stretch_mode, bool p_use_filter = true) = 0; virtual void set_default_clear_color(const Color &p_color) = 0; enum Features { @@ -1578,6 +1587,7 @@ VARIANT_ENUM_CAST(RenderingServer::RenderingInfo); VARIANT_ENUM_CAST(RenderingServer::Features); VARIANT_ENUM_CAST(RenderingServer::CanvasTextureChannel); VARIANT_ENUM_CAST(RenderingServer::BakeChannels); +VARIANT_ENUM_CAST(RenderingServer::SplashStretchMode); // Alias to make it easier to use. #define RS RenderingServer From 210dd4fbab490a52f3518f730b3b3ba68e9d5d52 Mon Sep 17 00:00:00 2001 From: Samuel Pedrajas Date: Mon, 25 Oct 2021 21:12:09 +0200 Subject: [PATCH 2/2] update 'application/boot_splash/stretch_mode' doc --- doc/classes/ProjectSettings.xml | 6 +++--- doc/classes/RenderingServer.xml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/classes/ProjectSettings.xml b/doc/classes/ProjectSettings.xml index 3e6d500d15..9c0151b059 100644 --- a/doc/classes/ProjectSettings.xml +++ b/doc/classes/ProjectSettings.xml @@ -198,12 +198,12 @@ If [code]true[/code], scale the boot splash image according to the mode selected in the stretch_mode property. If [code]false[/code], the engine will leave it at the default pixel size. - - Specifies how the splash image will be stretched. Takes no effect if fullsize is false. For more information about available stretch modes look for SPLASH_STRETCH_MODE* constants in the documentation. - Path to an image used as the boot splash. + + Specifies how the splash image will be stretched. Takes no effect if fullsize is false. For more information about available stretch modes look for SPLASH_STRETCH_MODE* constants in the documentation. + If [code]true[/code], applies linear filtering when scaling the image (recommended for high resolution artwork). If [code]false[/code], uses nearest-neighbor interpolation (recommended for pixel art). diff --git a/doc/classes/RenderingServer.xml b/doc/classes/RenderingServer.xml index 0e1aa8f633..23a5dd1253 100644 --- a/doc/classes/RenderingServer.xml +++ b/doc/classes/RenderingServer.xml @@ -2575,7 +2575,7 @@ - + Sets a boot image. The color defines the background color. The value of [code]stretch_mode[/code] indicates how the image will be stretched (for more information about stretch modes see SPLASH_STRETCH_MODE* constants). If [code]use_filter[/code] is [code]true[/code], the image will be scaled with linear interpolation. If [code]use_filter[/code] is [code]false[/code], the image will be scaled with nearest-neighbor interpolation.