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 Dec 12, 2024
1 parent 19e003b commit 9ad961c
Show file tree
Hide file tree
Showing 26 changed files with 181 additions and 31 deletions.
7 changes: 7 additions & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,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 @@ -1001,6 +1002,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_error("Build option `disable_3d=yes` cannot be used for editor builds, only for export template builds.")
Expand Down
31 changes: 29 additions & 2 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@
#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"
#include "servers/physics_server_2d_dummy.h"
#endif // _2D_DISABLED

#ifndef _3D_DISABLED
#include "servers/physics_server_3d.h"
Expand Down Expand Up @@ -162,9 +163,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 @@ -336,6 +339,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 @@ -353,6 +357,7 @@ void initialize_physics() {
// Should be impossible, but make sure it's not null.
ERR_FAIL_NULL_MSG(physics_server_2d, "Failed to initialize PhysicsServer2D.");
physics_server_2d->init();
#endif // _2D_DISABLED
}

void finalize_physics() {
Expand All @@ -361,8 +366,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 @@ -374,7 +381,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 @@ -388,6 +394,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 @@ -396,6 +404,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 @@ -404,10 +413,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 @@ -734,7 +745,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()`.
register_early_core_singletons();
Expand Down Expand Up @@ -2900,7 +2913,9 @@ Error Main::setup2(bool p_show_boot_logo) {
#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 @@ -3019,9 +3034,11 @@ Error Main::setup2(bool p_show_boot_logo) {
memdelete(physics_server_3d_manager);
}
#endif // _3D_DISABLED
#ifndef _2D_DISABLED
if (physics_server_2d_manager) {
memdelete(physics_server_2d_manager);
}
#endif // _2D_DISABLED

return err;
}
Expand Down Expand Up @@ -4368,7 +4385,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 @@ -4391,14 +4410,18 @@ bool Main::iteration() {
PhysicsServer3D::get_singleton()->flush_queries();
#endif // _3D_DISABLED

#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

Engine::get_singleton()->_in_physics = false;
exit = true;
Expand All @@ -4419,8 +4442,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 @@ -4699,9 +4724,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 @@ -202,12 +203,14 @@ double Performance::get_monitor(Monitor p_monitor) const {
return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_DRAW);
case PIPELINE_COMPILATIONS_SPECIALIZATION:
return RS::get_singleton()->get_rendering_info(RS::RENDERING_INFO_PIPELINE_COMPILATIONS_SPECIALIZATION);
#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 @@ -5938,10 +5938,12 @@ void GLTFDocument::_convert_csg_shape_to_gltf(CSGShape3D *p_current, GLTFNodeInd
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
2 changes: 1 addition & 1 deletion modules/godot_physics_2d/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def can_build(env, platform):
return True
return not env["disable_2d"]


def configure(env):
Expand Down
3 changes: 2 additions & 1 deletion modules/navigation/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,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 @@ -11,9 +11,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
20 changes: 14 additions & 6 deletions scene/debugger/scene_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,19 @@
#include "core/io/marshalls.h"
#include "core/object/script_language.h"
#include "core/templates/local_vector.h"
#include "scene/gui/popup_menu.h"
#include "scene/main/canvas_layer.h"
#include "scene/main/scene_tree.h"
#include "scene/main/window.h"
#include "scene/resources/packed_scene.h"
#include "scene/theme/theme_db.h"

#ifndef _2D_DISABLED
#include "scene/2d/physics/collision_object_2d.h"
#include "scene/2d/physics/collision_polygon_2d.h"
#include "scene/2d/physics/collision_shape_2d.h"
#endif // _2D_DISABLED

#ifndef _3D_DISABLED
#include "scene/3d/label_3d.h"
#include "scene/3d/mesh_instance_3d.h"
Expand All @@ -46,12 +56,6 @@
#include "scene/3d/sprite_3d.h"
#include "scene/resources/surface_tool.h"
#endif // _3D_DISABLED
#include "scene/gui/popup_menu.h"
#include "scene/main/canvas_layer.h"
#include "scene/main/scene_tree.h"
#include "scene/main/window.h"
#include "scene/resources/packed_scene.h"
#include "scene/theme/theme_db.h"

SceneDebugger::SceneDebugger() {
singleton = this;
Expand Down Expand Up @@ -1608,13 +1612,15 @@ void RuntimeNodeSelect::_update_selection() {
if (ci->_edit_use_rect()) {
rect = ci->_edit_get_rect();
} else {
#ifndef _2D_DISABLED
CollisionShape2D *collision_shape = Object::cast_to<CollisionShape2D>(ci);
if (collision_shape) {
Ref<Shape2D> shape = collision_shape->get_shape();
if (shape.is_valid()) {
rect = shape->get_rect();
}
}
#endif // _2D_DISABLED
}

RS::get_singleton()->canvas_item_set_visible(sbox_2d_ci, selection_visible);
Expand Down Expand Up @@ -1801,6 +1807,7 @@ void RuntimeNodeSelect::_find_canvas_items_at_pos(const Point2 &p_pos, Node *p_n
res.order = ci->get_effective_z_index() + ci->get_canvas_layer();
r_items.push_back(res);

#ifndef _2D_DISABLED
// If it's a shape, get the collision object it's from.
// FIXME: If the collision object has multiple shapes, only the topmost will be above it in the list.
if (Object::cast_to<CollisionShape2D>(ci) || Object::cast_to<CollisionPolygon2D>(ci)) {
Expand All @@ -1812,6 +1819,7 @@ void RuntimeNodeSelect::_find_canvas_items_at_pos(const Point2 &p_pos, Node *p_n
r_items.push_back(res_col);
}
}
#endif // _2D_DISABLED
}
}
}
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 @@ -31,7 +31,9 @@
#include "canvas_item.h"
#include "canvas_item.compat.inc"

#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/atlas_texture.h"
Expand Down Expand Up @@ -1538,13 +1540,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
Loading

0 comments on commit 9ad961c

Please sign in to comment.