Make it build with -DCOMPILE_AMALGATION=ON. Still not certain how intern_draw/next_po...
[supertux.git] / src / scripting / squirrel_util.hpp
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
17 #ifndef HEADER_SUPERTUX_SCRIPTING_SQUIRREL_UTIL_HPP
18 #define HEADER_SUPERTUX_SCRIPTING_SQUIRREL_UTIL_HPP
19
20 #include <sstream>
21
22 #include "scripting/squirrel_error.hpp"
23 #include "scripting/wrapper.hpp"
24
25 namespace scripting {
26
27 extern HSQUIRRELVM global_vm;
28
29 void init_squirrel(bool enable_debugger);
30 void exit_squirrel();
31 void update_debugger();
32
33 std::string squirrel2string(HSQUIRRELVM vm, SQInteger i);
34 void print_squirrel_stack(HSQUIRRELVM vm);
35
36 SQInteger squirrel_read_char(SQUserPointer file);
37
38 HSQOBJECT create_thread(HSQUIRRELVM vm);
39 SQObject vm_to_object(HSQUIRRELVM vm);
40 HSQUIRRELVM object_to_vm(HSQOBJECT object);
41
42 void compile_script(HSQUIRRELVM vm, std::istream& in,
43                     const std::string& sourcename);
44 void compile_and_run(HSQUIRRELVM vm, std::istream& in,
45                      const std::string& sourcename);
46
47 template<typename T>
48 void expose_object(HSQUIRRELVM v, SQInteger table_idx, T* object,
49                    const std::string& name, bool free = false)
50 {
51   sq_pushstring(v, name.c_str(), -1);
52   scripting::create_squirrel_instance(v, object, free);
53
54   if(table_idx < 0)
55     table_idx -= 2;
56
57   // register instance in root table
58   if(SQ_FAILED(sq_createslot(v, table_idx))) {
59     std::ostringstream msg;
60     msg << "Couldn't register object '" << name << "' in squirrel table";
61     throw scripting::SquirrelError(v, msg.str());
62   }
63 }
64
65 static inline void unexpose_object(HSQUIRRELVM v, SQInteger table_idx,
66                                    const std::string& name)
67 {
68   sq_pushstring(v, name.c_str(), name.length());
69
70   if(table_idx < 0)
71     table_idx -= 1;
72
73   if(SQ_FAILED(sq_deleteslot(v, table_idx, SQFalse))) {
74     std::ostringstream msg;
75     msg << "Couldn't unregister object '" << name << "' in squirrel root table";
76     throw scripting::SquirrelError(v, msg.str());
77   }
78 }
79
80 // begin serialization functions
81 void store_float(HSQUIRRELVM vm, const char* name, float val);
82 void store_int(HSQUIRRELVM vm, const char* name, int val);
83 void store_string(HSQUIRRELVM vm, const char* name, const std::string& val);
84 void store_bool(HSQUIRRELVM vm, const char* name, bool val);
85
86 bool has_float(HSQUIRRELVM vm, const char* name);
87 bool has_int(HSQUIRRELVM vm, const char* name);
88 bool has_string(HSQUIRRELVM vm, const char* name);
89 bool has_bool(HSQUIRRELVM vm, const char* name);
90
91 bool get_float(HSQUIRRELVM vm, const char* name, float& val);
92 bool get_int(HSQUIRRELVM vm, const char* name, int& val);
93 bool get_string(HSQUIRRELVM vm, const char* name, std::string& val);
94 bool get_bool(HSQUIRRELVM vm, const char* name, bool& val);
95
96 float read_float(HSQUIRRELVM vm, const char* name);
97 int read_int(HSQUIRRELVM vm, const char* name);
98 std::string read_string(HSQUIRRELVM vm, const char* name);
99 bool read_bool(HSQUIRRELVM vm, const char* name);
100 // end serialization functions
101
102 }
103
104 #endif
105
106 /* EOF */