From: Ingo Ruhnke Date: Mon, 1 Sep 2014 19:37:16 +0000 (+0200) Subject: Replaced Ref and RefCounter with std::shared_ptr<> X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=5c7020e504303cf18e472283ab614ef08ad43b2c;p=supertux.git Replaced Ref and RefCounter with std::shared_ptr<> --- diff --git a/src/badguy/bomb.cpp b/src/badguy/bomb.cpp index 629b75b57..86ef0e728 100644 --- a/src/badguy/bomb.cpp +++ b/src/badguy/bomb.cpp @@ -90,7 +90,7 @@ Bomb::explode() if(is_valid()) { remove_me(); - Explosion* explosion = new Explosion(get_bbox().get_middle()); + auto explosion = std::make_shared(get_bbox().get_middle()); Sector::current()->add_object(explosion); } diff --git a/src/badguy/darttrap.cpp b/src/badguy/darttrap.cpp index 3885cb684..1fb822edd 100644 --- a/src/badguy/darttrap.cpp +++ b/src/badguy/darttrap.cpp @@ -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(Vector(px, py), dir, this)); state = IDLE; sprite->set_action(dir == LEFT ? "idle-left" : "idle-right"); } diff --git a/src/badguy/dispenser.cpp b/src/badguy/dispenser.cpp index e3deca06a..1f48673bd 100644 --- a/src/badguy/dispenser.cpp +++ b/src/badguy/dispenser.cpp @@ -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 (game_object); - if (bad_guy == NULL) - throw std::runtime_error(badguy + " is not a badguy."); + BadGuy& bad_guy = dynamic_cast(*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; } diff --git a/src/badguy/flame.cpp b/src/badguy/flame.cpp index 66ce6d316..d0406ea54 100644 --- a/src/badguy/flame.cpp +++ b/src/badguy/flame.cpp @@ -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("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 diff --git a/src/badguy/flyingsnowball.cpp b/src/badguy/flyingsnowball.cpp index 1102e9aad..577078b61 100644 --- a/src/badguy/flyingsnowball.cpp +++ b/src/badguy/flyingsnowball.cpp @@ -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("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); diff --git a/src/badguy/ghosttree.cpp b/src/badguy/ghosttree.cpp index 4e8832be1..088d73bd1 100644 --- a/src/badguy/ghosttree.cpp +++ b/src/badguy/ghosttree.cpp @@ -70,10 +70,9 @@ GhostTree::die() sprite->set_action("dying", 1); glow_sprite->set_action("dying", 1); - std::vector::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::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(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(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& 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(get_bbox().get_middle() + SUCK_TARGET_OFFSET); Sector::current()->add_object(lantern); } diff --git a/src/badguy/ghosttree.hpp b/src/badguy/ghosttree.hpp index d71cad891..e902cc84d 100644 --- a/src/badguy/ghosttree.hpp +++ b/src/badguy/ghosttree.hpp @@ -62,7 +62,7 @@ private: Lantern* suck_lantern; /**< Lantern that is currently being sucked in */ - std::vector willowisps; + std::vector > willowisps; bool is_color_deadly(Color color) const; void spawn_lantern(); diff --git a/src/badguy/goldbomb.cpp b/src/badguy/goldbomb.cpp index 67c8002e4..87000f78e 100644 --- a/src/badguy/goldbomb.cpp +++ b/src/badguy/goldbomb.cpp @@ -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(get_bbox().get_middle())); + Sector::current()->add_object(std::make_shared(get_pos() + Vector (0, -40))); } run_dead_script(); diff --git a/src/badguy/haywire.cpp b/src/badguy/haywire.cpp index f3d7242b2..01a4b9179 100644 --- a/src/badguy/haywire.cpp +++ b/src/badguy/haywire.cpp @@ -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(get_bbox().get_middle()); Sector::current()->add_object(explosion); } diff --git a/src/badguy/iceflame.cpp b/src/badguy/iceflame.cpp index 630da0a44..be51c7865 100644 --- a/src/badguy/iceflame.cpp +++ b/src/badguy/iceflame.cpp @@ -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("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 diff --git a/src/badguy/igel.cpp b/src/badguy/igel.cpp index 1fc48c0c7..457b22d7f 100644 --- a/src/badguy/igel.cpp +++ b/src/badguy/igel.cpp @@ -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(*i); + Bullet* bullet = dynamic_cast(i->get()); if (!bullet) continue; if (bullet->get_type() != FIRE_BONUS) continue; if (can_see(*bullet)) wants_to_flee = true; diff --git a/src/badguy/livefire.cpp b/src/badguy/livefire.cpp index 55c91ad6e..dd9c1655f 100644 --- a/src/badguy/livefire.cpp +++ b/src/badguy/livefire.cpp @@ -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("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); diff --git a/src/badguy/mole.cpp b/src/badguy/mole.cpp index f6ab193cc..fd6601253 100644 --- a/src/badguy/mole.cpp +++ b/src/badguy/mole.cpp @@ -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(Vector(px, py), Vector(vx, vy), this)); } void diff --git a/src/badguy/mrbomb.cpp b/src/badguy/mrbomb.cpp index d9688ddbb..b3b4a9e69 100644 --- a/src/badguy/mrbomb.cpp +++ b/src/badguy/mrbomb.cpp @@ -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(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(get_bbox().get_middle()); Sector::current()->add_object(explosion); } diff --git a/src/badguy/mrtree.cpp b/src/badguy/mrtree.cpp index 74c46a567..500bd30ec 100644 --- a/src/badguy/mrtree.cpp +++ b/src/badguy/mrtree.cpp @@ -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_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("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(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(leaf2_bbox.p1, RIGHT); leaf2->countMe = false; Sector::current()->add_object(leaf2); } diff --git a/src/badguy/owl.cpp b/src/badguy/owl.cpp index 54c9433a8..d7d2a53e2 100644 --- a/src/badguy/owl.cpp +++ b/src/badguy/owl.cpp @@ -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 (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(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) diff --git a/src/badguy/poisonivy.cpp b/src/badguy/poisonivy.cpp index c8973af19..799d040c8 100644 --- a/src/badguy/poisonivy.cpp +++ b/src/badguy/poisonivy.cpp @@ -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("images/objects/particles/poisonivy.sprite", + "default", + ppos, ANCHOR_MIDDLE, + pspeed, paccel, + LAYER_OBJECTS-1)); } kill_squished(object); return true; diff --git a/src/badguy/short_fuse.cpp b/src/badguy/short_fuse.cpp index 0bd6472f4..f92ec4867 100644 --- a/src/badguy/short_fuse.cpp +++ b/src/badguy/short_fuse.cpp @@ -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(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 diff --git a/src/badguy/skydive.cpp b/src/badguy/skydive.cpp index e1305575d..d707636a9 100644 --- a/src/badguy/skydive.cpp +++ b/src/badguy/skydive.cpp @@ -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(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 */ diff --git a/src/badguy/snowman.cpp b/src/badguy/snowman.cpp index 7edc1c14e..e0fa39a1e 100644 --- a/src/badguy/snowman.cpp +++ b/src/badguy/snowman.cpp @@ -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_pos, dir, dead_script); Sector::current()->add_object(snowball); } diff --git a/src/badguy/stumpy.cpp b/src/badguy/stumpy.cpp index 7eb6c4e76..921d9d29a 100644 --- a/src/badguy/stumpy.cpp +++ b/src/badguy/stumpy.cpp @@ -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("images/objects/particles/bark.sprite", + "default", + ppos, ANCHOR_MIDDLE, + pspeed, paccel, + LAYER_OBJECTS-1)); } return true; diff --git a/src/badguy/yeti.cpp b/src/badguy/yeti.cpp index e958d53b5..3e5c420de 100644 --- a/src/badguy/yeti.cpp +++ b/src/badguy/yeti.cpp @@ -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 (*i); + YetiStalactite* stalactite = dynamic_cast(i->get()); if(stalactite && stalactite->is_hanging()) { float distancex; if (hit_points >= 3) { diff --git a/src/control/joystick_manager.cpp b/src/control/joystick_manager.cpp index b6a31c260..c0d4196e8 100644 --- a/src/control/joystick_manager.cpp +++ b/src/control/joystick_manager.cpp @@ -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); diff --git a/src/object/bicycle_platform.cpp b/src/object/bicycle_platform.cpp index 43a8a6836..4ff074ebc 100644 --- a/src/object/bicycle_platform.cpp +++ b/src/object/bicycle_platform.cpp @@ -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(this)); return; } if (!master) { diff --git a/src/object/block.cpp b/src/object/block.cpp index c8d5338bf..1e115d904 100644 --- a/src/object/block.cpp +++ b/src/object/block.cpp @@ -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(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(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(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(sprite->clone(), get_pos() + Vector(16, 16), + Vector(150, -300))); remove_me(); } diff --git a/src/object/bonus_block.cpp b/src/object/bonus_block.cpp index dbcc20c07..29f6fa648 100644 --- a/src/object/bonus_block.cpp +++ b/src/object/bonus_block.cpp @@ -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(get_pos(), true); break; case 9: contents = CONTENT_CUSTOM; - object = new Rock(get_pos(), "images/objects/rock/rock.sprite"); + object = std::make_shared(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(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 (game_object); + GameObjectPtr game_object = ObjectFactory::instance().create(token, *(iter.lisp())); + object = std::dynamic_pointer_cast(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(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(get_pos(), std::make_shared(direction)); sector->add_object(riser); } else { - SpecialRiser* riser = new SpecialRiser( - get_pos(), new Flower(FIRE_BONUS)); + auto riser = std::make_shared( + get_pos(), std::make_shared(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(get_pos(), std::make_shared(direction)); sector->add_object(riser); } else { - SpecialRiser* riser = new SpecialRiser( - get_pos(), new Flower(ICE_BONUS)); + auto riser = std::make_shared( + get_pos(), std::make_shared(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(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(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(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(get_pos(), std::make_shared(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(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(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(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(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(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(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(get_pos() + Vector (0, 40))); SoundManager::current()->play("sounds/upgrade.wav"); countdown = true; break; diff --git a/src/object/bonus_block.hpp b/src/object/bonus_block.hpp index 5dd506e81..816fa1939 100644 --- a/src/object/bonus_block.hpp +++ b/src/object/bonus_block.hpp @@ -50,7 +50,7 @@ protected: public: Contents contents; - MovingObject* object; + std::shared_ptr object; int hit_counter; void draw(DrawingContext& context); diff --git a/src/object/brick.cpp b/src/object/brick.cpp index 4b2b90c1e..ebee43803 100644 --- a/src/object/brick.cpp +++ b/src/object/brick.cpp @@ -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(get_pos(), true)); coin_counter--; player_one.get_status()->add_coins(1); if(coin_counter == 0) diff --git a/src/object/candle.cpp b/src/object/candle.cpp index 84535f64a..ec0ff01dd 100644 --- a/src/object/candle.cpp +++ b/src/object/candle.cpp @@ -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("images/objects/particles/smoke.sprite", + "default", + ppos, ANCHOR_MIDDLE, + pspeed, paccel, + LAYER_BACKGROUNDTILES+2)); } bool diff --git a/src/object/coin.cpp b/src/object/coin.cpp index 0d3a77105..a298454ee 100644 --- a/src/object/coin.cpp +++ b/src/object/coin.cpp @@ -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(get_pos())); Sector::current()->get_level()->stats.coins++; remove_me(); } diff --git a/src/object/coin_explode.cpp b/src/object/coin_explode.cpp index a900def1c..bd73e0764 100644 --- a/src/object/coin_explode.cpp +++ b/src/object/coin_explode.cpp @@ -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(position, Vector (2.5,-4.5)*(mag-gameRandom.rand(rand)))); + Sector::current()->add_object(std::make_shared(position, Vector (2,-5)*(mag-gameRandom.rand(rand)))); + Sector::current()->add_object(std::make_shared(position, Vector (1.5,-5.5)*(mag-gameRandom.rand(rand)))); + Sector::current()->add_object(std::make_shared(position, Vector (1,-6)*(mag+gameRandom.rand(rand)))); + Sector::current()->add_object(std::make_shared(position, Vector (0.5,-6.5)*(mag-gameRandom.rand(rand)))); + Sector::current()->add_object(std::make_shared(position, Vector (-2.5,-4.5)*(mag-gameRandom.rand(rand)))); + Sector::current()->add_object(std::make_shared(position, Vector (-2,-5)*(mag-gameRandom.rand(rand)))); + Sector::current()->add_object(std::make_shared(position, Vector (-1.5,-5.5)*(mag-gameRandom.rand(rand)))); + Sector::current()->add_object(std::make_shared(position, Vector (-1,-6)*(mag+gameRandom.rand(rand)))); + Sector::current()->add_object(std::make_shared(position, Vector (-0.5,-6.5)*(mag-gameRandom.rand(rand)))); remove_me(); } diff --git a/src/object/coin_rain.cpp b/src/object/coin_rain.cpp index c3b10e5d7..7e6b4c3ff 100644 --- a/src/object/coin_rain.cpp +++ b/src/object/coin_rain.cpp @@ -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(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(Vector (position.x+32*((drop<5)?-drop-1:drop-4),-32), Vector (0,0))); counter++; timer.start(DROP_TIME); }else{ diff --git a/src/object/endsequence_fireworks.cpp b/src/object/endsequence_fireworks.cpp index f2f0ba435..b971b25ba 100644 --- a/src/object/endsequence_fireworks.cpp +++ b/src/object/endsequence_fireworks.cpp @@ -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()); } void diff --git a/src/object/firefly.cpp b/src/object/firefly.cpp index a7198ec47..0077c9bd4 100644 --- a/src/object/firefly.cpp +++ b/src/object/firefly.cpp @@ -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("images/objects/particles/reset.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1)); } if( sprite_name.find("vbell", 0) == std::string::npos ) { diff --git a/src/object/fireworks.cpp b/src/object/fireworks.cpp index bd1814e37..c557453f2 100644 --- a/src/object/fireworks.cpp +++ b/src/object/fireworks.cpp @@ -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(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)); } diff --git a/src/object/icecrusher.cpp b/src/object/icecrusher.cpp index d2fcf5c80..55129ce34 100644 --- a/src/object/icecrusher.cpp +++ b/src/object/icecrusher.cpp @@ -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( + 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( + 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( + 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( + 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); diff --git a/src/object/infoblock.cpp b/src/object/infoblock.cpp index d927f4585..c6e834542 100644 --- a/src/object/infoblock.cpp +++ b/src/object/infoblock.cpp @@ -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(*i); + InfoBlock* block = dynamic_cast(i->get()); if (!block) continue; if (block != this) block->hide_message(); } diff --git a/src/object/player.cpp b/src/object/player.cpp index 7a3c5a1b0..c2bfb0e28 100644 --- a/src/object/player.cpp +++ b/src/object/player.cpp @@ -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( + "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( 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("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("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( 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( 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(get_pos() + Vector(graphicsRandom.rand(5), graphicsRandom.rand(-32,18)), graphicsRandom.rand(-100,100))); } diff --git a/src/object/pneumatic_platform.cpp b/src/object/pneumatic_platform.cpp index b295de829..c76f8b18b 100644 --- a/src/object/pneumatic_platform.cpp +++ b/src/object/pneumatic_platform.cpp @@ -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(this)); return; } if (!master) { diff --git a/src/object/portable.hpp b/src/object/portable.hpp index 93f8dbc53..d8ab02eb8 100644 --- a/src/object/portable.hpp +++ b/src/object/portable.hpp @@ -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 diff --git a/src/object/powerup.cpp b/src/object/powerup.cpp index 686b4072d..0ac73fdca 100644 --- a/src/object/powerup.cpp +++ b/src/object/powerup.cpp @@ -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( + "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)); } } } diff --git a/src/object/rain_particle_system.cpp b/src/object/rain_particle_system.cpp index 4beda1c9c..85cd01941 100644 --- a/src/object/rain_particle_system.cpp +++ b/src/object/rain_particle_system.cpp @@ -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(Vector(splash_x, splash_y),vertical)); } // Uncomment the following to display vertical splashes, too /* else { diff --git a/src/object/specialriser.cpp b/src/object/specialriser.cpp index 013109a8f..d9e8f09d2 100644 --- a/src/object/specialriser.cpp +++ b/src/object/specialriser.cpp @@ -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 _child) : offset(), child(_child) { diff --git a/src/object/specialriser.hpp b/src/object/specialriser.hpp index 9d4237032..f3a62b3b3 100644 --- a/src/object/specialriser.hpp +++ b/src/object/specialriser.hpp @@ -26,7 +26,7 @@ class SpecialRiser : public GameObject { public: - SpecialRiser(Vector pos, MovingObject* child); + SpecialRiser(Vector pos, std::shared_ptr child); ~SpecialRiser(); virtual void update(float elapsed_time); @@ -34,7 +34,7 @@ public: private: float offset; - MovingObject* child; + std::shared_ptr child; private: SpecialRiser(const SpecialRiser&); diff --git a/src/object/star.cpp b/src/object/star.cpp index 66f82114a..fb505b684 100644 --- a/src/object/star.cpp +++ b/src/object/star.cpp @@ -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( + "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)); } } } diff --git a/src/object/thunderstorm.cpp b/src/object/thunderstorm.cpp index 187a4c30d..af9e41043 100644 --- a/src/object/thunderstorm.cpp +++ b/src/object/thunderstorm.cpp @@ -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(200, 1421, ELECTRIFY_TIME)); + Sector::current()->add_object(std::make_shared(201, 1422, ELECTRIFY_TIME)); } /* EOF */ diff --git a/src/object/weak_block.cpp b/src/object/weak_block.cpp index 834dc5fdb..261128553 100644 --- a/src/object/weak_block.cpp +++ b/src/object/weak_block.cpp @@ -184,7 +184,7 @@ WeakBlock::spreadHit() return; } for(Sector::GameObjects::iterator i = sector->gameobjects.begin(); i != sector->gameobjects.end(); ++i) { - WeakBlock* wb = dynamic_cast(*i); + WeakBlock* wb = dynamic_cast(i->get()); if (!wb) continue; if (wb == this) continue; if (wb->state != STATE_NORMAL) continue; diff --git a/src/object/wind.cpp b/src/object/wind.cpp index ea5f3a3be..db27bb46e 100644 --- a/src/object/wind.cpp +++ b/src/object/wind.cpp @@ -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(ppos, 44, 46, pspeed, Vector(0,0), 1, Color(.4f, .4f, .4f), 3, .1f, LAYER_BACKGROUNDTILES+1)); } } diff --git a/src/scripting/floating_image.cpp b/src/scripting/floating_image.cpp index b45eb0906..2264d0d66 100644 --- a/src/scripting/floating_image.cpp +++ b/src/scripting/floating_image.cpp @@ -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"); } diff --git a/src/scripting/floating_image.hpp b/src/scripting/floating_image.hpp index 4365e0939..9f08379e1 100644 --- a/src/scripting/floating_image.hpp +++ b/src/scripting/floating_image.hpp @@ -18,9 +18,9 @@ #define HEADER_SUPERTUX_SCRIPTING_FLOATING_IMAGE_HPP #ifndef SCRIPTING_API -#define HEADER_SUPERTUX_SCRIPTING_FLOATING_IMAGE_HPP +#include -#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 }; diff --git a/src/scripting/scripting.cpp b/src/scripting/scripting.cpp index 4efac9abe..71645ab38 100644 --- a/src/scripting/scripting.cpp +++ b/src/scripting/scripting.cpp @@ -127,7 +127,7 @@ Scripting::~Scripting() global_vm = NULL; } - + void Scripting::update_debugger() { diff --git a/src/scripting/scripting.hpp b/src/scripting/scripting.hpp index 5f985b462..b17bd2784 100644 --- a/src/scripting/scripting.hpp +++ b/src/scripting/scripting.hpp @@ -31,7 +31,7 @@ private: public: Scripting(bool enable_debugger); ~Scripting(); - + void update_debugger(); private: diff --git a/src/supertux/command_line_arguments.cpp b/src/supertux/command_line_arguments.cpp index e40503b8d..6c14e02a8 100644 --- a/src/supertux/command_line_arguments.cpp +++ b/src/supertux/command_line_arguments.cpp @@ -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; } diff --git a/src/supertux/flip_level_transformer.cpp b/src/supertux/flip_level_transformer.cpp index 53f285ab6..3065fa833 100644 --- a/src/supertux/flip_level_transformer.cpp +++ b/src/supertux/flip_level_transformer.cpp @@ -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 (object); + TileMap* tilemap = dynamic_cast(object.get()); if(tilemap) { - transform_tilemap(height, tilemap); + transform_tilemap(height, *tilemap); } - Player* player = dynamic_cast (object); + Player* player = dynamic_cast(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 (object); + BadGuy* badguy = dynamic_cast(object.get()); if(badguy) { - transform_badguy(height, badguy); + transform_badguy(height, *badguy); } - Flower* flower = dynamic_cast (object); + Flower* flower = dynamic_cast(object.get()); if(flower) { - transform_flower(flower); + transform_flower(*flower); } - Platform* platform = dynamic_cast (object); + Platform* platform = dynamic_cast(object.get()); if(platform) { transform_platform(height, *platform); } - Block* block = dynamic_cast (object); + Block* block = dynamic_cast(object.get()); if(block) { transform_block(height, *block); } - MovingObject* mobject = dynamic_cast (object); + MovingObject* mobject = dynamic_cast(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, MovingObject& object) { - 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 diff --git a/src/supertux/flip_level_transformer.hpp b/src/supertux/flip_level_transformer.hpp index b7bef11b4..25bfa7d4d 100644 --- a/src/supertux/flip_level_transformer.hpp +++ b/src/supertux/flip_level_transformer.hpp @@ -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(Flower& flower); void transform_platform(float height, Platform& platform); void transform_block(float height, Block& block); }; diff --git a/src/supertux/game_object.cpp b/src/supertux/game_object.cpp index aef45f928..3e34a535f 100644 --- a/src/supertux/game_object.cpp +++ b/src/supertux/game_object.cpp @@ -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) diff --git a/src/supertux/game_object.hpp b/src/supertux/game_object.hpp index 180630ee4..f8bc80261 100644 --- a/src/supertux/game_object.hpp +++ b/src/supertux/game_object.hpp @@ -17,11 +17,11 @@ #ifndef HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_HPP #define HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_HPP +#include #include -#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 index 000000000..d19a37fce --- /dev/null +++ b/src/supertux/game_object_ptr.hpp @@ -0,0 +1,28 @@ +// SuperTux +// Copyright (C) 2014 Ingo Ruhnke +// +// 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 . + +#ifndef HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_PTR_HPP +#define HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_PTR_HPP + +#include + +class GameObject; + +using GameObjectPtr = std::shared_ptr; + +#endif + +/* EOF */ diff --git a/src/supertux/game_session.cpp b/src/supertux/game_session.cpp index 9c06460c6..efe4c78d1 100644 --- a/src/supertux/game_session.cpp +++ b/src/supertux/game_session.cpp @@ -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(); } else { - end_sequence = new EndSequenceWalkRight(); + end_sequence = std::make_shared(); } } else if (sequencename == "fireworks") { - end_sequence = new EndSequenceFireworks(); + end_sequence = std::make_shared(); } 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::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 (obj); + auto lt = std::dynamic_pointer_cast(obj); if(lt) lt->stop(); } diff --git a/src/supertux/game_session.hpp b/src/supertux/game_session.hpp index fdae70554..4609f8b4a 100644 --- a/src/supertux/game_session.hpp +++ b/src/supertux/game_session.hpp @@ -119,7 +119,7 @@ private: int levelnb; int pause_menu_frame; - EndSequence* end_sequence; + std::shared_ptr end_sequence; bool game_pause; float speed_before_pause; diff --git a/src/supertux/level.cpp b/src/supertux/level.cpp index 2a3674e01..386cd3f11 100644 --- a/src/supertux/level.cpp +++ b/src/supertux/level.cpp @@ -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 (*o); + for(auto o = sector->gameobjects.begin(); o != sector->gameobjects.end(); ++o) { + Coin* coin = dynamic_cast(o->get()); if(coin) { total_coins++; continue; } - BonusBlock *block = dynamic_cast (*o); + BonusBlock *block = dynamic_cast(o->get()); if(block) { if (block->contents == BonusBlock::CONTENT_COIN) @@ -214,7 +213,7 @@ Level::get_total_coins() continue; } } - GoldBomb *goldbomb = dynamic_cast (*o); + GoldBomb *goldbomb = dynamic_cast(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(); return total_secrets; } diff --git a/src/supertux/object_factory.cpp b/src/supertux/object_factory.cpp index 2c1fd84d5..3699f3c8f 100644 --- a/src/supertux/object_factory.cpp +++ b/src/supertux/object_factory.cpp @@ -278,7 +278,7 @@ ObjectFactory::init_factories() add_factory("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 */ diff --git a/src/supertux/object_factory.hpp b/src/supertux/object_factory.hpp index d6b16248b..9e3d2f126 100644 --- a/src/supertux/object_factory.hpp +++ b/src/supertux/object_factory.hpp @@ -23,6 +23,7 @@ #include #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 @@ -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(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 diff --git a/src/supertux/sector.cpp b/src/supertux/sector.cpp index 519a2f4c8..3553d59d6 100644 --- a/src/supertux/sector.cpp +++ b/src/supertux/sector.cpp @@ -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(GameSession::current()->get_savegame().get_player_status(), "Tux")); + add_object(std::make_shared("Effect")); + add_object(std::make_shared("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(this, "Camera"); camera_->parse(reader); return camera_; } else if(name_ == "particles-snow") { - SnowParticleSystem* partsys = new SnowParticleSystem(); + auto partsys = std::make_shared(); partsys->parse(reader); return partsys; } else if(name_ == "particles-rain") { - RainParticleSystem* partsys = new RainParticleSystem(); + auto partsys = std::make_shared(); partsys->parse(reader); return partsys; } else if(name_ == "particles-comets") { - CometParticleSystem* partsys = new CometParticleSystem(); + auto partsys = std::make_shared(); partsys->parse(reader); return partsys; } else if(name_ == "particles-ghosts") { - GhostParticleSystem* partsys = new GhostParticleSystem(); + auto partsys = std::make_shared(); partsys->parse(reader); return partsys; } else if(name_ == "particles-clouds") { - CloudParticleSystem* partsys = new CloudParticleSystem(); + auto partsys = std::make_shared(); partsys->parse(reader); return partsys; } else if(name_ == "money") { // for compatibility with old maps - return new Jumpy(reader); + return std::make_shared(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(*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(object)) { + if(std::dynamic_pointer_cast(object)) { has_background = true; - } else if(dynamic_cast(object)) { + } else if(std::dynamic_pointer_cast(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->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(this, "Camera")); } update_game_objects(); @@ -282,11 +277,11 @@ Sector::parse_old_format(const Reader& reader) bkgd_bottom.blue = static_cast (b) / 255.0f; if(backgroundimage != "") { - Background* background = new Background(); + auto background = std::make_shared(); background->set_image(backgroundimage, bgspeed); add_object(background); } else { - Gradient* gradient = new Gradient(); + auto gradient = std::make_shared(); 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()); else if(particlesystem == "snow") - add_object(new SnowParticleSystem()); + add_object(std::make_shared()); else if(particlesystem == "rain") - add_object(new RainParticleSystem()); + add_object(std::make_shared()); 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(); spawn->pos = startpos; spawn->name = "main"; spawnpoints.push_back(spawn); @@ -323,7 +318,7 @@ Sector::parse_old_format(const Reader& reader) std::vector tiles; if(reader.get("interactive-tm", tiles) || reader.get("tilemap", tiles)) { - TileMap* tilemap = new TileMap(level->get_tileset()); + auto tilemap = std::make_shared(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(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(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(); 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(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(pos)); solids->change(x, y, 0); } else if(tile->getAttributes() & Tile::COIN) { - add_object(new Coin(pos, solids)); + add_object(std::make_shared(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(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(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(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(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(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(*i); + TileMap* tm = dynamic_cast(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(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(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 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(*i); + for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) { + Player* p = dynamic_cast(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::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::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::iterator i = gameobjects.begin(); - i != gameobjects.end(); ++i) + for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) { - TileMap* tm = dynamic_cast(*i); + TileMap* tm = dynamic_cast(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 (object); - if(bullet != NULL) { + auto bullet = dynamic_cast(object.get()); + if (bullet) + { bullets.push_back(bullet); } - MovingObject* movingobject = dynamic_cast (object); - if(movingobject != NULL) { + auto movingobject = dynamic_cast(object.get()); + if (movingobject) + { moving_objects.push_back(movingobject); } - Portable* portable = dynamic_cast (object); - if(portable != NULL) { + auto portable = dynamic_cast(object.get()); + if(portable) + { portables.push_back(portable); } - TileMap* tilemap = dynamic_cast (object); - if(tilemap != NULL && tilemap->is_solid()) { + auto tilemap = dynamic_cast(object.get()); + if(tilemap && tilemap->is_solid()) { solid_tilemaps.push_back(tilemap); } - Camera* camera_ = dynamic_cast (object); - if(camera_ != NULL) { + auto camera_ = dynamic_cast(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 (object); - if(player_ != NULL) { + auto player_ = dynamic_cast(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 (object); - if(effect_ != NULL) { + auto effect_ = dynamic_cast(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 (object); + ScriptInterface* object_ = dynamic_cast(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 (this); + scripting::SSector* this_ = static_cast(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 (object); - if(portable != NULL) { + Portable* portable = dynamic_cast(object.get()); + if (portable) { portables.erase(std::find(portables.begin(), portables.end(), portable)); } - Bullet* bullet = dynamic_cast (object); - if(bullet != NULL) { + Bullet* bullet = dynamic_cast(object.get()); + if (bullet) { bullets.erase(std::find(bullets.begin(), bullets.end(), bullet)); } - MovingObject* moving_object = dynamic_cast (object); - if(moving_object != NULL) { + MovingObject* moving_object = dynamic_cast(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 (object); + ScriptInterface* object_ = dynamic_cast(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(object); + TileMap* tm = dynamic_cast(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(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(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 (*i); + for(auto i = gameobjects.begin(); i != gameobjects.end(); ++i) { + BadGuy* badguy = dynamic_cast(i->get()); if (badguy && badguy->countMe) total_badguys++; } diff --git a/src/supertux/sector.hpp b/src/supertux/sector.hpp index 71232d7d8..4388c857f 100644 --- a/src/supertux/sector.hpp +++ b/src/supertux/sector.hpp @@ -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(*i)) total++; + if (dynamic_cast(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 GameObjects; + typedef std::vector GameObjects; typedef std::vector MovingObjects; - typedef std::vector SpawnPoints; + typedef std::vector > SpawnPoints; typedef std::vector 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(); diff --git a/src/trigger/secretarea_trigger.cpp b/src/trigger/secretarea_trigger.cpp index 335609917..85872b521 100644 --- a/src/trigger/secretarea_trigger.cpp +++ b/src/trigger/secretarea_trigger.cpp @@ -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(*i); + TileMap* tm = dynamic_cast(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 index 199cb2c88..000000000 --- a/src/util/ref.hpp +++ /dev/null @@ -1,89 +0,0 @@ -// SuperTux -// Copyright (C) 2006 Matthias Braun -// -// 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 . - -#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 -class Ref -{ -public: - Ref(T* object_ = 0) - : object(object_) - { - if(object) - object->ref(); - } - Ref(const Ref& other) - : object(other.object) - { - if(object) - object->ref(); - } - ~Ref() - { - if(object) - object->unref(); - } - - Ref& operator= (const Ref& other) - { - *this = other.get(); - return *this; - } - - Ref& 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 index 1aa5c8981..000000000 --- a/src/util/refcounter.hpp +++ /dev/null @@ -1,60 +0,0 @@ -// Windstille - A Jump'n Shoot Game -// Copyright (C) 2005 Matthias Braun -// -// 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 . - -#ifndef HEADER_SUPERTUX_UTIL_REFCOUNTER_HPP -#define HEADER_SUPERTUX_UTIL_REFCOUNTER_HPP - -#include - -/** - * 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 */ diff --git a/src/worldmap/worldmap.cpp b/src/worldmap/worldmap.cpp index e6b479a94..3300fa8db 100644 --- a/src/worldmap/worldmap.cpp +++ b/src/worldmap/worldmap.cpp @@ -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(this); add_object(tux); name = ""; @@ -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 (object); + TileMap* tilemap = dynamic_cast(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 (object); + ScriptInterface* object_ = dynamic_cast(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 (object); + ScriptInterface* object_ = dynamic_cast(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(*(iter.lisp()))); } else if(iter.item() == "background") { - add_object(new Background(*(iter.lisp()))); + add_object(std::make_shared(*(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(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(*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(*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(*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(*iter.lisp()); add_object(decal); } else if(iter.item() == "ambient-light") { std::vector 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::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(*i); + TileMap* tm = dynamic_cast(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); } diff --git a/src/worldmap/worldmap.hpp b/src/worldmap/worldmap.hpp index b0e56acd0..c42e5510b 100644 --- a/src/worldmap/worldmap.hpp +++ b/src/worldmap/worldmap.hpp @@ -22,15 +22,16 @@ #include #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 SpriteChanges; typedef std::vector SpawnPoints; typedef std::vector LevelTiles; - typedef std::vector GameObjects; + typedef std::vector GameObjects; typedef std::vector ScriptList; - Tux* tux; + std::shared_ptr 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; }