C ++ SFML创建一个形状向量并在函数中绘制它们

时间:2017-05-17 13:00:57

标签: c++ function vector sfml

我想创建一个随机放置的正方形矢量并将它们绘制到屏幕上,尝试将引用传递给矢量而我无法使其工作:(

consumable.h

#ifndef CONSUMABLE_H
#define CONSUMABLE_H

#include <SFML/Graphics.hpp>

using namespace std;
using namespace sf;

class consumable
{
    public:
     consumable();
    virtual ~consumable();

    vector<RectangleShape> fCreateConsumable(vector<RectangleShape>& vConsumable);
    void fDrawTarget(float x, float y, RenderWindow &thatWindow);

protected:

private:
    vector<RectangleShape> vConsumable;
    RectangleShape _consumable;
};

consumable.cpp

#include "consumable.h"

consumable::consumable()
{
    //ctor
}

consumable::~consumable()
{
    //dtor
}
void consumable::fCreateConsumable(){
    int consumableX{0}, consumableY{0};

    for(int i=0;i<4;i++){
        consumableX = (rand() % 31) + 1;
        consumableY = (rand() % 22) + 1;
        _consumable.setPosition((consumableX * 25), (consumableY * 25));
        _consumable.setSize(sf::Vector2f(25.0f,25.0f));
        _consumable.setFillColor(sf::Color::Magenta);
        vConsumable.push_back(_consumable);
    }
}
void consumable::fDrawTarget(float x, float y, RenderWindow &thatWindow){
    void fCreateConsumable();

    for(int i{0};i< vConsumable.size();i++){
        thatWindow.draw(vConsumable[i]);
    }
}

的main.cpp

#include <iostream>
#include <SFML/Graphics.hpp>

#include "consumable.h"

using namespace std;
using namespace sf;

int main()
{
    consumable Consumable;
    RenderWindow window(VideoMode(800,750), "C++ Snake"); 

    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
          switch(event.type)
          {
            case Event::Closed:
              window.close();
              break;
            default:
              break;
          }
        }
        window.clear();
        Consumable.fDrawTarget(25,25,window);
        window.display();
    }
    std::cout << "Finished" << std::endl;
    return 0;
}

我想循环

1 个答案:

答案 0 :(得分:0)

查看您的课程consumable,我首先要使_consumable本地和重构fCreateConsumable添加其他功能。

class consumable
{
    public:
    // ...    
    vector<RectangleShape> fCreateConsumable(vector<RectangleShape>& vConsumable);
    void fDrawTarget(float x, float y, RenderWindow &thatWindow);
    void UpdatePositions();
private:
    vector<RectangleShape> vConsumable;
};

fCreateConsumable移除fDrawTarget,您可以避免创建新的RectangleShape,重复使用旧的void consumable::UpdatePositions(){ srand(time(NULL)); for(int i{0};i< vConsumable.size();i++){ vConsumable[i].setPosition(((rand() % 31) + 1) * 25, ((rand() % 22) + 1) * 25); } } void consumable::fCreateConsumable(){ int consumableX{0}, consumableY{0}; srand(time(NULL)); for(int i=0;i<4;i++){ consumableX = (rand() % 31) + 1; consumableY = (rand() % 22) + 1; _consumable.setPosition((consumableX * 25), (consumableY * 25)); _consumable.setSize(sf::Vector2f(25.0f,25.0f)); _consumable.setFillColor(sf::Color::Magenta); vConsumable.push_back(_consumable); } } void consumable::fDrawTarget(float x, float y, RenderWindow &thatWindow){ UpdatePositions(); for(int i{0};i< vConsumable.size();i++){ thatWindow.draw(vConsumable[i]); } } ,并使用新的位置更新它们。

UpdatePositions

就个人而言,我也会从fDrawTarget移出UpdatePositions并使drawcall成为const函数。这样就可以分离更新和渲染。如果不这样做,请将<script src="js/anime.min.js" type="text/javascript"></script> <script type="text/javascript"> window.onload = function() { anime({ targets: ['.purple', '.darkgreen', '.darkred'], rotate: 360, borderRadius: '50%', duration: 3000, loop: true }); } </script> 移至私有范围。