SFML C ++绘制形状矢量

时间:2017-01-30 18:35:16

标签: c++ sfml

我刚回到编码。试图做一个简单的突破性游戏,我开始做一个简单的'乒乓'游戏,但发现它很容易,所以我试图将它扩展到一个突破性的游戏(图像附加给那些不知道它是什么)。

为了处理屏幕顶部的块,我使用了一个块矢量,现在我正试图将它们绘制到屏幕上。我无法做到这一点因为我收到错误:

  

错误C2664:'void sf :: RenderTarget :: draw(const sf :: Vertex   *,size_t,sf :: PrimitiveType,const sf :: RenderStates&)':不能将参数1从'Block'转换为'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;
};

感谢您的帮助:)

1 个答案:

答案 0 :(得分:2)

(我必须把它放在这里因为我没有足够的声誉尚未发表评论)

一些事情

  • 我不明白为什么你有这个代码void Block::draw(Block block, sf::RenderWindow &window)。它应该是void Block::draw(sf::RenderWindow &window),然后只绘制block(这是一个类成员)或者如果你想从其他地方绘制块,则通过引用传递block

  • 在任何情况下,Block都应该从sf::Drawable继承并使用其功能进行绘制。我认为这是错误信息所说的内容。例如,class Block : public sf::Drawable { ... };和要绘制的函数在标题中为virtual void draw(sf::RenderTarget &renderTarget, sf::RenderStates renderStates) const;,在.cpp中为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;
}

正如您所看到的,Block现在继承自sf::Drawable,可以使用xxx.draw(块)绘制。

BlockContainer现在有两个不同的函数来绘制其内容(这仅用于演示目的,您只需要根据自己喜欢的内容绘制一个函数)。如果您想保留自己的绘图功能,可以从: public sf::Drawable删除BlockContainer

main()中创建了两个块容器,将使用原始代码中的testBlocks BlockContainer绘制函数绘制一个(void drawContainer(sf::RenderWindow &window)),其他(testBlocks2)使用sf::Drawable&#39; s。

还要注意&getContainer()现在如何返回对块向量的引用。如果您没有返回引用,原始向量将不会受到您想从外部对其进行操作的影响。