moved some sprites to separate files
[supertux.git] / src / scripting / script_interpreter.hpp
1 #ifndef __SCRIPT_INTERPRETER_H__
2 #define __SCRIPT_INTERPRETER_H__
3
4 #include <squirrel.h>
5 #include <iostream>
6 #include <sstream>
7 #include <string>
8 #include "timer.hpp"
9 #include "game_object.hpp"
10 #include "scripting/wrapper.hpp"
11 #include "scripting/wrapper_util.hpp"
12 #include "scripting/sound.hpp"
13 #include "scripting/level.hpp"
14 #include "scripting/squirrel_error.hpp"
15
16 class Sector;
17
18 class ScriptInterpreter : public GameObject
19 {
20 public:
21   ScriptInterpreter(const std::string& working_dir);
22   ~ScriptInterpreter();
23
24   void register_sector(Sector* sector);
25
26   void draw(DrawingContext& );
27   void update(float );
28
29   void run_script(std::istream& in, const std::string& sourcename = "",
30           bool remove_when_terminated = true);
31
32   template<typename T>
33   void expose_object(T* object, const std::string& name, bool free = false)
34   {
35     sq_pushroottable(v);
36     sq_pushstring(v, name.c_str(), -1);
37
38     sq_pushroottable(v);
39     Scripting::create_squirrel_instance(v, object, free);
40     sq_remove(v, -2);
41                         
42     // register instance in root table
43     if(sq_createslot(v, -3) < 0) {
44       std::ostringstream msg;
45       msg << "Couldn't register object '" << name << "' in squirrel root table";
46       throw Scripting::SquirrelError(v, msg.str());
47     }
48     
49     sq_pop(v, 1);
50   }
51
52   void set_wakeup_time(float seconds);
53
54   /** helper function that parses a script, starts it and adds it to the sector
55    * specified
56    */
57   static void add_script_object(Sector* sector, const std::string& scriptname,
58       const std::string& script);
59
60   static ScriptInterpreter* current()
61   {
62     return _current;
63   }
64
65   const std::string& get_working_directory() const
66   {
67       return working_directory;
68   }
69
70 private:
71   HSQUIRRELVM v;
72   static ScriptInterpreter* _current;
73   Timer wakeup_timer;
74
75   /// this directory is used as base for all filenames used in scripts
76   std::string working_directory;
77   Scripting::Sound* sound;
78   Scripting::Level* level;
79 };
80
81 #endif
82