SFML community forums
Help => Graphics => Topic started by: mnajrg on December 11, 2011, 04:15:38 am
-
My Main.cpp
#include "SFML/Graphics.hpp"
#include "SFML/System.hpp"
int main()
{
sf::RenderWindow App(sf::VideoMode(1024, 768,32), "Test");
// Load a sprite to display
sf::Image Image;
if (!Image.LoadFromFile("characters.bmp"))
return 1;
Image.CreateMaskFromColor(sf::Color::Black);
sf::Texture Texture;
if (!Texture.LoadFromImage(Image))
return 1;
AniSprite Sprite(Texture,47,64);
Sprite.SetLoopSpeed(60); //60 fps
Sprite.Play();
while(App.IsOpened())
{
// Clear screen
App.Clear();
//update sprite antimation
Sprite.Update();
App.Draw(Sprite);
App.Display();
}
}
AniSprite.h
#pragma once
#include <SFML/Graphics.hpp>
class AniSprite : public sf::Sprite
{
public:
AniSprite();
//initialtion list
AniSprite(const sf::Image& Img, int frameWidth, int frameHeight);
~AniSprite(void);
//Get
sf::IntRect AniSprite::GetFramePosition(int frame);
int GetFrameCount();
//set
void SetFrameSize(int frameWidth, int frameHeight);
//Sets current frame
void SetFrame(int frame);
//sets loop speed in fps
void SetLoopSpeed(float fps);
//start looping
void Play();
void Play(int start, int end);
//stop
void Stop();
//draw functions
void Update();
private:
sf::Clock clock;
float fps;
bool isPlaying;
int loopStart;
int loopEnd;
int currentFrame;
int frameWidth;
int frameHeight;
};
AniSprite.cpp
#include "AniSprite.h"
#include "SFML/Graphics.hpp"
#include "SFML/System.hpp"
AniSprite::AniSprite(void)
: sf::Sprite()
{
this->fps=1;
this->currentFrame=0;
this->isPlaying = false;
this->loopStart = 0;
this->SetFrameSize(0, 0);
}
AniSprite::AniSprite(const sf::Image& Img, int frameW, int frameH)
: sf::Sprite(Img)
{
this->fps=1;
this->currentFrame=0;
this->isPlaying = false;
this->loopStart = 0;
this->SetFrameSize(frameW, frameH);
//now calculate stuff
}
AniSprite::~AniSprite(void)
{
}
int AniSprite::GetFrameCount()
{
unsigned int across =
this->GetImage()->GetWidth() /
this->frameWidth;
unsigned int down =
this->GetImage()->GetHeight() /
this->frameHeight;
return across*down;
}
//first frame is frame ZERO
sf::IntRect AniSprite::GetFramePosition(int frame)
{
//get number across and down
unsigned int across =
(this->GetImage()->GetWidth() /
this->frameWidth);
unsigned int down =
(this->GetImage()->GetHeight() /
this->frameHeight);
int tileY = frame / across ; //get Y on grid
int tileX = frame % across ; //get X on grid
sf::IntRect result(
tileX*this->frameWidth,
tileY*this->frameHeight,
this->frameWidth,
this->frameHeight);
// for SFML 2.0 comment out above lines and use this instead
/* sf::IntRect result(
tileX*this->frameWidth,
tileY*this->frameHeight,
this->frameWidth,
this->frameHeight);
*/
//end
return result;
}
//
void
AniSprite::SetFrameSize(int frameW, int frameH)
{
this->frameWidth = frameW;
this->frameHeight = frameH;
this->SetSubRect(sf::IntRect(0,0,frameW,frameH));
}
//Sets current frame
void
AniSprite::SetFrame(int frame)
{
this->currentFrame = frame;
}
//sets loop speed in fps
void
AniSprite::SetLoopSpeed(float newfps)
{
this->fps = newfps;
}
//start looping
void
AniSprite::Play()
{
this->Play(0,GetFrameCount());
}
void
AniSprite::Play(int start, int end)
{
loopStart = start;
loopEnd = end;
currentFrame = start;
isPlaying = true;
clock.Reset();
}
//stop
void
AniSprite::Stop()
{
isPlaying = false;
}
//update function
void
AniSprite::Update()
{
if(isPlaying)
{
int frameCount = (loopEnd+1) - loopStart;
// for sfml 2.0 replace the above line with...
// int frameCount = (loopEnd+1) - loopStart;
float timePosition = (clock.GetElapsedTime() * fps);
currentFrame = loopStart +
(int)timePosition % frameCount;
//printf("%f:%i\n",clock.GetElapsedTime(),currentFrame);
this->SetSubRect(GetFramePosition(currentFrame));
}
}
I got this error:
In function 'int main()'
error: 'AniSprite' was not declared in this scope|
error: expected ';' before 'Sprite'|
error: 'Sprite' was not declared in this scope|
-
You have not included "AniSprite.h" on the main file.
-
it works! thankyou..
but another error occurs
in AniSprite.h
error: extra qualification 'AniSprite::' on member 'GetFramePosition'|
what could be the problem?
im sorry im total noob in sfml
By the way, is it okay in sfml 2 to directly load Image in
AniSprite Sprite(image,47,64);
HELP PLEASE!
-
Change
sf::IntRect AniSprite::GetFramePosition(int frame);
to
sf::IntRect GetFramePosition(int frame);
on the header file.
-
thank you sir, Ill try it as soon as I get home!
-
Sir, another error.. :(
Maybe this is not yet fully implemented to sfml 2.0?
#include "AniSprite.h"
AniSprite::AniSprite(void)
: sf::Sprite()
{
this->fps=1;
this->currentFrame=0;
this->isPlaying = false;
this->loopStart = 0;
this->SetFrameSize(0, 0);
}
AniSprite::AniSprite(const sf::Image& Img, int frameW, int frameH)
: sf::Sprite(Img)
{
this->fps=1;
this->currentFrame=0;
this->isPlaying = false;
this->loopStart = 0;
this->SetFrameSize(frameW, frameH);
//now calculate stuff
}
AniSprite::~AniSprite(void)
{
}
int AniSprite::GetFrameCount()
{
unsigned int across =
this->GetImage()->GetWidth() /
this->frameWidth;
unsigned int down =
this->GetImage()->GetHeight() /
this->frameHeight;
return across*down;
}
//first frame is frame ZERO
sf::IntRect AniSprite::GetFramePosition(int frame)
{
//get number across and down
unsigned int across =
(this->GetImage()->GetWidth() /
this->frameWidth);
unsigned int down =
(this->GetImage()->GetHeight() /
this->frameHeight);
int tileY = frame / across ; //get Y on grid
int tileX = frame % across ; //get X on grid
sf::IntRect result(
tileX*this->frameWidth,
tileY*this->frameHeight,
this->frameWidth,
this->frameHeight);
// for SFML 2.0 comment out above lines and use this instead
/* sf::IntRect result(
tileX*this->frameWidth,
tileY*this->frameHeight,
this->frameWidth,
this->frameHeight);
*/
//end
return result;
}
//
void
AniSprite::SetFrameSize(int frameW, int frameH)
{
this->frameWidth = frameW;
this->frameHeight = frameH;
this->SetSubRect(sf::IntRect(0,0,frameW,frameH));
}
//Sets current frame
void
AniSprite::SetFrame(int frame)
{
this->currentFrame = frame;
}
//sets loop speed in fps
void
AniSprite::SetLoopSpeed(float newfps)
{
this->fps = newfps;
}
//start looping
void
AniSprite::Play()
{
this->Play(0,GetFrameCount());
}
void
AniSprite::Play(int start, int end)
{
loopStart = start;
loopEnd = end;
currentFrame = start;
isPlaying = true;
clock.Reset();
}
//stop
void
AniSprite::Stop()
{
isPlaying = false;
}
//update function
void
AniSprite::Update()
{
if(isPlaying)
{
int frameCount = (loopEnd+1) - loopStart;
// for sfml 2.0 replace the above line with...
// int frameCount = (loopEnd+1) - loopStart;
float timePosition = (clock.GetElapsedTime() * fps);
currentFrame = loopStart +
(int)timePosition % frameCount;
//printf("%f:%i\n",clock.GetElapsedTime(),currentFrame);
this->SetSubRect(GetFramePosition(currentFrame));
}
}
||=== Animation, Debug ===|
C:\Documents and Settings\JONG\My Documents\CodeBlocks\Animation\AniSprite.cpp||In constructor 'AniSprite::AniSprite(const sf::Image&, int, int)'
C:\Documents and Settings\JONG\My Documents\CodeBlocks\Animation\AniSprite.cpp|15|error: no matching function for call to 'sf::Sprite::Sprite(const sf::Image&)'|
C:\sfml\sfml2\include\SFML\Graphics\Sprite.hpp|62|note: candidates are: sf::Sprite::Sprite(const sf::Texture&)|
C:\sfml\sfml2\include\SFML\Graphics\Sprite.hpp|54|note: sf::Sprite::Sprite()|
C:\sfml\sfml2\include\SFML\Graphics\Sprite.hpp|45|note: sf::Sprite::Sprite(const sf::Sprite&)|
C:\Documents and Settings\JONG\My Documents\CodeBlocks\Animation\AniSprite.cpp||In member function 'int AniSprite::GetFrameCount()'
C:\Documents and Settings\JONG\My Documents\CodeBlocks\Animation\AniSprite.cpp|31|error: 'class AniSprite' has no member named 'GetImage'|
C:\Documents and Settings\JONG\My Documents\CodeBlocks\Animation\AniSprite.cpp|34|error: 'class AniSprite' has no member named 'GetImage'|
C:\Documents and Settings\JONG\My Documents\CodeBlocks\Animation\AniSprite.cpp||In member function 'sf::IntRect AniSprite::GetFramePosition(int)'
C:\Documents and Settings\JONG\My Documents\CodeBlocks\Animation\AniSprite.cpp|44|error: 'class AniSprite' has no member named 'GetImage'|
C:\Documents and Settings\JONG\My Documents\CodeBlocks\Animation\AniSprite.cpp|47|error: 'class AniSprite' has no member named 'GetImage'|
C:\Documents and Settings\JONG\My Documents\CodeBlocks\Animation\AniSprite.cpp|46|warning: unused variable 'down'|
||=== Build finished: 5 errors, 1 warnings ===|
-
These are beginner problems. You should really learn C++ before you start with a library like SFML.
And apart from that, you can actually read the error messages. One says that you can't pass sf::Image to the sf::Sprite constructor and suggests to use sf::Texture instead, and one tells you that AniSprite has no GetImage member.
-
Yeah I know sir, Thats why im asking if this is fully implemented in sfml 2.
Because I know that sf::Image will pass to sf::Texture first before passing to sf::Sprite..
-
Thankyou for your reply sir, It somehow push me to analyze the code.
And I made it work..
Im sorry because Im new to SFML and libraries. I Studied C++ 2weeks ago
so im new to the environment..
By the way..Again Thankyou!
-
Care to explain how you got it working?
-
Hmmm, ok I got it all working apart from the clock.
I get these issues
Undefined symbols:
"sf::Clock::GetElapsedTime() const", referenced from:
AniSprite::Update() in AniSprite.o
"sf::Clock::Clock()", referenced from:
AniSprite::AniSprite(sf::Texture const&, int, int)in AniSprite.o
AniSprite::AniSprite(sf::Texture const&, int, int)in AniSprite.o
AniSprite::AniSprite()in AniSprite.o
AniSprite::AniSprite()in AniSprite.o
"sf::Clock::Reset()", referenced from:
AniSprite::Play(int, int)in AniSprite.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
But the clock is setup in the head file? Any ideas?
-
I made some change to convert it to my SFML 2.0, it compiles and when i start it, it shows me only top-left character.
http://www.sfml-dev.org/wiki/_media/en/sources/character.png
AniSprite.h
#include <SFML/Graphics.hpp>
class AniSprite : public sf::Sprite
{
public:
AniSprite();
//initialtion list
AniSprite(const sf::Texture &, int, int);
~AniSprite(){};
//Get
sf::IntRect GetFramePosition(int);
int GetFrameCount();
//set
void SetFrameSize(int, int);
//Sets current frame
void SetFrame(int);
//sets loop speed in fps
void SetLoopSpeed(float);
//start looping
void Play();
void Play(int, int);
//stop
void Stop();
//draw functions
void Update();
private:
sf::Clock clock;
float fps;
bool isPlaying;
int loopStart;
int loopEnd;
int currentFrame;
int frameWidth;
int frameHeight;
};
AniSprite.cpp
AniSprite::AniSprite(): sf::Sprite()
{
fps=1;
currentFrame=0;
isPlaying = false;
loopStart = 0;
SetFrameSize(0, 0);
}
AniSprite::AniSprite(const sf::Texture & Text, int frameW, int frameH): sf::Sprite(Text)
{
fps=1;
currentFrame=0;
isPlaying = false;
loopStart = 0;
SetFrameSize(frameW, frameH);
//now calculate stuff
}
int AniSprite::GetFrameCount()
{
unsigned int across = GetGlobalBounds().Width / frameWidth;
unsigned int down = GetGlobalBounds().Height / frameHeight;
return across*down;
}
//first frame is frame ZERO
sf::IntRect AniSprite::GetFramePosition(int frame)
{
//get number across and down
unsigned int across = GetGlobalBounds().Width / frameWidth;
unsigned int down = GetGlobalBounds().Height / frameHeight;
int tileY = frame / across ; //get Y on grid
int tileX = frame % across ; //get X on grid
sf::IntRect result(tileX*frameWidth, tileY*frameHeight, frameWidth, frameHeight);
//end
return result;
}
void AniSprite::SetFrameSize(int frameW, int frameH)
{
frameWidth = frameW;
frameHeight = frameH;
SetTextureRect(sf::IntRect(0,0,frameW,frameH));
}
//Sets current frame
void AniSprite::SetFrame(int frame)
{
currentFrame = frame;
}
//sets loop speed in fps
void AniSprite::SetLoopSpeed(float newfps)
{
fps = newfps;
}
//start looping
void AniSprite::Play()
{
Play(0,GetFrameCount());
}
void AniSprite::Play(int start, int end)
{
loopStart = start;
loopEnd = end;
currentFrame = start;
isPlaying = true;
clock.Reset();
}
//stop
void AniSprite::Stop()
{
isPlaying = false;
}
//update function
void AniSprite::Update()
{
if(isPlaying)
{
int frameCount = (loopEnd+1) - loopStart;
float timePosition = (clock.GetElapsedTime() * fps);
currentFrame = loopStart + (int)timePosition % frameCount;
SetTextureRect(GetFramePosition(currentFrame));
}
}
What is wrong?
-
Are you playing the animation? Do you have any code to show us?