2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #ifndef HEADER_SUPERTUX_SCRIPTING_SQUIRREL_UTIL_HPP
18 #define HEADER_SUPERTUX_SCRIPTING_SQUIRREL_UTIL_HPP
22 #include "scripting/squirrel_error.hpp"
23 #include "scripting/wrapper.hpp"
27 extern HSQUIRRELVM global_vm;
29 void init_squirrel(bool enable_debugger);
31 void update_debugger();
33 std::string squirrel2string(HSQUIRRELVM vm, SQInteger i);
34 void print_squirrel_stack(HSQUIRRELVM vm);
36 SQInteger squirrel_read_char(SQUserPointer file);
38 HSQOBJECT create_thread(HSQUIRRELVM vm);
39 SQObject vm_to_object(HSQUIRRELVM vm);
40 HSQUIRRELVM object_to_vm(HSQOBJECT object);
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);
48 void expose_object(HSQUIRRELVM v, SQInteger table_idx, T* object,
49 const std::string& name, bool free = false)
51 sq_pushstring(v, name.c_str(), -1);
52 scripting::create_squirrel_instance(v, object, free);
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());
65 static inline void unexpose_object(HSQUIRRELVM v, SQInteger table_idx,
66 const std::string& name)
68 sq_pushstring(v, name.c_str(), name.length());
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());
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);
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);
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);
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