commit 5c4e0c71
Moved common code to a function
Changed files
| M | src/saland/model/World.cpp before |
diff --git a/src/saland/model/World.cpp b/src/saland/model/World.cpp
index e38f2bd..2866d2b 100644
--- a/src/saland/model/World.cpp
+++ b/src/saland/model/World.cpp
@@ -27,6 +27,29 @@ https://github.com/sago007/saland
World::World() {
}
+/**
+ * Adds a static body to the world. Returns a pointer to the body
+ * @param world The world to use for the body
+ * @param x X coordiante for the top left corner in pixels
+ * @param y Y coordinate for the top left corner in pixels
+ * @param width Width of the rectangle in pixels
+ * @param hight Hight of the rectangle in pixels
+ * @return A pointer to the body. Valid until the world is destroyed or destroyBody is called.
+ */
+static b2Body* AddStaticRect(b2World* world, float x, float y, float width, float hight) {
+ b2BodyDef myBodyDef;
+ myBodyDef.type = b2_staticBody;
+ myBodyDef.position.Set(x/pixel2unit+width/pixel2unit/2.0, y/pixel2unit + hight/pixel2unit/2.0 );
+ myBodyDef.linearDamping = 1.0f;
+ b2Body* body = world->CreateBody(&myBodyDef);
+ b2PolygonShape polygonShape;
+ polygonShape.SetAsBox(width/pixel2unit/2.0, hight/pixel2unit/2.0);
+ b2FixtureDef myFixtureDef;
+ myFixtureDef.shape = &polygonShape;
+ body->CreateFixture(&myFixtureDef);
+ return body;
+}
+
static void AddStaticTilesToWorld(b2World* world, const sago::tiled::TileMap& tm, const sago::tiled::TileLayer& layer) {
for (int i = 0; i < tm.height; ++i) {
for (int j = 0; j < tm.width; ++j) {
@@ -34,16 +57,7 @@ static void AddStaticTilesToWorld(b2World* world, const sago::tiled::TileMap& tm
if (gid == 0) {
continue;
}
- b2BodyDef myBodyDef;
- myBodyDef.type = b2_staticBody;
- myBodyDef.position.Set(i*32.0f/pixel2unit+32.0f/pixel2unit/2.0, j*32.0f/pixel2unit + 32.0f/pixel2unit/2.0 );
- myBodyDef.linearDamping = 1.0f;
- b2Body* body = world->CreateBody(&myBodyDef);
- b2PolygonShape polygonShape;
- polygonShape.SetAsBox(32.0f/pixel2unit/2.0, 32.0f/pixel2unit/2.0);
- b2FixtureDef myFixtureDef;
- myFixtureDef.shape = &polygonShape;
- body->CreateFixture(&myFixtureDef);
+ AddStaticRect(world, i*32.0f, j*32.0f, 32.0f, 32.0f);
}
}
}
@@ -62,16 +76,7 @@ void World::init(std::shared_ptr<b2World>& world) {
continue;
}
if (item.x > 0 && item.y > 0 && item.width > 0 && item.height > 0) {
- b2BodyDef myBodyDef;
- myBodyDef.type = b2_staticBody;
- myBodyDef.position.Set(item.x/pixel2unit+item.width/pixel2unit/2.0, item.y/pixel2unit + item.height/pixel2unit/2.0 );
- myBodyDef.linearDamping = 1.0f;
- b2Body* body = physicsWorld->CreateBody(&myBodyDef);
- b2PolygonShape polygonShape;
- polygonShape.SetAsBox(item.width/pixel2unit/2.0, item.height/pixel2unit/2.0);
- b2FixtureDef myFixtureDef;
- myFixtureDef.shape = &polygonShape;
- body->CreateFixture(&myFixtureDef);
+ AddStaticRect(physicsWorld.get(), item.x, item.y, item.width, item.height);
}
}
}