squirrel_scheduler.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "squirrel/squirrel_scheduler.hpp"
  17. #include <algorithm>
  18. #include "squirrel/squirrel_virtual_machine.hpp"
  19. #include "squirrel/squirrel_util.hpp"
  20. #include "supertux/level.hpp"
  21. #include "util/log.hpp"
  22. SquirrelScheduler::SquirrelScheduler(SquirrelVM& vm) :
  23. m_vm(vm),
  24. schedule()
  25. {
  26. }
  27. void
  28. SquirrelScheduler::update(float time)
  29. {
  30. while (!schedule.empty() && (schedule.front().wakeup_time < time ||
  31. (schedule.front().skippable &&
  32. Level::current() != nullptr &&
  33. Level::current()->m_skip_cutscene)
  34. )) {
  35. HSQOBJECT thread_ref = schedule.front().thread_ref;
  36. sq_pushobject(m_vm.get_vm(), thread_ref);
  37. sq_getweakrefval(m_vm.get_vm(), -1);
  38. HSQUIRRELVM scheduled_vm;
  39. if (sq_gettype(m_vm.get_vm(), -1) == OT_THREAD &&
  40. SQ_SUCCEEDED(sq_getthread(m_vm.get_vm(), -1, &scheduled_vm))) {
  41. if (SQ_FAILED(sq_wakeupvm(scheduled_vm, SQFalse, SQFalse, SQTrue, SQFalse))) {
  42. std::ostringstream msg;
  43. msg << "Error waking VM: ";
  44. sq_getlasterror(scheduled_vm);
  45. if (sq_gettype(scheduled_vm, -1) != OT_STRING) {
  46. msg << "(no info)";
  47. } else {
  48. const char* lasterr;
  49. sq_getstring(scheduled_vm, -1, &lasterr);
  50. msg << lasterr;
  51. }
  52. log_warning << msg.str() << std::endl;
  53. sq_pop(scheduled_vm, 1);
  54. }
  55. }
  56. sq_release(m_vm.get_vm(), &thread_ref);
  57. sq_pop(m_vm.get_vm(), 2);
  58. std::pop_heap(schedule.begin(), schedule.end());
  59. schedule.pop_back();
  60. }
  61. }
  62. void
  63. SquirrelScheduler::schedule_thread(HSQUIRRELVM scheduled_vm, float time, bool skippable)
  64. {
  65. // create a weakref to the VM
  66. sq_pushthread(m_vm.get_vm(), scheduled_vm);
  67. sq_weakref(m_vm.get_vm(), -1);
  68. ScheduleEntry entry;
  69. if (SQ_FAILED(sq_getstackobj(m_vm.get_vm(), -1, & entry.thread_ref))) {
  70. sq_pop(m_vm.get_vm(), 2);
  71. throw SquirrelError(m_vm.get_vm(), "Couldn't get thread weakref from vm");
  72. }
  73. entry.wakeup_time = time;
  74. entry.skippable = skippable;
  75. sq_addref(m_vm.get_vm(), & entry.thread_ref);
  76. sq_pop(m_vm.get_vm(), 2);
  77. schedule.push_back(entry);
  78. std::push_heap(schedule.begin(), schedule.end());
  79. }
  80. /* EOF */