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

Author Topic: AniSprite Error  (Read 4385 times)

0 Members and 1 Guest are viewing this topic.

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
AniSprite Error
« on: December 11, 2011, 04:15:38 am »
My Main.cpp
Code: [Select]
#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
Code: [Select]
#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
Code: [Select]
#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|

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
AniSprite Error
« Reply #1 on: December 11, 2011, 04:17:36 am »
You have not included "AniSprite.h" on the main file.

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
AniSprite Error
« Reply #2 on: December 11, 2011, 04:24:25 am »
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!

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
AniSprite Error
« Reply #3 on: December 11, 2011, 08:01:06 am »
Change
Code: [Select]
sf::IntRect AniSprite::GetFramePosition(int frame);
to
Code: [Select]
sf::IntRect GetFramePosition(int frame);
on the header file.

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
AniSprite Error
« Reply #4 on: December 11, 2011, 08:20:58 am »
thank you sir, Ill try it as soon as I get home!

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
AniSprite Error
« Reply #5 on: December 11, 2011, 11:48:31 am »
Sir, another error..  :(
Maybe this is not yet fully implemented to sfml 2.0?

Code: [Select]
#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));
   }
}


Quote

||=== 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 ===|

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
AniSprite Error
« Reply #6 on: December 11, 2011, 11:52:27 am »
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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
AniSprite Error
« Reply #7 on: December 11, 2011, 02:03:58 pm »
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..

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
AniSprite Error
« Reply #8 on: December 11, 2011, 04:46:08 pm »
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!

dotty

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
AniSprite Error
« Reply #9 on: January 17, 2012, 08:28:18 pm »
Care to explain how you got it working?

dotty

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
AniSprite Error
« Reply #10 on: January 17, 2012, 09:18:39 pm »
Hmmm, ok I got it all working apart from the clock.

I get these issues

Code: [Select]
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?

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
AniSprite Error
« Reply #11 on: January 18, 2012, 06:46:25 pm »
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
Code: [Select]
#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
Code: [Select]

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?

dotty

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
AniSprite Error
« Reply #12 on: January 18, 2012, 07:50:19 pm »
Are you playing the animation? Do you have any code to show us?