2017-05-17 4 views
0

무작위로 배치 된 사각형의 벡터를 만들고이를 화면에 그리려면 벡터에 대한 참조를 전달하여 작업 할 수 없었습니다. (C++ 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

012,377

내가 클래스 consumable 보면

+0

consumable.h를 포함 할 때 main.cpp 사본을 포함 시켰습니다. 이 문제를 해결하려면 질문을 편집하십시오. –

+0

필자는'consumableX'와'consumableY'를'const int'로 루프 안에서 선언 할 것입니다. 마찬가지로,'_consumable'을'fCreateConsumable' (아마도 루프 내에서) 내에 로컬로 만들 것입니다. 'fDrawTarget'에서 루프를'for (const auto & item : vConsumable)) {thatWindow.draw (item);}' –

+0

으로 작성했습니다. 마지막으로 "작동시키지 못했습니다"라는 식으로 말입니다. 컴파일러 오류 메시지 (정확하게 표시하십시오)? 예기치 않은 결과가 나왔습니까? (무엇?) 충돌? (어디?) –

답변

0

을 통해 루프 싶어, 나는 _consumable 지역함으로써 시작하고 다른 기능을 추가 fCreateConsumable을 리팩토링 것입니다. 당신이 새로운 RectangleShape을 만들지 않도록 이전 세 사람을 다시, 새로운 위치로 업데이트 할 수 있습니다 fDrawTarget에서 fCreateConsumable을 제거함으로써

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

.

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]); 
    } 
} 

개인적으로, 나는 너무 fDrawTarget에서 UpdatePositions을 이동하고 drawcall에게 CONST 기능을 만들 것입니다. 그렇게하면 업데이트와 렌더링을 분리 할 수 ​​있습니다. 그렇지 않으면 UpdatePositions을 개인 범위로 이동하십시오.