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

Author Topic: trying to do fire animation  (Read 2735 times)

0 Members and 1 Guest are viewing this topic.

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
trying to do fire animation
« on: June 19, 2011, 11:32:47 pm »
hello everyone
so i have 7 images of fire and i was trying to make an animation with them but from the looks of it i failed.

i looked at http://www.sfml-dev.org/wiki/en/sources/frame_anim_animated but i didn't really understand a lot from it but i did my best.

here is what i did

Code: [Select]
int main()
{
Event Event;
Clock Clock;


//Create SFML windows
RenderWindow Game(VideoMode(800,600,32), "Game");

Image Img1;
Sprite Sprt1;
Image Img2;
Sprite Sprt2;
Image Img3;
Sprite Sprt3;
Image Img4;
Sprite Sprt4;
Image Img5;
Sprite Sprt5;
Image Img6;
Sprite Sprt6;
Image Img7;
Sprite Sprt7;

Img1.LoadFromFile("1.png");
Sprt1.SetImage(Img1);
Sprt1.Move(100,100);
Img2.LoadFromFile("2.png");
Sprt2.SetImage(Img2);
Sprt2.Move(100,100);
Img3.LoadFromFile("3.png");
Sprt3.SetImage(Img3);
Sprt3.Move(100,100);
Img4.LoadFromFile("4.png");
Sprt4.SetImage(Img4);
Sprt4.Move(100,100);
Img5.LoadFromFile("5.png");
Sprt5.SetImage(Img5);
Sprt5.Move(100,100);
Img6.LoadFromFile("6.png");
Sprt6.SetImage(Img6);
Sprt6.Move(100,100);
Img7.LoadFromFile("7.png");
Sprt7.SetImage(Img7);
Sprt7.Move(100,100);

Game.UseVerticalSync(true);

//Game loop
while(Game.IsOpened())
{

float Framerate = 1.f / Clock.GetElapsedTime();
Clock.Reset();

//Event handle loop
while(Game.GetEvent(Event))
{

if(Event.Type == Event::Closed)
Game.Close();

if (Game.GetInput().IsKeyDown(Key::Escape))
Game.Close();

if (Game.GetInput().IsKeyDown(Key::Num1))
StartPlaying();

if (Game.GetInput().IsKeyDown(Key::Num2))
Settings();
}

String Text("Press 1 to start playing the game\n"
"Press 2 to open Settings\n"
"Press ESC to Exit the game\n");

Text.SetColor(Color(255, 0, 0));
Text.Move(100.f, 100.f);

//Clear the game window
Game.Clear();

//Display Text
Game.Draw(Text);
//
for(int x=1; x<=7; x++)
{
Game.Draw(Sprt1);
Game.Draw(Sprt2);
Game.Draw(Sprt3);
Game.Draw(Sprt4);
Game.Draw(Sprt5);
Game.Draw(Sprt6);
Game.Draw(Sprt7);
}
//Display SFML graphics to window
Game.Display();

}
return 0;

}


the end result is all i see is Sprt7 and it doesnt loop again to draw Sprt1 i dont know why is that

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: trying to do fire animation
« Reply #1 on: June 19, 2011, 11:49:26 pm »
Quote from: "Fantasy"
i looked at http://www.sfml-dev.org/wiki/en/sources/frame_anim_animated but i didn't really understand a lot from it but i did my best.
Did you try the sample ? Just ask if something in particular is unclear.

Quote from: "Fantasy"
the end result is all i see is Sprt7 and it doesnt loop again to draw Sprt1 i dont know why is that
In fact all sprites are drawn but the second one goes over the first, the third overs the second, ... the seventh over the sixth, in that order. Why ? Because there is no Display between the Draw.
SFML / OS X developer

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
Re: trying to do fire animation
« Reply #2 on: June 20, 2011, 12:03:32 am »
Quote from: "Hiura"
Quote from: "Fantasy"
i looked at http://www.sfml-dev.org/wiki/en/sources/frame_anim_animated but i didn't really understand a lot from it but i did my best.
Did you try the sample ? Just ask if something in particular is unclear.

Quote from: "Fantasy"
the end result is all i see is Sprt7 and it doesnt loop again to draw Sprt1 i dont know why is that
In fact all sprites are drawn but the second one goes over the first, the third overs the second, ... the seventh over the sixth, in that order. Why ? Because there is no Display between the Draw.


well there are some things i dont understand and its not because its complicated its because i still didn't study what pointers are and how to work with them, but other than that i think its a good tutorial.

but i dont understand one thing is that how can you display only one part of a sprite but not the whole sprite ? for example if i want to only show the first 32px ->X and 32px->Y, i know how in allegro but not in sfml.
 
WOW thank you so much now its working, i did as you said
Code: [Select]
for(int x=1; x<=7; x++)
{
Game.Draw(Sprt1);
Game.Display();
Game.Draw(Sprt2);
Game.Display();
Game.Draw(Sprt3);
Game.Display();
Game.Draw(Sprt4);
Game.Display();
Game.Draw(Sprt5);
Game.Display();
Game.Draw(Sprt6);
Game.Display();
Game.Draw(Sprt7);
}

now its working but its animating very fast, even though i fixed the FPS to 60FPS its still animating fast like crazy.

ok so i guess i'm going to get 60 fire sprites instead of 7 to make it slower and more realistic, and i think i'm going to use array to load the 60 sprites, its going to be a looooong long page of code for 60 sprites.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: trying to do fire animation
« Reply #3 on: June 20, 2011, 12:23:40 am »
Quote from: "Fantasy"
well there are some things i dont understand and its not because its complicated its because i still didn't study what pointers are and how to work with them, but other than that i think its a good tutorial.
Good news is that there is no pointer involved here.

But if I may, you should first learn C++ with basic feature like console application and then come back to graphics library. It's much more easier to learn C++ that way. :wink:

Anyway :

Quote from: "Fantasy"
but i dont understand one thing is that how can you display only one part of a sprite but not the whole sprite ? for example if i want to only show the first 32px ->X and 32px->Y, i know how in allegro but not in sfml.
You can use SetSubRect
 
Quote from: "Fantasy"
WOW thank you so much now its working, i did as you said
(I didn't say you should put more Display :-° )
The problem is that there is no pause between two display so it goes as fast as it can go. In fact, your for loop is not a good idea. You should at least have something like that :
Code: [Select]
:
:
main
:
:
int frame = 0; // current frame index
while (window is open)
:
:
:
// Draw logics here :
switch (frame%7) { // frame%7 goes from 0 to 6.
  case 0:
    Game.Draw(Sprt1);
    break;
  case 1:
    Game.Draw(Sprt2);
    break;
:
:
  case 6:
    Game.Draw(Sprt7);
    break;
} // end of switch

Game.Display();

// Go to next frame :
++frame;
} // end of main loop
:
:

But this still isn't the best code ever. You can for example replace all SprtX variables by an array so you can remove the switch and do 'Game.Draw(myArrayOfSprite[frame%7]);' but that's another story.
SFML / OS X developer

 

anything