我可以同时绘制所有相同类型的对象吗?

Can I draw all objects of the same type at the same time?

我正在创建一个 2d 平台游戏,并在不同的 classes 中创建了多个对象。

下面的代码显示了我当前如何创建对象并将它们绘制到 window。

有没有一种方法可以绘制 class 中的所有对象(例如所有可收集的对象),而不必绘制每个单独的对象,如下所示?

/*!
\file game.cpp
*/
#include "game.h"

//TODO:
//Collectibles - delete on contact
//Enemies - move away on contact (maybe delete on contact?)
//HUD -lives, minmap and score system
//Win condition
//Lose Condition
//Build level

Game::Game()
{
    // Load textures.
    m_texture.loadFromFile("./assets/textures/sky.png");
    m_floor.loadFromFile("./assets/textures/floor.jpg");
    m_collect.loadFromFile("./assets/textures/diamond.png");
    m_bed.loadFromFile("./assets/textures/bed.png");
    m_bounce.loadFromFile("./assets/textures/bounce.png");
    m_sprite.loadFromFile("./assets/textures/spriteTexture.png");
    m_enemy.loadFromFile("./assets/textures/enemySpriteSheet.png");

    //Initialise picture variables
    m_picture.setSize(sf::Vector2f(8.f, 6.f));
    m_picture.setTexture(&m_texture);
    m_picture.setPosition(0.f, 0.f);
    m_picture.setOrigin(4.f, 3.f);

    m_view.setCenter(0.f, 0.f);
    m_view.setSize(m_worldSize);

    m_pWorld = new b2World(mk_gravity);

    m_drawable_objects = {&m_picture, m_character, m_simpEnemy1, m_ground, m_roof, m_plat1, m_bounce1, m_move1, m_coin, m_win};

    //create static blocks
    m_ground = new StaticBlock(m_pWorld, sf::Vector2f(0.f, 2.75f), sf::Vector2f(400.f, 0.5f), 0.f, &m_floor, false);
    m_roof = new StaticBlock(m_pWorld, sf::Vector2f(0.f, -3.25f), sf::Vector2f(400.f, 0.5f), 0.f, &m_floor, false);
    m_plat1 = new StaticBlock(m_pWorld, sf::Vector2f(3.f, 1.5f), sf::Vector2f(4.f, 0.5f), 0.f, &m_floor, false);
    m_bounce1 = new StaticBlock(m_pWorld, sf::Vector2f(7.f, 1.5f), sf::Vector2f(2.f, 0.5f), 0.f, &m_bounce, true);

    //create kinematic blocks
    m_move1 = new KinematicBlock(m_pWorld, sf::Vector2f(0.f, 0.25f), 1.f, sf::Vector2f(5.f, 0.5f), 0.f, &m_floor);

    //create collectibles
    //m_coins = {m_coin};
    m_coin = new Collectible(m_pWorld, sf::Vector2f(2.f, 0.85f), sf::Vector2f(0.3f, 0.3f), 0.f, &m_collect);
    //m_coin2 = new Collectible(m_pWorld, sf::Vector2f(2.f, 0.85f), sf::Vector2f(0.3f, 0.3f), 0.f, &m_collect);

    //create character
    m_character = new Character(m_pWorld, sf::Vector2f(0.f, 2.25f), sf::Vector2f(0.25f, 0.5f), 0.f, &m_sprite);

    //create enemies
    m_simpEnemy1 = new SimpleEnemy(m_pWorld, sf::Vector2f(4.f, 2.325f), 3.f, sf::Vector2f(0.75f, 0.375f), 0.f, &m_enemy, sf::IntRect(0,17,41,14));

    //create winner pole
    m_win = new WinnerPole(m_pWorld, sf::Vector2f(15.f, 2.25f), sf::Vector2f(1.f, 0.5f), 0.f, &m_bed);

    //set contact listener
    m_pWorld->SetContactListener(&m_listener);
}

Game::~Game()
{
    // Clean up all pointers
    delete m_pWorld;
    m_pWorld = nullptr;
}

void Game::update(float timestep)
{
    // Update the world
    m_pWorld->Step(timestep, mk_iVelIterations, mk_iVelIterations);
    m_picture.setPosition(m_character->getPosition().x, 0.f);
    m_view.setCenter(m_character->getPosition().x, m_view.getCenter().y);

    // Update each dyanmic element - effectively update render information
    m_character->update();
    m_simpEnemy1->update();
    m_move1->update();
    m_coin->update();
}

void Game::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
    // Set the view
    target.setView(m_view);

    // Draw everything
    for (auto * drawable : m_drawable_objects) 
    {
        target.draw(*drawable);
    }
}

假设您的所有对象都继承自 sf::Drawable:

// Declare this field in your class
std::vector<sf::Drawable*> m_drawable_objects;

// Fill it in your Game constructor
m_drawable_objects = {
    &m_picture, m_character, m_simpEnemy1, ...
};

// Use it in your draw method
for (auto * drawable : drawable_objects) {
    target.draw(*drawable);
}

这还将使您能够动态添加和删除新的游戏对象。