【问题标题】:C++ SFML creating a vector of shapes and drawing them in a functionC++ SFML 创建形状向量并将它们绘制在函数中
【发布时间】:2017-05-17 13:00:56
【问题描述】:

我想创建一个由随机放置的正方形组成的向量并将它们绘制到屏幕上,尝试传递对向量的引用,但我无法让它工作:(

消耗品.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;
}

我想循环

【问题讨论】:

  • 当您打算包含 consumable.h 时,您已经包含了 main.cpp 的副本。请edit你的问题来解决这个问题。
  • 我会在循环中将consumableXconsumableY 声明为const int。同样,我会将_consumable 设为fCreateConsumable 内的本地(可能再次在循环内)。在fDrawTarget 中,我会将循环写为for(const auto&amp; item: vConsumable)){ thatWindow.draw(item);}
  • 最后,以什么方式“你不能让它工作”?编译器错误信息(请准确显示)?出乎意料的输出? (什么?)崩溃? (在哪里?)
  • 添加了consumable.h,至于错误什么都不会被绘制到屏幕上,我尝试找到一种方法来使用调试工具查看向量,但我找不到任何有用的东西
  • 我目前正在尝试将我的原始项目转换为面向对象的格式,因为我不擅长类等。可以在我原来的 main.cpp 中发布文本墙,但即使看起来真的很难看对我来说

标签: c++ function vector sfml


【解决方案1】:

查看您的课程 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;
};

通过从fDrawTarget 中删除fCreateConsumable,您可以避免创建新的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]);
    }
}

就个人而言,我也会从fDrawTarget 中移出UpdatePositions,并将drawcall 设为const 函数。这样你就可以将更新和渲染分开。如果不这样做,请将 UpdatePositions 移至私有范围。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 2013-09-10
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    相关资源
    最近更新 更多