godot_instance.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**************************************************************************/
  2. /* godot_instance.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 "godot_instance.h"
  31. #include "core/extension/gdextension_manager.h"
  32. #include "core/os/main_loop.h"
  33. #include "main/main.h"
  34. #include "servers/display/display_server.h"
  35. void GodotInstance::_bind_methods() {
  36. ClassDB::bind_method(D_METHOD("start"), &GodotInstance::start);
  37. ClassDB::bind_method(D_METHOD("is_started"), &GodotInstance::is_started);
  38. ClassDB::bind_method(D_METHOD("iteration"), &GodotInstance::iteration);
  39. ClassDB::bind_method(D_METHOD("focus_in"), &GodotInstance::focus_in);
  40. ClassDB::bind_method(D_METHOD("focus_out"), &GodotInstance::focus_out);
  41. ClassDB::bind_method(D_METHOD("pause"), &GodotInstance::pause);
  42. ClassDB::bind_method(D_METHOD("resume"), &GodotInstance::resume);
  43. }
  44. GodotInstance::GodotInstance() {
  45. }
  46. GodotInstance::~GodotInstance() {
  47. }
  48. bool GodotInstance::initialize(GDExtensionInitializationFunction p_init_func) {
  49. print_verbose("Godot Instance initialization");
  50. GDExtensionManager *gdextension_manager = GDExtensionManager::get_singleton();
  51. GDExtensionConstPtr<const GDExtensionInitializationFunction> ptr((const GDExtensionInitializationFunction *)&p_init_func);
  52. GDExtensionManager::LoadStatus status = gdextension_manager->load_extension_from_function("libgodot://main", ptr);
  53. return status == GDExtensionManager::LoadStatus::LOAD_STATUS_OK;
  54. }
  55. bool GodotInstance::start() {
  56. print_verbose("GodotInstance::start()");
  57. Error err = Main::setup2();
  58. if (err != OK) {
  59. return false;
  60. }
  61. started = Main::start() == EXIT_SUCCESS;
  62. if (started) {
  63. OS::get_singleton()->get_main_loop()->initialize();
  64. }
  65. return started;
  66. }
  67. bool GodotInstance::is_started() {
  68. return started;
  69. }
  70. bool GodotInstance::iteration() {
  71. DisplayServer::get_singleton()->process_events();
  72. return Main::iteration();
  73. }
  74. void GodotInstance::stop() {
  75. print_verbose("GodotInstance::stop()");
  76. if (started) {
  77. OS::get_singleton()->get_main_loop()->finalize();
  78. }
  79. started = false;
  80. }
  81. void GodotInstance::focus_out() {
  82. print_verbose("GodotInstance::focus_out()");
  83. if (started) {
  84. if (OS::get_singleton()->get_main_loop()) {
  85. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT);
  86. }
  87. }
  88. }
  89. void GodotInstance::focus_in() {
  90. print_verbose("GodotInstance::focus_in()");
  91. if (started) {
  92. if (OS::get_singleton()->get_main_loop()) {
  93. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN);
  94. }
  95. }
  96. }
  97. void GodotInstance::pause() {
  98. print_verbose("GodotInstance::pause()");
  99. if (started) {
  100. if (OS::get_singleton()->get_main_loop()) {
  101. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_PAUSED);
  102. }
  103. }
  104. }
  105. void GodotInstance::resume() {
  106. print_verbose("GodotInstance::resume()");
  107. if (started) {
  108. if (OS::get_singleton()->get_main_loop()) {
  109. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_RESUMED);
  110. }
  111. }
  112. }