1
Window / sf::Keyboard::isKeyPressed() stops working after a few times. [sfml 2.1]
« on: May 11, 2014, 04:34:06 pm »
Hey guys,
I have the following Code in my main loop
It allways stopped working after a few times, so I decided to check wether the Keys are still recognized like this:
Dont now, wether this is important, but im using tgui for GUI
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);
}
{
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?std::cout << "Z" << std::endl;
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;
}
{
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