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

Author Topic: Animating Sprites  (Read 17770 times)

0 Members and 1 Guest are viewing this topic.

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Animating Sprites
« on: September 12, 2009, 02:06:00 am »
How would you go about using several images to animate a moving sprite? Sprite.move seems to be geared just to one static image.

93interactive

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • http://93-interactive.com
Animating Sprites
« Reply #1 on: September 12, 2009, 06:26:55 am »
well, you'll have to handle it manually, something like:

Code: [Select]

#include <vector>

struct myAnimFrame {
  sf::Sprite sprite;
  float frameTime;
};

class myAnim {
private:
  vector <myAnimFrame *>frames;
  float currentTime;
  int currentFrame;

purblic:
  MyAnim() {
    this->currentTime=0.0f;
    this->currentFrame=0;

    myAnim *frame;

    // set up your animation frame by frame
    frame=(myAnimFrame *)malloc(sizeof myAnimFrame *);
    frame->frameTime=0.1f; // time how long frame is displayed
    frame->sprite=sf::Sprite(frame1Image);
    this->frames->push_back(frame);

    frame=(myAnimFrame *)malloc(sizeof myAnimFrame *);
    frame->frameTime=0.2f;
    frame->sprite=sf::Sprite(frame1Image);
    this->frames->push_back(frame);
  }

  void Draw(sf::Window target) {
    // get current frame, update pos and draw
    myAnimFrame *f=this->frames.at(this->currentFrame);
    myAnimFrame->sprite.SetPosition(x,y);
    target->draw(myAnimFrame->sprite);

    // update time and check if we need to change frame
    this->currentTime+=target->GetFrameTime();
    if (this->currentTime>=f->frameTime) {
      this->currentTime-=f->frameTime;
      this->currentFrame++;
      if (this->currentFrame>this->frames.size()-1) this->currentFrame=0;
    }
  }
};



this is untested code, just to show the principle, ofcourse you would have to hold the images too and clean up on destruction, or use classes instead of malloced structs, then you don't have to clean up, but it would be slightly slower.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Animating Sprites
« Reply #2 on: September 12, 2009, 04:47:57 pm »
NEVER use malloc ( and his friends ) with C++, use new instead.
( Googlize to know exactly why if you want. )

There are some Animation tools on this wiki.
SFML / OS X developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Animating Sprites
« Reply #3 on: September 12, 2009, 05:44:46 pm »
Quote from: "93interactive"
ofcourse you would have to hold the images too and clean up on destruction, or use classes instead of malloced structs, then you don't have to clean up, but it would be slightly slower.
No, it would not! That's a common opinion, but OOP techniques do not imply a loss of performance. C is not fundamentally faster than C++. But even if it were - wouldn't a well-formed, safe program get a higher priority than a little speed gain which isn't even proven? This is also known as premature optimization. Risking undefined behaviour because of such belief is just not worth it. An application programmed like this is difficult to maintain and to debug, and in many cases, it is even slower, because the programmer based his optimizations on assumptions instead of measurement. Thus, you combine all the drawbacks, which is really evil.

And why are so many people using raw, possessing pointers inside STL containers even if they don't need it? That can be quite dangerous (regarding memory leaks, dangling pointers and those problems). There are some reasons to use owning pointers (polymorphism, noncopyables, exception safety or general flexibility), but often, none of them is required. Even for these cases, there are better alternatives like Boost's pointer containers; for the ordinary cases, use instances directly instead of pointer indirections.

Additionally, your code contains several other mistakes, for example copying noncopyable objects or using sizeof on types without parentheses. By the way: structs are classes.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

93interactive

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • http://93-interactive.com
Animating Sprites
« Reply #4 on: September 13, 2009, 01:23:41 am »
Quote from: "Nexus"
why are so many people using raw, possessing pointers inside STL containers even if they don't need it? That can be quite dangerous (regarding memory leaks, dangling pointers and those problems).


because there is no law how to code. i code how i want and i explicitly mentioned, that there is a option. pointers are just dangerous if you don't know what your are doing.

Quote

Even for these cases, there are better alternatives like Boost's pointer containers


but thats another library, i dont want to use tons of libraries for things that are already there

Quote

Additionally, your code contains several other mistakes, for example copying noncopyable objects or using sizeof on types without parentheses. By the way: structs are classes.


read my lips: this is untested code, just to show the principle

why next time not try to answer his question instead of trying to force others program your way?

i am not interested in pure oo, as it makes everything slow, and 25 years of experience in game industry are my proof (ok, i admit, the first commercial game was in pure assembler), but i always encourage people to program the way *they* like instead of forcing them my pattern.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Animating Sprites
« Reply #5 on: September 13, 2009, 02:36:49 am »
Quote from: "93interactive"
i am not interested in pure oo, as it makes everything slow, and 25 years of experience in game industry are my proof
Times change......
Quote

read my lips: this is untested code, just to show the principle
A noob won't see the mistakes so you've better give good examples.

Quote
because there is no law how to code.
Do you mean "a tailor can use scotch tape instead of thread" ? :roll:
SFML / OS X developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Animating Sprites
« Reply #6 on: September 13, 2009, 02:49:52 am »
Quote from: "93interactive"
because there is no law how to code. i code how i want and i explicitly mentioned, that there is a option. pointers are just dangerous if you don't know what your are doing.
[...]
why next time not try to answer his question instead of trying to force others program your way?
Do you see the difference between "to force" and "to advise"? Lots of people using pointers are not aware of what they're doing, that's why pointers are dangerous for them (of course not in general, pointers are a powerful feature). If a problem can be solved in two ways - the easy, obvious one, and the other one, which is much more error-prone and where you have to be very attentive - which one would you recommend to people who aren't used to core language features?

It's exactly because of people like you why I say "do rather not use raw pointers in STL containers". You are obviously part of the people not knowing what they're doing as you lack important knowledge about C++. Do you know, why malloc() may be faster in your example? Because it just allocates memory, no construction is performed. Do you also know what "no construction" means if you are dealing with Non-POD-objects? That's very hazardous. In your example, you generate massive undefined behaviour. Sorry, but with a knowledge like that, I do not consider you to be able to judge about what is dangerous or not.

Some of your code I referred to:
Code: [Select]
struct myAnimFrame {
  sf::Sprite sprite;
  float frameTime;
};

    frame=(myAnimFrame *)malloc(sizeof myAnimFrame *);
    frame->frameTime=0.1f; // time how long frame is displayed
    frame->sprite=sf::Sprite(frame1Image);
    this->frames->push_back(frame);


Quote from: "93interactive"
but thats another library, i dont want to use tons of libraries for things that are already there
I explicitely said "even for these cases", which means they're rather exceptional. And of course everyone can choose, if he does all work manually and makes  debugging and maintenance to his hell. At least, I don't, and I think it's not wrong to share this approach with others. By the way, many C++ programmers use Boost.

Quote from: "93interactive"
read my lips: this is untested code, just to show the principle
And it's a very bad way to show the principle. Better use pseudocode, if you aren't sure about the programming language. Your example is only misleading beginners.

Quote from: "93interactive"
i am not interested in pure oo, as it makes everything slow, and 25 years of experience in game industry are my proof (ok, i admit, the first commercial game was in pure assembler), but i always encourage people to program the way *they* like instead of forcing them my pattern.
Don't take it personally, but I don't believe the 25 years at all. You are making trivial errors and have those views of programming. Statements like "OO is slower" are pure propaganda, that must already be nonsense because OOP is a technique, not a concrete realization. Even if the 25 years should be right - what would that prove? That you're an expert and know what really matters? Besides, no one spoke of pure OOP, that's not possible in C++ anyway.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Animating Sprites
« Reply #7 on: September 13, 2009, 03:27:33 am »
Quote from: "Hiura"
NEVER use malloc ( and his friends ) with C++, use new instead.
( Googlize to know exactly why if you want. )

There are some Animation tools on this wiki.


hi, when I try your example it says it can't open animated.hpp even though I included it in the project.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Animating Sprites
« Reply #8 on: September 13, 2009, 03:29:40 am »
Quote from: "Jarwulf"
hi, when I try your example it says it can't open animated.hpp even though I included it in the project.
Did you make sure the file is in the project's working directory or in one of its include paths?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Animating Sprites
« Reply #9 on: September 13, 2009, 04:10:08 am »
Quote from: "Nexus"
Quote from: "Jarwulf"
hi, when I try your example it says it can't open animated.hpp even though I included it in the project.
Did you make sure the file is in the project's working directory or in one of its include paths?


its in the same directory and included in the project itself shouldn't that be enough?

efeX

  • Newbie
  • *
  • Posts: 13
    • View Profile
Animating Sprites
« Reply #10 on: September 13, 2009, 04:15:08 am »
Quote from: "93interactive"
Quote from: "Nexus"
why are so many people using raw, possessing pointers inside STL containers even if they don't need it? That can be quite dangerous (regarding memory leaks, dangling pointers and those problems).


because there is no law how to code. i code how i want and i explicitly mentioned, that there is a option. pointers are just dangerous if you don't know what your are doing.

Quote

Even for these cases, there are better alternatives like Boost's pointer containers


but thats another library, i dont want to use tons of libraries for things that are already there

Quote

Additionally, your code contains several other mistakes, for example copying noncopyable objects or using sizeof on types without parentheses. By the way: structs are classes.


read my lips: this is untested code, just to show the principle

why next time not try to answer his question instead of trying to force others program your way?

i am not interested in pure oo, as it makes everything slow, and 25 years of experience in game industry are my proof (ok, i admit, the first commercial game was in pure assembler), but i always encourage people to program the way *they* like instead of forcing them my pattern.



Ahahahaha. There is so many things wrong with this post I don't know where to start.

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Animating Sprites
« Reply #11 on: September 13, 2009, 07:05:41 am »
Quote from: "Nexus"
Quote from: "Jarwulf"
hi, when I try your example it says it can't open animated.hpp even though I included it in the project.
Did you make sure the file is in the project's working directory or in one of its include paths?


I went ahead and included all the files in src and now its whining for pauseableclock.hpp which I included and it still won't work.

K-Bal

  • Full Member
  • ***
  • Posts: 104
    • View Profile
    • pencilcase.bandcamp.com
    • Email
Animating Sprites
« Reply #12 on: September 13, 2009, 10:52:17 am »
Do you have a seperate include and src folder?
Listen to my band: pencilcase.bandcamp.com

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Animating Sprites
« Reply #13 on: September 13, 2009, 11:02:48 am »
Jarwulf, how d'you organize your files into directories ?
SFML / OS X developer

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Animating Sprites
« Reply #14 on: September 13, 2009, 07:46:00 pm »
Quote from: "Hiura"
Jarwulf, how d'you organize your files into directories ?


In the projects folder I have the 'animation' project folder and I put it in there. Usually with a header file though I only have to include it through the IDE and it works without having to do this.


I'm using VS2008 just in case that matters.

 

anything