SFML community forums

Help => Window => Topic started by: Finn on August 08, 2010, 09:45:56 pm

Title: Clear the Input
Post by: Finn 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
Title: Clear the Input
Post by: Nexus 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);
Title: Clear the Input
Post by: Finn 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 :-/
Title: Clear the Input
Post by: Nexus 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?
Title: Clear the Input
Post by: Laurent 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)? ;)
Title: Clear the Input
Post by: Finn 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
Title: Clear the Input
Post by: Laurent 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 ;)
Title: Clear the Input
Post by: Finn 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))
        {
        //...
        }
}
Title: Clear the Input
Post by: Laurent 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 ;)
Title: Clear the Input
Post by: Nexus 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... ;)
Title: Clear the Input
Post by: Finn 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();
}
}
Title: Clear the Input
Post by: Laurent 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?
Title: Clear the Input
Post by: Finn on August 10, 2010, 03:59:43 pm
Linux Ubuntu 10.04
Title: Clear the Input
Post by: Finn on August 11, 2010, 12:58:03 pm
Anything I can do to solve this problem?
Title: Clear the Input
Post by: Laurent on August 11, 2010, 01:17:33 pm
What you could do it to test the same piece of code with SFML 2.
Title: Clear the Input
Post by: Finn on August 13, 2010, 06:57:33 pm
Ok....I installed SFMl 2!
But...is there something wrong with the sf::String class? With 1.6 my project compiled without any errors...but now I get various errors on using the sf:String class.. :(
like
Code: [Select]

error: ‘class sf::String’ has no member named ‘SetFont’


What the... :-o
Title: Clear the Input
Post by: Dig on August 13, 2010, 10:10:50 pm
sf::String is now sf::Text I believe (which is a much better name).
Title: Clear the Input
Post by: Finn on August 14, 2010, 04:18:59 pm
Code: [Select]

engine.cpp:85: error: ‘class sf::Text’ has no member named ‘SetText’


Just the same
Title: Clear the Input
Post by: Laurent on August 14, 2010, 04:35:26 pm
sf::String::SetText -> sf::Text::SetString ;)
Title: Clear the Input
Post by: Finn on August 14, 2010, 05:03:51 pm
Omg xD Why??? :D
Title: Clear the Input
Post by: Laurent on August 14, 2010, 05:06:17 pm
I think they are better names, aren't they?
Title: Clear the Input
Post by: Finn on August 14, 2010, 05:09:46 pm
kk....but other errors left here!
Code: [Select]

error: ‘class sf::FloatRect’ has no member named ‘Right’
error: ‘class sf::FloatRect’ has no member named ‘Bottom’



And many issues with the sf::IntRect class...like:
Code: [Select]

‘class sf::IntRect’ has no member named ‘Offset’

Also a renamed class?
Title: Clear the Input
Post by: Laurent on August 14, 2010, 05:25:03 pm
A huge number of things have changed... that's why it's SFML 2.0 and not 1.7 ;)

You'd better have a look at the documentation:
http://www.sfml-dev.org/documentation/2.0/