SFML community forums

Help => General => Topic started by: BeautiCode on June 06, 2015, 04:43:37 am

Title: Flickering
Post by: BeautiCode on June 06, 2015, 04:43:37 am
So I'm just getting back into SFML...This program is meant to display the message as I hold down the backspace button. But it's flickering badly, why is this?
Code: [Select]
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Text.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Window/Event.hpp>
int main()
{
sf::RenderWindow win(sf::VideoMode(500, 500), "Refresher", sf::Style::Default);
sf::Text wb;
sf::Font font;
font.loadFromFile("C:\\Windows\\Fonts\\Arial.ttf");
win.setVerticalSyncEnabled(true);
wb.setColor(sf::Color::Blue);
wb.setString("Welcome Back to SFML!");
wb.setCharacterSize(20);
wb.setStyle(sf::Text::Bold);
wb.setFont(font);
wb.setOrigin(wb.getLocalBounds().width / 2.0f, wb.getLocalBounds().height / 2.0f);
wb.setPosition(win.getSize().x / 2.0f, win.getSize().y / 2.0f);
while (win.isOpen())
{
sf::Event e;
win.clear();
while (win.pollEvent(e))
{
switch (e.type)
{
case sf::Event::Closed:
{
win.close();
}break;
case sf::Event::KeyPressed:
{
switch (e.key.code)
{
case sf::Keyboard::BackSpace:
{
win.draw(wb);
}break;
}
}break;
}
}
win.display();
}
return 0;
}
Title: Re: Flickering
Post by: AlejandroCoria on June 06, 2015, 04:56:45 am
That's because the event is generated when the key is pressed, and every few milliseconds (depending on the configuration of your operating system). They do not occur in all frames. You should use a bool to maintain the state of the key.
Title: Re: Flickering
Post by: BeautiCode on June 06, 2015, 05:02:24 am
That's because the event is generated when the key is pressed, and every few milliseconds (depending on the configuration of your operating system). They do not occur in all frames. You should use a bool to maintain the state of the key.
Alright thank you. One more question, I forgot...How do you remove the console in the back of the window?
Title: Re: Flickering
Post by: AlejandroCoria on June 06, 2015, 05:07:55 am
You should look at your project settings (if you are using an IDE).
Title: Re: Flickering
Post by: BeautiCode on June 06, 2015, 05:10:55 am
You should look at your project settings (if you are using an IDE).
Trying to find the setting in VS2013
Title: Re: Flickering
Post by: BeautiCode on June 06, 2015, 05:20:08 am
My solution in Code::Blocks was to change the "Build Targets" setting, but I don't see that in VS2013.
Title: Re: Flickering
Post by: BeautiCode on June 06, 2015, 05:31:33 am
Nvm I found a solution.
I linked
Code: [Select]
sfml-main-d.lib
and changed
debug -> configeration properties -> linker -> system -> SubSystem to "Windows"
Title: Re: Flickering
Post by: Hapax on June 09, 2015, 12:45:10 am
There is no need to store a boolean value in conjunction with events here. It would make more sense to simply read the current state of the key (outside of the event loop) with:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace))
    window.draw(wb);
See the keyboard tutorial (http://www.sfml-dev.org/tutorials/2.3/window-inputs.php#keyboard)  :)
Title: Re: Flickering
Post by: AlejandroCoria on June 09, 2015, 01:55:12 am
That's right. I usually recommend the use of events because they are only activated when the window is in focus, which is what you usually want. But obviously it is another possible solution :)
Title: Re: Flickering
Post by: Hapax on June 10, 2015, 09:09:52 pm
I usually recommend the use of events because they are only activated when the window is in focus, which is what you usually want
Or you could combine isKeyPressed() with this (http://www.sfml-dev.org/documentation/2.3/classsf_1_1Window.php#ac4dce670f07c5039a732ba0903ce3a77)  ;)
Title: Re: Flickering
Post by: AlejandroCoria on June 10, 2015, 09:54:05 pm
I started using SFML in version 1.6 and for some reason I never remember that function was added :P