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:
(http://imageshack.us/a/img62/481/linkl.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;
}
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.