gpt4 book ai didi

c++ - sfml pollEvent,每个新图像都会删除前一个图像

转载 作者:行者123 更新时间:2023-11-30 05:17:07 25 4
gpt4 key购买 nike

我的问题是函数没有按照我的要求执行。这个“CreateWindow”函数有主循环。在主循环中,我想要一个固定的背景,每次按下 H 按钮时,我都想在背景上绘制一张卡片( Sprite )。这里有什么问题?我的功能是抽牌,但是当我按 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;
}

最佳答案

好吧,你不应该在你的 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 事件被触发调用你的抽牌函数,但是因为你的卡片是你编写的函数的局部变量,所以在函数结束时它被清除。您需要将卡片存储在某个地方,例如 vector 或 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;
}

关于c++ - sfml pollEvent,每个新图像都会删除前一个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42277879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com