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

Author Topic: Clear the Input  (Read 6832 times)

0 Members and 1 Guest are viewing this topic.

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Clear the Input
« on: August 08, 2010, 09:45:56 pm »
Hey
I'm working on a rpg at the moment. Now I want to know how to clear the Input. For example:
I want to level up the character by pressing 'F1'.
If I do it like:
Code: [Select]

if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F1))
{
Player.levelup();
        //...
}


it's like 30 level ups by pressing 'f1'....any suggestions?
thanxs,
Finn

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6275
  • Thor Developer
    • View Profile
    • Bromeon
Clear the Input
« Reply #1 on: August 08, 2010, 10:07:32 pm »
There is a function to enable or disable automatic key repeats:
Code: [Select]
sf::Window::EnableKeyRepeat(bool enable);
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Clear the Input
« Reply #2 on: August 08, 2010, 10:40:27 pm »
Quote from: "Nexus"
There is a function to enable or disable automatic key repeats:
Code: [Select]
sf::Window::EnableKeyRepeat(bool enable);


Didn't change a thing :-/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6275
  • Thor Developer
    • View Profile
    • Bromeon
Clear the Input
« Reply #3 on: August 08, 2010, 11:16:58 pm »
Can you show a minimal and complete code example still reproducing the problem?

Which version of SFML do you use?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Clear the Input
« Reply #4 on: August 09, 2010, 08:48:51 am »
Just to be sure: you didn't copy & paste the code given by Nexus, right? You wrote window.EnableKeyRepeat(false)? ;)
Laurent Gomila - SFML developer

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Clear the Input
« Reply #5 on: August 09, 2010, 10:27:27 am »
Quote from: "Laurent"
Just to be sure: you didn't copy & paste the code given by Nexus, right? You wrote window.EnableKeyRepeat(false)? ;)


Sure I did O_o
Version 1.6

Code: [Select]

if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F1))
{
window.EnableKeyRepeat(false);
Player.levelup();
Text_Playerlevel.SetText(Player.GetLevel());
}


I also tried to put it on the beginning of the main function...the same

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Clear the Input
« Reply #6 on: August 09, 2010, 10:41:02 am »
Quote
I also tried to put it on the beginning of the main function...

This is where it's supposed to be put, yes.

Ok, so now we'll need the complete and minimal example that Nexus talked about, to figure out what you did wrong ;)
Laurent Gomila - SFML developer

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Clear the Input
« Reply #7 on: August 09, 2010, 04:19:54 pm »
It's like:
main.cpp
Code: [Select]

#include "main.h"
#include "engine.h"

int main()
{
engine engine;
engine.game();

return EXIT_SUCCESS;
}


engine.cpp
Code: [Select]

engine::engine()
{
window.EnableKeyRepeat(false);
window.Create(sf::VideoMode(1024,768,32),"RPG");
}

void engine::game()
{
        while(window.IsOpened())
{
while(window.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
window.Close();//exit
}

if(GSchanged)//GameState changed
Load();//load stuff for the new GameState

Draw();//Draw everything onto the screen
HandleInput();//handle input for each gamestate
}
}

void engine::HandleInput()
{
        if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F1))
        {
        //...
        }
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Clear the Input
« Reply #8 on: August 09, 2010, 04:22:55 pm »
We really need a complete and minimal code. The prupose is to have something that we can compile, execute and debug without doing anything else than copying and pasting your code in a cpp file.

So you actually need to take some time to write this code and make sure it compiles and still produces the error ;)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6275
  • Thor Developer
    • View Profile
    • Bromeon
Clear the Input
« Reply #9 on: August 09, 2010, 04:45:04 pm »
Remove all the stuff that is not directly required for a working application or related to the error. This concerns game logics, graphic handling, user-defined classes, and so on. Only leave what's really necessary.

Use a single file (no headers) to demonstrate the problem with as few code lines as possible. Then you may call your code minimal. But don't forget it still has to be complete and compilable... ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Clear the Input
« Reply #10 on: August 10, 2010, 11:22:40 am »
This should work:
Code: [Select]

#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
sf::RenderWindow window(sf::VideoMode(800,600),"Test");
sf::Event Event;

window.EnableKeyRepeat(false);
int i = 0;

while(window.IsOpened())
    {
window.GetEvent(Event);
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F1))
{
std::cout << i << std::endl;
i++;
}

window.Clear();
window.Display();
}
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Clear the Input
« Reply #11 on: August 10, 2010, 11:38:13 am »
That's perfect, thank you :)

So with the code above, when you hold F1 you get repeated KeyPressed events?

What operating system do you use?
Laurent Gomila - SFML developer

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Clear the Input
« Reply #12 on: August 10, 2010, 03:59:43 pm »
Linux Ubuntu 10.04

Finn

  • Jr. Member
  • **
  • Posts: 90
    • View Profile
Clear the Input
« Reply #13 on: August 11, 2010, 12:58:03 pm »
Anything I can do to solve this problem?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Clear the Input
« Reply #14 on: August 11, 2010, 01:17:33 pm »
What you could do it to test the same piece of code with SFML 2.
Laurent Gomila - SFML developer