Skip to content

Commit

Permalink
Allow disabling 2D when compiling export templates
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronfranke committed Apr 13, 2024
1 parent 517b450 commit 021fe9c
Show file tree
Hide file tree
Showing 24 changed files with 156 additions and 25 deletions.
7 changes: 7 additions & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ opts.Add("object_prefix", "Custom prefix added to the base filename of all gener
opts.Add(BoolVariable("vsproj", "Generate a Visual Studio solution", False))
opts.Add("vsproj_name", "Name of the Visual Studio solution", "godot")
opts.Add("import_env_vars", "A comma-separated list of environment variables to copy from the outer environment.", "")
opts.Add(BoolVariable("disable_2d", "Disable 2D nodes for a smaller executable", False))
opts.Add(BoolVariable("disable_3d", "Disable 3D nodes for a smaller executable", False))
opts.Add(BoolVariable("disable_advanced_gui", "Disable advanced GUI nodes and behaviors", False))
opts.Add("build_profile", "Path to a file containing a feature build profile", "")
Expand Down Expand Up @@ -910,6 +911,12 @@ env["SHLIBSUFFIX"] = suffix + env["SHLIBSUFFIX"]
env["OBJPREFIX"] = env["object_prefix"]
env["SHOBJPREFIX"] = env["object_prefix"]

if env["disable_2d"]:
if env.editor_build:
print("Build option 'disable_2d=yes' cannot be used for editor builds, but only for export templates.")
Exit(255)
else:
env.Append(CPPDEFINES=["_2D_DISABLED"])
if env["disable_3d"]:
if env.editor_build:
print("Build option 'disable_3d=yes' cannot be used for editor builds, only for export template builds.")
Expand Down
29 changes: 27 additions & 2 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@
#include "servers/text/text_server_dummy.h"
#include "servers/text_server.h"

// 2D
#ifndef _2D_DISABLED
#include "servers/navigation_server_2d.h"
#include "servers/navigation_server_2d_dummy.h"
#include "servers/physics_server_2d.h"
#endif // _2D_DISABLED

#ifndef _3D_DISABLED
#include "servers/physics_server_3d.h"
Expand Down Expand Up @@ -150,9 +151,11 @@ static DisplayServer *display_server = nullptr;
static RenderingServer *rendering_server = nullptr;
static TextServerManager *tsman = nullptr;
static ThemeDB *theme_db = nullptr;
#ifndef _2D_DISABLED
static NavigationServer2D *navigation_server_2d = nullptr;
static PhysicsServer2DManager *physics_server_2d_manager = nullptr;
static PhysicsServer2D *physics_server_2d = nullptr;
#endif // _2D_DISABLED
static NavigationServer3D *navigation_server_3d = nullptr;
#ifndef _3D_DISABLED
static PhysicsServer3DManager *physics_server_3d_manager = nullptr;
Expand Down Expand Up @@ -314,6 +317,7 @@ void initialize_physics() {
physics_server_3d->init();
#endif // _3D_DISABLED

#ifndef _2D_DISABLED
// 2D Physics server
physics_server_2d = PhysicsServer2DManager::get_singleton()->new_server(
GLOBAL_GET(PhysicsServer2DManager::get_singleton()->setting_property_name));
Expand All @@ -323,6 +327,7 @@ void initialize_physics() {
}
ERR_FAIL_NULL(physics_server_2d);
physics_server_2d->init();
#endif // _2D_DISABLED
}

void finalize_physics() {
Expand All @@ -331,8 +336,10 @@ void finalize_physics() {
memdelete(physics_server_3d);
#endif // _3D_DISABLED

#ifndef _2D_DISABLED
physics_server_2d->finish();
memdelete(physics_server_2d);
#endif // _2D_DISABLED
}

void finalize_display() {
Expand All @@ -344,7 +351,6 @@ void finalize_display() {

void initialize_navigation_server() {
ERR_FAIL_COND(navigation_server_3d != nullptr);
ERR_FAIL_COND(navigation_server_2d != nullptr);

// Init 3D Navigation Server
navigation_server_3d = NavigationServer3DManager::new_default_server();
Expand All @@ -358,6 +364,8 @@ void initialize_navigation_server() {
ERR_FAIL_NULL_MSG(navigation_server_3d, "Failed to initialize NavigationServer3D.");
navigation_server_3d->init();

#ifndef _2D_DISABLED
ERR_FAIL_COND(navigation_server_2d != nullptr);
// Init 2D Navigation Server
navigation_server_2d = NavigationServer2DManager::new_default_server();
if (!navigation_server_2d) {
Expand All @@ -366,6 +374,7 @@ void initialize_navigation_server() {

ERR_FAIL_NULL_MSG(navigation_server_2d, "Failed to initialize NavigationServer2D.");
navigation_server_2d->init();
#endif // _2D_DISABLED
}

void finalize_navigation_server() {
Expand All @@ -374,10 +383,12 @@ void finalize_navigation_server() {
memdelete(navigation_server_3d);
navigation_server_3d = nullptr;

#ifndef _2D_DISABLED
ERR_FAIL_NULL(navigation_server_2d);
navigation_server_2d->finish();
memdelete(navigation_server_2d);
navigation_server_2d = nullptr;
#endif // _2D_DISABLED
}

void initialize_theme_db() {
Expand Down Expand Up @@ -687,7 +698,9 @@ Error Main::test_setup() {
#ifndef _3D_DISABLED
physics_server_3d_manager = memnew(PhysicsServer3DManager);
#endif // _3D_DISABLED
#ifndef _2D_DISABLED
physics_server_2d_manager = memnew(PhysicsServer2DManager);
#endif // _2D_DISABLED

// From `Main::setup2()`.
initialize_modules(MODULE_INITIALIZATION_LEVEL_CORE);
Expand Down Expand Up @@ -2605,7 +2618,9 @@ Error Main::setup2() {
#ifndef _3D_DISABLED
physics_server_3d_manager = memnew(PhysicsServer3DManager);
#endif // _3D_DISABLED
#ifndef _2D_DISABLED
physics_server_2d_manager = memnew(PhysicsServer2DManager);
#endif // _2D_DISABLED

register_server_types();
{
Expand Down Expand Up @@ -3948,7 +3963,9 @@ bool Main::iteration() {
XRServer::get_singleton()->_process();
#endif // _3D_DISABLED

#ifndef _2D_DISABLED
NavigationServer2D::get_singleton()->sync();
#endif // _2D_DISABLED
NavigationServer3D::get_singleton()->sync();

for (int iters = 0; iters < advance.physics_steps; ++iters) {
Expand All @@ -3970,14 +3987,18 @@ bool Main::iteration() {
// may be the same, and no interpolation takes place.
OS::get_singleton()->get_main_loop()->iteration_prepare();

#ifndef _2D_DISABLED
PhysicsServer2D::get_singleton()->sync();
PhysicsServer2D::get_singleton()->flush_queries();
#endif // _2D_DISABLED

if (OS::get_singleton()->get_main_loop()->physics_process(physics_step * time_scale)) {
#ifndef _3D_DISABLED
PhysicsServer3D::get_singleton()->end_sync();
#endif // _3D_DISABLED
#ifndef _2D_DISABLED
PhysicsServer2D::get_singleton()->end_sync();
#endif // _2D_DISABLED

exit = true;
break;
Expand All @@ -3997,8 +4018,10 @@ bool Main::iteration() {
PhysicsServer3D::get_singleton()->step(physics_step * time_scale);
#endif // _3D_DISABLED

#ifndef _2D_DISABLED
PhysicsServer2D::get_singleton()->end_sync();
PhysicsServer2D::get_singleton()->step(physics_step * time_scale);
#endif // _2D_DISABLED

message_queue->flush();

Expand Down Expand Up @@ -4255,9 +4278,11 @@ void Main::cleanup(bool p_force) {
memdelete(physics_server_3d_manager);
}
#endif // _3D_DISABLED
#ifndef _2D_DISABLED
if (physics_server_2d_manager) {
memdelete(physics_server_2d_manager);
}
#endif // _2D_DISABLED
if (globals) {
memdelete(globals);
}
Expand Down
5 changes: 4 additions & 1 deletion main/performance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
#include "servers/navigation_server_3d.h"
#include "servers/rendering_server.h"

// 2D
#ifndef _2D_DISABLED
#include "servers/physics_server_2d.h"
#endif // _2D_DISABLED

#ifndef _3D_DISABLED
#include "servers/physics_server_3d.h"
Expand Down Expand Up @@ -183,12 +184,14 @@ double Performance::get_monitor(Monitor p_monitor) const {
return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_TEXTURE_MEM_USED);
case RENDER_BUFFER_MEM_USED:
return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_BUFFER_MEM_USED);
#ifndef _2D_DISABLED
case PHYSICS_2D_ACTIVE_OBJECTS:
return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ACTIVE_OBJECTS);
case PHYSICS_2D_COLLISION_PAIRS:
return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_COLLISION_PAIRS);
case PHYSICS_2D_ISLAND_COUNT:
return PhysicsServer2D::get_singleton()->get_process_info(PhysicsServer2D::INFO_ISLAND_COUNT);
#endif // _2D_DISABLED
#ifdef _3D_DISABLED
case PHYSICS_3D_ACTIVE_OBJECTS:
return 0;
Expand Down
2 changes: 2 additions & 0 deletions modules/gltf/gltf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5342,10 +5342,12 @@ void GLTFDocument::_convert_animation_player_to_gltf(AnimationPlayer *p_animatio
void GLTFDocument::_check_visibility(Node *p_node, bool &r_retflag) {
r_retflag = true;
Node3D *spatial = Object::cast_to<Node3D>(p_node);
#ifndef _2D_DISABLED
Node2D *node_2d = Object::cast_to<Node2D>(p_node);
if (node_2d && !node_2d->is_visible()) {
return;
}
#endif // _2D_DISABLED
if (spatial && !spatial->is_visible()) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion modules/navigation/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ env.modules_sources += thirdparty_obj
module_obj = []

env_navigation.add_source_files(module_obj, "*.cpp")
env_navigation.add_source_files(module_obj, "2d/*.cpp")
if not env["disable_2d"]:
env_navigation.add_source_files(module_obj, "2d/*.cpp")
if not env["disable_3d"]:
env_navigation.add_source_files(module_obj, "3d/*.cpp")
if env.editor_build:
Expand Down
9 changes: 8 additions & 1 deletion modules/navigation/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@

#include "register_types.h"

#include "2d/godot_navigation_server_2d.h"
#include "3d/godot_navigation_server_3d.h"

#ifndef _2D_DISABLED
#include "2d/godot_navigation_server_2d.h"
#endif // _2D_DISABLED

#ifndef DISABLE_DEPRECATED
#ifndef _3D_DISABLED
#include "3d/navigation_mesh_generator.h"
Expand All @@ -57,14 +60,18 @@ NavigationServer3D *new_navigation_server_3d() {
return memnew(GodotNavigationServer3D);
}

#ifndef _2D_DISABLED
NavigationServer2D *new_navigation_server_2d() {
return memnew(GodotNavigationServer2D);
}
#endif // _2D_DISABLED

void initialize_navigation_module(ModuleInitializationLevel p_level) {
if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
NavigationServer3DManager::set_default_server(new_navigation_server_3d);
#ifndef _2D_DISABLED
NavigationServer2DManager::set_default_server(new_navigation_server_2d);
#endif // _2D_DISABLED

#ifndef DISABLE_DEPRECATED
#ifndef _3D_DISABLED
Expand Down
3 changes: 2 additions & 1 deletion scene/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ env.add_source_files(env.scene_sources, "*.cpp")
# Chain load SCsubs
SConscript("main/SCsub")
SConscript("gui/SCsub")
if not env["disable_2d"]:
SConscript("2d/SCsub")
if not env["disable_3d"]:
SConscript("3d/SCsub")
SConscript("2d/SCsub")
SConscript("animation/SCsub")
SConscript("audio/SCsub")
SConscript("resources/SCsub")
Expand Down
2 changes: 2 additions & 0 deletions scene/animation/animation_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
#define ANIMATION_PLAYER_H

#include "animation_mixer.h"
#ifndef _2D_DISABLED
#include "scene/2d/node_2d.h"
#endif // _2D_DISABLED
#include "scene/resources/animation.h"

class AnimationPlayer : public AnimationMixer {
Expand Down
5 changes: 5 additions & 0 deletions scene/main/canvas_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@

#include "canvas_item.h"

#ifndef _2D_DISABLED
#include "scene/2d/canvas_group.h"
#endif // _2D_DISABLED
#include "scene/main/canvas_layer.h"
#include "scene/main/window.h"
#include "scene/resources/canvas_item_material.h"
Expand Down Expand Up @@ -1496,13 +1498,16 @@ void CanvasItem::set_clip_children_mode(ClipChildrenMode p_clip_mode) {
}
clip_children_mode = p_clip_mode;

#ifndef _2D_DISABLED
if (Object::cast_to<CanvasGroup>(this) != nullptr) {
//avoid accidental bugs, make this not work on CanvasGroup
return;
}
#endif // _2D_DISABLED

RS::get_singleton()->canvas_item_set_canvas_group_mode(get_canvas_item(), RS::CanvasGroupMode(clip_children_mode));
}

CanvasItem::ClipChildrenMode CanvasItem::get_clip_children_mode() const {
ERR_READ_THREAD_GUARD_V(CLIP_CHILDREN_DISABLED);
return clip_children_mode;
Expand Down
12 changes: 9 additions & 3 deletions scene/main/scene_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@
#include "scene/scene_string_names.h"
#include "servers/display_server.h"
#include "servers/navigation_server_3d.h"
#include "window.h"

#ifndef _2D_DISABLED
#include "servers/physics_server_2d.h"
#endif // _2D_DISABLED

#ifndef _3D_DISABLED
#include "scene/resources/3d/world_3d.h"
#include "servers/physics_server_3d.h"
#endif // _3D_DISABLED
#include "window.h"
#include <stdio.h>
#include <stdlib.h>

void SceneTreeTimer::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_time_left", "time"), &SceneTreeTimer::set_time_left);
Expand Down Expand Up @@ -913,7 +915,9 @@ void SceneTree::set_pause(bool p_enabled) {
#ifndef _3D_DISABLED
PhysicsServer3D::get_singleton()->set_active(!p_enabled);
#endif // _3D_DISABLED
#ifndef _2D_DISABLED
PhysicsServer2D::get_singleton()->set_active(!p_enabled);
#endif // _2D_DISABLED
if (get_root()) {
get_root()->_propagate_pause_notification(p_enabled);
}
Expand Down Expand Up @@ -1781,7 +1785,9 @@ SceneTree::SceneTree() {
// Initialize network state.
set_multiplayer(MultiplayerAPI::create_default_interface());

#ifndef _2D_DISABLED
root->set_as_audio_listener_2d(true);
#endif // _2D_DISABLED
current_scene = nullptr;

const int msaa_mode_2d = GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "rendering/anti_aliasing/quality/msaa_2d", PROPERTY_HINT_ENUM, String::utf8("Disabled (Fastest),2× (Average),4× (Slow),8× (Slowest)")), 0);
Expand Down
Loading

0 comments on commit 021fe9c

Please sign in to comment.