- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的问题是函数没有按照我的要求执行。这个“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/
我一直在对我的游戏引擎进行一些基准测试,并发现了一个错误的罪魁祸首: glfwPollevents(); 我对此进行了彻底的计时,95% 的时间它的表现符合预期,但有时它会占用我的更新间隔的 5-20
我的问题是函数没有按照我的要求执行。这个“CreateWindow”函数有主循环。在主循环中,我想要一个固定的背景,每次按下 H 按钮时,我都想在背景上绘制一张卡片( Sprite )。这里有什么问题
我正在尝试让 SDL 检测 OS X 上 C++ 控制台应用程序/游戏的键盘事件。但不仅是 SDL_PollEvent() 没有返回任何键盘事件,据我所知通过调试它应该更新的 SDL_Event* 根
显然,当我连续按下某个键时,我的 pollEvent 会稍作停顿, 例如,如果按下但不释放一个键,它会调用按下事件,但会暂停 0.5 秒,只有在那之后才开始连续调用按下事件简单地说,如果我只是按下一个
这是我在最近的 SFML 2.4.2 中的代码 #include #include int main() { sf::RenderWindow window(sf::VideoMode(800,
我正在尝试注册 Controller 按钮/方向键按下并持续按住所述按钮,这样它会连续吐出输出,而不是一次按下一个按钮,然后退出轮询事件循环。现在我有一小段虚拟代码,如果我按住一个按钮,我正试图在流中
我有这样的主要内容: #include #include #include "okno.h" using namespace sf; int main() { // Create the
我正在使用监视服务来监视目录并在新创建的文件上触发逻辑。我最近遇到的挑战之一是当大量文件需要处理并且被快速复制到监视目录而无法处理时会触发溢出。 API说到溢出: File systems may r
http://www.sfml-dev.org/documentation/1.6/classsf_1_1RenderWindow.php 根据 1.6 文档,渲染窗口没有 pollEvent 我想知
本文整理了Java中org.uberfire.java.nio.file.WatchKey.pollEvents()方法的一些代码示例,展示了WatchKey.pollEvents()的具体用法。这些
看着这个Java example ,关于关键状态,Oracle 说: Ready indicates that the key is ready to accept events. When firs
我正在尝试查看对特定文件夹所做的所有修改。当我复制一个包含 2~5 个子文件夹且每个子文件夹有 2~3 个文件的文件夹时。一切都很好。 当我复制复杂的文件夹结构时出现问题。例如,当我复制任何示例 We
我是一名优秀的程序员,十分优秀!