【问题标题】:Can I draw all objects of the same type at the same time?我可以同时绘制相同类型的所有对象吗?
【发布时间】:2020-05-18 09:20:09
【问题描述】:

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

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

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

/*!
\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);
    }
}

【问题讨论】:

    标签: c++ box2d sfml


    【解决方案1】:

    假设你所有的对象都继承自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);
    }
    

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

    【讨论】:

    • 谢谢你,我已经按照你说的做了,我在绘制对象时抛出了异常。我已经更新了我的问题中的代码。
    • 什么异常?
    • 您需要在分配所有游戏对象之后填充向量,而不是之前。
    • 在分配游戏对象后移动它已经完美了,非常感谢!
    • 是否可以对更新功能做同样的事情?
    猜你喜欢
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多