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

Author Topic: Trying to do a very basic sprite animation (SFML and C++ noob)  (Read 10959 times)

0 Members and 1 Guest are viewing this topic.

klefmung

  • Newbie
  • *
  • Posts: 12
    • View Profile
Hey. I just picked up C++ a couple weeks ago, and now I'm trying to learn the basics of SFML. I can put a shape or sprite image on the screen and move it at this point, so I tried to do some animating using a basic Link sprite as practice. I saw that you can take a rectangle out of a sheet, so I thought I'd give try and cycle through them. I set variables for the reference coordinates of each rectangle, and for some reason, it only ones to give me the second one over on the x axis and the second one down on the y axis.  I can even move it around. It is just that one section though, and it doesn't change. I imagine the reasons for the x axis on the source image and the y axis not changing are completely different. Thanks for looking! Heres the image and the code:

Link.png:


#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;

int main()
{
    cout<<"Blah";
sf::Image sLink;
sf::Sprite Link;
sLink.LoadFromFile("Link.png");
Link.SetImage(sLink);

    sf::RenderWindow GameWindow(sf::VideoMode(800, 600, 32), "SFML Graphics");

    while (GameWindow.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (GameWindow.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                GameWindow.Close();
        }

    GameWindow.Clear();




const sf::Input& Input = GameWindow.GetInput();
bool         LeftKey    = Input.IsKeyDown(sf::Key::Left);
bool         RightKey     = Input.IsKeyDown(sf::Key::Right);
bool         UpKey    = Input.IsKeyDown(sf::Key::Up);
bool         DownKey    = Input.IsKeyDown(sf::Key::Down);

int Facing;
int AniFrame;
int Height=sLink.GetHeight();
int Width=sLink.GetWidth();
int FrameV;
int FrameH;
bool Moving;




//moving Link
if(RightKey)
{
    Link.Move(1,0);
    sf::Sleep(.025);
    Facing=3;
    Moving=true;
}
else if (LeftKey)
{
  Link.Move(-1,0);
  sf::Sleep(.025);
  Facing=1;
}
else if (DownKey)
{
    Link.Move(0,1);
    sf::Sleep(.025);
    Facing=4;
}
else if (UpKey)
{
    Link.Move(0,-1);
    sf::Sleep(.025);
    Facing=2;
}
else Moving=false;

//Setting vertical pixel count
if(Facing=3)
{
FrameV=2*(Height/4);
}
if(Facing=1)
{
    FrameV=0;
}
if(Facing=4)
{
    FrameV=3*(Height/4);
}
if(Facing=2)
{
    FrameV=Height/4;
}
int FrameVT=FrameV+(Height/4);




//Handling Animation
sf::Clock clock;
if(Moving=true)
{

    if(AniFrame<=4)
    {
        clock.Reset();
        if(clock.GetElapsedTime()==.1)
        {
            FrameH+=Width/4;
            clock.Reset();
        }
    }
    else FrameH=0;
}
else FrameH=0;
int FrameHT=FrameH+(Width/4);

if(FrameH=0)
{
    AniFrame=1;
}
else if(FrameH=Width/4)
{
    AniFrame=2;
}
else if(FrameH=2*(Width/4))
{
    AniFrame=3;
}
else if(FrameH=3*(Width/4))
{
    AniFrame=4;
}
else AniFrame=5;

Link.SetSubRect(sf::IntRect(FrameH,FrameV,FrameHT,FrameVT));


GameWindow.Draw(Link);


    GameWindow.Display();
    }




    return 0;
}
 
« Last Edit: April 02, 2013, 11:49:54 am by klefmung »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Trying to do a very basic sprite animation (SFML and C++ noob)
« Reply #1 on: April 02, 2013, 12:17:02 pm »
You should learn more about C++ basics. The equality operator is == and not =. Also, you're doing several weird things like sleeping only if key is pressed etc. Take a look at the SFML tutorials to see how the event-handling and drawing loops look like.

Furthermore, use SFML 2. Version 1.6 is terribly outdated.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

klefmung

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Trying to do a very basic sprite animation (SFML and C++ noob)
« Reply #2 on: April 02, 2013, 01:46:03 pm »
Oh man, I made some dumb mistakes involving the lack of equality operator. Sorry, I knew that, I just was thinking about other things. I know most of the basics (and am still trying to solidify them every day!) but they often just kind of fall out of my head. When I put in the sleeping, I had something else in mind. I went through and fixed some things. Now the vertical coordinates on the sheet work, but not the horizontal. That whole thing was a messy operation that I wasnt too terribly sure about. I looked over the clock section again. I still cant resolve an efficient way to handle that though. Any suggestions? Here is the updated product:
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;

int main()
{
    cout<<"Blah";
sf::Image sLink;
sf::Sprite Link;
sLink.LoadFromFile("Link.png");
Link.SetImage(sLink);

    sf::RenderWindow GameWindow(sf::VideoMode(800, 600, 32), "SFML Graphics");

    while (GameWindow.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (GameWindow.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                GameWindow.Close();
        }

    GameWindow.Clear();




const sf::Input& Input = GameWindow.GetInput();
bool         LeftKey    = Input.IsKeyDown(sf::Key::Left);
bool         RightKey     = Input.IsKeyDown(sf::Key::Right);
bool         UpKey    = Input.IsKeyDown(sf::Key::Up);
bool         DownKey    = Input.IsKeyDown(sf::Key::Down);

int Facing;
int AniFrame;
int Height=sLink.GetHeight();
int Width=sLink.GetWidth();
int FrameV;
int FrameH;
bool Moving;




//moving Link
if(RightKey)
{
    Link.Move(.025,0);
    Facing=3;
    Moving=true;
}
else if (LeftKey)
{
  Link.Move(-.025,0);
  Facing=1;
  Moving=true;
}
else if (DownKey)
{
    Link.Move(0,.025);
    Facing=4;
    Moving=true;
}
else if (UpKey)
{
    Link.Move(0,-.025);
    Facing=2;
    Moving=true;
}
else Moving=false;

//Setting vertical pixel count
if(Facing==3)
{
FrameV=2*(Height/4);
}
if(Facing==1)
{
    FrameV=0;
}
if(Facing==4)
{
    FrameV=3*(Height/4);
}
if(Facing==2)
{
    FrameV=Height/4;
}
int FrameVT=FrameV+(Height/4);




//Handling Animation
sf::Clock clock;
if(Moving=true)
{

    if(AniFrame<=4)
    {
        clock.Reset();
        if(clock.GetElapsedTime()==.1)
        {
            FrameH+=Width/4;
            clock.Reset();
        }
    }
    else FrameH=0;
}
else FrameH=0;
int FrameHT=FrameH+(Width/4);

if(FrameH==0)
{
    AniFrame=1;
}
else if(FrameH==Width/4)
{
    AniFrame=2;
}
else if(FrameH==2*(Width/4))
{
    AniFrame=3;
}
else if(FrameH==3*(Width/4))
{
    AniFrame=4;
}
else AniFrame=5;

Link.SetSubRect(sf::IntRect(FrameH,FrameV,FrameHT,FrameVT));


GameWindow.Draw(Link);


    GameWindow.Display();
    }




    return 0;
}
 
Also, as far as SFML 2 goes, I looked over the tutorials. Is there no graphics package for it? Or is there just no tutorial written for it yet? As in, does one have to do the graphics in OpenGL? Thanks.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Trying to do a very basic sprite animation (SFML and C++ noob)
« Reply #3 on: April 02, 2013, 02:08:27 pm »
One can't learn the basics of C++ in a week, but well one has to start somewhere. I can only suggest to get a good C++ book and work through it from front to back. ;)

Also, as far as SFML 2 goes, I looked over the tutorials. Is there no graphics package for it? Or is there just no tutorial written for it yet? As in, does one have to do the graphics in OpenGL?
The tutorials are written, but they are not available yet. Laurent wants to release them with the new website and with the official release of SFML 2. So no you don't need to draw anything with OpenGL directly.

I'm not sure if it's just a problem of coping the code to the forum, but the indenting is terrible. I can't see where what begins and ends. You should strictly follow one way to indent stuff and apply it always and every where, otherwise your code becomes unreadable very quickly.
You should also not be using namspace std. It seems like a shortcut in the beginning, but it makes everything just harder to follow. With namespaces one know directly where a class/function comes from.

For SFML 2 there's a nice animation class by Foaly on the wiki or you can even use Thor's animation class. They might at least give some insight on how to nicely do sprite animations.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/