squirrel_environment.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. // 2018 Ingo Ruhnke <grumbel@gmail.com>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #ifndef HEADER_SUPERTUX_SQUIRREL_SQUIRREL_ENVIRONMENT_HPP
  18. #define HEADER_SUPERTUX_SQUIRREL_SQUIRREL_ENVIRONMENT_HPP
  19. #include <string>
  20. #include <vector>
  21. #include <squirrel.h>
  22. #include "squirrel/squirrel_util.hpp"
  23. class GameObject;
  24. class ScriptInterface;
  25. class SquirrelVM;
  26. /** The SquirrelEnvironment contains the environment in which a script
  27. is executed, meaning a root table containing objects and
  28. variables. */
  29. class SquirrelEnvironment
  30. {
  31. public:
  32. SquirrelEnvironment(SquirrelVM& vm, const std::string& name);
  33. virtual ~SquirrelEnvironment();
  34. public:
  35. SquirrelVM& get_vm() const { return m_vm; }
  36. /** Expose this engine under 'name' */
  37. void expose_self();
  38. void unexpose_self();
  39. /** Expose the GameObject if it has a ScriptInterface, otherwise do
  40. nothing. */
  41. void try_expose(GameObject& object);
  42. void try_unexpose(GameObject& object);
  43. /** Generic expose function, T must be a type that has a
  44. create_squirrel_instance() associated with it. */
  45. template<typename T>
  46. void expose(const std::string& name, std::unique_ptr<T> script_object)
  47. {
  48. sq_pushobject(m_vm.get_vm(), m_table);
  49. expose_object(m_vm.get_vm(), -1, std::move(script_object), name);
  50. sq_pop(m_vm.get_vm(), 1);
  51. }
  52. void unexpose(const std::string& name);
  53. /** Convenience function that takes an std::string instead of an
  54. std::istream& */
  55. void run_script(const std::string& script, const std::string& sourcename);
  56. /** Runs a script in the context of the SquirrelEnvironment (m_table will
  57. be the roottable of this squirrel VM) and keeps a reference to
  58. the script so the script gets destroyed when the SquirrelEnvironment is
  59. destroyed). */
  60. void run_script(std::istream& in, const std::string& sourcename);
  61. void update(float dt_sec);
  62. void wait_for_seconds(HSQUIRRELVM vm, float seconds);
  63. void skippable_wait_for_seconds(HSQUIRRELVM vm, float seconds);
  64. private:
  65. void garbage_collect();
  66. private:
  67. SquirrelVM& m_vm;
  68. HSQOBJECT m_table;
  69. std::string m_name;
  70. SquirrelObjectList m_scripts;
  71. std::unique_ptr<SquirrelScheduler> m_scheduler;
  72. private:
  73. SquirrelEnvironment(const SquirrelEnvironment&) = delete;
  74. SquirrelEnvironment& operator=(const SquirrelEnvironment&) = delete;
  75. };
  76. #endif
  77. /* EOF */