X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fscripting%2Fwrapper.cpp;h=a90bb81b88d55d1673d9ce72ae0cb1cdc9fe9f83;hb=eca1813742e9f0fc71268a0a2c0071380e9b850d;hp=529d8897db21738b237e69527f06ea0fd024b67c;hpb=88fac7ee1f38465b6b3d2a9946962db1b1c41a94;p=supertux.git diff --git a/src/scripting/wrapper.cpp b/src/scripting/wrapper.cpp index 529d8897d..a90bb81b8 100644 --- a/src/scripting/wrapper.cpp +++ b/src/scripting/wrapper.cpp @@ -1060,9 +1060,10 @@ static SQInteger Player_add_bonus_wrapper(HSQUIRRELVM vm) } try { - _this->add_bonus(arg0); + bool return_value = _this->add_bonus(arg0); - return 0; + sq_pushbool(vm, return_value); + return 1; } catch(std::exception& e) { sq_throwerror(vm, e.what()); @@ -2092,6 +2093,157 @@ static SQInteger AmbientSound_get_pos_y_wrapper(HSQUIRRELVM vm) } +static SQInteger Thunderstorm_release_hook(SQUserPointer ptr, SQInteger ) +{ + Scripting::Thunderstorm* _this = reinterpret_cast (ptr); + delete _this; + return 0; +} + +static SQInteger Thunderstorm_start_wrapper(HSQUIRRELVM vm) +{ + SQUserPointer data; + if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) { + sq_throwerror(vm, _SC("'start' called without instance")); + return SQ_ERROR; + } + Scripting::Thunderstorm* _this = reinterpret_cast (data); + + try { + _this->start(); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'start'")); + return SQ_ERROR; + } + +} + +static SQInteger Thunderstorm_stop_wrapper(HSQUIRRELVM vm) +{ + SQUserPointer data; + if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) { + sq_throwerror(vm, _SC("'stop' called without instance")); + return SQ_ERROR; + } + Scripting::Thunderstorm* _this = reinterpret_cast (data); + + try { + _this->stop(); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'stop'")); + return SQ_ERROR; + } + +} + +static SQInteger Thunderstorm_thunder_wrapper(HSQUIRRELVM vm) +{ + SQUserPointer data; + if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) { + sq_throwerror(vm, _SC("'thunder' called without instance")); + return SQ_ERROR; + } + Scripting::Thunderstorm* _this = reinterpret_cast (data); + + try { + _this->thunder(); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'thunder'")); + return SQ_ERROR; + } + +} + +static SQInteger Thunderstorm_lightning_wrapper(HSQUIRRELVM vm) +{ + SQUserPointer data; + if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) { + sq_throwerror(vm, _SC("'lightning' called without instance")); + return SQ_ERROR; + } + Scripting::Thunderstorm* _this = reinterpret_cast (data); + + try { + _this->lightning(); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'lightning'")); + return SQ_ERROR; + } + +} + +static SQInteger Thunderstorm_flash_wrapper(HSQUIRRELVM vm) +{ + SQUserPointer data; + if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) { + sq_throwerror(vm, _SC("'flash' called without instance")); + return SQ_ERROR; + } + Scripting::Thunderstorm* _this = reinterpret_cast (data); + + try { + _this->flash(); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'flash'")); + return SQ_ERROR; + } + +} + +static SQInteger Thunderstorm_electrify_wrapper(HSQUIRRELVM vm) +{ + SQUserPointer data; + if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) { + sq_throwerror(vm, _SC("'electrify' called without instance")); + return SQ_ERROR; + } + Scripting::Thunderstorm* _this = reinterpret_cast (data); + + try { + _this->electrify(); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'electrify'")); + return SQ_ERROR; + } + +} + static SQInteger display_wrapper(HSQUIRRELVM vm) { return Scripting::display(vm); @@ -2968,6 +3120,32 @@ void create_squirrel_instance(HSQUIRRELVM v, Scripting::AmbientSound* object, bo sq_remove(v, -2); // remove root table } +void create_squirrel_instance(HSQUIRRELVM v, Scripting::Thunderstorm* object, bool setup_releasehook) +{ + using namespace Wrapper; + + sq_pushroottable(v); + sq_pushstring(v, "Thunderstorm", -1); + if(SQ_FAILED(sq_get(v, -2))) { + std::ostringstream msg; + msg << "Couldn't resolved squirrel type 'Thunderstorm'"; + throw SquirrelError(v, msg.str()); + } + + if(SQ_FAILED(sq_createinstance(v, -1)) || SQ_FAILED(sq_setinstanceup(v, -1, object))) { + std::ostringstream msg; + msg << "Couldn't setup squirrel instance for object of type 'Thunderstorm'"; + throw SquirrelError(v, msg.str()); + } + sq_remove(v, -2); // remove object name + + if(setup_releasehook) { + sq_setreleasehook(v, -1, Thunderstorm_release_hook); + } + + sq_remove(v, -2); // remove root table +} + void register_supertux_wrapper(HSQUIRRELVM v) { using namespace Wrapper; @@ -3753,6 +3931,53 @@ void register_supertux_wrapper(HSQUIRRELVM v) throw SquirrelError(v, "Couldn't register class 'AmbientSound'"); } + // Register class Thunderstorm + sq_pushstring(v, "Thunderstorm", -1); + if(sq_newclass(v, SQFalse) < 0) { + std::ostringstream msg; + msg << "Couldn't create new class 'Thunderstorm'"; + throw SquirrelError(v, msg.str()); + } + sq_pushstring(v, "start", -1); + sq_newclosure(v, &Thunderstorm_start_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'start'"); + } + + sq_pushstring(v, "stop", -1); + sq_newclosure(v, &Thunderstorm_stop_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'stop'"); + } + + sq_pushstring(v, "thunder", -1); + sq_newclosure(v, &Thunderstorm_thunder_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'thunder'"); + } + + sq_pushstring(v, "lightning", -1); + sq_newclosure(v, &Thunderstorm_lightning_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'lightning'"); + } + + sq_pushstring(v, "flash", -1); + sq_newclosure(v, &Thunderstorm_flash_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'flash'"); + } + + sq_pushstring(v, "electrify", -1); + sq_newclosure(v, &Thunderstorm_electrify_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'electrify'"); + } + + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register class 'Thunderstorm'"); + } + } } // end of namespace Scripting