add code to debug collision rectangles
authorMatthias Braun <matze@braunis.de>
Thu, 13 Apr 2006 15:00:11 +0000 (15:00 +0000)
committerMatthias Braun <matze@braunis.de>
Thu, 13 Apr 2006 15:00:11 +0000 (15:00 +0000)
SVN-Revision: 3326

src/badguy/yeti.cpp
src/scripting/functions.cpp
src/scripting/functions.hpp
src/scripting/wrapper.cpp
src/sector.cpp
src/sector.hpp
src/video/drawing_context.cpp
src/video/drawing_context.hpp

index f34a550..d66b55f 100644 (file)
@@ -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;
index aa08d28..d872740 100644 (file)
@@ -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)
index 83b03a0..eacb469 100644 (file)
@@ -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
index dd45026..7bbb4ea 100644 (file)
@@ -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) {
index 063e56b..506ff4b 100644 (file)
@@ -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();
 }
 
index d88bd45..3a4a1c9 100644 (file)
@@ -171,6 +171,9 @@ private:
   std::auto_ptr<ScriptManager> 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;                       
index fe8bf09..19ca657 100644 (file)
@@ -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
index dccd31b..8b7dcab 100644 (file)
@@ -29,6 +29,7 @@
 #include <memory>
 
 #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();