C++ Sfml pollEvent,每个新映像都会删除上一个映像
我的问题是这个函数不符合我的要求。 此“CreateWindow”函数具有主循环。在主循环中,我想要一个固定的背景,每次按下H按钮,我都想在背景上画一张卡片(精灵)。 这里有什么问题?我的功能是绘制卡片,但当我按H时,上一张卡片被删除,下一张卡片被绘制。 我认为这与事件有关,因为每次事件发生时(我移动鼠标、按下其他键等),前一张卡都会被删除。。。 我正在使用SFML2.0 下面是我对Graphic类的实现C++ Sfml pollEvent,每个新映像都会删除上一个映像,c++,sfml,polling,C++,Sfml,Polling,我的问题是这个函数不符合我的要求。 此“CreateWindow”函数具有主循环。在主循环中,我想要一个固定的背景,每次按下H按钮,我都想在背景上画一张卡片(精灵)。 这里有什么问题?我的功能是绘制卡片,但当我按H时,上一张卡片被删除,下一张卡片被绘制。 我认为这与事件有关,因为每次事件发生时(我移动鼠标、按下其他键等),前一张卡都会被删除。。。 我正在使用SFML2.0 下面是我对Graphic类的实现 #include "Graphic.h" #include "SFML/Graphics.
#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;
}
#包括“Graphic.h”
#包括“SFML/Graphics.hpp”
#包括
#包括
#包括“Card.h”
#包括
#包括
图形::图形(整数偏移)
{
此->偏移=偏移;
}
图形::~Graphic()
{
//dtor
}
int Graphic::CreateWindow(sf::RenderWindow&window、Deck&deck0)
{
矢量2i屏幕尺寸(800600);
//三角窗
创建(sf::VideoMode(screenDimensions.x,screenDimensions.y),“21点”,sf::Style::Titlebar | sf::Style::Close);
int指数=0;
window.setKeyRepeatEnabled(假);
//背景色
sf::纹理;
sf::雪碧双图像;
如果(!bTexture.loadFromFile(“Background.png”))
std::cout getSeed(),offset);
偏移量=偏移量+50;
}
window.display();
}
}
}
内部图形::绘图卡(sf::渲染窗口和窗口、内部图形编号、字符串种子、内部偏移)
{
std::ostringstream oss;
oss好吧,你不应该在循环中画图,而(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事件正在被调用draw card函数的触发器触发,然而,由于您的卡是您编写的函数的局部变量,因此在函数的末尾它会被清除。您需要将卡存储在某个位置,例如向量或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;
}
#包括“Graphic.h”
#包括“SFML/Graphics.hpp”
#包括
#包括
#包括“Card.h”
#包括
#包括
图形::图形(整数偏移)
{
此->偏移=偏移;
}
图形::~Graphic()
{
//dtor
}
int Graphic::CreateWindow(sf::RenderWindow&window、Deck&deck0)
{
矢量2i屏幕尺寸(800600);
//三角窗
创建(sf::VideoMode(screenDimensions.x,screenDimensions.y),“21点”,sf::Style::Titlebar | sf::Style::Close);
int指数=0;
window.setKeyRepeatEnabled(假);
//背景色
sf::纹理;
sf::雪碧双图像;
如果(!bTexture.loadFromFile(“Background.png”))
std::cout getSeed(),offset);
偏移量=偏移量+50;
}
}
window.clear();
窗口绘制(双页);//这是绘制背景的函数
对于(int i=0;i oss将程序的逻辑和渲染分开。你的程序应该有一个状态,不仅由程序计数器、输出缓冲区和一些变量表示,还应该有一个存储所有卡的数据结构。我在这里没有看到。哦,是的,可能是因为这不是MVCE。好吧,你只是没有绘制所有卡……PS:你可能不想校准lsf::Texture::loadFromFile
每帧。我想每帧调用loadFromFile,因为我想从52幅图像中画一幅,然后我不调用loadFromFile everyframe,但我只在按下H时调用此方法。我想问题应该在drawCard()中方法,因为可能我覆盖了以前的纹理和精灵。您在一个帧中只绘制了一个精灵/纹理/图像。该帧在开始时被清除。