src/shape.cpp in reflexion-0.1.32 vs src/shape.cpp in reflexion-0.1.33

- old
+ new

@@ -1,14 +1,14 @@ #include "shape.h" -#include <Box2D/Dynamics/b2Body.h> -#include <Box2D/Dynamics/b2Fixture.h> -#include <Box2D/Collision/Shapes/b2CircleShape.h> -#include <Box2D/Collision/Shapes/b2EdgeShape.h> -#include <Box2D/Collision/Shapes/b2ChainShape.h> -#include <Box2D/Collision/Shapes/b2PolygonShape.h> +#include <box2d/b2_body.h> +#include <box2d/b2_fixture.h> +#include <box2d/b2_circle_shape.h> +#include <box2d/b2_edge_shape.h> +#include <box2d/b2_chain_shape.h> +#include <box2d/b2_polygon_shape.h> #include <rays/polygon.h> #include "reflex/exception.h" #include "reflex/debug.h" #include "selector.h" #include "view.h" @@ -207,13 +207,19 @@ for (const auto& point : polyline) buffer->emplace_back(to_b2vec2(point, ppm)); b2ChainShape b2shape; if (polyline.loop()) - b2shape.CreateLoop( &(*buffer)[0], (int32) buffer->size()); + b2shape.CreateLoop(&(*buffer)[0], (int32) buffer->size()); else - b2shape.CreateChain(&(*buffer)[0], (int32) buffer->size()); + { + b2shape.CreateChain( + &(*buffer)[0], + (int32) buffer->size(), + (*buffer)[0], + (*buffer)[buffer->size() - 1]); + } builder->add(&b2shape); } static Fixture* @@ -237,11 +243,11 @@ enum Flags { UPDATE_POLYGON = Xot::bit(0), - DEFAULT_FLAGS = 0 + DEFAULT_FLAGS = UPDATE_POLYGON };// Flags View* owner = NULL; @@ -267,11 +273,11 @@ if (pframe) *pframe = frame; else pframe.reset(new Bounds(frame)); - update_polygon_on_next_update(); + update_polygon_later(); } virtual Bounds get_frame () const { if (pframe) @@ -285,11 +291,11 @@ virtual bool has_frame () const { return (bool) pframe; } - void update_polygon_on_next_update () + void update_polygon_later () { if (!owner) return; Xot::add_flag(&flags, UPDATE_POLYGON); View_update_shapes(owner); @@ -297,10 +303,13 @@ void update_polygon (Shape* shape, bool force = false) { assert(shape); + bool update = Xot::check_and_remove_flag(&flags, UPDATE_POLYGON); + if (!update && !force) return; + if (!owner || !View_is_active(*owner)) polygon = Polygon(); else { Polygon poly = get_polygon_for_shape(); @@ -362,10 +371,14 @@ virtual Fixture* create_fixtures (Shape* shape) { if (!owner) invalid_state_error(__FILE__, __LINE__); + // if the view has been added to the window but on_update() has not yet + // been called, update_polygon() has also never been called. + update_polygon(shape); + Polygon polygon = get_polygon_for_fixtures(); if (!polygon || polygon.empty()) return NULL; float ppm = owner->meter2pixel(); @@ -422,15 +435,11 @@ Shape_update (Shape* shape, bool force) { if (!shape) argument_error(__FILE__, __LINE__); - bool update = Xot::check_and_remove_flag( - &shape->self->flags, Shape::Data::UPDATE_POLYGON); - - if (update || force) - shape->self->update_polygon(shape, force); + shape->self->update_polygon(shape, force); } void Shape_call_contact_event (Shape* shape, ContactEvent* event) { @@ -551,40 +560,10 @@ const Fixture* f = self->pfixtures.get(); return f ? f->is_sensor() : false; } void - Shape::set_category_bits (uint bits) - { - if (bits == category_bits()) return; - - self->fixtures(this).set_category_bits(bits); - } - - uint - Shape::category_bits () const - { - const Fixture* f = self->pfixtures.get(); - return f ? f->category_bits() : 0x1; - } - - void - Shape::set_collision_mask (uint mask) - { - if (mask == collision_mask()) return; - - self->fixtures(this).set_collision_mask(mask); - } - - uint - Shape::collision_mask () const - { - const Fixture* f = self->pfixtures.get(); - return f ? f->collision_mask() : 0xffff; - } - - void Shape::on_draw (DrawEvent* e) { assert(e && e->painter()); e->painter()->polygon(self->polygon); @@ -592,13 +571,19 @@ void Shape::on_resize (FrameEvent* e) { if (!self->has_frame()) - self->update_polygon_on_next_update(); + self->update_polygon_later(); } + bool + Shape::will_contact (Shape* s) + { + return true; + } + void Shape::on_contact (ContactEvent* e) { } @@ -693,11 +678,11 @@ void PolygonShape::set_polygon (const Polygon& polygon) { get_data(*this).polygon = polygon; - self->update_polygon_on_next_update(); + self->update_polygon_later(); } const Polygon& PolygonShape::polygon () const { @@ -769,20 +754,20 @@ void LineShape::add_point (const Point& point) { get_data(*this).points.emplace_back(point); - self->update_polygon_on_next_update(); + self->update_polygon_later(); } void LineShape::add_points (const Point* points, size_t size) { auto& array = get_data(*this).points; array.insert(array.end(), points, points + size); - self->update_polygon_on_next_update(); + self->update_polygon_later(); } void LineShape::set_loop (bool loop) { @@ -873,19 +858,19 @@ data.round_left_top = left_top; data.round_right_top = right_top; data.round_left_bottom = left_bottom; data.round_right_bottom = right_bottom; - self->update_polygon_on_next_update(); + self->update_polygon_later(); } void RectShape::set_round_left_top (coord round) { get_data(*this).round_left_top = round; - self->update_polygon_on_next_update(); + self->update_polygon_later(); } coord RectShape::round_left_top () const { @@ -895,11 +880,11 @@ void RectShape::set_round_right_top (coord round) { get_data(*this).round_right_top = round; - self->update_polygon_on_next_update(); + self->update_polygon_later(); } coord RectShape::round_right_top () const { @@ -909,11 +894,11 @@ void RectShape::set_round_left_bottom (coord round) { get_data(*this).round_left_bottom = round; - self->update_polygon_on_next_update(); + self->update_polygon_later(); } coord RectShape::round_left_bottom () const { @@ -923,11 +908,11 @@ void RectShape::set_round_right_bottom (coord round) { get_data(*this).round_right_bottom = round; - self->update_polygon_on_next_update(); + self->update_polygon_later(); } coord RectShape::round_right_bottom () const { @@ -937,11 +922,11 @@ void RectShape::set_nsegment (uint nsegment) { get_data(*this).nsegment = nsegment; - self->update_polygon_on_next_update(); + self->update_polygon_later(); } uint RectShape::nsegment () const { @@ -1052,11 +1037,11 @@ void EllipseShape::set_hole_size (coord width, coord height) { get_data(*this).hole_size.reset(width, height); - self->update_polygon_on_next_update(); + self->update_polygon_later(); } void EllipseShape::set_hole_size (const Point& hole_size) { @@ -1072,11 +1057,11 @@ void EllipseShape::set_angle_from (float degree) { get_data(*this).angle_from = degree; - self->update_polygon_on_next_update(); + self->update_polygon_later(); } float EllipseShape::angle_from () const { @@ -1086,11 +1071,11 @@ void EllipseShape::set_angle_to (float degree) { get_data(*this).angle_to = degree; - self->update_polygon_on_next_update(); + self->update_polygon_later(); } float EllipseShape::angle_to () const { @@ -1100,10 +1085,10 @@ void EllipseShape::set_nsegment (uint num_of_segments) { get_data(*this).nsegment = num_of_segments; - self->update_polygon_on_next_update(); + self->update_polygon_later(); } uint EllipseShape::nsegment () const {