Advertisement

Event Problems in SFML

Started by June 23, 2012 09:58 AM
9 comments, last by Dragonsoulj 12 years, 5 months ago
Hi forum.

I've been putting together a small 2d space game, and I recently decided to switch from using SDL to SFML. However, I'm having a little bit of trouble getting my event handling to work. This is a little bit of code I threw together to get a handle on various features in SFML before I move my project over. The relevant code looks like this:

[source lang="cpp"]while (Program.GetEvent(Event));
{
switch (Event.Type)
{
case sf::Event::Closed:
Program.Close();
break;
case sf::Event::KeyPressed:
if (Event.Key.Code == sf::Key:)
Velocity.x = .01;
if (Event.Key.Code == sf::Key::A)
Velocity.x = 0;
break;
case sf::Event::KeyReleased:
if (Event.Key.Code == sf::Key:)
Velocity.x = .01;
if (Event.Key.Code == sf::Key::A)
Velocity.x = 0;
break;
}
}[/source]

Now, the weird part about this is that the KeyPressed case only seems to fire when the shift, alt or control keys are pressed, which not what I had gathered from the documentation. I put the velocity changes in both places to test out what exactly was happening, and as mentioned above, it only correctly applies the changes on the KeyReleased event.

I would really like to know why the KeyPressed event just doesn't seem to be firing as expected.
Maybe I still need a decent grasp on programming, but what does this line check for?
if (Event.Key.Code == sf::Key:)

I've never had a problem with KeyPressed. It works for capturing keys for me. I am thinking that line I asked about is causing problems.
Advertisement
That's actually very odd.
That line is supposed to say
[source lang="cpp"]if (Event.Key.Code == sf::Key:: D)[/source]
It says that in my project, so it must have just copied over strangely, which is quite weird.
In any case, it still doesn't work.

EDIT: The forum is turning it into a smiley, its supposed to be the D key in there.
Well, for starters, you have that statement saying "If I press D, go right." "If I release D, still go right" "If I press A, stop" "If I release A, stop". Why the redundancy? Perhaps it should start on press, stop on release?

As for it only working for the mod keys, I'm not sure. Do you have a full sample of just your test code to see it work?
When running this code I usually comment out one of the sets of velocity changes. I mentioned the reason for the redundancy in the first post, but perhaps it wasn't clear.

Heres the full file:

[source lang="cpp"]#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

int main()
{
sf::RenderWindow Program(sf::VideoMode(800, 600, 32), "Junk and Tacos", sf::Style::Close);
sf::Event Event;
sf::Image Image;
Image.LoadFromFile("Dat Spaceship.bmp");
sf::Sprite Sprite;
Sprite.SetImage(Image);
sf::Vector2<float> Vector;
Vector = Sprite.GetSize();
Vector.x = .5 * Vector.x;
Vector.y = .5 * Vector.y;
Sprite.SetCenter(Vector);

Sprite.SetPosition(100, 100);
sf::Vector2f Velocity(0,0);

bool quit = false;

while (Program.IsOpened())
{
while (Program.GetEvent(Event));
{
switch (Event.Type)
{
case sf::Event::Closed:
Program.Close();
break;
case sf::Event::KeyPressed:
if (Event.Key.Code == sf::Key:biggrin.png)
Velocity.x = .01;
if (Event.Key.Code == sf::Key::A)
Velocity.x = 0;
break;
case sf::Event::KeyReleased:
if (Event.Key.Code == sf::Key:biggrin.png)
Velocity.x = .01;
if (Event.Key.Code == sf::Key::A)
Velocity.x = 0;
break;
}
}
Sprite.Move(Velocity);
Program.Clear();
Program.Draw(Sprite);
Program.Display();
}
}
[/source]

The includes didn't show up, but trust me, there are there. :P
[source ] tags are messed up on GameDev. You should prefer to use [code ] tags instead, until they fix the dumb issues (if they ever fix them, that is...).

Anyway, I'm not sure why it's happening like this. They may be coming in a sf::Event::TextEvent instead of a KeyEvent.

I'd seriously suggest upgrading to SFML 2.0. It's very stable and a release candidate, and has loads of fixes that SFML 1.6 doesn't.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Advertisement
Yep, its showing up as a textevent, which seems pretty stupid and insane to me. Oh well.
I guess I'll try upgrading to 2.0. I started with 1.6 since the sfml website gave me the impression it would best to use that until 2.0 is a little more developed, but if you say its stable it might be worth a shot.

Thanks!

I guess I'll try upgrading to 2.0. I started with 1.6 since the sfml website gave me the impression it would best to use that until 2.0 is a little more developed, but if you say its stable it might be worth a shot.

It is. Laurent, the creator, has said that the main reason 2.0 isn't marked as "finished" is because he hasn't finished rewriting all the tutorials (linky) and he is now suggesting people use SFML 2.0 in favor of 1.6, noting that SFML 1.6 is now deprecated (linky).
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Thanks so much for telling me!
Is the documentation written? Because as long as there's documentation I can live without tutorials.

Thanks so much for telling me!
Is the documentation written? Because as long as there's documentation I can live without tutorials.

Yes, the docs are pretty good for 2.0. And it's still good old SFML, so it's pretty easy to follow a 1.6 tutorial and update it to work for 2.0.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement