Skip to content

Commit

Permalink
Introduce Time, Engine and Input as singletons in the C++ API
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Oct 24, 2024
1 parent 88da105 commit 7013679
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 65 deletions.
13 changes: 0 additions & 13 deletions program/cpp/api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,6 @@ extern "C" __attribute__((used, retain, noreturn)) void fast_exit() {
__builtin_unreachable();
}

// Various singletons
Object Engine::get_singleton() {
return Object("Engine");
}

Object input() {
return Object("Input");
}

Object Time::get_singleton() {
return Object("Time");
}

// ClassDB::instantiate
Object ClassDB::instantiate(std::string_view class_name, std::string_view name) {
return Object(sys_node_create(Node_Create_Shortlist::CREATE_CLASSDB, class_name.data(), class_name.size(), name.data(), name.size()));
Expand Down
47 changes: 0 additions & 47 deletions program/cpp/api/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,53 +129,6 @@ inline bool is_editor_hint() {
/// @return The loaded resource.
extern Variant load(std::string_view path);

struct Engine {
/// @brief Check if the program is running in the Godot editor.
/// @return True if running in the editor, false otherwise.
static bool is_editor_hint() {
return is_editor(); // Fast-path for the hint.
}

/// @brief Get the current time scale.
/// @return The current time scale.
static double get_time_scale() {
return get_singleton().call("get_time_scale");
}

/// @brief Set a new time scale.
/// @param scale The new time scale.
static void set_time_scale(double scale) {
get_singleton().call("set_time_scale", scale);
}

/// @brief Get the singleton instance of the Engine.
/// @return The Engine singleton.
static Object get_singleton();
};

/// @brief Get the singleton instance of the Input class.
/// @return The Input singleton.
extern Object input();

struct Time {
/// @brief Get the current time in milliseconds.
/// @return The current time in milliseconds.
static int64_t get_ticks_msec() {
return get_singleton().call("get_ticks_msec");
}

/// @brief Get the current time in microseconds.
/// @return The current time in microseconds.
static int64_t get_ticks_usec() {
return get_singleton().call("get_ticks_usec");
}

/// @brief Get the singleton instance of the Time class.
/// @return The Time singleton.
static Object get_singleton();
};


/// @brief The class database for instantiating Godot objects.
struct ClassDB {
/// @brief Instantiate a new object of the given class.
Expand Down
17 changes: 12 additions & 5 deletions src/sandbox_generated_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ String Sandbox::generate_api(String language, String header) {
return header + *current_generated_api;
}

static String emit_class(ClassDBSingleton *class_db, const HashSet<String> &cpp_keywords, const String &class_name) {
static String emit_class(ClassDBSingleton *class_db, const HashSet<String> &cpp_keywords, const HashSet<String> &singletons, const String &class_name) {
// Generate a simple API for each class using METHOD() and PROPERTY() macros to a string.
if constexpr (VERBOSE) {
UtilityFunctions::print("* Currently generating: " + class_name);
Expand Down Expand Up @@ -179,6 +179,10 @@ static String emit_class(ClassDBSingleton *class_db, const HashSet<String> &cpp_
api += String(" TYPED_METHOD(") + cpp_compatible_variant_type(type) + ", " + method_name + ");\n";
}

if (singletons.has(class_name)) {
api += " static " + class_name + " get_singleton() { return " + class_name + "(Object(\"" + class_name + "\").address()); }\n";
}

api += "};\n";
return api;
}
Expand Down Expand Up @@ -226,8 +230,11 @@ void Sandbox::generate_runtime_cpp_api() {
emitted_classes.insert("Node3D");
// Also skip some classes we simply don't want to expose.
emitted_classes.insert("ClassDB");
emitted_classes.insert("Engine");
emitted_classes.insert("Time");
// Finally, add singleton getters to certain classes.
HashSet<String> singletons;
singletons.insert("Engine");
singletons.insert("Time");
singletons.insert("Input");

// 3. Get all methods and properties for each class.
for (int i = 0; i < classes.size(); i++) {
Expand All @@ -251,7 +258,7 @@ void Sandbox::generate_runtime_cpp_api() {
continue;
}
// Emit the class.
*current_generated_api += emit_class(class_db, cpp_keywords, class_name);
*current_generated_api += emit_class(class_db, cpp_keywords, singletons, class_name);
emitted_classes.insert(class_name);
}

Expand All @@ -265,7 +272,7 @@ void Sandbox::generate_runtime_cpp_api() {
const TypedArray<String> &waiting = it->value;
for (int i = 0; i < waiting.size(); i++) {
String class_name = waiting[i];
*current_generated_api += emit_class(class_db, cpp_keywords, class_name);
*current_generated_api += emit_class(class_db, cpp_keywords, singletons, class_name);
emitted_classes.insert(class_name);
}
waiting_classes.erase(parent_name);
Expand Down

0 comments on commit 7013679

Please sign in to comment.