SFML community forums

Help => Graphics => Topic started by: David on April 27, 2011, 03:48:43 pm

Title: Sprite Help
Post by: David on April 27, 2011, 03:48:43 pm
I just started SFML a few days ago, and I need an explaination on the sprites tutorial :?

Is it saying that you take small sections of a sprite sheet and displaying them accordingly or is it saying that you have to upload all of the images and then displaying them :?:

Can someone also help explain the Event function? I'm kinda confused

I'm using Code::Blocks if that helps, which I doubt :wink:
Title: Sprite Help
Post by: Nexus on April 27, 2011, 03:55:34 pm
sf::Sprite is just a class that references a sf::Image (or parts of it). So the real image data, i.e. the pixels, are stored inside sf::Image. You normally initialize images once, afterwards you work with sprites. Sprites can be colorized, moved, rotated, scaled, flipped and drawn on the screen.

I don't know whether this answers your question, but your formulation is really unprecise and hard to understand. Same with events, please tell us in a concrete way what you don't understand. By the way, there is nowhere a "Event" function.
Title: Events
Post by: David on April 27, 2011, 04:33:54 pm
Your explaination helped

And for the Events part, I just want to know how can you program a key listener so the user can press the SHIFT key and make it do something
Title: Sprite Help
Post by: Fred_FS on April 27, 2011, 05:26:36 pm
Have a look at this: http://www.sfml-dev.org/tutorials/1.6/window-events.php
There is everything explained what you want to know.
Title: Re: Events
Post by: Dimple on April 27, 2011, 05:38:45 pm
Quote from: "David"
Your explaination helped

And for the Events part, I just want to know how can you program a key listener so the user can press the SHIFT key and make it do something

There is no need for a key listener (Java programmer?). If you want something to happen when the user presses shift, you can do this (inside the event-loop):
Code: [Select]

if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::LShift)) {
    // Do here what you want to happen when the user presses shift
}
Title: Re: Events
Post by: Nexus on April 27, 2011, 06:08:34 pm
Quote from: "Dimple"
There is no need for a key listener
Sometimes there is, for example when you want to add/remove functions associated with events at runtime. That's also why I have implemented event listeners in my library ;)
Title: Sprite Help
Post by: Dimple on April 27, 2011, 06:35:32 pm
You are right, I never thought about that. I assumed that he just wanted to execute some code when shift-key is pressed, not to do anything as fancy as that. :D
Title: Re: Events
Post by: David on April 27, 2011, 07:05:04 pm
Quote from: "Dimple"
Quote from: "David"
Your explaination helped

And for the Events part, I just want to know how can you program a key listener so the user can press the SHIFT key and make it do something

There is no need for a key listener (Java programmer?). If you want something to happen when the user presses shift, you can do this (inside the event-loop):
Code: [Select]

if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::LShift)) {
    // Do here what you want to happen when the user presses shift
}



Can you only do it on one of the shift keys? :?:

Can you do this? :?

Code: [Select]

if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Shift)) {
    // Do here what you want to happen when the user presses shift
}


Or this?

 :idea:
Code: [Select]

if ((Event.Type == sf::Event::KeyPressed) && ((Event.Key.Code == sf::Key::LShift) || (Event.Key.Code == sf::Key::RShift))  {
    // Do here what you want to happen when the user presses shift
}
Title: Sprite Help
Post by: Nexus on April 27, 2011, 07:09:50 pm
The second code works, the first doesn't. You could also find this out on your own.

And lookup the documentation, there you see which identifiers are valid keys. "Shift" is none of them.
Title: Re: Events
Post by: Fred_FS on April 27, 2011, 11:37:05 pm
Quote from: "David"

Can you do this? :?
Code: [Select]

if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Shift)) {
    // Do here what you want to happen when the user presses shift
}



You can do it in a very similar way. Key Event (http://www.sfml-dev.org/documentation/2.0/structsf_1_1Event_1_1KeyEvent.htm#a024b19f70f7a0c04f358b6fb5b818984) has special variables to check, wether shift, control or alt was pressed or not.

Code: [Select]

if ((Event.Type == sf::Event::KeyPressed) && Event.Key.Shift )
{
    // Do here what you want to happen when the user presses shift
}

So this should work.
Title: Re: Events
Post by: David on April 27, 2011, 11:50:44 pm
Quote from: "Fred_FS"
Quote from: "David"

Can you do this? :?
Code: [Select]

if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Shift)) {
    // Do here what you want to happen when the user presses shift
}



You can do it in a very similar way. Key Event (http://www.sfml-dev.org/documentation/2.0/structsf_1_1Event_1_1KeyEvent.htm#a024b19f70f7a0c04f358b6fb5b818984) has special variables to check, wether shift, control or alt was pressed or not.

Code: [Select]

if ((Event.Type == sf::Event::KeyPressed) && Event.Key.Shift )
{
    // Do here what you want to happen when the user presses shift
}

So this should work.


That fills the missing gaps. Thanks for everyone's contribution to this :D
Title: Sprite Help
Post by: Laurent on April 28, 2011, 07:48:01 am
Code: [Select]
if ((Event.Type == sf::Event::KeyPressed) && Event.Key.Shift )
{
    // Do here what you want to happen when the user presses shift
}

This code is not equivalent. It's not triggered when shift is pressed, but when any key is pressed while holding shift.
Title: Sprite Help
Post by: Fred_FS on April 28, 2011, 11:31:16 am
That was my fault. I am sorry. I just saw this code and thought it would be possible to use it in this way. But actually it is logical, because KeyEvent::Shift is not a key code. At least I now know, what it is good for ;).