Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Flickering  (Read 4865 times)

0 Members and 1 Guest are viewing this topic.

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Flickering
« 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;
}
« Last Edit: June 06, 2015, 05:31:48 am by BeautiCode »

AlejandroCoria

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • alejandrocoria.games
    • Email
Re: Flickering
« Reply #1 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.

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Flickering
« Reply #2 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?

AlejandroCoria

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • alejandrocoria.games
    • Email
Re: Flickering
« Reply #3 on: June 06, 2015, 05:07:55 am »
You should look at your project settings (if you are using an IDE).

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Flickering
« Reply #4 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

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Flickering
« Reply #5 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.

BeautiCode

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Flickering
« Reply #6 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"

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Flickering
« Reply #7 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  :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

AlejandroCoria

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • alejandrocoria.games
    • Email
Re: Flickering
« Reply #8 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 :)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Flickering
« Reply #9 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  ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

AlejandroCoria

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • alejandrocoria.games
    • Email
Re: Flickering
« Reply #10 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

 

anything