【问题标题】:Sfml pollEvent, every new image deletes the previous oneSfml pollEvent,每一个新的图像删除前一个
【发布时间】:2017-02-16 15:16:31
【问题描述】:

我的问题是该功能不符合我的要求。 这个“CreateWindow”函数有主循环。在主循环中,我想要一个固定的背景,每次按下 H 按钮时,我都想在背景上绘制一张卡片(精灵)。 这里有什么问题?我的功能抽牌,但是当我按 H 时,前一张牌被删除,下一张牌被抽出。 这与我认为的事件有关,因为每次发生事件(我移动鼠标,按其他键等)时,前一张卡都会被删除...... 我正在使用 sfml 2.0

这是我对 Graphic 类的实现

#include "Graphic.h"
#include "SFML/Graphics.hpp"
#include <iostream>
#include <string>
#include "Card.h"
#include <string>
#include <sstream>

Graphic::Graphic(int offset)
{
    this->offset = offset;
}

Graphic::~Graphic()
{
    //dtor
}

int Graphic::CreateWindow(sf::RenderWindow& window, Deck &deck0)
{
    sf::Vector2i screenDimensions(800, 600);

    //Dimensioni della window
    window.create(sf::VideoMode(screenDimensions.x, screenDimensions.y), "BlackJack", sf::Style::Titlebar | sf::Style::Close);

    int index = 0;
    window.setKeyRepeatEnabled(false);

    //settare il background
    sf::Texture bTexture;
    sf::Sprite bImage;
    if(!bTexture.loadFromFile("Background.png"))
        std::cout << "Error" << std::endl;
    bImage.setTexture(bTexture);
    bImage.setScale(1.0, (float)screenDimensions.y / bTexture.getSize().y);

    //MAIN LOOP----------------------
    while(window.isOpen())
    {
        sf::Event Event;

        while(window.pollEvent(Event))
        {
            window.clear();
            window.draw(bImage);  // this is the function which draw the background

            if (Event.type == sf::Event::Closed)
            {
                window.close();
            }

            if(Event.key.code == sf::Keyboard::H)
            {

                Card * y = deck0.dealFirst();
                drawCard(window,y->graphNumber,y->getSeed(),offset);
                offset = offset + 50;
            }

            window.display();

        }
    }
}

int Graphic::drawCard(sf::RenderWindow &window, int graphNumber, string seed, int offset)
{
    std::ostringstream oss;
    oss <<  graphNumber << seed << ".png";
    std::string var = oss.str();

    sf::Texture QHTexture;
    sf::Sprite QHImage;
    if(!QHTexture.loadFromFile(var))
        std::cout<< "Error" <<std::endl;
    QHImage.setTexture(QHTexture);
    QHImage.setScale(0.5, 0.5);
    QHImage.setPosition(offset + 100, 400);
    window.draw(QHImage);  //this is the function which draw the card's sprite
    return 0;
}

【问题讨论】:

  • 分离程序的逻辑和渲染。你的程序应该有一个状态,不仅仅由程序计数器、输出缓冲区和一些变量来表示,而是一个存储你所有卡片的数据结构。我在这里没有看到一个。哦,是的,可能是因为这不是 MVCE。好的,所以你只是没有抽出所有的牌...... PS:你可能不想每帧都调用sf::Texture::loadFromFile。
  • 我想每帧调用 loadFromFile 因为我想从 52 个图像中绘制一个,然后我不调用 loadFromFile 每一帧但我只在按 H 时调用此方法。我认为问题应该在 drawCard() 方法中,因为也许我覆盖了以前的纹理和精灵。
  • 您在一帧中只绘制了一个精灵/纹理/图像。框架在开始时被清除。

标签: c++ sfml polling


【解决方案1】:

好吧,你不应该在 while(window.pollEvent()) 循环内绘图,你应该像这样绘制:

while(window.isOpen())
    {
    sf::Event Event;
    while(window.pollEvent(Event))
    {
        if (Event.type == sf::Event::Closed)
        {
            window.close();
        }

        if(Event.key.code == sf::Keyboard::H)
        {

            Card * y = deck0.dealFirst();
            drawCard(window,y->graphNumber,y->getSeed(),offset);
            offset = offset + 50;
        }
    }

    window.clear();
    window.draw(bImage);  // this is the function which draw the background

    window.display();
}

您绘制绘图调用的方式只会在存在 SFML 事件时发生,并且只有在存在 sfml 事件时才会清除(如果您不希望它不断地渲染每一帧,这没关系......并且没有计划任何类型的动画...)。

因此,当您点击 H 时,一个 sfml 事件正在触发调用您的抽牌函数,但是由于您的卡片是您编写的函数的局部变量,因此在函数结束时它会被清除。您需要将卡片存储在某个地方,例如 sf::Sprite 的向量或列表。所以一个例子是:

#include "Graphic.h"
#include "SFML/Graphics.hpp"
#include <iostream>
#include <string>
#include "Card.h"
#include <string>
#include <sstream>

Graphic::Graphic(int offset)
{
    this->offset = offset;
}

Graphic::~Graphic()
{
    //dtor
}

int Graphic::CreateWindow(sf::RenderWindow& window, Deck &deck0)
{
    sf::Vector2i screenDimensions(800, 600);

    //Dimensioni della window
    window.create(sf::VideoMode(screenDimensions.x, screenDimensions.y), "BlackJack", sf::Style::Titlebar | sf::Style::Close);

    int index = 0;
    window.setKeyRepeatEnabled(false);

    //settare il background
    sf::Texture bTexture;
    sf::Sprite bImage;
    if(!bTexture.loadFromFile("Background.png"))
        std::cout << "Error" << std::endl;
    bImage.setTexture(bTexture);
    bImage.setScale(1.0, (float)screenDimensions.y / bTexture.getSize().y);

    //MAIN LOOP----------------------
        while(window.isOpen())
    {
    sf::Event Event;
    while(window.pollEvent(Event))
    {
        if (Event.type == sf::Event::Closed)
        {
            window.close();
        }

        if(Event.key.code == sf::Keyboard::H)
        {

            Card * y = deck0.dealFirst();
            drawCard(window,y->graphNumber,y->getSeed(),offset);
            offset = offset + 50;
        }
    }

    window.clear();
    window.draw(bImage);  // this is the function which draw the background

    for(int i = 0; i < cardList.size(); ++i)
    {
       window.draw(cardList[i]);
    }

    window.display();
    }

}

int Graphic::drawCard(sf::RenderWindow &window, int graphNumber, string seed, int offset)
{
    std::ostringstream oss;
    oss <<  graphNumber << seed << ".png";
    std::string var = oss.str();

    sf::Texture QHTexture;
    sf::Sprite QHImage;
    if(!QHTexture.loadFromFile(var))
        std::cout<< "Error" <<std::endl;
    QHImage.setTexture(QHTexture);
    QHImage.setScale(0.5, 0.5);
    QHImage.setPosition(offset + 100, 400);
    cardList.push_back(QHImage);  //this adds the sprite to our list
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-22
    • 2014-03-09
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多