main_loop.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*************************************************************************/
  2. /* main_loop.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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 "main_loop.h"
  31. #include "core/object/script_language.h"
  32. void MainLoop::_bind_methods() {
  33. BIND_VMETHOD(MethodInfo("_initialize"));
  34. BIND_VMETHOD(MethodInfo(Variant::BOOL, "_iteration", PropertyInfo(Variant::FLOAT, "delta")));
  35. BIND_VMETHOD(MethodInfo(Variant::BOOL, "_idle", PropertyInfo(Variant::FLOAT, "delta")));
  36. BIND_VMETHOD(MethodInfo("_finalize"));
  37. BIND_CONSTANT(NOTIFICATION_OS_MEMORY_WARNING);
  38. BIND_CONSTANT(NOTIFICATION_TRANSLATION_CHANGED);
  39. BIND_CONSTANT(NOTIFICATION_WM_ABOUT);
  40. BIND_CONSTANT(NOTIFICATION_CRASH);
  41. BIND_CONSTANT(NOTIFICATION_OS_IME_UPDATE);
  42. BIND_CONSTANT(NOTIFICATION_APPLICATION_RESUMED);
  43. BIND_CONSTANT(NOTIFICATION_APPLICATION_PAUSED);
  44. BIND_CONSTANT(NOTIFICATION_APPLICATION_FOCUS_IN);
  45. BIND_CONSTANT(NOTIFICATION_APPLICATION_FOCUS_OUT);
  46. BIND_CONSTANT(NOTIFICATION_TEXT_SERVER_CHANGED);
  47. ADD_SIGNAL(MethodInfo("on_request_permissions_result", PropertyInfo(Variant::STRING, "permission"), PropertyInfo(Variant::BOOL, "granted")));
  48. };
  49. void MainLoop::set_init_script(const Ref<Script> &p_init_script) {
  50. init_script = p_init_script;
  51. }
  52. void MainLoop::init() {
  53. if (init_script.is_valid()) {
  54. set_script(init_script);
  55. }
  56. if (get_script_instance()) {
  57. get_script_instance()->call("_initialize");
  58. }
  59. }
  60. bool MainLoop::iteration(float p_time) {
  61. if (get_script_instance()) {
  62. return get_script_instance()->call("_iteration", p_time);
  63. }
  64. return false;
  65. }
  66. bool MainLoop::idle(float p_time) {
  67. if (get_script_instance()) {
  68. return get_script_instance()->call("_idle", p_time);
  69. }
  70. return false;
  71. }
  72. void MainLoop::finish() {
  73. if (get_script_instance()) {
  74. get_script_instance()->call("_finalize");
  75. set_script(Variant()); //clear script
  76. }
  77. }