Replaced Ref and RefCounter with std::shared_ptr<>
authorIngo Ruhnke <grumbel@gmail.com>
Mon, 1 Sep 2014 19:37:16 +0000 (21:37 +0200)
committerIngo Ruhnke <grumbel@gmail.com>
Wed, 1 Oct 2014 16:41:56 +0000 (18:41 +0200)
70 files changed:
src/badguy/bomb.cpp
src/badguy/darttrap.cpp
src/badguy/dispenser.cpp
src/badguy/flame.cpp
src/badguy/flyingsnowball.cpp
src/badguy/ghosttree.cpp
src/badguy/ghosttree.hpp
src/badguy/goldbomb.cpp
src/badguy/haywire.cpp
src/badguy/iceflame.cpp
src/badguy/igel.cpp
src/badguy/livefire.cpp
src/badguy/mole.cpp
src/badguy/mrbomb.cpp
src/badguy/mrtree.cpp
src/badguy/owl.cpp
src/badguy/poisonivy.cpp
src/badguy/short_fuse.cpp
src/badguy/skydive.cpp
src/badguy/snowman.cpp
src/badguy/stumpy.cpp
src/badguy/yeti.cpp
src/control/joystick_manager.cpp
src/object/bicycle_platform.cpp
src/object/block.cpp
src/object/bonus_block.cpp
src/object/bonus_block.hpp
src/object/brick.cpp
src/object/candle.cpp
src/object/coin.cpp
src/object/coin_explode.cpp
src/object/coin_rain.cpp
src/object/endsequence_fireworks.cpp
src/object/firefly.cpp
src/object/fireworks.cpp
src/object/icecrusher.cpp
src/object/infoblock.cpp
src/object/player.cpp
src/object/pneumatic_platform.cpp
src/object/portable.hpp
src/object/powerup.cpp
src/object/rain_particle_system.cpp
src/object/specialriser.cpp
src/object/specialriser.hpp
src/object/star.cpp
src/object/thunderstorm.cpp
src/object/weak_block.cpp
src/object/wind.cpp
src/scripting/floating_image.cpp
src/scripting/floating_image.hpp
src/scripting/scripting.cpp
src/scripting/scripting.hpp
src/supertux/command_line_arguments.cpp
src/supertux/flip_level_transformer.cpp
src/supertux/flip_level_transformer.hpp
src/supertux/game_object.cpp
src/supertux/game_object.hpp
src/supertux/game_object_ptr.hpp [new file with mode: 0644]
src/supertux/game_session.cpp
src/supertux/game_session.hpp
src/supertux/level.cpp
src/supertux/object_factory.cpp
src/supertux/object_factory.hpp
src/supertux/sector.cpp
src/supertux/sector.hpp
src/trigger/secretarea_trigger.cpp
src/util/ref.hpp [deleted file]
src/util/refcounter.hpp [deleted file]
src/worldmap/worldmap.cpp
src/worldmap/worldmap.hpp

index 629b75b..86ef0e7 100644 (file)
@@ -90,7 +90,7 @@ Bomb::explode()
 
   if(is_valid()) {
     remove_me();
-    Explosion* explosion = new Explosion(get_bbox().get_middle());
+    auto explosion = std::make_shared<Explosion>(get_bbox().get_middle());
     Sector::current()->add_object(explosion);
   }
 
index 3885cb6..1fb822e 100644 (file)
@@ -97,7 +97,7 @@ DartTrap::fire()
   py += MUZZLE_Y;
 
   SoundManager::current()->play("sounds/dartfire.wav", get_pos());
-  Sector::current()->add_object(new Dart(Vector(px, py), dir, this));
+  Sector::current()->add_object(std::make_shared<Dart>(Vector(px, py), dir, this));
   state = IDLE;
   sprite->set_action(dir == LEFT ? "idle-left" : "idle-right");
 }
index e3deca0..1f48673 100644 (file)
@@ -205,8 +205,7 @@ Dispenser::launch_badguy()
     }
 
     try {
-      GameObject *game_object;
-      BadGuy *bad_guy;
+      GameObjectPtr game_object;
       Vector spawnpoint;
       Rectf object_bbox;
 
@@ -215,33 +214,33 @@ Dispenser::launch_badguy()
       if (game_object == NULL)
         throw std::runtime_error("Creating " + badguy + " object failed.");
 
-      bad_guy = dynamic_cast<BadGuy *> (game_object);
-      if (bad_guy == NULL)
-        throw std::runtime_error(badguy + " is not a badguy.");
+      BadGuy& bad_guy = dynamic_cast<BadGuy&>(*game_object);
 
-      object_bbox = bad_guy->get_bbox ();
+      object_bbox = bad_guy.get_bbox();
 
-      if (type == "dropper") {
-        spawnpoint = get_anchor_pos (get_bbox (), ANCHOR_BOTTOM);
-        spawnpoint.x -= 0.5 * object_bbox.get_width ();
+      if (type == "dropper")
+      {
+        spawnpoint = get_anchor_pos (get_bbox(), ANCHOR_BOTTOM);
+        spawnpoint.x -= 0.5 * object_bbox.get_width();
       }
-      else if ((type == "cannon") || (type == "rocketlauncher")) {
-        spawnpoint = get_pos (); /* top-left corner of the cannon */
+      else if ((type == "cannon") || (type == "rocketlauncher"))
+      {
+        spawnpoint = get_pos(); /* top-left corner of the cannon */
         if (launchdir == LEFT)
-          spawnpoint.x -= object_bbox.get_width () + 1;
+          spawnpoint.x -= object_bbox.get_width() + 1;
         else
-          spawnpoint.x += get_bbox ().get_width () + 1;
+          spawnpoint.x += get_bbox().get_width() + 1;
       }
 
       /* Now we set the real spawn position */
-      bad_guy->set_pos (spawnpoint);
+      bad_guy.set_pos(spawnpoint);
 
       /* We don't want to count dispensed badguys in level stats */
-      if(bad_guy->countMe)
-        bad_guy->countMe = false;
+      if(bad_guy.countMe)
+        bad_guy.countMe = false;
 
-      Sector::current()->add_object(bad_guy);
-    } catch(std::exception& e) {
+      Sector::current()->add_object(game_object);
+    } catch(const std::exception& e) {
       log_warning << "Error dispensing badguy: " << e.what() << std::endl;
       return;
     }
index 66ce6d3..d0406ea 100644 (file)
@@ -110,7 +110,10 @@ Flame::freeze()
 {
   SoundManager::current()->play("sounds/sizzle.ogg", get_pos());
   sprite->set_action("fade", 1);
-  Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", bbox.get_middle(), ANCHOR_MIDDLE, Vector(0, -150), Vector(0,0), LAYER_BACKGROUNDTILES+2));
+  Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/smoke.sprite",
+                                                                 "default",
+                                                                 bbox.get_middle(), ANCHOR_MIDDLE,
+                                                                 Vector(0, -150), Vector(0,0), LAYER_BACKGROUNDTILES+2));
   set_group(COLGROUP_DISABLED);
 
   // start dead-script
index 1102e9a..577078b 100644 (file)
@@ -113,7 +113,10 @@ FlyingSnowBall::active_update(float elapsed_time)
     Vector ppos = bbox.get_middle();
     Vector pspeed = Vector(gameRandom.randf(-10, 10), 150);
     Vector paccel = Vector(0,0);
-    Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
+    Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/smoke.sprite",
+                                                                   "default",
+                                                                   ppos, ANCHOR_MIDDLE, pspeed, paccel,
+                                                                   LAYER_OBJECTS-1));
     puff_timer.start(gameRandom.randf(PUFF_INTERVAL_MIN, PUFF_INTERVAL_MAX));
 
     normal_propeller_speed = gameRandom.randf(0.95, 1.05);
index 4e8832b..088d73b 100644 (file)
@@ -70,10 +70,9 @@ GhostTree::die()
   sprite->set_action("dying", 1);
   glow_sprite->set_action("dying", 1);
 
-  std::vector<TreeWillOWisp*>::iterator iter;
-  for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
-    TreeWillOWisp *willo = *iter;
-    willo->vanish();
+  for(auto iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
+    TreeWillOWisp& willo = **iter;
+    willo.vanish();
   }
   run_dead_script();
 }
@@ -113,11 +112,10 @@ GhostTree::active_update(float elapsed_time)
     if(suck_timer.check()) {
       Color col = glow_sprite->get_color();
       SoundManager::current()->play("sounds/tree_suck.ogg", get_pos());
-      std::vector<TreeWillOWisp*>::iterator iter;
-      for(iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
-        TreeWillOWisp *willo = *iter;
-        if(willo->get_color() == col) {
-          willo->start_sucking(get_bbox().get_middle() + SUCK_TARGET_OFFSET + Vector(gameRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD), gameRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD)));
+      for(auto iter = willowisps.begin(); iter != willowisps.end(); ++iter) {
+        TreeWillOWisp& willo = **iter;
+        if(willo.get_color() == col) {
+          willo.start_sucking(get_bbox().get_middle() + SUCK_TARGET_OFFSET + Vector(gameRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD), gameRandom.randf(-SUCK_TARGET_SPREAD, SUCK_TARGET_SPREAD)));
         }
       }
       mystate = STATE_SUCKING;
@@ -126,8 +124,7 @@ GhostTree::active_update(float elapsed_time)
     if(willowisp_timer.check()) {
       if(willowisps.size() < WILLOWISP_COUNT) {
         Vector pos = Vector(bbox.get_width() / 2, bbox.get_height() / 2 + willo_spawn_y + WILLOWISP_TOP_OFFSET);
-        TreeWillOWisp *willowisp
-          = new TreeWillOWisp(this, pos, 200 + willo_radius, willo_speed);
+        auto willowisp = std::make_shared<TreeWillOWisp>(this, pos, 200 + willo_radius, willo_speed);
 
         Sector::current()->add_object(willowisp);
         willowisps.push_back(willowisp);
@@ -166,7 +163,7 @@ GhostTree::active_update(float elapsed_time)
       /* TODO indicate root with an animation */
       Player* player = get_nearest_player();
       if (player) {
-        Root* root = new Root(Vector(player->get_bbox().get_left(), get_bbox().get_bottom()+ROOT_TOP_OFFSET));
+        auto root = std::make_shared<Root>(Vector(player->get_bbox().get_left(), get_bbox().get_bottom()+ROOT_TOP_OFFSET));
         Sector::current()->add_object(root);
       }
     }
@@ -215,7 +212,11 @@ GhostTree::willowisp_died(TreeWillOWisp *willowisp)
   if ((mystate == STATE_SUCKING) && (willowisp->was_sucked)) {
     mystate = STATE_IDLE;
   }
-  willowisps.erase(std::find(willowisps.begin(), willowisps.end(), willowisp));
+  willowisps.erase(std::find_if(willowisps.begin(), willowisps.end(),
+                                [willowisp](const std::shared_ptr<TreeWillOWisp>& lhs)
+                                {
+                                  return lhs.get() == willowisp;
+                                }));
 }
 
 void
@@ -266,7 +267,7 @@ GhostTree::collision(GameObject& other, const CollisionHit& ) {
 
 void
 GhostTree::spawn_lantern() {
-  Lantern* lantern = new Lantern(get_bbox().get_middle() + SUCK_TARGET_OFFSET);
+  auto lantern = std::make_shared<Lantern>(get_bbox().get_middle() + SUCK_TARGET_OFFSET);
   Sector::current()->add_object(lantern);
 }
 
index d71cad8..e902cc8 100644 (file)
@@ -62,7 +62,7 @@ private:
 
   Lantern* suck_lantern; /**< Lantern that is currently being sucked in */
 
-  std::vector<TreeWillOWisp*> willowisps;
+  std::vector<std::shared_ptr<TreeWillOWisp> > willowisps;
 
   bool is_color_deadly(Color color) const;
   void spawn_lantern();
index 67c8002..87000f7 100644 (file)
@@ -160,8 +160,8 @@ GoldBomb::kill_fall()
 
   if(is_valid()) {
     remove_me();
-    Sector::current()->add_object(new Explosion(get_bbox().get_middle()));
-    Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, -40)));
+    Sector::current()->add_object(std::make_shared<Explosion>(get_bbox().get_middle()));
+    Sector::current()->add_object(std::make_shared<CoinExplode>(get_pos() + Vector (0, -40)));
   }
 
   run_dead_script();
index f3d7242..01a4b91 100644 (file)
@@ -173,7 +173,7 @@ Haywire::kill_fall()
   }
   if(is_valid()) {
     remove_me();
-    Explosion* explosion = new Explosion(get_bbox().get_middle());
+    auto explosion = std::make_shared<Explosion>(get_bbox().get_middle());
     Sector::current()->add_object(explosion);
   }
 
index 630da0a..be51c78 100644 (file)
@@ -88,7 +88,11 @@ Iceflame::ignite()
 {
   SoundManager::current()->play("sounds/sizzle.ogg", get_pos());
   sprite->set_action("fade", 1);
-  Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", bbox.get_middle(), ANCHOR_MIDDLE, Vector(0, -150), Vector(0,0), LAYER_BACKGROUNDTILES+2));
+  Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/smoke.sprite",
+                                                                 "default",
+                                                                 bbox.get_middle(), ANCHOR_MIDDLE,
+                                                                 Vector(0, -150), Vector(0,0),
+                                                                 LAYER_BACKGROUNDTILES+2));
   set_group(COLGROUP_DISABLED);
 
   // start dead-script
index 1fc48c0..457b22d 100644 (file)
@@ -79,7 +79,7 @@ Igel::active_update(float elapsed_time)
   // check if we see a fire bullet
   Sector* sector = Sector::current();
   for (Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) {
-    Bullet* bullet = dynamic_cast<Bullet*>(*i);
+    Bullet* bullet = dynamic_cast<Bullet*>(i->get());
     if (!bullet) continue;
     if (bullet->get_type() != FIRE_BONUS) continue;
     if (can_see(*bullet)) wants_to_flee = true;
index 55c91ad..dd9c165 100644 (file)
@@ -136,7 +136,10 @@ LiveFire::kill_fall()
   Vector ppos = bbox.get_middle();
   Vector pspeed = Vector(0, -150);
   Vector paccel = Vector(0,0);
-  Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_BACKGROUNDTILES+2));
+  Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/smoke.sprite",
+                                                                 "default", ppos, ANCHOR_MIDDLE,
+                                                                 pspeed, paccel,
+                                                                 LAYER_BACKGROUNDTILES+2));
   // extinguish the flame
   sprite->set_action(dir == LEFT ? "extinguish-left" : "extinguish-right", 1);
   physic.set_velocity_y(0);
index f6ab193..fd66012 100644 (file)
@@ -96,7 +96,7 @@ Mole::throw_rock()
   float vy = -sin(angle) * THROW_VELOCITY;
 
   SoundManager::current()->play("sounds/dartfire.wav", get_pos());
-  Sector::current()->add_object(new MoleRock(Vector(px, py), Vector(vx, vy), this));
+  Sector::current()->add_object(std::make_shared<MoleRock>(Vector(px, py), Vector(vx, vy), this));
 }
 
 void
index d9688dd..b3b4a9e 100644 (file)
@@ -86,7 +86,7 @@ MrBomb::collision_squished(GameObject& object)
   }
   if(is_valid()) {
     remove_me();
-    Sector::current()->add_object(new Bomb(get_pos(), dir, sprite_name ));
+    Sector::current()->add_object(std::make_shared<Bomb>(get_pos(), dir, sprite_name));
   }
   kill_squished(object);
   return true;
@@ -105,7 +105,7 @@ MrBomb::kill_fall()
 {
   if(is_valid()) {
     remove_me();
-    Explosion* explosion = new Explosion(get_bbox().get_middle());
+    auto explosion = std::make_shared<Explosion>(get_bbox().get_middle());
     Sector::current()->add_object(explosion);
   }
 
index 74c46a5..500bd30 100644 (file)
@@ -54,7 +54,7 @@ MrTree::collision_squished(GameObject& object)
   Vector stumpy_pos = get_pos();
   stumpy_pos.x += 20;
   stumpy_pos.y += 25;
-  Stumpy* stumpy = new Stumpy(stumpy_pos, dir);
+  auto stumpy = std::make_shared<Stumpy>(stumpy_pos, dir);
   remove_me();
   Sector::current()->add_object(stumpy);
 
@@ -73,14 +73,18 @@ MrTree::collision_squished(GameObject& object)
     float vy = -cos(angle)*velocity;
     Vector pspeed = Vector(vx, vy);
     Vector paccel = Vector(0, 100);
-    Sector::current()->add_object(new SpriteParticle("images/objects/particles/leaf.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
+    Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/leaf.sprite",
+                                                                   "default",
+                                                                   ppos, ANCHOR_MIDDLE,
+                                                                   pspeed, paccel,
+                                                                   LAYER_OBJECTS-1));
   }
 
   // spawn PoisonIvy
   Vector leaf1_pos(stumpy_pos.x - POISONIVY_WIDTH - 1, stumpy_pos.y - POISONIVY_Y_OFFSET);
   Rectf leaf1_bbox(leaf1_pos.x, leaf1_pos.y, leaf1_pos.x + POISONIVY_WIDTH, leaf1_pos.y + POISONIVY_HEIGHT);
   if (Sector::current()->is_free_of_movingstatics(leaf1_bbox, this)) {
-    PoisonIvy* leaf1 = new PoisonIvy(leaf1_bbox.p1, LEFT);
+    auto leaf1 = std::make_shared<PoisonIvy>(leaf1_bbox.p1, LEFT);
     leaf1->countMe = false;
     Sector::current()->add_object(leaf1);
   }
@@ -89,7 +93,7 @@ MrTree::collision_squished(GameObject& object)
   Vector leaf2_pos(stumpy_pos.x + sprite->get_current_hitbox_width() + 1, stumpy_pos.y - POISONIVY_Y_OFFSET);
   Rectf leaf2_bbox(leaf2_pos.x, leaf2_pos.y, leaf2_pos.x + POISONIVY_WIDTH, leaf2_pos.y + POISONIVY_HEIGHT);
   if (Sector::current()->is_free_of_movingstatics(leaf2_bbox, this)) {
-    PoisonIvy* leaf2 = new PoisonIvy(leaf2_bbox.p1, RIGHT);
+    auto leaf2 = std::make_shared<PoisonIvy>(leaf2_bbox.p1, RIGHT);
     leaf2->countMe = false;
     Sector::current()->add_object(leaf2);
   }
index 54c9433..d7d2a53 100644 (file)
@@ -50,27 +50,28 @@ Owl::Owl(const Vector& pos, Direction d) :
 void
 Owl::initialize()
 {
-  GameObject *game_object;
-
   physic.set_velocity_x(dir == LEFT ? -FLYING_SPEED : FLYING_SPEED);
   physic.enable_gravity(false);
   sprite->set_action(dir == LEFT ? "left" : "right");
 
-  game_object = ObjectFactory::instance().create(carried_obj_name, get_pos(), dir);
-  if (game_object == NULL) {
+  auto game_object = ObjectFactory::instance().create(carried_obj_name, get_pos(), dir);
+  if (game_object == NULL)
+  {
     log_fatal << "Creating \"" << carried_obj_name << "\" object failed." << std::endl;
-    return;
   }
-
-  carried_object = dynamic_cast<Portable *> (game_object);
-  if (carried_object == NULL) {
-    log_warning << "Object is not portable: " << carried_obj_name << std::endl;
-    delete game_object;
-    return;
+  else
+  {
+    carried_object = dynamic_cast<Portable*>(game_object.get());
+    if (carried_object == NULL)
+    {
+      log_warning << "Object is not portable: " << carried_obj_name << std::endl;
+    }
+    else
+    {
+      Sector::current()->add_object(game_object);
+    }
   }
-
-  Sector::current ()->add_object (game_object);
-} /* void initialize */
+}
 
 bool
 Owl::is_above_player (void)
index c8973af..799d040 100644 (file)
@@ -54,7 +54,11 @@ PoisonIvy::collision_squished(GameObject& object)
     float vy = -cos(angle)*velocity;
     Vector pspeed = Vector(vx, vy);
     Vector paccel = Vector(0, 100);
-    Sector::current()->add_object(new SpriteParticle("images/objects/particles/poisonivy.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
+    Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/poisonivy.sprite",
+                                                                   "default",
+                                                                   ppos, ANCHOR_MIDDLE,
+                                                                   pspeed, paccel,
+                                                                   LAYER_OBJECTS-1));
   }
   kill_squished(object);
   return true;
index 0bd6472..f92ec48 100644 (file)
@@ -48,19 +48,19 @@ ShortFuse::ShortFuse(const Reader& reader) :
 }
 
 void
-ShortFuse::explode (void)
+ShortFuse::explode()
 {
-  if (!is_valid ())
+  if (!is_valid())
     return;
 
-  Explosion *explosion = new Explosion (get_bbox ().get_middle ());
+  auto explosion = std::make_shared<Explosion>(get_bbox ().get_middle());
 
-  explosion->hurts (false);
-  explosion->pushes (true);
-  Sector::current()->add_object (explosion);
+  explosion->hurts(false);
+  explosion->pushes(true);
+  Sector::current()->add_object(explosion);
 
-  run_dead_script ();
-  remove_me ();
+  run_dead_script();
+  remove_me();
 }
 
 bool
index e130557..d707636 100644 (file)
@@ -117,14 +117,14 @@ SkyDive::active_update (float elapsed_time)
 void
 SkyDive::explode (void)
 {
-  if (!is_valid ())
+  if (!is_valid())
     return;
 
-  Explosion *explosion = new Explosion (get_anchor_pos (bbox, ANCHOR_BOTTOM));
+  auto explosion = std::make_shared<Explosion>(get_anchor_pos (bbox, ANCHOR_BOTTOM));
 
-  explosion->hurts (true);
-  explosion->pushes (false);
-  Sector::current()->add_object (explosion);
+  explosion->hurts(true);
+  explosion->pushes(false);
+  Sector::current()->add_object(explosion);
 
   remove_me ();
 } /* void explode */
index 7edc1c1..e0fa39a 100644 (file)
@@ -47,7 +47,7 @@ Snowman::loose_head()
   set_state (STATE_FALLING);
 
   /* Create a new snowball where the snowman's head was */
-  SnowBall* snowball = new SnowBall(snowball_pos, dir, dead_script);
+  auto snowball = std::make_shared<SnowBall>(snowball_pos, dir, dead_script);
   Sector::current()->add_object(snowball);
 }
 
index 7eb6c4e..921d9d2 100644 (file)
@@ -108,7 +108,11 @@ Stumpy::collision_squished(GameObject& object)
       float vy = -cos(angle)*velocity;
       Vector pspeed = Vector(vx, vy);
       Vector paccel = Vector(0, 100);
-      Sector::current()->add_object(new SpriteParticle("images/objects/particles/bark.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
+      Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/bark.sprite",
+                                                                     "default",
+                                                                     ppos, ANCHOR_MIDDLE,
+                                                                     pspeed, paccel,
+                                                                     LAYER_OBJECTS-1));
     }
 
     return true;
index e958d53..3e5c420 100644 (file)
@@ -244,7 +244,7 @@ Yeti::drop_stalactite()
   Sector* sector = Sector::current();
   for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
       i != sector->gameobjects.end(); ++i) {
-    YetiStalactite* stalactite = dynamic_cast<YetiStalactite*> (*i);
+    YetiStalactite* stalactite = dynamic_cast<YetiStalactite*>(i->get());
     if(stalactite && stalactite->is_hanging()) {
       float distancex;
       if (hit_points >= 3) {
index b6a31c2..c0d4196 100644 (file)
@@ -229,7 +229,7 @@ JoystickManager::bind_next_event_to(Controller::Control id)
 void
 JoystickManager::set_joy_controls(Controller::Control id, bool value)
 {
-  if (m_joystick_config.jump_with_up_joy && 
+  if (m_joystick_config.jump_with_up_joy &&
       id == Controller::UP)
   {
     parent->get_controller()->set_control(Controller::JUMP, value);
index 43a8a68..4ff074e 100644 (file)
@@ -92,7 +92,7 @@ void
 BicyclePlatform::update(float elapsed_time)
 {
   if (!slave) {
-    Sector::current()->add_object(new BicyclePlatform(this));
+    Sector::current()->add_object(std::make_shared<BicyclePlatform>(this));
     return;
   }
   if (!master) {
index c8d5338..1e115d9 100644 (file)
@@ -151,16 +151,16 @@ Block::break_me()
 {
   Sector* sector = Sector::current();
   sector->add_object(
-    new BrokenBrick(sprite->clone(), get_pos(), Vector(-100, -400)));
+    std::make_shared<BrokenBrick>(sprite->clone(), get_pos(), Vector(-100, -400)));
   sector->add_object(
-    new BrokenBrick(sprite->clone(), get_pos() + Vector(0, 16),
-                    Vector(-150, -300)));
+    std::make_shared<BrokenBrick>(sprite->clone(), get_pos() + Vector(0, 16),
+                                  Vector(-150, -300)));
   sector->add_object(
-    new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 0),
-                    Vector(100, -400)));
+    std::make_shared<BrokenBrick>(sprite->clone(), get_pos() + Vector(16, 0),
+                                  Vector(100, -400)));
   sector->add_object(
-    new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 16),
-                    Vector(150, -300)));
+    std::make_shared<BrokenBrick>(sprite->clone(), get_pos() + Vector(16, 16),
+                                  Vector(150, -300)));
   remove_me();
 }
 
index dbcc20c..29f6fa6 100644 (file)
@@ -42,7 +42,7 @@
 BonusBlock::BonusBlock(const Vector& pos, int data) :
   Block(SpriteManager::current()->create("images/objects/bonus_block/bonusblock.sprite")),
   contents(),
-  object(0),
+  object(),
   hit_counter(1),
   sprite_name(),
   script(),
@@ -64,15 +64,15 @@ BonusBlock::BonusBlock(const Vector& pos, int data) :
       //object = new Trampoline(get_pos(), false); //needed if this is to be moved to custom
       break;
     case 8: contents = CONTENT_CUSTOM;
-      object = new Trampoline(get_pos(), true);
+      object = std::make_shared<Trampoline>(get_pos(), true);
       break;
     case 9: contents = CONTENT_CUSTOM;
-      object = new Rock(get_pos(), "images/objects/rock/rock.sprite");
+      object = std::make_shared<Rock>(get_pos(), "images/objects/rock/rock.sprite");
       break;
     case 10: contents = CONTENT_RAIN; break;
     case 11: contents = CONTENT_EXPLODE; break;
     case 12: contents = CONTENT_CUSTOM;
-      object = new PowerUp(get_pos(), "images/powerups/potions/red-potion.sprite");
+      object = std::make_shared<PowerUp>(get_pos(), "images/powerups/potions/red-potion.sprite");
       break;
     default:
       log_warning << "Invalid box contents" << std::endl;
@@ -138,8 +138,8 @@ BonusBlock::BonusBlock(const Reader& lisp) :
       }
     } else {
       if(contents == CONTENT_CUSTOM) {
-        GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
-        object = dynamic_cast<MovingObject*> (game_object);
+        GameObjectPtr game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
+        object = std::dynamic_pointer_cast<MovingObject>(game_object);
         if(object == 0)
           throw std::runtime_error(
             "Only MovingObjects are allowed inside BonusBlocks");
@@ -159,7 +159,6 @@ BonusBlock::BonusBlock(const Reader& lisp) :
 
 BonusBlock::~BonusBlock()
 {
-  delete object;
 }
 
 void
@@ -218,7 +217,7 @@ BonusBlock::try_open(Player *player)
   switch(contents) {
     case CONTENT_COIN:
     {
-      Sector::current()->add_object(new BouncyCoin(get_pos(), true));
+      Sector::current()->add_object(std::make_shared<BouncyCoin>(get_pos(), true));
       player->get_status()->add_coins(1);
       if (hit_counter != 0)
         Sector::current()->get_level()->stats.coins++;
@@ -228,11 +227,11 @@ BonusBlock::try_open(Player *player)
     case CONTENT_FIREGROW:
     {
       if(player->get_status()->bonus == NO_BONUS) {
-        SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
+        auto riser = std::make_shared<SpecialRiser>(get_pos(), std::make_shared<GrowUp>(direction));
         sector->add_object(riser);
       } else {
-        SpecialRiser* riser = new SpecialRiser(
-          get_pos(), new Flower(FIRE_BONUS));
+        auto riser = std::make_shared<SpecialRiser>(
+          get_pos(), std::make_shared<Flower>(FIRE_BONUS));
         sector->add_object(riser);
       }
       SoundManager::current()->play("sounds/upgrade.wav");
@@ -242,11 +241,11 @@ BonusBlock::try_open(Player *player)
     case CONTENT_ICEGROW:
     {
       if(player->get_status()->bonus == NO_BONUS) {
-        SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
+        auto riser = std::make_shared<SpecialRiser>(get_pos(), std::make_shared<GrowUp>(direction));
         sector->add_object(riser);
       } else {
-        SpecialRiser* riser = new SpecialRiser(
-          get_pos(), new Flower(ICE_BONUS));
+        auto riser = std::make_shared<SpecialRiser>(
+          get_pos(), std::make_shared<Flower>(ICE_BONUS));
         sector->add_object(riser);
       }
       SoundManager::current()->play("sounds/upgrade.wav");
@@ -255,21 +254,21 @@ BonusBlock::try_open(Player *player)
 
     case CONTENT_STAR:
     {
-      sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
+      sector->add_object(std::make_shared<Star>(get_pos() + Vector(0, -32), direction));
       SoundManager::current()->play("sounds/upgrade.wav");
       break;
     }
 
     case CONTENT_1UP:
     {
-      sector->add_object(new OneUp(get_pos(), direction));
+      sector->add_object(std::make_shared<OneUp>(get_pos(), direction));
       SoundManager::current()->play("sounds/upgrade.wav");
       break;
     }
 
     case CONTENT_CUSTOM:
     {
-      SpecialRiser* riser = new SpecialRiser(get_pos(), object);
+      auto riser = std::make_shared<SpecialRiser>(get_pos(), object);
       object = 0;
       sector->add_object(riser);
       SoundManager::current()->play("sounds/upgrade.wav");
@@ -290,7 +289,7 @@ BonusBlock::try_open(Player *player)
     }
     case CONTENT_TRAMPOLINE:
     {
-      SpecialRiser* riser = new SpecialRiser(get_pos(), new Trampoline(get_pos(), false));
+      auto riser = std::make_shared<SpecialRiser>(get_pos(), std::make_shared<Trampoline>(get_pos(), false));
       sector->add_object(riser);
       SoundManager::current()->play("sounds/upgrade.wav");
       break;
@@ -298,14 +297,14 @@ BonusBlock::try_open(Player *player)
     case CONTENT_RAIN:
     {
       hit_counter = 1; // multiple hits of coin rain is not allowed
-      Sector::current()->add_object(new CoinRain(get_pos(), true));
+      Sector::current()->add_object(std::make_shared<CoinRain>(get_pos(), true));
       SoundManager::current()->play("sounds/upgrade.wav");
       break;
     }
     case CONTENT_EXPLODE:
     {
       hit_counter = 1; // multiple hits of coin explode is not allowed
-      Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, -40)));
+      Sector::current()->add_object(std::make_shared<CoinExplode>(get_pos() + Vector (0, -40)));
       SoundManager::current()->play("sounds/upgrade.wav");
       break;
     }
@@ -366,7 +365,7 @@ BonusBlock::try_drop(Player *player)
 
     case CONTENT_FIREGROW:
     {
-      sector->add_object(new PowerUp(get_pos() + Vector(0, 32), "images/powerups/fireflower/fireflower.sprite"));
+      sector->add_object(std::make_shared<PowerUp>(get_pos() + Vector(0, 32), "images/powerups/fireflower/fireflower.sprite"));
       SoundManager::current()->play("sounds/upgrade.wav");
       countdown = true;
       break;
@@ -374,7 +373,7 @@ BonusBlock::try_drop(Player *player)
 
     case CONTENT_ICEGROW:
     {
-      sector->add_object(new PowerUp(get_pos() + Vector(0, 32), "images/powerups/iceflower/iceflower.sprite"));
+      sector->add_object(std::make_shared<PowerUp>(get_pos() + Vector(0, 32), "images/powerups/iceflower/iceflower.sprite"));
       SoundManager::current()->play("sounds/upgrade.wav");
       countdown = true;
       break;
@@ -382,7 +381,7 @@ BonusBlock::try_drop(Player *player)
 
     case CONTENT_STAR:
     {
-      sector->add_object(new Star(get_pos() + Vector(0, 32), direction));
+      sector->add_object(std::make_shared<Star>(get_pos() + Vector(0, 32), direction));
       SoundManager::current()->play("sounds/upgrade.wav");
       countdown = true;
       break;
@@ -390,7 +389,7 @@ BonusBlock::try_drop(Player *player)
 
     case CONTENT_1UP:
     {
-      sector->add_object(new OneUp(get_pos(), DOWN));
+      sector->add_object(std::make_shared<OneUp>(get_pos(), DOWN));
       SoundManager::current()->play("sounds/upgrade.wav");
       countdown = true;
       break;
@@ -428,7 +427,7 @@ BonusBlock::try_drop(Player *player)
     case CONTENT_EXPLODE:
     {
       hit_counter = 1; // multiple hits of coin explode is not allowed
-      Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, 40)));
+      Sector::current()->add_object(std::make_shared<CoinExplode>(get_pos() + Vector (0, 40)));
       SoundManager::current()->play("sounds/upgrade.wav");
       countdown = true;
       break;
index 5dd506e..816fa19 100644 (file)
@@ -50,7 +50,7 @@ protected:
 
 public:
   Contents contents;
-  MovingObject* object;
+  std::shared_ptr<MovingObject> object;
   int hit_counter;
   void draw(DrawingContext& context);
 
index 4b2b90c..ebee438 100644 (file)
@@ -88,7 +88,7 @@ Brick::try_break(Player* player)
   Sector* sector = Sector::current();
   Player& player_one = *(sector->player);
   if(coin_counter > 0) {
-    sector->add_object(new BouncyCoin(get_pos(),true));
+    sector->add_object(std::make_shared<BouncyCoin>(get_pos(), true));
     coin_counter--;
     player_one.get_status()->add_coins(1);
     if(coin_counter == 0)
index 84535f6..ec0ff01 100644 (file)
@@ -107,7 +107,11 @@ Candle::puff_smoke()
   Vector ppos = bbox.get_middle();
   Vector pspeed = Vector(0, -150);
   Vector paccel = Vector(0,0);
-  Sector::current()->add_object(new SpriteParticle("images/objects/particles/smoke.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_BACKGROUNDTILES+2));
+  Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/smoke.sprite",
+                                                                 "default",
+                                                                 ppos, ANCHOR_MIDDLE,
+                                                                 pspeed, paccel,
+                                                                 LAYER_BACKGROUNDTILES+2));
 }
 
 bool
index 0d3a771..a298454 100644 (file)
@@ -156,7 +156,7 @@ Coin::collect()
     SoundManager::current()->manage_source(soundSource);
   */
   Sector::current()->player->get_status()->add_coins(1);
-  Sector::current()->add_object(new BouncyCoin(get_pos()));
+  Sector::current()->add_object(std::make_shared<BouncyCoin>(get_pos()));
   Sector::current()->get_level()->stats.coins++;
   remove_me();
 }
index a900def..bd73e07 100644 (file)
@@ -31,16 +31,16 @@ CoinExplode::update(float )
   int mag = 100; // madnitude that coins are to be thrown
   int rand = 30; // max variation to be subtracted from magnitide
 
-  Sector::current()->add_object(new HeavyCoin(position, Vector (2.5,-4.5)*(mag-gameRandom.rand(rand))));
-  Sector::current()->add_object(new HeavyCoin(position, Vector (2,-5)*(mag-gameRandom.rand(rand))));
-  Sector::current()->add_object(new HeavyCoin(position, Vector (1.5,-5.5)*(mag-gameRandom.rand(rand))));
-  Sector::current()->add_object(new HeavyCoin(position, Vector (1,-6)*(mag+gameRandom.rand(rand))));
-  Sector::current()->add_object(new HeavyCoin(position, Vector (0.5,-6.5)*(mag-gameRandom.rand(rand))));
-  Sector::current()->add_object(new HeavyCoin(position, Vector (-2.5,-4.5)*(mag-gameRandom.rand(rand))));
-  Sector::current()->add_object(new HeavyCoin(position, Vector (-2,-5)*(mag-gameRandom.rand(rand))));
-  Sector::current()->add_object(new HeavyCoin(position, Vector (-1.5,-5.5)*(mag-gameRandom.rand(rand))));
-  Sector::current()->add_object(new HeavyCoin(position, Vector (-1,-6)*(mag+gameRandom.rand(rand))));
-  Sector::current()->add_object(new HeavyCoin(position, Vector (-0.5,-6.5)*(mag-gameRandom.rand(rand))));
+  Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (2.5,-4.5)*(mag-gameRandom.rand(rand))));
+  Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (2,-5)*(mag-gameRandom.rand(rand))));
+  Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (1.5,-5.5)*(mag-gameRandom.rand(rand))));
+  Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (1,-6)*(mag+gameRandom.rand(rand))));
+  Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (0.5,-6.5)*(mag-gameRandom.rand(rand))));
+  Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (-2.5,-4.5)*(mag-gameRandom.rand(rand))));
+  Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (-2,-5)*(mag-gameRandom.rand(rand))));
+  Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (-1.5,-5.5)*(mag-gameRandom.rand(rand))));
+  Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (-1,-6)*(mag+gameRandom.rand(rand))));
+  Sector::current()->add_object(std::make_shared<HeavyCoin>(position, Vector (-0.5,-6.5)*(mag-gameRandom.rand(rand))));
 
   remove_me();
 }
index c3b10e5..7e6b4c3 100644 (file)
@@ -50,7 +50,7 @@ CoinRain::update(float elapsed_time)
   } // then the first collectable coin drops from one of ten random positions
   else if (counter==0){
     drop = gameRandom.rand(10);
-    Sector::current()->add_object(new HeavyCoin(Vector (position.x+32*((drop<5)?-drop-1:drop-4),-32), Vector (0,0)));
+    Sector::current()->add_object(std::make_shared<HeavyCoin>(Vector (position.x+32*((drop<5)?-drop-1:drop-4),-32), Vector (0,0)));
     counter++;
     timer.start(DROP_TIME);
   } // finally the remainder of the coins drop in a determined but appears to be a random order
@@ -58,7 +58,7 @@ CoinRain::update(float elapsed_time)
     if(counter<10){
       drop += 7;
       if(drop >= 10) drop -=10;
-      Sector::current()->add_object(new HeavyCoin(Vector (position.x+32*((drop<5)?-drop-1:drop-4),-32), Vector (0,0)));
+      Sector::current()->add_object(std::make_shared<HeavyCoin>(Vector (position.x+32*((drop<5)?-drop-1:drop-4),-32), Vector (0,0)));
       counter++;
       timer.start(DROP_TIME);
     }else{
index f2f0ba4..b971b25 100644 (file)
@@ -41,7 +41,7 @@ EndSequenceFireworks::starting()
 {
   EndSequence::starting();
   endsequence_timer.start(7.3f * ScreenManager::current()->get_speed());
-  Sector::current()->add_object(new Fireworks());
+  Sector::current()->add_object(std::make_shared<Fireworks>());
 }
 
 void
index a7198ec..0077c9b 100644 (file)
@@ -89,7 +89,7 @@ Firefly::collision(GameObject& other, const CollisionHit& )
       float vy = -cos(angle)*velocity;
       Vector pspeed = Vector(vx, vy);
       Vector paccel = Vector(0, 1000);
-      Sector::current()->add_object(new SpriteParticle("images/objects/particles/reset.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
+      Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/reset.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
     }
 
     if( sprite_name.find("vbell", 0) == std::string::npos ) {
index bd1814e..c557453 100644 (file)
@@ -49,9 +49,9 @@ Fireworks::update(float )
     //float green = 0.9;
     (void) red;
     (void) green;
-    sector->add_object(new Particles(pos, 0, 360, Vector(140, 140),
-                                     Vector(0, 0), 45, Color(red, green, 0), 3, 1.3f,
-                                     LAYER_FOREGROUND1+1));
+    sector->add_object(std::make_shared<Particles>(pos, 0, 360, Vector(140, 140),
+                                                   Vector(0, 0), 45, Color(red, green, 0), 3, 1.3f,
+                                                   LAYER_FOREGROUND1+1));
     SoundManager::current()->play("sounds/fireworks.wav");
     timer.start(graphicsRandom.randf(1.0, 1.5));
   }
index d2fcf5c..55129ce 100644 (file)
@@ -147,14 +147,14 @@ IceCrusher::collision_solid(const CollisionHit& hit)
           // throw some particles, bigger and more for large icecrusher
           for(int j = 0; j < 9; j++)
           {
-          Sector::current()->add_object(
-            new Particles(Vector(get_bbox().p2.x - j*8 - 4, get_bbox().p2.y),
-              0, 90-5*j, Vector(140, -380), Vector(0, 300),
-              1, Color(.6f, .6f, .6f), 5, 1.8f, LAYER_OBJECTS+1));
-          Sector::current()->add_object(
-            new Particles(Vector(get_bbox().p1.x + j*8 + 4, get_bbox().p2.y),
-              270+5*j, 360, Vector(140, -380), Vector(0, 300),
-              1, Color(.6f, .6f, .6f), 5, 1.8f, LAYER_OBJECTS+1));
+          Sector::current()->add_object(std::make_shared<Particles>(
+                                          Vector(get_bbox().p2.x - j*8 - 4, get_bbox().p2.y),
+                                          0, 90-5*j, Vector(140, -380), Vector(0, 300),
+                                          1, Color(.6f, .6f, .6f), 5, 1.8f, LAYER_OBJECTS+1));
+          Sector::current()->add_object(std::make_shared<Particles>(
+                                          Vector(get_bbox().p1.x + j*8 + 4, get_bbox().p2.y),
+                                          270+5*j, 360, Vector(140, -380), Vector(0, 300),
+                                          1, Color(.6f, .6f, .6f), 5, 1.8f, LAYER_OBJECTS+1));
           }
         }
         else {
@@ -164,14 +164,14 @@ IceCrusher::collision_solid(const CollisionHit& hit)
           // throw some particles
           for(int j = 0; j < 5; j++)
           {
-          Sector::current()->add_object(
-            new Particles(Vector(get_bbox().p2.x - j*8 - 4, get_bbox().p2.y),
-              0, 90+10*j, Vector(140, -260), Vector(0, 300),
-              1, Color(.6f, .6f, .6f), 4, 1.6f, LAYER_OBJECTS+1));
-          Sector::current()->add_object(
-            new Particles(Vector(get_bbox().p1.x + j*8 + 4, get_bbox().p2.y),
-              270+10*j, 360, Vector(140, -260), Vector(0, 300),
-              1, Color(.6f, .6f, .6f), 4, 1.6f, LAYER_OBJECTS+1));
+          Sector::current()->add_object(std::make_shared<Particles>(
+                                          Vector(get_bbox().p2.x - j*8 - 4, get_bbox().p2.y),
+                                          0, 90+10*j, Vector(140, -260), Vector(0, 300),
+                                          1, Color(.6f, .6f, .6f), 4, 1.6f, LAYER_OBJECTS+1));
+          Sector::current()->add_object(std::make_shared<Particles>(
+                                          Vector(get_bbox().p1.x + j*8 + 4, get_bbox().p2.y),
+                                          270+10*j, 360, Vector(140, -260), Vector(0, 300),
+                                          1, Color(.6f, .6f, .6f), 4, 1.6f, LAYER_OBJECTS+1));
           }
         }
         set_state(RECOVERING);
index d927f45..c6e8345 100644 (file)
@@ -74,7 +74,7 @@ InfoBlock::hit(Player& player)
     Sector* parent = Sector::current();
     if (!parent) return;
     for (Sector::GameObjects::iterator i = parent->gameobjects.begin(); i != parent->gameobjects.end(); i++) {
-      InfoBlock* block = dynamic_cast<InfoBlock*>(*i);
+      InfoBlock* block = dynamic_cast<InfoBlock*>(i->get());
       if (!block) continue;
       if (block != this) block->hide_message();
     }
index 7a3c5a1..c2bfb0e 100644 (file)
@@ -343,11 +343,11 @@ Player::update(float elapsed_time)
   if(!dying && !deactivated)
     handle_input();
 
-/*
+  /*
   // handle_input() calls apply_friction() when Tux is not walking, so we'll have to do this ourselves
   if (deactivated)
-    apply_friction();
-*/
+  apply_friction();
+  */
 
   // extend/shrink tux collision rectangle so that we fall through/walk over 1
   // tile holes
@@ -427,13 +427,15 @@ Player::update(float elapsed_time)
       Vector ppos = Vector(px, py);
       Vector pspeed = Vector(0, 0);
       Vector paccel = Vector(0, 0);
-      Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite",
-                                                       // draw bright sparkle when there is lots of time left, dark sparkle when invincibility is about to end
-                                                       (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) ?
-                                                       // make every other a longer sparkle to make trail a bit fuzzy
-                                                       (size_t(game_time*20)%2) ? "small" : "medium"
-                                                       :
-                                                       "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
+      Sector::current()->add_object(std::make_shared<SpriteParticle>(
+                                      "images/objects/particles/sparkle.sprite",
+                                      // draw bright sparkle when there is lots of time left,
+                                      // dark sparkle when invincibility is about to end
+                                      (invincible_timer.get_timeleft() > TUX_INVINCIBLE_TIME_WARNING) ?
+                                      // make every other a longer sparkle to make trail a bit fuzzy
+                                      (size_t(game_time*20)%2) ? "small" : "medium"
+                                      :
+                                      "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
     }
   }
 
@@ -550,7 +552,7 @@ Player::handle_horizontal_input()
       SoundManager::current()->play("sounds/skid.wav");
       // dust some particles
       Sector::current()->add_object(
-        new Particles(
+        std::make_shared<Particles>(
           Vector(dir == RIGHT ? get_bbox().p2.x : get_bbox().p1.x, get_bbox().p2.y),
           dir == RIGHT ? 270+20 : 90-40, dir == RIGHT ? 270+40 : 90-20,
           Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
@@ -1008,7 +1010,7 @@ Player::set_bonus(BonusType type, bool animate)
       Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
       Vector paccel = Vector(0, 1000);
       std::string action = (dir==LEFT)?"left":"right";
-      Sector::current()->add_object(new SpriteParticle("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
+      Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/firetux-helmet.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
       if (climbing) stop_climbing(*climbing);
     }
     if ((player_status->bonus == ICE_BONUS) && (animate)) {
@@ -1017,7 +1019,7 @@ Player::set_bonus(BonusType type, bool animate)
       Vector pspeed = Vector(((dir==LEFT) ? +100 : -100), -300);
       Vector paccel = Vector(0, 1000);
       std::string action = (dir==LEFT)?"left":"right";
-      Sector::current()->add_object(new SpriteParticle("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
+      Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/icetux-cap.sprite", action, ppos, ANCHOR_TOP, pspeed, paccel, LAYER_OBJECTS-1));
       if (climbing) stop_climbing(*climbing);
     }
     player_status->max_fire_bullets = 0;
@@ -1208,12 +1210,12 @@ Player::collision_solid(const CollisionHit& hit)
       does_buttjump = false;
       physic.set_velocity_y(-300);
       on_ground_flag = false;
-      Sector::current()->add_object(new Particles(
+      Sector::current()->add_object(std::make_shared<Particles>(
                                       Vector(get_bbox().p2.x, get_bbox().p2.y),
                                       270+20, 270+40,
                                       Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
                                       LAYER_OBJECTS+1));
-      Sector::current()->add_object(new Particles(
+      Sector::current()->add_object(std::make_shared<Particles>(
                                       Vector(get_bbox().p1.x, get_bbox().p2.y),
                                       90-40, 90-20,
                                       Vector(280, -260), Vector(0, 300), 3, Color(.4f, .4f, .4f), 3, .8f,
@@ -1338,7 +1340,7 @@ Player::kill(bool completely)
       for (int i = 0; i < 5; i++)
       {
         // the numbers: starting x, starting y, velocity y
-        Sector::current()->add_object(new FallingCoin(get_pos() +
+        Sector::current()->add_object(std::make_shared<FallingCoin>(get_pos() +
                                                       Vector(graphicsRandom.rand(5), graphicsRandom.rand(-32,18)),
                                                       graphicsRandom.rand(-100,100)));
       }
index b295de8..c76f8b1 100644 (file)
@@ -86,7 +86,7 @@ void
 PneumaticPlatform::update(float elapsed_time)
 {
   if (!slave) {
-    Sector::current()->add_object(new PneumaticPlatform(this));
+    Sector::current()->add_object(std::make_shared<PneumaticPlatform>(this));
     return;
   }
   if (!master) {
index 93f8dbc..d8ab02e 100644 (file)
@@ -19,7 +19,6 @@
 
 #include "supertux/direction.hpp"
 #include "supertux/moving_object.hpp"
-#include "util/refcounter.hpp"
 
 /**
  * An object that inherits from this object is considered "portable" and can
index 686b407..0ac73fd 100644 (file)
@@ -151,11 +151,13 @@ PowerUp::update(float elapsed_time)
           Vector ppos = Vector(px, py);
           Vector pspeed = Vector(0, 0);
           Vector paccel = Vector(0, 0);
-          Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite",
-                                                          // draw bright sparkles when very close to Tux, dark sparkles when slightly further
-                                                          (disp_x*disp_x + disp_y*disp_y <= 128*128) ?
-                                                          // make every other a longer sparkle to make trail a bit fuzzy
-                                                          (size_t(game_time*20)%2) ? "small" : "medium" : "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
+          Sector::current()->add_object(std::make_shared<SpriteParticle>(
+                                          "images/objects/particles/sparkle.sprite",
+                                          // draw bright sparkles when very close to Tux, dark sparkles when slightly further
+                                          (disp_x*disp_x + disp_y*disp_y <= 128*128) ?
+                                          // make every other a longer sparkle to make trail a bit fuzzy
+                                          (size_t(game_time*20)%2) ? "small" : "medium" : "dark",
+                                          ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
         }
       }
     }
index 4beda1c..85cd019 100644 (file)
@@ -75,7 +75,7 @@ void RainParticleSystem::update(float elapsed_time)
         if (!vertical) { //check if collision happened from above
           splash_x = int(particle->pos.x);
           splash_y = int(particle->pos.y) - (int(particle->pos.y) % 32) + 32;
-          Sector::current()->add_object(new RainSplash(Vector(splash_x, splash_y),vertical));
+          Sector::current()->add_object(std::make_shared<RainSplash>(Vector(splash_x, splash_y),vertical));
         }
         // Uncomment the following to display vertical splashes, too
         /* else {
index 013109a..d9e8f09 100644 (file)
@@ -18,7 +18,7 @@
 #include "object/specialriser.hpp"
 #include "supertux/sector.hpp"
 
-SpecialRiser::SpecialRiser(Vector pos, MovingObject* _child) :
+SpecialRiser::SpecialRiser(Vector pos, std::shared_ptr<MovingObject> _child) :
   offset(),
   child(_child)
 {
index 9d42370..f3a62b3 100644 (file)
@@ -26,7 +26,7 @@
 class SpecialRiser : public GameObject
 {
 public:
-  SpecialRiser(Vector pos, MovingObject* child);
+  SpecialRiser(Vector pos, std::shared_ptr<MovingObject> child);
   ~SpecialRiser();
 
   virtual void update(float elapsed_time);
@@ -34,7 +34,7 @@ public:
 
 private:
   float offset;
-  MovingObject* child;
+  std::shared_ptr<MovingObject> child;
 
 private:
   SpecialRiser(const SpecialRiser&);
index 66f8211..fb505b6 100644 (file)
@@ -57,11 +57,13 @@ Star::update(float elapsed_time)
         Vector ppos = Vector(px, py);
         Vector pspeed = Vector(0, 0);
         Vector paccel = Vector(0, 0);
-        Sector::current()->add_object(new SpriteParticle("images/objects/particles/sparkle.sprite",
-                                                         // draw bright sparkles when very close to Tux, dark sparkles when slightly further
-                                                         (disp_x*disp_x + disp_y*disp_y <= 128*128) ?
-                                                         // make every other a longer sparkle to make trail a bit fuzzy
-                                                         (size_t(game_time*20)%2) ? "small" : "medium" : "dark", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
+        Sector::current()->add_object(std::make_shared<SpriteParticle>(
+                                        "images/objects/particles/sparkle.sprite",
+                                        // draw bright sparkles when very close to Tux, dark sparkles when slightly further
+                                        (disp_x*disp_x + disp_y*disp_y <= 128*128) ?
+                                        // make every other a longer sparkle to make trail a bit fuzzy
+                                        (size_t(game_time*20)%2) ? "small" : "medium" : "dark", 
+                                        ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS+1+5));
       }
     }
   }
index 187a4c3..af9e410 100644 (file)
@@ -138,8 +138,8 @@ Thunderstorm::flash()
 void
 Thunderstorm::electrify()
 {
-  Sector::current()->add_object(new Electrifier(200, 1421, ELECTRIFY_TIME));
-  Sector::current()->add_object(new Electrifier(201, 1422, ELECTRIFY_TIME));
+  Sector::current()->add_object(std::make_shared<Electrifier>(200, 1421, ELECTRIFY_TIME));
+  Sector::current()->add_object(std::make_shared<Electrifier>(201, 1422, ELECTRIFY_TIME));
 }
 
 /* EOF */
index 834dc5f..2611285 100644 (file)
@@ -184,7 +184,7 @@ WeakBlock::spreadHit()
       return;
     }
     for(Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) {
-      WeakBlock* wb = dynamic_cast<WeakBlock*>(*i);
+      WeakBlock* wb = dynamic_cast<WeakBlock*>(i->get());
       if (!wb) continue;
       if (wb == this) continue;
       if (wb->state != STATE_NORMAL) continue;
index ea5f3a3..db27bb4 100644 (file)
@@ -64,7 +64,7 @@ Wind::update(float elapsed_time_)
     // emit a particle
     Vector ppos = Vector(graphicsRandom.randf(bbox.p1.x+8, bbox.p2.x-8), graphicsRandom.randf(bbox.p1.y+8, bbox.p2.y-8));
     Vector pspeed = Vector(speed.x, speed.y);
-    Sector::current()->add_object(new Particles(ppos, 44, 46, pspeed, Vector(0,0), 1, Color(.4f, .4f, .4f), 3, .1f,
+    Sector::current()->add_object(std::make_shared<Particles>(ppos, 44, 46, pspeed, Vector(0,0), 1, Color(.4f, .4f, .4f), 3, .1f,
                                                 LAYER_BACKGROUNDTILES+1));
   }
 }
index b45eb09..2264d0d 100644 (file)
@@ -28,11 +28,11 @@ FloatingImage::FloatingImage(const std::string& spritefile) :
 {
   using namespace worldmap;
 
-  floating_image = new _FloatingImage(spritefile);
+  floating_image = std::make_shared<_FloatingImage>(spritefile);
   if(Sector::current() != NULL) {
-    Sector::current()->add_object(floating_image.get());
+    Sector::current()->add_object(floating_image);
   } else if(WorldMap::current() != NULL) {
-    WorldMap::current()->add_object(floating_image.get());
+    WorldMap::current()->add_object(floating_image);
   } else {
     throw new std::runtime_error("Neither sector nor worldmap active");
   }
index 4365e09..9f08379 100644 (file)
@@ -18,9 +18,9 @@
 #define HEADER_SUPERTUX_SCRIPTING_FLOATING_IMAGE_HPP
 
 #ifndef SCRIPTING_API
-#define HEADER_SUPERTUX_SCRIPTING_FLOATING_IMAGE_HPP
+#include <memory>
 
-#include "util/ref.hpp"
+#define HEADER_SUPERTUX_SCRIPTING_FLOATING_IMAGE_HPP
 
 class FloatingImage;
 typedef FloatingImage _FloatingImage;
@@ -50,7 +50,7 @@ public:
 
 #ifndef SCRIPTING_API
 private:
-  Ref<_FloatingImage> floating_image;
+  std::shared_ptr<_FloatingImage> floating_image;
 #endif
 };
 
index 4efac9a..71645ab 100644 (file)
@@ -127,7 +127,7 @@ Scripting::~Scripting()
 
   global_vm = NULL;
 }
-  
+
 void
 Scripting::update_debugger()
 {
index 5f985b4..b17bd27 100644 (file)
@@ -31,7 +31,7 @@ private:
 public:
   Scripting(bool enable_debugger);
   ~Scripting();
-  
+
   void update_debugger();
 
 private:
index e40503b..6c14e02 100644 (file)
@@ -104,9 +104,9 @@ CommandLineArguments::print_help(const char* arg0)
             << _(     "Environment variables:") << "\n"
             << _(     "  SUPERTUX2_USER_DIR           Directory for user data (savegames, etc.)" ) << "\n"
             << _(     "  SUPERTUX2_DATA_DIR           Directory for the games datafiles" ) << "\n"<< "\n"
-    
-                 
-    
+
+
+
             << std::flush;
 }
 
index 53f285a..3065fa8 100644 (file)
@@ -32,43 +32,42 @@ FlipLevelTransformer::transform_sector(Sector* sector)
 
   for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
       i != sector->gameobjects.end(); ++i) {
-    GameObject* object = *i;
+    GameObjectPtr object = *i;
 
-    TileMap* tilemap = dynamic_cast<TileMap*> (object);
+    TileMap* tilemap = dynamic_cast<TileMap*>(object.get());
     if(tilemap) {
-      transform_tilemap(height, tilemap);
+      transform_tilemap(height, *tilemap);
     }
-    Player* player = dynamic_cast<Player*> (object);
+    Player* player = dynamic_cast<Player*>(object.get());
     if(player) {
       Vector pos = player->get_pos();
       pos.y = height - pos.y - player->get_bbox().get_height();
       player->move(pos);
       continue;
     }
-    BadGuy* badguy = dynamic_cast<BadGuy*> (object);
+    BadGuy* badguy = dynamic_cast<BadGuy*>(object.get());
     if(badguy) {
-      transform_badguy(height, badguy);
+      transform_badguy(height, *badguy);
     }
-    Flower* flower = dynamic_cast<Flower*> (object);
+    Flower* flower = dynamic_cast<Flower*>(object.get());
     if(flower) {
-      transform_flower(flower);
+      transform_flower(*flower);
     }
-    Platform* platform = dynamic_cast<Platform*> (object);
+    Platform* platform = dynamic_cast<Platform*>(object.get());
     if(platform) {
       transform_platform(height, *platform);
     }
-    Block* block = dynamic_cast<Block*> (object);
+    Block* block = dynamic_cast<Block*>(object.get());
     if(block) {
       transform_block(height, *block);
     }
-    MovingObject* mobject = dynamic_cast<MovingObject*> (object);
+    MovingObject* mobject = dynamic_cast<MovingObject*>(object.get());
     if(mobject) {
-      transform_moving_object(height, mobject);
+      transform_moving_object(height, *mobject);
     }
   }
-  for(Sector::SpawnPoints::iterator i = sector->spawnpoints.begin();
-      i != sector->spawnpoints.end(); ++i) {
-    transform_spawnpoint(height, *i);
+  for(auto i = sector->spawnpoints.begin(); i != sector->spawnpoints.end(); ++i) {
+    transform_spawnpoint(height, **i);
   }
 
   if(sector->camera != 0 && sector->player != 0)
@@ -95,55 +94,55 @@ FlipLevelTransformer::transform_path(float height, float obj_height, Path& path)
 }
 
 void
-FlipLevelTransformer::transform_tilemap(float height, TileMap* tilemap)
+FlipLevelTransformer::transform_tilemap(float height, TileMap& tilemap)
 {
-  for(size_t x = 0; x < tilemap->get_width(); ++x) {
-    for(size_t y = 0; y < tilemap->get_height()/2; ++y) {
+  for(size_t x = 0; x < tilemap.get_width(); ++x) {
+    for(size_t y = 0; y < tilemap.get_height()/2; ++y) {
       // swap tiles
-      int y2 = tilemap->get_height()-1-y;
-      uint32_t t1 = tilemap->get_tile_id(x, y);
-      uint32_t t2 = tilemap->get_tile_id(x, y2);
-      tilemap->change(x, y, t2);
-      tilemap->change(x, y2, t1);
+      int y2 = tilemap.get_height()-1-y;
+      uint32_t t1 = tilemap.get_tile_id(x, y);
+      uint32_t t2 = tilemap.get_tile_id(x, y2);
+      tilemap.change(x, y, t2);
+      tilemap.change(x, y2, t1);
     }
   }
-  tilemap->set_drawing_effect(transform_drawing_effect(tilemap->get_drawing_effect()));
-  Vector offset = tilemap->get_offset();
-  offset.y = height - offset.y - tilemap->get_bbox().get_height();
-  tilemap->set_offset(offset);
-  Path* path = tilemap->get_path().get();
+  tilemap.set_drawing_effect(transform_drawing_effect(tilemap.get_drawing_effect()));
+  Vector offset = tilemap.get_offset();
+  offset.y = height - offset.y - tilemap.get_bbox().get_height();
+  tilemap.set_offset(offset);
+  Path* path = tilemap.get_path().get();
   if (path)
-    transform_path(height, tilemap->get_bbox().get_height(), *path);
+    transform_path(height, tilemap.get_bbox().get_height(), *path);
 }
 
 void
-FlipLevelTransformer::transform_badguy(float height, BadGuy* badguy)
+FlipLevelTransformer::transform_badguy(float height, BadGuy& badguy)
 {
-  Vector pos = badguy->get_start_position();
+  Vector pos = badguy.get_start_position();
   pos.y = height - pos.y;
-  badguy->set_start_position(pos);
+  badguy.set_start_position(pos);
 }
 
 void
-FlipLevelTransformer::transform_spawnpoint(float height, SpawnPoint* spawn)
+FlipLevelTransformer::transform_spawnpoint(float height, SpawnPoint& spawn)
 {
-  Vector pos = spawn->pos;
+  Vector pos = spawn.pos;
   pos.y = height - pos.y;
-  spawn->pos = pos;
+  spawn.pos = pos;
 }
 
 void
-FlipLevelTransformer::transform_moving_object(float height, MovingObject*object)
+FlipLevelTransformer::transform_moving_object(float height, MovingObjectobject)
 {
-  Vector pos = object->get_pos();
-  pos.y = height - pos.y - object->get_bbox().get_height();
-  object->set_pos(pos);
+  Vector pos = object.get_pos();
+  pos.y = height - pos.y - object.get_bbox().get_height();
+  object.set_pos(pos);
 }
 
 void
-FlipLevelTransformer::transform_flower(Flower* flower)
+FlipLevelTransformer::transform_flower(Flower& flower)
 {
-  flower->drawing_effect = transform_drawing_effect(flower->drawing_effect);
+  flower.drawing_effect = transform_drawing_effect(flower.drawing_effect);
 }
 
 void
index b7bef11..25bfa7d 100644 (file)
@@ -38,11 +38,11 @@ public:
 private:
   DrawingEffect transform_drawing_effect(DrawingEffect effect);
   void transform_path(float height, float obj_height, Path& path);
-  void transform_tilemap(float height, TileMap* tilemap);
-  void transform_moving_object(float height, MovingObject* object);
-  void transform_badguy(float height, BadGuy* badguy);
-  void transform_spawnpoint(float height, SpawnPoint* spawnpoint);
-  void transform_flower(Flower *flower);
+  void transform_tilemap(float height, TileMap& tilemap);
+  void transform_moving_object(float height, MovingObject& object);
+  void transform_badguy(float height, BadGuy& badguy);
+  void transform_spawnpoint(float height, SpawnPoint& spawnpoint);
+  void transform_flower(Flowerflower);
   void transform_platform(float height, Platform& platform);
   void transform_block(float height, Block& block);
 };
index aef45f9..3e34a53 100644 (file)
@@ -25,7 +25,6 @@ GameObject::GameObject() :
 }
 
 GameObject::GameObject(const GameObject& rhs) :
-  RefCounter(),
   wants_to_die(rhs.wants_to_die),
   remove_listeners(NULL),
   name(rhs.name)
index 180630e..f8bc802 100644 (file)
 #ifndef HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_HPP
 #define HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_HPP
 
+#include <memory>
 #include <string>
 
-#include "util/refcounter.hpp"
-
 class DrawingContext;
+class GameObject;
 class ObjectRemoveListener;
 
 /**
@@ -36,7 +36,7 @@ class ObjectRemoveListener;
  *  - Providing a safe way to remove the object by calling the remove_me
  *    functions.
  */
-class GameObject : public RefCounter
+class GameObject
 {
 public:
   GameObject();
diff --git a/src/supertux/game_object_ptr.hpp b/src/supertux/game_object_ptr.hpp
new file mode 100644 (file)
index 0000000..d19a37f
--- /dev/null
@@ -0,0 +1,28 @@
+//  SuperTux
+//  Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
+//
+//  This program is free software: you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation, either version 3 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_PTR_HPP
+#define HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_PTR_HPP
+
+#include <memory>
+
+class GameObject;
+
+using GameObjectPtr = std::shared_ptr<GameObject>;
+
+#endif
+
+/* EOF */
index 9c06460..efe4c78 100644 (file)
@@ -567,12 +567,12 @@ GameSession::start_sequence(const std::string& sequencename)
 
   if (sequencename == "endsequence") {
     if (currentsector->get_players()[0]->get_physic().get_velocity_x() < 0) {
-      end_sequence = new EndSequenceWalkLeft();
+      end_sequence = std::make_shared<EndSequenceWalkLeft>();
     } else {
-      end_sequence = new EndSequenceWalkRight();
+      end_sequence = std::make_shared<EndSequenceWalkRight>();
     }
   } else if (sequencename == "fireworks") {
-    end_sequence = new EndSequenceFireworks();
+    end_sequence = std::make_shared<EndSequenceFireworks>();
   } else {
     log_warning << "Unknown sequence '" << sequencename << "'. Ignoring." << std::endl;
     return;
@@ -588,12 +588,12 @@ GameSession::start_sequence(const std::string& sequencename)
   currentsector->player->set_winning();
 
   // Stop all clocks.
-  for(std::vector<GameObject*>::iterator i = currentsector->gameobjects.begin();
+  for(auto i = currentsector->gameobjects.begin();
       i != currentsector->gameobjects.end(); ++i)
   {
-    GameObject* obj = *i;
+    GameObjectPtr obj = *i;
 
-    LevelTime* lt = dynamic_cast<LevelTime*> (obj);
+    auto lt = std::dynamic_pointer_cast<LevelTime>(obj);
     if(lt)
       lt->stop();
   }
index fdae705..4609f8b 100644 (file)
@@ -119,7 +119,7 @@ private:
   int levelnb;
   int pause_menu_frame;
 
-  EndSequence* end_sequence;
+  std::shared_ptr<EndSequence> end_sequence;
 
   bool  game_pause;
   float speed_before_pause;
index 2a3674e..386cd3f 100644 (file)
@@ -191,15 +191,14 @@ Level::get_total_coins()
   int total_coins = 0;
   for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i) {
     Sector* sector = *i;
-    for(Sector::GameObjects::iterator o = sector->gameobjects.begin();
-        o != sector->gameobjects.end(); ++o) {
-      Coin* coin = dynamic_cast<Coin*> (*o);
+    for(auto o = sector->gameobjects.begin(); o != sector->gameobjects.end(); ++o) {
+      Coin* coin = dynamic_cast<Coin*>(o->get());
       if(coin)
       {
         total_coins++;
         continue;
       }
-      BonusBlock *block = dynamic_cast<BonusBlock*> (*o);
+      BonusBlock *block = dynamic_cast<BonusBlock*>(o->get());
       if(block)
       {
         if (block->contents == BonusBlock::CONTENT_COIN)
@@ -214,7 +213,7 @@ Level::get_total_coins()
           continue;
         }
       }
-      GoldBomb *goldbomb = dynamic_cast<GoldBomb*> (*o);
+      GoldBomb *goldbomb = dynamic_cast<GoldBomb*>(o->get());
       if(goldbomb)
         total_coins += 10;
     }
@@ -235,7 +234,7 @@ int
 Level::get_total_secrets()
 {
   int total_secrets = 0;
-  for(Sectors::iterator i = sectors.begin(); i != sectors.end(); ++i)
+  for(auto i = sectors.begin(); i != sectors.end(); ++i)
     total_secrets += (*i)->get_total_count<SecretAreaTrigger>();
   return total_secrets;
 }
index 2c1fd84..3699f3c 100644 (file)
@@ -278,7 +278,7 @@ ObjectFactory::init_factories()
   add_factory<Switch>("switch");
 }
 
-GameObject*
+GameObjectPtr
 ObjectFactory::create(const std::string& name, const Reader& reader)
 {
   Factories::iterator i = factories.find(name);
@@ -295,7 +295,7 @@ ObjectFactory::create(const std::string& name, const Reader& reader)
   }
 }
 
-GameObject*
+GameObjectPtr
 ObjectFactory::create(const std::string& name, const Vector& pos, const Direction dir)
 {
   std::stringstream lisptext;
@@ -307,8 +307,7 @@ ObjectFactory::create(const std::string& name, const Vector& pos, const Directio
   lisp::Parser parser;
   const lisp::Lisp* lisp = parser.parse(lisptext, "create_object");
 
-  GameObject* object = create(name, *(lisp->get_car()));
-  return object;
+  return create(name, *(lisp->get_car()));
 }
 
 /* EOF */
index d6b1624..9e3d2f1 100644 (file)
@@ -23,6 +23,7 @@
 #include <memory>
 
 #include "supertux/direction.hpp"
+#include "supertux/game_object_ptr.hpp"
 #include "util/reader_fwd.hpp"
 
 class Vector;
@@ -37,7 +38,7 @@ public:
   /** Creates a new gameobject from a lisp node.
    * Remember to delete the objects later
    */
-  virtual GameObject* create(const Reader& reader) = 0;
+  virtual GameObjectPtr create(const Reader& reader) = 0;
 };
 
 template<class C>
@@ -47,9 +48,9 @@ public:
   ConcreteObjectFactory() {}
   ~ConcreteObjectFactory() {}
 
-  GameObject* create(const Reader& reader)
+  GameObjectPtr create(const Reader& reader)
   {
-    return new C(reader);
+    return std::make_shared<C>(reader);
   }
 };
 
@@ -66,8 +67,8 @@ public:
   ObjectFactory();
   ~ObjectFactory();
 
-  GameObject* create(const std::string& name, const Reader& reader);
-  GameObject* create(const std::string& name, const Vector& pos, const Direction dir = AUTO);
+  GameObjectPtr create(const std::string& name, const Reader& reader);
+  GameObjectPtr create(const std::string& name, const Vector& pos, const Direction dir = AUTO);
 
 private:
   template<class C>
index 519a2f4..3553d59 100644 (file)
@@ -87,9 +87,9 @@ Sector::Sector(Level* parent) :
   camera(0),
   effect(0)
 {
-  add_object(new Player(GameSession::current()->get_savegame().get_player_status(), "Tux"));
-  add_object(new DisplayEffect("Effect"));
-  add_object(new TextObject("Text"));
+  add_object(std::make_shared<Player>(GameSession::current()->get_savegame().get_player_status(), "Tux"));
+  add_object(std::make_shared<DisplayEffect>("Effect"));
+  add_object(std::make_shared<TextObject>("Text"));
 
   SoundManager::current()->preload("sounds/shoot.wav");
 
@@ -129,14 +129,9 @@ Sector::~Sector()
 
   for(GameObjects::iterator i = gameobjects.begin();
       i != gameobjects.end(); ++i) {
-    GameObject* object = *i;
+    GameObjectPtr object = *i;
     before_object_remove(object);
-    object->unref();
   }
-
-  for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
-      ++i)
-    delete *i;
 }
 
 Level*
@@ -145,41 +140,41 @@ Sector::get_level()
   return level;
 }
 
-GameObject*
+GameObjectPtr
 Sector::parse_object(const std::string& name_, const Reader& reader)
 {
   if(name_ == "camera") {
-    Camera* camera_ = new Camera(this, "Camera");
+    auto camera_ = std::make_shared<Camera>(this, "Camera");
     camera_->parse(reader);
     return camera_;
   } else if(name_ == "particles-snow") {
-    SnowParticleSystem* partsys = new SnowParticleSystem();
+    auto partsys = std::make_shared<SnowParticleSystem>();
     partsys->parse(reader);
     return partsys;
   } else if(name_ == "particles-rain") {
-    RainParticleSystem* partsys = new RainParticleSystem();
+    auto partsys = std::make_shared<RainParticleSystem>();
     partsys->parse(reader);
     return partsys;
   } else if(name_ == "particles-comets") {
-    CometParticleSystem* partsys = new CometParticleSystem();
+    auto partsys = std::make_shared<CometParticleSystem>();
     partsys->parse(reader);
     return partsys;
   } else if(name_ == "particles-ghosts") {
-    GhostParticleSystem* partsys = new GhostParticleSystem();
+    auto partsys = std::make_shared<GhostParticleSystem>();
     partsys->parse(reader);
     return partsys;
   } else if(name_ == "particles-clouds") {
-    CloudParticleSystem* partsys = new CloudParticleSystem();
+    auto partsys = std::make_shared<CloudParticleSystem>();
     partsys->parse(reader);
     return partsys;
   } else if(name_ == "money") { // for compatibility with old maps
-    return new Jumpy(reader);
+    return std::make_shared<Jumpy>(reader);
   } else {
     try {
       return ObjectFactory::instance().create(name_, reader);
     } catch(std::exception& e) {
       log_warning << e.what() << "" << std::endl;
-      return 0;
+      return {};
     }
   }
 }
@@ -198,7 +193,7 @@ Sector::parse(const Reader& sector)
     } else if(token == "music") {
       iter.value()->get(music);
     } else if(token == "spawnpoint") {
-      SpawnPoint* sp = new SpawnPoint(*iter.lisp());
+      auto sp = std::make_shared<SpawnPoint>(*iter.lisp());
       spawnpoints.push_back(sp);
     } else if(token == "init-script") {
       iter.value()->get(init_script);
@@ -211,11 +206,11 @@ Sector::parse(const Reader& sector)
         ambient_light = Color( vColor );
       }
     } else {
-      GameObject* object = parse_object(token, *(iter.lisp()));
+      GameObjectPtr object = parse_object(token, *(iter.lisp()));
       if(object) {
-        if(dynamic_cast<Background *>(object)) {
+        if(std::dynamic_pointer_cast<Background>(object)) {
           has_background = true;
-        } else if(dynamic_cast<Gradient *>(object)) {
+        } else if(std::dynamic_pointer_cast<Gradient>(object)) {
           has_background = true;
         }
         add_object(object);
@@ -224,7 +219,7 @@ Sector::parse(const Reader& sector)
   }
 
   if(!has_background) {
-    Gradient* gradient = new Gradient();
+    auto gradient = std::make_shared<Gradient>();
     gradient->set_gradient(Color(0.3, 0.4, 0.75), Color(1, 1, 1));
     add_object(gradient);
   }
@@ -237,7 +232,7 @@ Sector::parse(const Reader& sector)
   if(!camera) {
     log_warning << "sector '" << name << "' does not contain a camera." << std::endl;
     update_game_objects();
-    add_object(new Camera(this, "Camera"));
+    add_object(std::make_shared<Camera>(this, "Camera"));
   }
 
   update_game_objects();
@@ -282,11 +277,11 @@ Sector::parse_old_format(const Reader& reader)
   bkgd_bottom.blue = static_cast<float> (b) / 255.0f;
 
   if(backgroundimage != "") {
-    Background* background = new Background();
+    auto background = std::make_shared<Background>();
     background->set_image(backgroundimage, bgspeed);
     add_object(background);
   } else {
-    Gradient* gradient = new Gradient();
+    auto gradient = std::make_shared<Gradient>();
     gradient->set_gradient(bkgd_top, bkgd_bottom);
     add_object(gradient);
   }
@@ -294,17 +289,17 @@ Sector::parse_old_format(const Reader& reader)
   std::string particlesystem;
   reader.get("particle_system", particlesystem);
   if(particlesystem == "clouds")
-    add_object(new CloudParticleSystem());
+    add_object(std::make_shared<CloudParticleSystem>());
   else if(particlesystem == "snow")
-    add_object(new SnowParticleSystem());
+    add_object(std::make_shared<SnowParticleSystem>());
   else if(particlesystem == "rain")
-    add_object(new RainParticleSystem());
+    add_object(std::make_shared<RainParticleSystem>());
 
   Vector startpos(100, 170);
   reader.get("start_pos_x", startpos.x);
   reader.get("start_pos_y", startpos.y);
 
-  SpawnPoint* spawn = new SpawnPoint;
+  auto spawn = std::make_shared<SpawnPoint>();
   spawn->pos = startpos;
   spawn->name = "main";
   spawnpoints.push_back(spawn);
@@ -323,7 +318,7 @@ Sector::parse_old_format(const Reader& reader)
   std::vector<unsigned int> tiles;
   if(reader.get("interactive-tm", tiles)
      || reader.get("tilemap", tiles)) {
-    TileMap* tilemap = new TileMap(level->get_tileset());
+    auto tilemap = std::make_shared<TileMap>(level->get_tileset());
     tilemap->set(width, height, tiles, LAYER_TILES, true);
 
     // replace tile id 112 (old invisible tile) with 1311 (new invisible tile)
@@ -340,14 +335,14 @@ Sector::parse_old_format(const Reader& reader)
   }
 
   if(reader.get("background-tm", tiles)) {
-    TileMap* tilemap = new TileMap(level->get_tileset());
+    auto tilemap = std::make_shared<TileMap>(level->get_tileset());
     tilemap->set(width, height, tiles, LAYER_BACKGROUNDTILES, false);
     if (height < 19) tilemap->resize(width, 19);
     add_object(tilemap);
   }
 
   if(reader.get("foreground-tm", tiles)) {
-    TileMap* tilemap = new TileMap(level->get_tileset());
+    auto tilemap = std::make_shared<TileMap>(level->get_tileset());
     tilemap->set(width, height, tiles, LAYER_FOREGROUNDTILES, false);
 
     // fill additional space in foreground with tiles of ID 2035 (lightmap/black)
@@ -365,7 +360,7 @@ Sector::parse_old_format(const Reader& reader)
         Vector sp_pos;
         if(reader.get("x", sp_pos.x) && reader.get("y", sp_pos.y))
         {
-          SpawnPoint* sp = new SpawnPoint;
+          auto sp = std::make_shared<SpawnPoint>();
           sp->name = "main";
           sp->pos = sp_pos;
           spawnpoints.push_back(sp);
@@ -381,7 +376,7 @@ Sector::parse_old_format(const Reader& reader)
   if(objects) {
     lisp::ListIterator iter(objects);
     while(iter.next()) {
-      GameObject* object = parse_object(iter.item(), *(iter.lisp()));
+      auto object = parse_object(iter.item(), *(iter.lisp()));
       if(object) {
         add_object(object);
       } else {
@@ -391,7 +386,7 @@ Sector::parse_old_format(const Reader& reader)
   }
 
   // add a camera
-  Camera* camera_ = new Camera(this, "Camera");
+  auto camera_ = std::make_shared<Camera>(this, "Camera");
   add_object(camera_);
 
   update_game_objects();
@@ -414,27 +409,27 @@ Sector::fix_old_tiles()
         Vector pos = solids->get_tile_position(x, y);
 
         if(id == 112) {
-          add_object(new InvisibleBlock(pos));
+          add_object(std::make_shared<InvisibleBlock>(pos));
           solids->change(x, y, 0);
         } else if(tile->getAttributes() & Tile::COIN) {
-          add_object(new Coin(pos, solids));
+          add_object(std::make_shared<Coin>(pos, solids));
           solids->change(x, y, 0);
         } else if(tile->getAttributes() & Tile::FULLBOX) {
-          add_object(new BonusBlock(pos, tile->getData()));
+          add_object(std::make_shared<BonusBlock>(pos, tile->getData()));
           solids->change(x, y, 0);
         } else if(tile->getAttributes() & Tile::BRICK) {
           if( ( id == 78 ) || ( id == 105 ) ){
-            add_object( new Brick(pos, tile->getData(), "images/objects/bonus_block/brickIce.sprite") );
+            add_object( std::make_shared<Brick>(pos, tile->getData(), "images/objects/bonus_block/brickIce.sprite") );
           } else if( ( id == 77 ) || ( id == 104 ) ){
-            add_object( new Brick(pos, tile->getData(), "images/objects/bonus_block/brick.sprite") );
+            add_object( std::make_shared<Brick>(pos, tile->getData(), "images/objects/bonus_block/brick.sprite") );
           } else {
             log_warning << "attribute 'brick #t' is not supported for tile-id " << id << std::endl;
-            add_object( new Brick(pos, tile->getData(), "images/objects/bonus_block/brick.sprite") );
+            add_object( std::make_shared<Brick>(pos, tile->getData(), "images/objects/bonus_block/brick.sprite") );
           }
           solids->change(x, y, 0);
         } else if(tile->getAttributes() & Tile::GOAL) {
           std::string sequence = tile->getData() == 0 ? "endsequence" : "stoptux";
-          add_object(new SequenceTrigger(pos, sequence));
+          add_object(std::make_shared<SequenceTrigger>(pos, sequence));
           solids->change(x, y, 0);
         }
       }
@@ -443,7 +438,7 @@ Sector::fix_old_tiles()
 
   // add lights for special tiles
   for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); i++) {
-    TileMap* tm = dynamic_cast<TileMap*>(*i);
+    TileMap* tm = dynamic_cast<TileMap*>(i->get());
     if (!tm) continue;
     for(size_t x=0; x < tm->get_width(); ++x) {
       for(size_t y=0; y < tm->get_height(); ++y) {
@@ -454,7 +449,7 @@ Sector::fix_old_tiles()
         // torch
         if (id == 1517) {
           float pseudo_rnd = (float)((int)pos.x % 10) / 10;
-          add_object(new PulsingLight(center, 1.0f + pseudo_rnd, 0.9f, 1.0f, Color(1.0f, 1.0f, 0.6f, 1.0f)));
+          add_object(std::make_shared<PulsingLight>(center, 1.0f + pseudo_rnd, 0.9f, 1.0f, Color(1.0f, 1.0f, 0.6f, 1.0f)));
         }
         // lava or lavaflow
         if ((id == 173) || (id == 1700) || (id == 1705) || (id == 1706)) {
@@ -463,7 +458,7 @@ Sector::fix_old_tiles()
                && (tm->get_tile_id(x, y-1) != tm->get_tile_id(x,y)))
               || ((x % 3 == 0) && (y % 3 == 0))) {
             float pseudo_rnd = (float)((int)pos.x % 10) / 10;
-            add_object(new PulsingLight(center, 1.0f + pseudo_rnd, 0.8f, 1.0f, Color(1.0f, 0.3f, 0.0f, 1.0f)));
+            add_object(std::make_shared<PulsingLight>(center, 1.0f + pseudo_rnd, 0.8f, 1.0f, Color(1.0f, 0.3f, 0.0f, 1.0f)));
           }
         }
 
@@ -512,7 +507,7 @@ Sector::run_script(std::istream& in, const std::string& sourcename)
 }
 
 void
-Sector::add_object(GameObject* object)
+Sector::add_object(GameObjectPtr object)
 {
   // make sure the object isn't already in the list
 #ifndef NDEBUG
@@ -526,14 +521,13 @@ Sector::add_object(GameObject* object)
   }
 #endif
 
-  object->ref();
   gameobjects_new.push_back(object);
 }
 
 void
 Sector::activate(const std::string& spawnpoint)
 {
-  SpawnPoint* sp = 0;
+  std::shared_ptr<SpawnPoint> sp;
   for(SpawnPoints::iterator i = spawnpoints.begin(); i != spawnpoints.end();
       ++i) {
     if((*i)->name == spawnpoint) {
@@ -570,9 +564,8 @@ Sector::activate(const Vector& player_pos)
       throw scripting::SquirrelError(vm, "Couldn't set sector in roottable");
     sq_pop(vm, 1);
 
-    for(GameObjects::iterator i = gameobjects.begin();
-        i != gameobjects.end(); ++i) {
-      GameObject* object = *i;
+    for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) {
+      GameObjectPtr object = *i;
 
       try_expose(object);
     }
@@ -582,9 +575,8 @@ Sector::activate(const Vector& player_pos)
 
   // two-player hack: move other players to main player's position
   // Maybe specify 2 spawnpoints in the level?
-  for(GameObjects::iterator i = gameobjects.begin();
-      i != gameobjects.end(); ++i) {
-    Player* p = dynamic_cast<Player*>(*i);
+  for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) {
+    Player* p = dynamic_cast<Player*>(i->get());
     if (!p) continue;
 
     // spawn smalltux below spawnpoint
@@ -646,9 +638,8 @@ Sector::deactivate()
     throw scripting::SquirrelError(vm, "Couldn't unset sector in roottable");
   sq_pop(vm, 1);
 
-  for(GameObjects::iterator i = gameobjects.begin();
-      i != gameobjects.end(); ++i) {
-    GameObject* object = *i;
+  for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) {
+    GameObjectPtr object = *i;
 
     try_unexpose(object);
   }
@@ -671,9 +662,8 @@ Sector::update(float elapsed_time)
   player->check_bounds();
 
   /* update objects */
-  for(GameObjects::iterator i = gameobjects.begin();
-      i != gameobjects.end(); ++i) {
-    GameObject* object = *i;
+  for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) {
+    GameObjectPtr& object = *i;
     if(!object->is_valid())
       continue;
 
@@ -689,9 +679,9 @@ void
 Sector::update_game_objects()
 {
   /** cleanup marked objects */
-  for(std::vector<GameObject*>::iterator i = gameobjects.begin();
+  for(auto i = gameobjects.begin();
       i != gameobjects.end(); /* nothing */) {
-    GameObject* object = *i;
+    GameObjectPtr& object = *i;
 
     if(object->is_valid()) {
       ++i;
@@ -700,15 +690,14 @@ Sector::update_game_objects()
 
     before_object_remove(object);
 
-    object->unref();
     i = gameobjects.erase(i);
   }
 
   /* add newly created objects */
-  for(std::vector<GameObject*>::iterator i = gameobjects_new.begin();
+  for(auto i = gameobjects_new.begin();
       i != gameobjects_new.end(); ++i)
   {
-    GameObject* object = *i;
+    GameObjectPtr object = *i;
 
     before_object_add(object);
 
@@ -719,10 +708,9 @@ Sector::update_game_objects()
   /* update solid_tilemaps list */
   //FIXME: this could be more efficient
   solid_tilemaps.clear();
-  for(std::vector<GameObject*>::iterator i = gameobjects.begin();
-      i != gameobjects.end(); ++i)
+  for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i)
   {
-    TileMap* tm = dynamic_cast<TileMap*>(*i);
+    TileMap* tm = dynamic_cast<TileMap*>(i->get());
     if (!tm) continue;
     if (tm->is_solid()) solid_tilemaps.push_back(tm);
   }
@@ -730,30 +718,33 @@ Sector::update_game_objects()
 }
 
 bool
-Sector::before_object_add(GameObject* object)
+Sector::before_object_add(GameObjectPtr object)
 {
-  Bullet* bullet = dynamic_cast<Bullet*> (object);
-  if(bullet != NULL) {
+  auto bullet = dynamic_cast<Bullet*>(object.get());
+  if (bullet)
+  {
     bullets.push_back(bullet);
   }
 
-  MovingObject* movingobject = dynamic_cast<MovingObject*> (object);
-  if(movingobject != NULL) {
+  auto movingobject = dynamic_cast<MovingObject*>(object.get());
+  if (movingobject)
+  {
     moving_objects.push_back(movingobject);
   }
 
-  Portable* portable = dynamic_cast<Portable*> (object);
-  if(portable != NULL) {
+  auto portable = dynamic_cast<Portable*>(object.get());
+  if(portable)
+  {
     portables.push_back(portable);
   }
 
-  TileMap* tilemap = dynamic_cast<TileMap*> (object);
-  if(tilemap != NULL && tilemap->is_solid()) {
+  auto tilemap = dynamic_cast<TileMap*>(object.get());
+  if(tilemap && tilemap->is_solid()) {
     solid_tilemaps.push_back(tilemap);
   }
 
-  Camera* camera_ = dynamic_cast<Camera*> (object);
-  if(camera_ != NULL) {
+  auto camera_ = dynamic_cast<Camera*>(object.get());
+  if(camera_) {
     if(this->camera != 0) {
       log_warning << "Multiple cameras added. Ignoring" << std::endl;
       return false;
@@ -761,8 +752,8 @@ Sector::before_object_add(GameObject* object)
     this->camera = camera_;
   }
 
-  Player* player_ = dynamic_cast<Player*> (object);
-  if(player_ != NULL) {
+  auto player_ = dynamic_cast<Player*>(object.get());
+  if(player_) {
     if(this->player != 0) {
       log_warning << "Multiple players added. Ignoring" << std::endl;
       return false;
@@ -770,8 +761,8 @@ Sector::before_object_add(GameObject* object)
     this->player = player_;
   }
 
-  DisplayEffect* effect_ = dynamic_cast<DisplayEffect*> (object);
-  if(effect_ != NULL) {
+  auto effect_ = dynamic_cast<DisplayEffect*>(object.get());
+  if(effect_) {
     if(this->effect != 0) {
       log_warning << "Multiple DisplayEffects added. Ignoring" << std::endl;
       return false;
@@ -787,9 +778,9 @@ Sector::before_object_add(GameObject* object)
 }
 
 void
-Sector::try_expose(GameObject* object)
+Sector::try_expose(GameObjectPtr object)
 {
-  ScriptInterface* object_ = dynamic_cast<ScriptInterface*> (object);
+  ScriptInterface* object_ = dynamic_cast<ScriptInterface*>(object.get());
   if(object_ != NULL) {
     HSQUIRRELVM vm = scripting::global_vm;
     sq_pushobject(vm, sector_table);
@@ -803,24 +794,24 @@ Sector::try_expose_me()
 {
   HSQUIRRELVM vm = scripting::global_vm;
   sq_pushobject(vm, sector_table);
-  scripting::SSector* this_ = static_cast<scripting::SSector*> (this);
+  scripting::SSector* this_ = static_cast<scripting::SSector*>(this);
   expose_object(vm, -1, this_, "settings", false);
   sq_pop(vm, 1);
 }
 
 void
-Sector::before_object_remove(GameObject* object)
+Sector::before_object_remove(GameObjectPtr object)
 {
-  Portable* portable = dynamic_cast<Portable*> (object);
-  if(portable != NULL) {
+  Portable* portable = dynamic_cast<Portable*>(object.get());
+  if (portable) {
     portables.erase(std::find(portables.begin(), portables.end(), portable));
   }
-  Bullet* bullet = dynamic_cast<Bullet*> (object);
-  if(bullet != NULL) {
+  Bullet* bullet = dynamic_cast<Bullet*>(object.get());
+  if (bullet) {
     bullets.erase(std::find(bullets.begin(), bullets.end(), bullet));
   }
-  MovingObject* moving_object = dynamic_cast<MovingObject*> (object);
-  if(moving_object != NULL) {
+  MovingObject* moving_object = dynamic_cast<MovingObject*>(object.get());
+  if (moving_object) {
     moving_objects.erase(
       std::find(moving_objects.begin(), moving_objects.end(), moving_object));
   }
@@ -830,9 +821,9 @@ Sector::before_object_remove(GameObject* object)
 }
 
 void
-Sector::try_unexpose(GameObject* object)
+Sector::try_unexpose(GameObjectPtr object)
 {
-  ScriptInterface* object_ = dynamic_cast<ScriptInterface*> (object);
+  ScriptInterface* object_ = dynamic_cast<ScriptInterface*>(object.get());
   if(object_ != NULL) {
     HSQUIRRELVM vm = scripting::global_vm;
     SQInteger oldtop = sq_gettop(vm);
@@ -866,15 +857,14 @@ Sector::draw(DrawingContext& context)
   context.push_transform();
   context.set_translation(camera->get_translation());
 
-  for(GameObjects::iterator i = gameobjects.begin();
-      i != gameobjects.end(); ++i) {
-    GameObject* object = *i;
+  for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) {
+    GameObjectPtr& object = *i;
     if(!object->is_valid())
       continue;
 
     if (draw_solids_only)
     {
-      TileMap* tm = dynamic_cast<TileMap*>(object);
+      TileMap* tm = dynamic_cast<TileMap*>(object.get());
       if (tm && !tm->is_solid())
         continue;
     }
@@ -1459,14 +1449,12 @@ bool
 Sector::add_bullet(const Vector& pos, const PlayerStatus* player_status, float xm, Direction dir)
 {
   // TODO remove this function and move these checks elsewhere...
-
-  Bullet* new_bullet = 0;
   if((player_status->bonus == FIRE_BONUS &&
       (int)bullets.size() >= player_status->max_fire_bullets) ||
      (player_status->bonus == ICE_BONUS &&
       (int)bullets.size() >= player_status->max_ice_bullets))
     return false;
-  new_bullet = new Bullet(pos, xm, dir, player_status->bonus);
+  auto new_bullet = std::make_shared<Bullet>(pos, xm, dir, player_status->bonus);
   add_object(new_bullet);
 
   SoundManager::current()->play("sounds/shoot.wav");
@@ -1477,7 +1465,7 @@ Sector::add_bullet(const Vector& pos, const PlayerStatus* player_status, float x
 bool
 Sector::add_smoke_cloud(const Vector& pos)
 {
-  add_object(new SmokeCloud(pos));
+  add_object(std::make_shared<SmokeCloud>(pos));
   return true;
 }
 
@@ -1511,9 +1499,8 @@ int
 Sector::get_total_badguys()
 {
   int total_badguys = 0;
-  for(GameObjects::iterator i = gameobjects.begin();
-      i != gameobjects.end(); ++i) {
-    BadGuy* badguy = dynamic_cast<BadGuy*> (*i);
+  for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) {
+    BadGuy* badguy = dynamic_cast<BadGuy*>(i->get());
     if (badguy && badguy->countMe)
       total_badguys++;
   }
index 71232d7..4388c85 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "scripting/ssector.hpp"
 #include "supertux/direction.hpp"
+#include "supertux/game_object_ptr.hpp"
 #include "util/reader_fwd.hpp"
 #include "util/writer_fwd.hpp"
 #include "util/currenton.hpp"
@@ -93,7 +94,7 @@ public:
   HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
 
   /// adds a gameobject
-  void add_object(GameObject* object);
+  void add_object(GameObjectPtr object);
 
   void set_name(const std::string& name_)
   { this->name = name_; }
@@ -124,7 +125,7 @@ public:
   {
     int total = 0;
     for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) {
-      if (dynamic_cast<T*>(*i)) total++;
+      if (dynamic_cast<T*>(i->get())) total++;
     }
     return total;
   }
@@ -184,9 +185,9 @@ public:
    */
   void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
 
-  typedef std::vector<GameObject*> GameObjects;
+  typedef std::vector<GameObjectPtr> GameObjects;
   typedef std::vector<MovingObject*> MovingObjects;
-  typedef std::vector<SpawnPoint*> SpawnPoints;
+  typedef std::vector<std::shared_ptr<SpawnPoint> > SpawnPoints;
   typedef std::vector<Portable*> Portables;
 
   // --- scripting ---
@@ -207,11 +208,11 @@ public:
 private:
   uint32_t collision_tile_attributes(const Rectf& dest) const;
 
-  void before_object_remove(GameObject* object);
-  bool before_object_add(GameObject* object);
+  void before_object_remove(GameObjectPtr object);
+  bool before_object_add(GameObjectPtr object);
 
-  void try_expose(GameObject* object);
-  void try_unexpose(GameObject* object);
+  void try_expose(GameObjectPtr object);
+  void try_unexpose(GameObjectPtr object);
   void try_expose_me();
   void try_unexpose_me();
 
@@ -240,7 +241,7 @@ private:
 
   void collision_static_constrains(MovingObject& object);
 
-  GameObject* parse_object(const std::string& name, const Reader& lisp);
+  GameObjectPtr parse_object(const std::string& name, const Reader& lisp);
 
   void fix_old_tiles();
 
index 3356099..85872b5 100644 (file)
@@ -94,7 +94,7 @@ SecretAreaTrigger::event(Player& , EventType type)
         // fade away tilemaps
         Sector& sector = *Sector::current();
         for(Sector::GameObjects::iterator i = sector.gameobjects.begin(); i != sector.gameobjects.end(); ++i) {
-          TileMap* tm = dynamic_cast<TileMap*>(*i);
+          TileMap* tm = dynamic_cast<TileMap*>(i->get());
           if (!tm) continue;
           if (tm->get_name() != fade_tilemap) continue;
           tm->fade(0.0, 1.0);
diff --git a/src/util/ref.hpp b/src/util/ref.hpp
deleted file mode 100644 (file)
index 199cb2c..0000000
+++ /dev/null
@@ -1,89 +0,0 @@
-//  SuperTux
-//  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
-//
-//  This program is free software: you can redistribute it and/or modify
-//  it under the terms of the GNU General Public License as published by
-//  the Free Software Foundation, either version 3 of the License, or
-//  (at your option) any later version.
-//
-//  This program is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-//  GNU General Public License for more details.
-//
-//  You should have received a copy of the GNU General Public License
-//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-#ifndef HEADER_SUPERTUX_UTIL_REF_HPP
-#define HEADER_SUPERTUX_UTIL_REF_HPP
-
-/** This class behaves like a pointer to a refcounted object, but increments the
- * reference count when new objects are assigned and decrements the refcounter
- * when its lifetime has expired. (similar to std::unique_ptr)
- */
-template<typename T>
-class Ref
-{
-public:
-  Ref(T* object_ = 0)
-    : object(object_)
-  {
-    if(object)
-      object->ref();
-  }
-  Ref(const Ref<T>& other)
-    : object(other.object)
-  {
-    if(object)
-      object->ref();
-  }
-  ~Ref()
-  {
-    if(object)
-      object->unref();
-  }
-
-  Ref<T>& operator= (const Ref<T>& other)
-  {
-    *this = other.get();
-    return *this;
-  }
-
-  Ref<T>& operator= (T* object_)
-  {
-    if(object_)
-      object_->ref();
-    if(this->object)
-      this->object->unref();
-    this->object = object_;
-
-    return *this;
-  }
-
-  T* operator ->() const
-  {
-    return object;
-  }
-
-  T& operator* () const
-  {
-    return *object;
-  }
-
-  operator const T* () const
-  {
-    return object;
-  }
-
-  T* get() const
-  {
-    return object;
-  }
-
-private:
-  T* object;
-};
-
-#endif
-
-/* EOF */
diff --git a/src/util/refcounter.hpp b/src/util/refcounter.hpp
deleted file mode 100644 (file)
index 1aa5c89..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-//  Windstille - A Jump'n Shoot Game
-//  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
-//
-//  This program is free software: you can redistribute it and/or modify
-//  it under the terms of the GNU General Public License as published by
-//  the Free Software Foundation, either version 3 of the License, or
-//  (at your option) any later version.
-//
-//  This program is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-//  GNU General Public License for more details.
-//
-//  You should have received a copy of the GNU General Public License
-//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-
-#ifndef HEADER_SUPERTUX_UTIL_REFCOUNTER_HPP
-#define HEADER_SUPERTUX_UTIL_REFCOUNTER_HPP
-
-#include <assert.h>
-
-/**
- * A base class that provides reference counting facilities
- */
-class RefCounter
-{
-public:
-  RefCounter()
-    : refcount(0)
-  { }
-
-  /** increases reference count */
-  void ref()
-  {
-    refcount++;
-  }
-  /** decreases reference count. Destroys the object if the reference count
-   * reaches 0
-   */
-  void unref()
-  {
-    refcount--;
-    if(refcount <= 0) {
-      delete this;
-    }
-  }
-
-protected:
-  virtual ~RefCounter()
-  {
-    assert(refcount == 0);
-  }
-
-private:
-  int refcount;
-};
-
-#endif
-
-/* EOF */
index e6b479a..3300fa8 100644 (file)
@@ -111,7 +111,7 @@ WorldMap::WorldMap(const std::string& filename, Savegame& savegame, const std::s
   last_position(),
   last_target_time()
 {
-  tux = new Tux(this);
+  tux = std::make_shared<Tux>(this);
   add_object(tux);
 
   name = "<no title>";
@@ -150,9 +150,8 @@ WorldMap::~WorldMap()
 
   for(GameObjects::iterator i = game_objects.begin();
       i != game_objects.end(); ++i) {
-    GameObject* object = *i;
+    GameObjectPtr& object = *i;
     try_unexpose(object);
-    object->unref();
   }
 
   for(SpawnPoints::iterator i = spawn_points.begin();
@@ -174,22 +173,21 @@ WorldMap::~WorldMap()
 }
 
 void
-WorldMap::add_object(GameObject* object)
+WorldMap::add_object(GameObjectPtr object)
 {
-  TileMap* tilemap = dynamic_cast<TileMap*> (object);
+  TileMap* tilemap = dynamic_cast<TileMap*>(object.get());
   if(tilemap != 0 && tilemap->is_solid()) {
     solid_tilemaps.push_back(tilemap);
   }
 
-  object->ref();
   try_expose(object);
   game_objects.push_back(object);
 }
 
 void
-WorldMap::try_expose(GameObject* object)
+WorldMap::try_expose(const GameObjectPtr& object)
 {
-  ScriptInterface* object_ = dynamic_cast<ScriptInterface*> (object);
+  ScriptInterface* object_ = dynamic_cast<ScriptInterface*>(object.get());
   if(object_ != NULL) {
     HSQUIRRELVM vm = scripting::global_vm;
     sq_pushobject(vm, worldmap_table);
@@ -199,9 +197,9 @@ WorldMap::try_expose(GameObject* object)
 }
 
 void
-WorldMap::try_unexpose(GameObject* object)
+WorldMap::try_unexpose(const GameObjectPtr& object)
 {
-  ScriptInterface* object_ = dynamic_cast<ScriptInterface*> (object);
+  ScriptInterface* object_ = dynamic_cast<ScriptInterface*>(object.get());
   if(object_ != NULL) {
     HSQUIRRELVM vm = scripting::global_vm;
     SQInteger oldtop = sq_gettop(vm);
@@ -287,9 +285,9 @@ WorldMap::load(const std::string& filename)
     lisp::ListIterator iter(sector);
     while(iter.next()) {
       if(iter.item() == "tilemap") {
-        add_object(new TileMap(*(iter.lisp())));
+        add_object(std::make_shared<TileMap>(*(iter.lisp())));
       } else if(iter.item() == "background") {
-        add_object(new Background(*(iter.lisp())));
+        add_object(std::make_shared<Background>(*(iter.lisp())));
       } else if(iter.item() == "music") {
         iter.value()->get(music);
       } else if(iter.item() == "init-script") {
@@ -298,23 +296,23 @@ WorldMap::load(const std::string& filename)
         SpawnPoint* sp = new SpawnPoint(*iter.lisp());
         spawn_points.push_back(sp);
       } else if(iter.item() == "level") {
-        LevelTile* level = new LevelTile(levels_path, *iter.lisp());
-        levels.push_back(level);
+        auto level = std::make_shared<LevelTile>(levels_path, *iter.lisp());
+        levels.push_back(level.get());
         add_object(level);
       } else if(iter.item() == "special-tile") {
-        SpecialTile* special_tile = new SpecialTile(*iter.lisp());
-        special_tiles.push_back(special_tile);
+        auto special_tile = std::make_shared<SpecialTile>(*iter.lisp());
+        special_tiles.push_back(special_tile.get());
         add_object(special_tile);
       } else if(iter.item() == "sprite-change") {
-        SpriteChange* sprite_change = new SpriteChange(*iter.lisp());
-        sprite_changes.push_back(sprite_change);
+        auto sprite_change = std::make_shared<SpriteChange>(*iter.lisp());
+        sprite_changes.push_back(sprite_change.get());
         add_object(sprite_change);
       } else if(iter.item() == "teleporter") {
-        Teleporter* teleporter = new Teleporter(*iter.lisp());
-        teleporters.push_back(teleporter);
+        auto teleporter = std::make_shared<Teleporter>(*iter.lisp());
+        teleporters.push_back(teleporter.get());
         add_object(teleporter);
       } else if(iter.item() == "decal") {
-        Decal* decal = new Decal(*iter.lisp());
+        auto decal = std::make_shared<Decal>(*iter.lisp());
         add_object(decal);
       } else if(iter.item() == "ambient-light") {
         std::vector<float> vColor;
@@ -574,7 +572,7 @@ WorldMap::update(float delta)
   {
     // update GameObjects
     for(size_t i = 0; i < game_objects.size(); ++i) {
-      GameObject* object = game_objects[i];
+      GameObjectPtr& object = game_objects[i];
       if(!panning || object != tux) {
         object->update(delta);
       }
@@ -583,10 +581,9 @@ WorldMap::update(float delta)
     // remove old GameObjects
     for(GameObjects::iterator i = game_objects.begin();
         i != game_objects.end(); ) {
-      GameObject* object = *i;
+      GameObjectPtr& object = *i;
       if(!object->is_valid()) {
         try_unexpose(object);
-        object->unref();
         i = game_objects.erase(i);
       } else {
         ++i;
@@ -596,10 +593,9 @@ WorldMap::update(float delta)
     /* update solid_tilemaps list */
     //FIXME: this could be more efficient
     solid_tilemaps.clear();
-    for(std::vector<GameObject*>::iterator i = game_objects.begin();
-        i != game_objects.end(); ++i)
+    for(auto i = game_objects.begin(); i != game_objects.end(); ++i)
     {
-      TileMap* tm = dynamic_cast<TileMap*>(*i);
+      TileMap* tm = dynamic_cast<TileMap*>(i->get());
       if (!tm) continue;
       if (tm->is_solid()) solid_tilemaps.push_back(tm);
     }
@@ -798,9 +794,9 @@ WorldMap::draw(DrawingContext& context)
   context.push_transform();
   context.set_translation(camera_offset);
 
-  for(GameObjects::iterator i = game_objects.begin();
-      i != game_objects.end(); ++i) {
-    GameObject* object = *i;
+  for(auto i = game_objects.begin(); i != game_objects.end(); ++i)
+  {
+    GameObjectPtr& object = *i;
     if(!panning || object != tux) {
       object->draw(context);
     }
index b0e56ac..c42e551 100644 (file)
 #include <vector>
 
 #include "control/controller.hpp"
-#include "util/reader_fwd.hpp"
 #include "math/vector.hpp"
 #include "supertux/console.hpp"
 #include "supertux/game_object.hpp"
+#include "supertux/game_object_ptr.hpp"
 #include "supertux/level.hpp"
 #include "supertux/screen.hpp"
 #include "supertux/statistics.hpp"
 #include "supertux/tile_manager.hpp"
 #include "supertux/timer.hpp"
+#include "util/reader_fwd.hpp"
 #include "worldmap/direction.hpp"
 #include "worldmap/spawn_point.hpp"
 #include "worldmap/special_tile.hpp"
@@ -73,10 +74,10 @@ private:
   typedef std::vector<SpriteChange*> SpriteChanges;
   typedef std::vector<SpawnPoint*> SpawnPoints;
   typedef std::vector<LevelTile*> LevelTiles;
-  typedef std::vector<GameObject*> GameObjects;
+  typedef std::vector<GameObjectPtr> GameObjects;
   typedef std::vector<HSQOBJECT> ScriptList;
 
-  Tux* tux;
+  std::shared_ptr<Tux> tux;
 
   Savegame& m_savegame;
 
@@ -127,10 +128,10 @@ public:
   WorldMap(const std::string& filename, Savegame& savegame, const std::string& force_spawnpoint = "");
   ~WorldMap();
 
-  void add_object(GameObject* object);
+  void add_object(GameObjectPtr object);
 
-  void try_expose(GameObject* object);
-  void try_unexpose(GameObject* object);
+  void try_expose(const GameObjectPtr& object);
+  void try_unexpose(const GameObjectPtr& object);
 
   static WorldMap* current()
   { return current_; }
@@ -167,7 +168,7 @@ public:
   void finished_level(Level* level);
 
   /** returns current Tux incarnation */
-  Tux* get_tux() { return tux; }
+  Tux* get_tux() { return tux.get(); }
 
   Savegame& get_savegame() { return m_savegame; }