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

Author Topic: Animation of some Sprites  (Read 9459 times)

0 Members and 1 Guest are viewing this topic.

woskaz0n

  • Newbie
  • *
  • Posts: 14
    • View Profile
Animation of some Sprites
« on: November 17, 2008, 08:46:43 pm »
Hi all,

befor I used the SFML, I tried to code some games with the SDL (and a little Framework).
Now I want to write a little programm, where the Player can move his character on two axes with the keyboard (the basic function of an jump´n´run game). But now I got the problem, that I don´t know, how I have to animate the Player. I allready solved that problem when I used the SDL with a little Framework, but now I want to write my programm, just with the API!

Some pictures to illustrate my problem:


Well, in this picture the Plane should lean to the right side, when the right key is pressed and to the left, while the left key is pressed. When no key is in action, the middle frame should appear on the screen! I hope I could explain this clearly enough.


Or perhaps like in this example, where the asteroid should be moving all the time in a loope.

I hope you can help me, beacause I need this animations for a lot of things I want to do.
Thanks in advance!

PS.: And sry if my english is too terrible.
Realism is what makes games suck. - Wouter van Oortmerssen

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Animation of some Sprites
« Reply #1 on: November 17, 2008, 09:56:40 pm »
In order to animate sprites using a sprite sheet image, you should set the sprite's sub rectangle to the frame you wish to display.

woskaz0n

  • Newbie
  • *
  • Posts: 14
    • View Profile
Animation of some Sprites
« Reply #2 on: November 19, 2008, 02:59:42 pm »
Thanks for your answer, but unfortunately I don´t understand a few things...

Code: [Select]

sf::Sprite::SetSubRect(IntRect& SubRect);

That´s the definition...

Well, so I tried this
Code: [Select]

//Open pic with fstream... no problem
sf::Image Image;
if(!Image.LoadFromMemory(memory, size));

sf::IntRect Rect(64, 0, 64, 64); //<- Here I´m not really sure, if this are the real coordinates... I´ll see!

sf::Sprite::SetSubRect(Rect& Image);


But that doesn´t work.
As you probably can see, I´m doing something really wrong... a little bit help would be nice  :)

Thanks in advance!
woskaz0n
Realism is what makes games suck. - Wouter van Oortmerssen

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Animation of some Sprites
« Reply #3 on: November 19, 2008, 03:46:13 pm »
SetSubRect is a member function of the Sprite class, it must be applied to a particular sprite object.

Code: [Select]
sf::Image Image;
Image.LoadFromMemory(memory, size);

sf::IntRect Rect(64, 0, 64, 64);

sf::Sprite sprite;
sprite.SetImage(Image);
sprite.SetSubRect(Rect);
Laurent Gomila - SFML developer

woskaz0n

  • Newbie
  • *
  • Posts: 14
    • View Profile
Animation of some Sprites
« Reply #4 on: November 19, 2008, 08:05:53 pm »
Thank you Laurent, now this works fine  :D

But now I got another problem (actually the mainly problem). When I used "SetSubRect", I can display one sprite of my sprite sheet on the screen, but I need an animation. Perhaps I´m not good enough at programming, but the only way (if you ask me) to animate it, would be to change the parameters of "sf::IntRect Rect( )", while the programm is running? As far as I know, this isn´t possible. So, what have I to do?

Thanks in advance!


P.S.: This would be a really interessting topic for a Tutorial, because I think that animations are one of the importants things for a (halfway) good looking game! (No matter if it is a RTS oder a simple Space Shooter!)
Realism is what makes games suck. - Wouter van Oortmerssen

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Animation of some Sprites
« Reply #5 on: November 19, 2008, 08:53:20 pm »
You probably need to practice more before doing such things ;)

You can get a source code for animation classes on the french wiki :
http://www.sfml-dev.org/wiki/fr/sources/frame_anim_animated
Laurent Gomila - SFML developer

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Animation of some Sprites
« Reply #6 on: November 19, 2008, 09:09:25 pm »
The idea is to change the sprite's sub rectangle every time you wish the change the animation frame.

As you can see from Laurent's link, this gets quite complicated.

woskaz0n

  • Newbie
  • *
  • Posts: 14
    • View Profile
Animation of some Sprites
« Reply #7 on: November 19, 2008, 09:51:49 pm »
Quote from: "Laurent"
You probably need to practice more before doing such things ;)


Yes, I think I´ll do this :D
And thanks for the Link!

Unfortunately it´s more complicated to make an animation with the SFML, as with the SDL...

Do you think it would be easier if I use for every animation frame an own picture instead of a sprite sheet?
Realism is what makes games suck. - Wouter van Oortmerssen

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Animation of some Sprites
« Reply #8 on: November 19, 2008, 10:27:19 pm »
Actually, unless you were using a helper library, it's the same thing as SDL.

You should really just start toying with it; nothing ever makes sense to me until I use it for myself.

Quote from: "woskaz0n"
Do you think it would be easier if I use for every animation frame an own picture instead of a sprite sheet?

It would be easier to code; people just use sprite sheets because they're easier to edit.

woskaz0n

  • Newbie
  • *
  • Posts: 14
    • View Profile
Animation of some Sprites
« Reply #9 on: November 19, 2008, 11:07:46 pm »
Quote from: "Wizzard"
Actually, unless you were using a helper library, it's the same thing as SDL.

In my opinion, the function "SDL_BlitSurface" is very usefull for programming animations...

Quote from: "Wizzard"

You should really just start toying with it; nothing ever makes sense to me until I use it for myself.

Yes, I know... I´m using the SFML since 3 or 4 days and of course there is still a lot to learn. Now I try to do a few things, which I would use in a game later (for example collision, animation or some easier things^^) - sometimes it is really easy to realise something with the SFML, but unfortunately not always. Thats why I asked a lot of things :)

Quote from: "Wizzard"

It would be easier to code; people just use sprite sheets because they're easier to edit.

Ok, thanks... than I´ll try this  :D


EDIT:
I´m very sorry that I´ve ask something (probably stupid easy) again :oops: , but I don´t find a good solution.

I want to do something like this:

Code: [Select]

if(Window.GetInput().IsKeyDown(sf::Key::Space))
      Window.Draw(sprite);


Ok, if I now press Space, the sprite appears until I release the space button. Similar situation when I use std::cout to write something in the console, the writing is shown again and again in the console while I´m holding the space button.
I think this is why I set my code into the Main-Loop, but out of the loop this piece of code is (of course) not working.
But I want that the sprite doesn´t disappear after I release the key. Or that the writing appears just one time...
You´re probably annoyed of my questions, but I´m not to lazy to try something by myself, it´s just that I don´t find a good or possible solution   :(
Realism is what makes games suck. - Wouter van Oortmerssen

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Animation of some Sprites
« Reply #10 on: November 20, 2008, 12:23:35 am »
You may ask all the questions, within reason, that you want. Nobody would be responding to them if they didn't want to help you. :)


As for your problem, the reason this is happening is because the Input class only updates when Window's GetEvent member is looped through. So, your code will have to be somewhere below the GetEvent loop that is inside your main loop.

To get the functionality that you want, simply make a bool variable above the main loop that will determine whether space has been pressed yet or not:
Code: [Select]
bool SpaceHasBeenPressed;

Then, in your main loop, when space is pressed, you will set SpaceHasBeenPressed to true:
Code: [Select]
SpaceHasBeenPressed = true;

Lastly, when it comes time to draw your sprites, you'll check if SpaceHasBeenPressed, and if it was, draw the sprite:
Code: [Select]
if (SpaceHasBeenPressed) Window.Draw(Sprite);

woskaz0n

  • Newbie
  • *
  • Posts: 14
    • View Profile
Animation of some Sprites
« Reply #11 on: November 22, 2008, 07:37:11 pm »
Hey, thanks very much for your quick answer... now this part with the Sprite ist working perfect  :D
But it´s not possible to solve the other problem (where I write something in the console) with your proposition.
But it don´t get this problem just with the console, when I want to realize something like a jump funciton, I would write
Code: [Select]

if(Window.GetInput().IsKeyDown(sf::Key::Up))
{
          Player.Move(0, -10);
}

//a very simple solution... gravitation and other things will follow

.

But because of the "game"-loop, the Player.Move part is executed as long as I hold down the arrow key. So, is there a function to deactivate keys for a certain time - but this is perhaps really not the best solution. Over and over again I don´t know what to do  :oops:

thanks in advance!
Realism is what makes games suck. - Wouter van Oortmerssen

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Animation of some Sprites
« Reply #12 on: November 23, 2008, 03:10:54 am »
Add a check to see if the player is on the ground before letting the player jump.
Code: [Select]
if (Window.GetInput().IsKeyDown(sf::Key::Up))
{
    if (Player.GetPosition().y + Player.GetSize().y >= 600)
    {
        Player.Move(0, -10);
    }
}

This assumes that the ground is along the 600th row of pixels.

 

anything