1
我的问题是,该功能不做我想要的。 这个“CreateWindow”函数有主循环。在主循环中,我想要一个固定的背景,每次按H按钮,我都想在背景上绘制一张卡片(精灵)。 这里有什么问题?我的功能画卡,但是当我按下H时,先前的卡被删除并且下一张卡被绘制。 这是关于我认为的事件,因为每次发生事件(我移动鼠标,我按其他键等)以前的卡被删除... 我使用sfml 2.0Sfml pollEvent,每个新的图像删除前一个
这是我的实现图形类
#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。 – LogicStuff
我想调用loadFromFile每一帧,因为我想绘制一个52的图像,然后我不调用loadFromFile everyframe,但我只有当我按下H时调用此方法。 我认为问题应该在drawCard( )方法,因为我可能会覆盖以前的纹理和精灵。 –
您只在一个框架中绘制一个精灵/纹理/图像。该帧在开始时被清除。 – LogicStuff