Hey guys,
I have the following Code in my main loop
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z) && ChernarusSprite.getScale().x <= 0.95f)
{
ChernarusSprite.setScale(ChernarusSprite.getScale().x + 0.05f, ChernarusSprite.getScale().y + 0.05f);
}
It allways stopped working after a few times, so I decided to check wether the Keys are still recognized like this:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
std::cout << "Z" << std::endl;
And it doesnt get recognized. Any Ideas why this part of SFML 2.1 stops working?
int main()
{
sf::RenderWindow window(sf::VideoMode(1200, 900), "asdasfasfasd", sf::Style::Close | sf::Style::Resize);
window.setFramerateLimit(60);
tgui::Gui gui(window);
sf::RectangleShape MapBG;
sf::Image Chernarus;
Chernarus.loadFromFile("chernarus_8000x7999px.jpg");
sf::Texture ChernarusTexture;
ChernarusTexture.loadFromImage(Chernarus);
ChernarusTexture.setSmooth(true);
sf::Sprite ChernarusSprite(ChernarusTexture);
ChernarusSprite.scale(sf::Vector2f(0.1f, 0.1f));
ChernarusSprite.setPosition(12, 12);
MapBG.setPosition(sf::Vector2f(12, 12));
MapBG.setSize(sf::Vector2f(800, 800));
MapBG.setOutlineThickness(2.0f);
MapBG.setOutlineColor(sf::Color(80, 80, 80));
MapBG.setFillColor(sf::Color(0, 0, 0));
if (gui.setGlobalFont("fonts/DejaVuSans.ttf") == false)
return 1;
loadWidgets(gui);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
gui.handleEvent(event);
}
tgui::Callback callback;
while (gui.pollCallback(callback))
{
switch (callback.id)
{
case 1:
window.setTitle(gen_random(32));
break;
case 2:
break;
case 3:
break;
default:
break;
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z) && ChernarusSprite.getScale().x <= 0.95f)
{
ChernarusSprite.setScale(ChernarusSprite.getScale().x + 0.05f, ChernarusSprite.getScale().y + 0.05f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
std::cout << "Z" << std::endl;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::H) && ChernarusSprite.getScale().x >= 0.15f)
{
ChernarusSprite.setScale(ChernarusSprite.getScale().x - 0.05f, ChernarusSprite.getScale().y - 0.05f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::H))
std::cout << "H" << std::endl;
window.clear(sf::Color(245, 245, 255));
gui.draw();
window.draw(MapBG);
window.draw(ChernarusSprite);
window.display();
}
return 0;
}
Dont now, wether this is important, but im using tgui for GUI
It looks like it should work but you could try putting the real time keyboard event in the poll event loop: while (Window.pollEvent(Event))
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
std::cout << "Z" << std::endl;
}
Are there any other parts of your program that could affect your input? I tested SFML handling multiple if statements of the same condition and my input works fine e.g.
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
{
std::cout << "A" << std::endl;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
std::cout << "Z" << std::endl;
I think it may be your gui hanging rather than SFML, could it be gui.handleEvent(event);
is maybe doing something you do not want it to do?