From 5fbb18dfd418f0ffc99d757f676504b9ed1aea71 Mon Sep 17 00:00:00 2001 From: Christoph Sommer Date: Sun, 1 Apr 2007 15:10:23 +0000 Subject: [PATCH] Exposed Tilemap alpha manipulation to squirrel and lisp SVN-Revision: 4956 --- src/object/tilemap.cpp | 29 +++++++++++-- src/object/tilemap.hpp | 12 +++++- src/scripting/tilemap.cpp | 15 +++++++ src/scripting/tilemap.hpp | 16 +++++++ src/scripting/wrapper.cpp | 106 ++++++++++++++++++++++++++++++++++++++++++++++ src/sector.cpp | 12 ++++++ 6 files changed, 186 insertions(+), 4 deletions(-) diff --git a/src/object/tilemap.cpp b/src/object/tilemap.cpp index e7d6367f2..576cf4867 100644 --- a/src/object/tilemap.cpp +++ b/src/object/tilemap.cpp @@ -85,6 +85,10 @@ TileMap::TileMap(const lisp::Lisp& reader, TileManager* new_tile_manager) if (draw_target_s == "normal") draw_target = DrawingContext::NORMAL; if (draw_target_s == "lightmap") draw_target = DrawingContext::LIGHTMAP; + if (reader.get("alpha", alpha)) { + current_alpha = alpha; + } + reader.get("width", width); reader.get("height", height); if(width < 0 || height < 0) @@ -148,7 +152,8 @@ TileMap::update(float elapsed_time) if (amt > 0) current_alpha = std::min(current_alpha + amt, alpha); if (amt < 0) current_alpha = std::max(current_alpha + amt, alpha); } - if (current_alpha < 0.25) set_solid(false); + if ((alpha < 0.25) && (current_alpha < 0.25)) set_solid(false); + if ((alpha > 0.75) && (current_alpha > 0.75)) set_solid(true); } // if we have a path to follow, follow it @@ -162,6 +167,9 @@ TileMap::update(float elapsed_time) void TileMap::draw(DrawingContext& context) { + // skip draw if current opacity is set to 0.0 + if (current_alpha == 0.0) return; + context.push_transform(); context.push_target(); context.set_target(draw_target); @@ -222,7 +230,6 @@ void TileMap::expose(HSQUIRRELVM vm, SQInteger table_idx) { if (name.empty()) return; - if (!walker.get()) return; Scripting::TileMap* interface = new Scripting::TileMap(this); expose_object(vm, table_idx, interface, name, true); } @@ -231,7 +238,6 @@ void TileMap::unexpose(HSQUIRRELVM vm, SQInteger table_idx) { if (name.empty()) return; - if (!walker.get()) return; Scripting::unexpose_object(vm, table_idx, name); } @@ -340,4 +346,21 @@ TileMap::fade(float alpha, float seconds) this->remaining_fade_time = seconds; } + +void +TileMap::set_alpha(float alpha) +{ + this->alpha = alpha; + this->current_alpha = alpha; + this->remaining_fade_time = 0; + if (current_alpha < 0.25) set_solid(false); + if (current_alpha > 0.75) set_solid(true); +} + +float +TileMap::get_alpha() +{ + return this->current_alpha; +} + IMPLEMENT_FACTORY(TileMap, "tilemap"); diff --git a/src/object/tilemap.hpp b/src/object/tilemap.hpp index 69a5b2ada..e80fb7335 100644 --- a/src/object/tilemap.hpp +++ b/src/object/tilemap.hpp @@ -134,10 +134,20 @@ public: /** * Start fading the tilemap to opacity given by @c alpha. - * Destination opacity will be reached after @c seconds seconds. + * Destination opacity will be reached after @c seconds seconds. Also influences solidity. */ void fade(float alpha, float seconds = 0); + /** + * Instantly switch tilemap's opacity to @c alpha. Also influences solidity. + */ + void set_alpha(float alpha); + + /** + * Return tilemap's opacity. Note that while the tilemap is fading in or out, this will return the current alpha value, not the target alpha. + */ + float get_alpha(); + private: typedef std::vector Tiles; Tiles tiles; diff --git a/src/scripting/tilemap.cpp b/src/scripting/tilemap.cpp index 581b31d29..6edf86a4f 100644 --- a/src/scripting/tilemap.cpp +++ b/src/scripting/tilemap.cpp @@ -52,4 +52,19 @@ namespace Scripting tilemap->stop_moving(); } + void TileMap::fade(float alpha, float seconds) + { + tilemap->fade(alpha, seconds); + } + + void TileMap::set_alpha(float alpha) + { + tilemap->set_alpha(alpha); + } + + float TileMap::get_alpha() + { + return tilemap->get_alpha(); + } + } diff --git a/src/scripting/tilemap.hpp b/src/scripting/tilemap.hpp index 5b2e3cd25..331580ed0 100644 --- a/src/scripting/tilemap.hpp +++ b/src/scripting/tilemap.hpp @@ -45,6 +45,22 @@ public: /** Stop tilemap at next node */ void stop_moving(); + /** + * Start fading the tilemap to opacity given by @c alpha. + * Destination opacity will be reached after @c seconds seconds. Also influences solidity. + */ + void fade(float alpha, float seconds); + + /** + * Instantly switch tilemap's opacity to @c alpha. Also influences solidity. + */ + void set_alpha(float alpha); + + /** + * Return tilemap's opacity. Note that while the tilemap is fading in or out, this will return the current alpha value, not the target alpha. + */ + float get_alpha(); + #ifndef SCRIPTING_API _TileMap* tilemap; #endif diff --git a/src/scripting/wrapper.cpp b/src/scripting/wrapper.cpp index c4a9e6148..216758891 100644 --- a/src/scripting/wrapper.cpp +++ b/src/scripting/wrapper.cpp @@ -2439,6 +2439,94 @@ static SQInteger TileMap_stop_moving_wrapper(HSQUIRRELVM vm) } +static SQInteger TileMap_fade_wrapper(HSQUIRRELVM vm) +{ + SQUserPointer data; + if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) { + sq_throwerror(vm, _SC("'fade' called without instance")); + return SQ_ERROR; + } + Scripting::TileMap* _this = reinterpret_cast (data); + SQFloat arg0; + if(SQ_FAILED(sq_getfloat(vm, 2, &arg0))) { + sq_throwerror(vm, _SC("Argument 1 not a float")); + return SQ_ERROR; + } + SQFloat arg1; + if(SQ_FAILED(sq_getfloat(vm, 3, &arg1))) { + sq_throwerror(vm, _SC("Argument 2 not a float")); + return SQ_ERROR; + } + + try { + _this->fade(static_cast (arg0), static_cast (arg1)); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'fade'")); + return SQ_ERROR; + } + +} + +static SQInteger TileMap_set_alpha_wrapper(HSQUIRRELVM vm) +{ + SQUserPointer data; + if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) { + sq_throwerror(vm, _SC("'set_alpha' called without instance")); + return SQ_ERROR; + } + Scripting::TileMap* _this = reinterpret_cast (data); + SQFloat arg0; + if(SQ_FAILED(sq_getfloat(vm, 2, &arg0))) { + sq_throwerror(vm, _SC("Argument 1 not a float")); + return SQ_ERROR; + } + + try { + _this->set_alpha(static_cast (arg0)); + + return 0; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'set_alpha'")); + return SQ_ERROR; + } + +} + +static SQInteger TileMap_get_alpha_wrapper(HSQUIRRELVM vm) +{ + SQUserPointer data; + if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) { + sq_throwerror(vm, _SC("'get_alpha' called without instance")); + return SQ_ERROR; + } + Scripting::TileMap* _this = reinterpret_cast (data); + + try { + float return_value = _this->get_alpha(); + + sq_pushfloat(vm, return_value); + return 1; + + } catch(std::exception& e) { + sq_throwerror(vm, e.what()); + return SQ_ERROR; + } catch(...) { + sq_throwerror(vm, _SC("Unexpected exception while executing function 'get_alpha'")); + return SQ_ERROR; + } + +} + static SQInteger SSector_release_hook(SQUserPointer ptr, SQInteger ) { Scripting::SSector* _this = reinterpret_cast (ptr); @@ -4579,6 +4667,24 @@ void register_supertux_wrapper(HSQUIRRELVM v) throw SquirrelError(v, "Couldn't register function 'stop_moving'"); } + sq_pushstring(v, "fade", -1); + sq_newclosure(v, &TileMap_fade_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'fade'"); + } + + sq_pushstring(v, "set_alpha", -1); + sq_newclosure(v, &TileMap_set_alpha_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'set_alpha'"); + } + + sq_pushstring(v, "get_alpha", -1); + sq_newclosure(v, &TileMap_get_alpha_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'get_alpha'"); + } + if(SQ_FAILED(sq_createslot(v, -3))) { throw SquirrelError(v, "Couldn't register class 'TileMap'"); } diff --git a/src/sector.cpp b/src/sector.cpp index 362f183de..5ced57b06 100644 --- a/src/sector.cpp +++ b/src/sector.cpp @@ -696,6 +696,18 @@ Sector::update_game_objects() gameobjects.push_back(object); } gameobjects_new.clear(); + + /* update solid_tilemaps list */ + //FIXME: this could be more efficient + solid_tilemaps.clear(); + for(std::vector::iterator i = gameobjects.begin(); + i != gameobjects.end(); ++i) + { + TileMap* tm = dynamic_cast(*i); + if (!tm) continue; + if (tm->is_solid()) solid_tilemaps.push_back(tm); + } + } bool -- 2.11.0