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

Author Topic: Animating Sprites  (Read 17760 times)

0 Members and 1 Guest are viewing this topic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Animating Sprites
« Reply #15 on: September 13, 2009, 07:51:11 pm »
You have to use "" and not <> at #include statements if you want the preprocessor to search in your project's directory, too. Did you do that?

Otherwise, double-check your path, this must work. If nothing helps, create a new empty console project and try again.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Animating Sprites
« Reply #16 on: September 13, 2009, 08:06:11 pm »
In addition to Hiura's files does anybody have a working barebones animation algorithm they can point me to? I think I found something on the french wiki but its hard to understand whats going on there with all the foreign words.

Spidyy

  • Sr. Member
  • ****
  • Posts: 493
    • View Profile
Animating Sprites
« Reply #17 on: September 13, 2009, 09:04:15 pm »
I'll give you my own Animated sprites class : Gfx

http://www.megaupload.com/?d=PPDBQ0E0

BE CAREFULL, THERE IS NO COMMENTS ON THE SOURCES§§ :(

In some word :

Gfx is an enhanced class inheriting of sf::Sprite. Gfx as just a supplemental parametter to make it rotate over time.

It is splitted into other inheriting class :
- AnimatedGfx : animate a gfx over the time from a tilesheet. You can set it to loop or not, and to play the animation backward.

- AngledGfx : animate a gfx from an angle. The best exemple is to animate a turret. From the direction the turret is facing, the class display the right frame.

- ControlledGfx : animate a gfx choosing a specific frame. It is used for exemple for the waving effect of a shmup ship going left and right. You change the desired frame and it go smoothly to it from the precedent frame.

For the multiple tile class to work, you need to have your tilesheet to be parsed in this way : From left to right, then from top to bottom AND the tiles must have the same width and height. If you don't do so, you will have some weird animation.
For angled animation, your first tile must face down (270°), or else you will have an offset. If you look into the AngledGfx.cpp, at this formulae :

current_frame = (static_cast<int>(frame_list.size()*((static_cast<unsigned int>(gfx_angle)%359 + (360.f/(frame_list.size()*2)))/360.f))+4)%16;

The 4 in bold is used to offset the frame orientation. If you want your tilesheets to start from 90° or 0°, juste change this value, but it will apply to all your tilesheets so be carefull.

Exemple :

http://img246.imageshack.us/img246/425/explosion1.png

It is also enhanced with class call GfxEffect, used to control over the time the basic attributes of a sprite, like alpha, size and in some way the position.

Is is based on raw pointers (I hear some guy growl from here!), so you use it with new and delete.

Here is an exemple to create it:
Code: [Select]

// We load an image
sf::Image image;
image.LoadFromFile("pwet.png");

// We create a simple animated gfx
// first param : the image we use for the graphics
// 2nd param : the number of horizontal tiles
// 3rd param : the total number of tiles
// If you specify the right amount of tiles in both previous parameter, your image can have blank tiles at the end, it will be ignored.
// 4th param : the delay between each frame. 0.033f is the good delay for a 30 fps animation
// 5th param : we want the animation to loop or not.

Gfx *gfx = new AnimatedGfx(pwet, 5, 14, 0.033f, false);

// We add a fading effect, to play on the alpha of the image.
gfx->AddEffect(new FadeEffect());

while(app.IsOpened())
{
// If we press E key, the gfx start fading from it's current alpha to 0 during 1 second
if(app.GetInput().IsKeyDown(sf::Key::E))
gfx->GetEffect(0)->Start(gfx, 0, 1.f);
// If we press T key, the gfx start fading from it's current alpha to 255 during 2,5 seconds
if(app.GetInput().IsKeyDown(sf::Key::T))
gfx->GetEffect(0)->Start(gfx, 255, 2.5f);

// We refresh the gfx to make it calculate the right frame and apply effect;
gfx->Refresh();
// And we draw it
app.Draw(*gfx);
}


OF COURSE, if somes want to enhance those classes (thinking about boost pointer), you are gladly welcome to do it. =p

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Animating Sprites
« Reply #18 on: September 13, 2009, 09:15:32 pm »
Quote from: "Nexus"
You have to use "" and not <> at #include statements if you want the preprocessor to search in your project's directory, too. Did you do that?

Otherwise, double-check your path, this must work. If nothing helps, create a new empty console project and try again.


I'm trying out a sample so the include statements were already like that. I tried a new project and the only thing that happens is that it thinks animated.hpp doesn't exist again. I added the directories through the VC++ directories option but no dice.

K-Bal

  • Full Member
  • ***
  • Posts: 104
    • View Profile
    • pencilcase.bandcamp.com
    • Email
Animating Sprites
« Reply #19 on: September 13, 2009, 09:27:43 pm »
Spidyy, looks interesting ;) Is there any license you are using for this?
Listen to my band: pencilcase.bandcamp.com

Spidyy

  • Sr. Member
  • ****
  • Posts: 493
    • View Profile
Animating Sprites
« Reply #20 on: September 13, 2009, 09:29:27 pm »
I don't know what licenses really are. I use the SFML class to make inheriting class working as I want, that's all. If it can be usefull for you, feel free to use it. =p

I developed it for my own game you can download here : http://www.megaupload.com/?d=GWTWZMU1
(Still in development, controls : Up/Down/Right/Left to move, W to shoot, F11 for screenshot, Esc to quit)

It lake an inheriting class for gfx both animated AND controlled (for a RPG walking character for exemple), I made it for a shmup game after all. =p

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Animating Sprites
« Reply #21 on: September 13, 2009, 09:47:06 pm »
Quote from: "Spidyy"
I'll give you my own Animated sprites class : Gfx

http://www.megaupload.com/?d=0S8ZXFO3



hi, I tried it out but I think I'm making the same mistake. this time its complaining about a missing 'videosystem.h'

Spidyy

  • Sr. Member
  • ****
  • Posts: 493
    • View Profile
Animating Sprites
« Reply #22 on: September 13, 2009, 09:54:30 pm »
Ooooooops, my bad. :s I forget

I created a singleton named VideoSystem to manage my RenderWindow more easily.

http://www.megaupload.com/?d=PPDBQ0E0

Just changed the Gfx::Refresh() to Gfx::Refresh(sf::RenderWindow &app). If you found another VideoSystem, replace them by app like that. :p

I updated the link in the previous post.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Animating Sprites
« Reply #23 on: September 13, 2009, 10:07:23 pm »
I don't know what you're doing wrong.. The only idea I've is about wrong paths in #include directives.
SFML / OS X developer

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Animating Sprites
« Reply #24 on: September 13, 2009, 10:28:14 pm »
Quote from: "Spidyy"
Ooooooops, my bad. :s I forget

I created a singleton named VideoSystem to manage my RenderWindow more easily.

http://www.megaupload.com/?d=D1P17JWO

Just changed the Gfx::Refresh() to Gfx::Refresh(sf::RenderWindow &app). If you found another VideoSystem, replace them by app like that. :p

I updated the link in the previous post.


hi, I'm still not clear how to fix the problem. Are the files ready to work out of the box or do I have to replace some source code? I haven't found any usable videosystem.h

sorry

Spidyy

  • Sr. Member
  • ****
  • Posts: 493
    • View Profile
Animating Sprites
« Reply #25 on: September 13, 2009, 10:35:48 pm »
videosystem.h is not supposed to exist, it is a sf::RenderWindow & instead. =p If it appear in the #include, juste delete it. If it appear in the source code, replace it with a sf::RenderWindow & passed in parametter. :\

It is taken from my own source code so it is not "ready to work out of the box", but the code to replace is really small.

EDIT :  Okay you got a #include "VideoSystem.h" in GfxEffect.h, juste delete this line. =p

EDIT 2 : I updated again the download links. :p

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Animating Sprites
« Reply #26 on: September 13, 2009, 11:51:22 pm »
Quote from: "Hiura"
I don't know what you're doing wrong.. The only idea I've is about wrong paths in #include directives.



Turns out main.cpp happened to be outside the directory. Anyway I fixed that but now I'm getting these errors

Code: [Select]
animated.obj : error LNK2001: unresolved external symbol __imp__glVertex2f@8
1>animated.obj : error LNK2001: unresolved external symbol __imp__glTexCoord2f@8
1>animated.obj : error LNK2001: unresolved external symbol __imp__glEnd@0
1>animated.obj : error LNK2001: unresolved external symbol __imp__glDisable@4
1>animated.obj : error LNK2001: unresolved external symbol __imp__glBegin@4
1>MSVCRT.lib(crtexew.obj) : error LNK2001: unresolved external symbol _WinMain@16
1>C:\Documents and Settings\Minion\My Documents\Visual Studio 2008\Projects\animation\Release\animation.exe : fatal error LNK1120: 6 unresolved externals
1>Build log was saved at "file://c:\Documents and Settings\Minion\My Documents\Visual Studio 2008\Projects\animation\animation\Release\BuildLog.htm"


I have sfml-system sfml-window and sfml-graphics lib files included...is there anything else I need?

Spidyy

  • Sr. Member
  • ****
  • Posts: 493
    • View Profile
Animating Sprites
« Reply #27 on: September 14, 2009, 12:03:39 am »
You need to include opengl32.lib (external lib needs to be included). =p

And if i'm not wrong, you need to link sfml-main.lib too.

Jarwulf

  • Newbie
  • *
  • Posts: 37
    • View Profile
Animating Sprites
« Reply #28 on: September 14, 2009, 02:02:39 am »
Alright I finally got Hiura's files working.

Sure takes a lot of code just to shuffle images quickly.

LGV

  • Newbie
  • *
  • Posts: 11
    • MSN Messenger - laureano_river@hotmail.com
    • View Profile
Animating Sprites
« Reply #29 on: September 16, 2009, 04:55:15 am »
Wow, my eyes are burning, I can't believe how ugly are your animation solutions!! Hahaha, just kidding  :lol:. My animation class manage tilesets like Spidyy one does, the tiles must be the same size aswell. But it's a little bit different, I can say to the class "Well, the frames 1,2,5 and 6 are the animation "Shooting"", and then I just say "Play the animation Shooting", and it's that simple. I use a hash_map to store this stuff.
Here is some sample code that actually works and shows an animation:
Code: [Select]

#include <SFML/Graphics.hpp>
#include "AnimatedSprite.hpp"

using namespace Engine;

int main(int argc, char* argv[])
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "AnimatedSprite Demo");

std::string file = "avion rojo 66x85 c3 f1.png"; // image file of the sprite
Tile tileSettings; // structure that hold the tile settings
tileSettings.Width = 67;
tileSettings.Height = 85;
tileSettings.Columns = 3;
tileSettings.Rows = 1;
float animationTime = 0.2f; // time between frame-to-frame in the animation

AnimatedSprite aircraft(&App, file, tileSettings, animationTime);
// Sets an animation with name "default", wich frames are 0,1 and 2 (frames start from zero)
aircraft.setAnimation("0,1,2", "default");
aircraft.SetPosition(400.f, 300.f);

sf::Event Event;
while(App.IsOpened())
{
aircraft.getFrameTime(); // to manage internal time stuff
while(App.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
App.Close();
}
if(App.GetInput().IsKeyDown(sf::Key::Escape))
App.Close();

aircraft.playAnimation("default"); // plays the animation "default"

App.Clear(sf::Color(192, 192, 192));
aircraft.draw(); // draw the sprite
App.Display();
}

return EXIT_SUCCESS;
}

Of course, I can give you the sources if you want it  8) (comments are in spanish, maybe that can be a trouble hehehe :P)

 

anything