From: Matthias Braun Date: Thu, 13 Apr 2006 15:00:11 +0000 (+0000) Subject: add code to debug collision rectangles X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=c0c4838b917943354c150d56ab970ca249267037;p=supertux.git add code to debug collision rectangles SVN-Revision: 3326 --- diff --git a/src/badguy/yeti.cpp b/src/badguy/yeti.cpp index f34a550fe..d66b55fc2 100644 --- a/src/badguy/yeti.cpp +++ b/src/badguy/yeti.cpp @@ -41,7 +41,7 @@ Yeti::Yeti(const lisp::Lisp& reader) { reader.get("x", start_position.x); reader.get("y", start_position.y); - bbox.set_size(80, 120); + bbox.set_size(90, 106); sprite = sprite_manager->create("images/creatures/yeti/yeti.sprite"); sprite->set_action("right"); state = INIT; diff --git a/src/scripting/functions.cpp b/src/scripting/functions.cpp index aa08d282c..d872740a1 100644 --- a/src/scripting/functions.cpp +++ b/src/scripting/functions.cpp @@ -36,6 +36,7 @@ #include "mainloop.hpp" #include "worldmap.hpp" #include "world.hpp" +#include "sector.hpp" #include "squirrel_error.hpp" #include "wrapper_util.hpp" @@ -124,6 +125,11 @@ void add_key(int new_key) player_status->set_keys(new_key); } +void debug_collrects(bool enable) +{ + Sector::show_collrects = enable; +} + void save_state() { if(World::current() == NULL) diff --git a/src/scripting/functions.hpp b/src/scripting/functions.hpp index 83b03a036..eacb469f9 100644 --- a/src/scripting/functions.hpp +++ b/src/scripting/functions.hpp @@ -95,6 +95,11 @@ void save_state(); */ void add_key(int new_key); +/** + * enable/disable drawing of collision rectangles + */ +void debug_collrects(bool enable); + } #endif diff --git a/src/scripting/wrapper.cpp b/src/scripting/wrapper.cpp index dd4502650..7bbb4ead0 100644 --- a/src/scripting/wrapper.cpp +++ b/src/scripting/wrapper.cpp @@ -1702,6 +1702,29 @@ static int add_key_wrapper(HSQUIRRELVM vm) } +static int debug_collrects_wrapper(HSQUIRRELVM vm) +{ + SQBool arg0; + if(SQ_FAILED(sq_getbool(vm, 2, &arg0))) { + sq_throwerror(vm, _SC("Argument 1 not a bool")); + return SQ_ERROR; + } + + try { + Scripting::debug_collrects(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 'debug_collrects'")); + return SQ_ERROR; + } + +} + } // end of namespace Wrapper void create_squirrel_instance(HSQUIRRELVM v, Scripting::DisplayEffect* object, bool setup_releasehook) @@ -2066,6 +2089,12 @@ void register_supertux_wrapper(HSQUIRRELVM v) throw SquirrelError(v, "Couldn't register function 'add_key'"); } + sq_pushstring(v, "debug_collrects", -1); + sq_newclosure(v, &debug_collrects_wrapper, 0); + if(SQ_FAILED(sq_createslot(v, -3))) { + throw SquirrelError(v, "Couldn't register function 'debug_collrects'"); + } + // Register class DisplayEffect sq_pushstring(v, "DisplayEffect", -1); if(sq_newclass(v, SQFalse) < 0) { diff --git a/src/sector.cpp b/src/sector.cpp index 063e56b83..506ff4b08 100644 --- a/src/sector.cpp +++ b/src/sector.cpp @@ -67,6 +67,8 @@ Sector* Sector::_current = 0; +bool Sector::show_collrects = false; + Sector::Sector() : currentmusic(LEVEL_MUSIC), gravity(10), player(0), solids(0), camera(0) @@ -702,6 +704,17 @@ Sector::draw(DrawingContext& context) object->draw(context); } + if(show_collrects) { + Color col(0.2, 0.2, 0.2, 0.7); + for(MovingObjects::iterator i = moving_objects.begin(); + i != moving_objects.end(); ++i) { + MovingObject* object = *i; + const Rect& rect = object->get_bbox(); + + context.draw_filled_rect(rect, col, LAYER_FOREGROUND1 + 10); + } + } + context.pop_transform(); } diff --git a/src/sector.hpp b/src/sector.hpp index d88bd4542..3a4a1c980 100644 --- a/src/sector.hpp +++ b/src/sector.hpp @@ -171,6 +171,9 @@ private: std::auto_ptr script_manager; public: // TODO make this private again + /// show collision rectangles of moving objects (for debugging) + static bool show_collrects; + GameObjects gameobjects; MovingObjects moving_objects; SpawnPoints spawnpoints; diff --git a/src/video/drawing_context.cpp b/src/video/drawing_context.cpp index fe8bf09c0..19ca65766 100644 --- a/src/video/drawing_context.cpp +++ b/src/video/drawing_context.cpp @@ -203,6 +203,28 @@ DrawingContext::draw_filled_rect(const Vector& topleft, const Vector& size, } void +DrawingContext::draw_filled_rect(const Rect& rect, const Color& color, + int layer) +{ + DrawingRequest request; + + request.type = FILLRECT; + request.pos = transform.apply(rect.p1); + request.layer = layer; + + request.drawing_effect = transform.drawing_effect; + request.alpha = transform.alpha; + + FillRectRequest* fillrectrequest = new FillRectRequest; + fillrectrequest->size = Vector(rect.get_width(), rect.get_height()); + fillrectrequest->color = color; + fillrectrequest->color.alpha = color.alpha * transform.alpha; + request.request_data = fillrectrequest; + + requests->push_back(request); +} + +void DrawingContext::draw_surface_part(DrawingRequest& request) { SurfacePartRequest* surfacepartrequest diff --git a/src/video/drawing_context.hpp b/src/video/drawing_context.hpp index dccd31bfc..8b7dcab63 100644 --- a/src/video/drawing_context.hpp +++ b/src/video/drawing_context.hpp @@ -29,6 +29,7 @@ #include #include "math/vector.hpp" +#include "math/rect.hpp" #include "surface.hpp" #include "font.hpp" #include "color.hpp" @@ -79,6 +80,7 @@ public: /// Fills a rectangle. void draw_filled_rect(const Vector& topleft, const Vector& size, const Color& color, int layer); + void draw_filled_rect(const Rect& rect, const Color& color, int layer); /// Processes all pending drawing requests and flushes the list. void do_drawing();