engine.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**************************************************************************/
  2. /* engine.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "engine.h"
  31. #include "core/authors.gen.h"
  32. #include "core/donors.gen.h"
  33. #include "core/license.gen.h"
  34. #include "core/version.h"
  35. void Engine::set_iterations_per_second(int p_ips) {
  36. ERR_FAIL_COND_MSG(p_ips <= 0, "Engine iterations per second must be greater than 0.");
  37. ips = p_ips;
  38. }
  39. int Engine::get_iterations_per_second() const {
  40. return ips;
  41. }
  42. void Engine::set_physics_jitter_fix(float p_threshold) {
  43. if (p_threshold < 0) {
  44. p_threshold = 0;
  45. }
  46. physics_jitter_fix = p_threshold;
  47. }
  48. float Engine::get_physics_jitter_fix() const {
  49. return physics_jitter_fix;
  50. }
  51. void Engine::set_target_fps(int p_fps) {
  52. _target_fps = p_fps > 0 ? p_fps : 0;
  53. }
  54. int Engine::get_target_fps() const {
  55. return _target_fps;
  56. }
  57. uint64_t Engine::get_frames_drawn() {
  58. return frames_drawn;
  59. }
  60. void Engine::set_frame_delay(uint32_t p_msec) {
  61. _frame_delay = p_msec;
  62. }
  63. uint32_t Engine::get_frame_delay() const {
  64. return _frame_delay;
  65. }
  66. void Engine::set_time_scale(float p_scale) {
  67. _time_scale = p_scale;
  68. }
  69. float Engine::get_time_scale() const {
  70. return _time_scale;
  71. }
  72. void Engine::set_portals_active(bool p_active) {
  73. _portals_active = p_active;
  74. }
  75. Dictionary Engine::get_version_info() const {
  76. Dictionary dict;
  77. dict["major"] = VERSION_MAJOR;
  78. dict["minor"] = VERSION_MINOR;
  79. dict["patch"] = VERSION_PATCH;
  80. dict["hex"] = VERSION_HEX;
  81. dict["status"] = VERSION_STATUS;
  82. dict["build"] = VERSION_BUILD;
  83. dict["year"] = VERSION_YEAR;
  84. String hash = String(VERSION_HASH);
  85. dict["hash"] = hash.empty() ? String("unknown") : hash;
  86. String stringver = String(dict["major"]) + "." + String(dict["minor"]);
  87. if ((int)dict["patch"] != 0) {
  88. stringver += "." + String(dict["patch"]);
  89. }
  90. stringver += "-" + String(dict["status"]) + " (" + String(dict["build"]) + ")";
  91. dict["string"] = stringver;
  92. return dict;
  93. }
  94. static Array array_from_info(const char *const *info_list) {
  95. Array arr;
  96. for (int i = 0; info_list[i] != nullptr; i++) {
  97. arr.push_back(String::utf8(info_list[i]));
  98. }
  99. return arr;
  100. }
  101. static Array array_from_info_count(const char *const *info_list, int info_count) {
  102. Array arr;
  103. for (int i = 0; i < info_count; i++) {
  104. arr.push_back(String::utf8(info_list[i]));
  105. }
  106. return arr;
  107. }
  108. Dictionary Engine::get_author_info() const {
  109. Dictionary dict;
  110. dict["lead_developers"] = array_from_info(AUTHORS_LEAD_DEVELOPERS);
  111. dict["project_managers"] = array_from_info(AUTHORS_PROJECT_MANAGERS);
  112. dict["founders"] = array_from_info(AUTHORS_FOUNDERS);
  113. dict["developers"] = array_from_info(AUTHORS_DEVELOPERS);
  114. return dict;
  115. }
  116. Array Engine::get_copyright_info() const {
  117. Array components;
  118. for (int component_index = 0; component_index < COPYRIGHT_INFO_COUNT; component_index++) {
  119. const ComponentCopyright &cp_info = COPYRIGHT_INFO[component_index];
  120. Dictionary component_dict;
  121. component_dict["name"] = String::utf8(cp_info.name);
  122. Array parts;
  123. for (int i = 0; i < cp_info.part_count; i++) {
  124. const ComponentCopyrightPart &cp_part = cp_info.parts[i];
  125. Dictionary part_dict;
  126. part_dict["files"] = array_from_info_count(cp_part.files, cp_part.file_count);
  127. part_dict["copyright"] = array_from_info_count(cp_part.copyright_statements, cp_part.copyright_count);
  128. part_dict["license"] = String::utf8(cp_part.license);
  129. parts.push_back(part_dict);
  130. }
  131. component_dict["parts"] = parts;
  132. components.push_back(component_dict);
  133. }
  134. return components;
  135. }
  136. Dictionary Engine::get_donor_info() const {
  137. Dictionary donors;
  138. donors["patrons"] = array_from_info(DONORS_PATRONS);
  139. donors["platinum_sponsors"] = array_from_info(DONORS_SPONSORS_PLATINUM);
  140. donors["gold_sponsors"] = array_from_info(DONORS_SPONSORS_GOLD);
  141. donors["silver_sponsors"] = array_from_info(DONORS_SPONSORS_SILVER);
  142. donors["diamond_members"] = array_from_info(DONORS_MEMBERS_DIAMOND);
  143. donors["titanium_members"] = array_from_info(DONORS_MEMBERS_TITANIUM);
  144. donors["platinum_members"] = array_from_info(DONORS_MEMBERS_PLATINUM);
  145. donors["gold_members"] = array_from_info(DONORS_MEMBERS_GOLD);
  146. return donors;
  147. }
  148. Dictionary Engine::get_license_info() const {
  149. Dictionary licenses;
  150. for (int i = 0; i < LICENSE_COUNT; i++) {
  151. licenses[LICENSE_NAMES[i]] = LICENSE_BODIES[i];
  152. }
  153. return licenses;
  154. }
  155. String Engine::get_license_text() const {
  156. return String(GODOT_LICENSE_TEXT);
  157. }
  158. void Engine::set_print_error_messages(bool p_enabled) {
  159. _print_error_enabled = p_enabled;
  160. }
  161. bool Engine::is_printing_error_messages() const {
  162. return _print_error_enabled;
  163. }
  164. void Engine::add_singleton(const Singleton &p_singleton) {
  165. singletons.push_back(p_singleton);
  166. singleton_ptrs[p_singleton.name] = p_singleton.ptr;
  167. }
  168. Object *Engine::get_singleton_object(const String &p_name) const {
  169. const Map<StringName, Object *>::Element *E = singleton_ptrs.find(p_name);
  170. ERR_FAIL_COND_V_MSG(!E, nullptr, "Failed to retrieve non-existent singleton '" + p_name + "'.");
  171. return E->get();
  172. };
  173. bool Engine::has_singleton(const String &p_name) const {
  174. return singleton_ptrs.has(p_name);
  175. };
  176. void Engine::get_singletons(List<Singleton> *p_singletons) {
  177. for (List<Singleton>::Element *E = singletons.front(); E; E = E->next()) {
  178. p_singletons->push_back(E->get());
  179. }
  180. }
  181. Engine *Engine::singleton = nullptr;
  182. Engine *Engine::get_singleton() {
  183. return singleton;
  184. }
  185. Engine::Engine() {
  186. singleton = this;
  187. frames_drawn = 0;
  188. ips = 60;
  189. physics_jitter_fix = 0.5;
  190. _physics_interpolation_fraction = 0.0f;
  191. _frame_delay = 0;
  192. _fps = 1;
  193. _target_fps = 0;
  194. _time_scale = 1.0;
  195. _gpu_pixel_snap = false;
  196. _physics_frames = 0;
  197. _idle_frames = 0;
  198. _in_physics = false;
  199. _frame_ticks = 0;
  200. _frame_step = 0;
  201. editor_hint = false;
  202. _portals_active = false;
  203. }
  204. Engine::Singleton::Singleton(const StringName &p_name, Object *p_ptr) :
  205. name(p_name),
  206. ptr(p_ptr) {
  207. #ifdef DEBUG_ENABLED
  208. Reference *ref = Object::cast_to<Reference>(p_ptr);
  209. if (ref && !ref->is_referenced()) {
  210. WARN_PRINT("You must use Ref<> to ensure the lifetime of a Reference object intended to be used as a singleton.");
  211. }
  212. #endif
  213. }