2017-01-30 126 views
0

我剛回到編碼。試圖做一個簡單的突破遊戲,我開始做一個簡單的「乒乓」遊戲,但發現它很容易,所以我試圖將其擴展到一個突破遊戲(圖片附加爲那些誰不知道它是什麼)。SFML C++繪製形狀矢量

要處理屏幕頂部的塊,我使用了一個塊向量,現在我正試圖將它們繪製到屏幕上。我不能這樣做,因爲我得到了一個錯誤:

error C2664: 'void sf::RenderTarget::draw(const sf::Vertex *,size_t,sf::PrimitiveType,const sf::RenderStates &)' : cannot convert argument 1 from 'Block' to 'const sf::Drawable &'

這是block.cpp文件

這裏面是相關的代碼,也有更多的功能,但它們並不適用於此。對不起,在沒有任何不好的代碼:)

block.cpp

Block::Block(float startX, float startY) 
{ 
    position.x = startX; 
    position.y = startY; 

    colour = sf::Color::White; 

    block.setSize(sf::Vector2f(width, height)); 
    block.setFillColor(colour); 
    block.setPosition(position); 
} 

void Block::draw(Block block, sf::RenderWindow& window) 
{ 
    window.draw(block); 
} 

blockContainer.cpp

#pragma once 

class ContainerOfBlocks 
{ 
public: 
    ContainerOfBlocks(int useless); 
    ~ContainerOfBlocks(); 

    std::vector<Block> getContainer(); 

    void drawContainer(sf::RenderWindow& window); 

private: 
    std::vector<Block> blockContainer; 
}; 

blockContainer.h

#pragma once 

class ContainerOfBlocks 
{ 
public: 
    ContainerOfBlocks(int useless); 
    ~ContainerOfBlocks(); 

    std::vector<Block> getContainer(); 

    void drawContainer(sf::RenderWindow& window); 

private: 
    std::vector<Block> blockContainer; 
}; 

感謝您的幫助:)

回答

2

(我會hav E要放在這裏,因爲我沒有足夠的聲譽尚未就此發表評論)

一些事情

  • 我不明白爲什麼你有這樣的代碼void Block::draw(Block block, sf::RenderWindow &window)。它應該是void Block::draw(sf::RenderWindow &window),然後就畫block(這是一個類成員),或者如果你想從別的

  • 地方在任何情況下繪製塊,Blocksf::Drawable繼承並使用其功能,通過引用傳遞block畫。我認爲這就是錯誤信息所說的。例如class Block : public sf::Drawable { ... };,並且在您的.cpp中將繪製的函數爲virtual void draw(sf::RenderTarget &renderTarget, sf::RenderStates renderStates) const;void draw(sf::RenderTarget &renderTarget, sf::RenderStates renderStates) const { renderTarget.draw(block); }。然後你可以遍歷塊的矢量你已經有了和繪製每塊

  • 功能std::vector<Block> getContainer()應該返回到矢量(std::vector<Block> &getContainer()

  • 參考它不是一個錯誤,但我更喜歡使用#ifndef ... #define... #endif頭文件保護而不是#pragma once

編輯(以下關於您的回覆):

我做了一個使用大部分代碼的快速項目。

(還要確保閱讀我的筆記下面的代碼)

這裏是什麼樣子的圖片編譯:

enter image description here

代碼:

block.h

#ifndef BLOCK_H_INCLUDED 
#define BLOCK_H_INCLUDED 

#include <SFML/Graphics.hpp> 

class Block : public sf::Drawable { 
public: 
    Block(); 
    Block::Block(float startX, float startY); 
    virtual ~Block(); 

private: 
    virtual void draw(sf::RenderTarget &renderTarget, sf::RenderStates renderStates) const; 

    sf::RectangleShape block; 
    sf::Vector2f position; 
    float width; 
    float height; 
    sf::Color colour; 
}; 

#endif 

block.cpp

#include "block.h" 

Block::Block() : 
    position(sf::Vector2f()), 
    width(40.0f), 
    height(20.0f), 
    colour(sf::Color()) 
{ 
} 

Block::Block(float startX, float startY) : 
    width(40.0f), 
    height(20.0f) 
{ 
    position.x = startX; 
    position.y = startY; 

    colour = sf::Color::White; 

    block.setSize(sf::Vector2f(width, height)); 
    block.setFillColor(colour); 
    block.setPosition(position); 
} 

Block::~Block() { 
} 

void Block::draw(sf::RenderTarget &renderTarget, sf::RenderStates renderStates) const { 
    renderTarget.draw(block); 
} 

blockContainer.h

#ifndef BLOCKCONTAINER_H_INCLUDED 
#define BLOCKCONTAINER_H_INCLUDED 

#include "block.h" 

class ContainerOfBlocks : public sf::Drawable { 
public: 
    ContainerOfBlocks(); 
    ContainerOfBlocks(int useless, const sf::Vector2f pos); 
    ~ContainerOfBlocks(); 

    std::vector<Block> &getContainer(); 
    void drawContainer(sf::RenderWindow &window); 

private: 
    virtual void draw(sf::RenderTarget &renderTarget, sf::RenderStates renderStates) const; 

    std::vector<Block> blockContainer; 
}; 

#endif 

blockContainer.cpp

#include "blockContainer.h" 

ContainerOfBlocks::ContainerOfBlocks() { 
} 

ContainerOfBlocks::ContainerOfBlocks(int useless, const sf::Vector2f pos) { 
    if (useless > 0) { 
    float x = pos.x; 
    float y = pos.y; 
    for (std::size_t i = 0; i < static_cast<std::size_t>(useless); ++i) { 
     blockContainer.push_back(Block(x, y)); 
     x += 50.0f; 
    } 
    } 
} 

ContainerOfBlocks::~ContainerOfBlocks() { 
} 

std::vector<Block> &ContainerOfBlocks::getContainer() { 
    return blockContainer; 
} 

void ContainerOfBlocks::drawContainer(sf::RenderWindow &window) { 
    for (std::size_t i = 0; i < blockContainer.size(); ++i) { 
    window.draw(blockContainer[i]); 
    } 
} 

void ContainerOfBlocks::draw(sf::RenderTarget &renderTarget, sf::RenderStates renderStates) const { 
    for (std::size_t i = 0; i < blockContainer.size(); ++i) { 
    renderTarget.draw(blockContainer[i]); 
    } 
} 

的main.cpp

#include <SFML/Graphics.hpp> 

#include "block.h" 
#include "blockContainer.h" 

int main() { 
    sf::RenderWindow window(sf::VideoMode(400, 200), "SFML works!"); 
    window.setFramerateLimit(30); 
    window.setVerticalSyncEnabled(false); 

    // create container with 5 blocks in it, starting at pos 10/10 
    // this container will be drawn using ContainerOfBlocks' void drawContainer(sf::RenderWindow &window) 
    ContainerOfBlocks testBlocks(5, sf::Vector2f(10.0f, 10.0f)); 

    // create another container, starting at pos 10/50 
    // this one will be drawn using sf::Drawable's function to draw 
    ContainerOfBlocks testBlocks2(5, sf::Vector2f(10.0f, 50.0f)); 

    while (window.isOpen()) { 
    sf::Event evt; 
    while (window.pollEvent(evt)) { 
     if (evt.type == sf::Event::Closed) { 
     window.close(); 
     } 
    } 
    window.clear(); 
    testBlocks.drawContainer(window); 
    window.draw(testBlocks2); 
    window.display(); 
    } 

    return 0; 
} 

正如你可以看到現在Blocksf::Drawable繼承並可以得出與xxx.d原始(塊)。

BlockContainer現在有兩個不同的函數來繪製其內容(這只是爲了演示的目的,你只需要一個函數來繪製取決於你更喜歡什麼)。如果您想保留自己的繪圖功能,可以從BlockContainer中刪除: public sf::Drawable

main()兩種嵌段容器創建,一個(testBlocks)將使用S BlockContainer「從原始代碼中使用sf::Drawable小號void drawContainer(sf::RenderWindow &window)繪製函數,其他(testBlocks2)」繪製。

另請注意&getContainer()現在如何返回對塊向量的引用。如果你沒有返回一個參考,那麼原始向量不會受到你想從外面做的任何事情的影響。

+0

好的我編輯了我的代碼,包括sf :: Drawable。我添加了在.h和.cpp中繪製的函數。我無法弄清楚如何讓它返回對矢量的引用,我現在將研究它。但是當我調用draw函數時。我實際上在參數中放置了什麼?因爲它需要(sf :: RenderTarget&renderTarget,sf :: RenderStates狀態)被覆蓋? –

+0

我已經想出瞭如何在沒有整個'sf :: Drawable'的情況下做到這一點。所以我的舊功能很好,我只是犯了一些簡單的錯誤(像平常一樣)。謝謝你的回答tho :) –

+0

@ D.Morgan查看我編輯的例子代碼 – user3881815