Animation class
class Animation
{
sf::Image img;
std::list<AnimationFrame> frames;
AnimationFrame currentFrame;
void CheckFrames();
public:
void LoadFromFile( std::string filename );
void SetPos( float x, float y );
sf::Sprite Output();
};
Function to load image
void Animation::LoadFromFile( std::string filename )
{
XMLDocument file;
file.LoadFile( filename.c_str() );
XMLElement* root = file.RootElement();
img.LoadFromFile( root->Attribute( "image" ) );
int w, h;
w = root->IntAttribute( "w" );
h = root->IntAttribute( "h" );
}
Function to store in list
void BuildMapFromFile( std::string filename )
{
XMLDocument file;
file.LoadFile( filename.c_str() );
XMLElement* root = file.RootElement();
//obj_animation
for( XMLElement* animation = root->FirstChildElement( "animation" ); animation; animation = animation->NextSiblingElement( "animation" ) )
{
//object
object new_object;
new_object.type = "animation";
int depth = animation->IntAttribute( "depth" );
new_object.depth = depth;
new_object._animation.LoadFromFile( animation->Attribute( "source" ) ); //loads image
new_object._animation.SetPos( animation->IntAttribute( "x" ), animation->IntAttribute( "y" ) );
std::list<object>::iterator i = q_object.begin();
bool varadded = false;
while( i != q_object.end() )
{
if( i->depth > depth )
{
q_object.insert( i, new_object );
varadded = true;
i = q_object.end();
}
else ++i;
}
if( varadded == false ) q_object.push_back( new_object );
}
}
The animation class loads its own image, which is kept inside the class (not the best idea, I know). The class itself is moved into the list.
EDIT: I just tried setting the sprites to a globally-declared image, and it failed as well.
Woops. I guess I should have read that first.
Anyway, here's a simple example I whipped up quickly. This should be a bit easier to read.
#include <SFML/Graphics.hpp>
#include <string>
#include <list>
struct ComplexImage
{
sf::Image img;
std::string file;
};
std::list<ComplexImage> buffer_image;
sf::Image GetImage( std::string filename )
{
std::list<ComplexImage>::iterator i = buffer_image.begin();
bool varfound = false;
while( i != buffer_image.end() )
{
if( i->file == filename )
{
varfound = true;
return i->img;
}
else ++i;
}
if( varfound == false )
{
//create new image
ComplexImage new_image;
new_image.img.LoadFromFile( filename.c_str() );
new_image.file = filename;
buffer_image.push_back( new_image );
return GetImage( filename );
}
}
int main( int argc, char* argv[] )
{
sf::RenderWindow game( sf::VideoMode( 800, 600, 32 ), "App Title" );
sf::Event event;
sf::Sprite test_sprite;
test_sprite.SetImage( GetImage( "ball.png" ) );
test_sprite.SetPosition( 40, 40 );
while( game.IsOpened() )
{
while( game.GetEvent( event ) )
{
//if window is closed or Escape key is pressed, close the game
if( event.Type == sf::Event::Closed or ( event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Key::Escape ) ) game.Close();
}
//clear display
game.Clear( sf::Color( 100, 149, 237 ) );
//draw sprite
game.Draw( test_sprite );
//update display
game.Display();
}
return EXIT_SUCCESS;
}
Why not use std::map?
std::map<string, ComplexImage> buffer_images;
if (buffer_images.find(name) != buffer_images.end())
{
return buffer_images[name];
}
//add into map
buffer_images[name] = ComplexImage; //etc
//etc