1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#include <ClanLib/core.h>
#include "spritedata.h"
#include "texture.h"
SpriteData::SpriteData(const char *section, CL_ResourceManager *resources)
{
CL_String res;
name = section;
totalFrames = 1;
speedAnimation = 100;
playPingPong = false;
playLoop = true;
initialAngle = 0;
try
{
res = section;
res += "/animationspeed";
speedAnimation = CL_Integer(res, resources);
} catch (CL_Error err) {};
try
{
res = section;
res += "/initialangle";
initialAngle = CL_Integer(res, resources);
} catch (CL_Error err) {};
try
{
res = section;
res += "/pingpong";
playPingPong = CL_Boolean(res, resources);
} catch (CL_Error err) {};
try
{
res = section;
res += "/loop";
playLoop = CL_Boolean(res, resources);
} catch (CL_Error err) {};
int currentSprite = 0;
try
{
for(;;) {
res = section;
res += "/frame";
res += currentSprite + 1;
Texture *surface = Texture::load(res, resources);
// int length = surface->get_num_frames();
// if(length > 1)
// TODO: Split surface into smaller surfaces
surfaces.push_back(surface);
currentSprite++;
}
}
catch (CL_Error err) { ; } // No more sprite resources found
totalFrames = surfaces.size();
if(totalFrames == 0)
{
std::string error = section;
error += " sprite contains no frames";
throw CL_Error(error);
}
}
SpriteData::~SpriteData()
{
for(int i=0; i<totalFrames; i++)
delete surfaces[i];
}
int SpriteData::getTotalFrames() const
{
return totalFrames;
}
int SpriteData::getInitialAngle() const
{
return initialAngle;
}
int SpriteData::getAnimationSpeed() const
{
return speedAnimation;
}
bool SpriteData::isPingPong() const
{
return playPingPong;
}
bool SpriteData::isLoop() const
{
return playLoop;
}
const std::string &SpriteData::getName() const
{
return name;
}
int SpriteData::getWidth(int frame) const
{
return surfaces[frame]->get_width();
}
int SpriteData::getHeight(int frame) const
{
return surfaces[frame]->get_height();
}
void SpriteData::draw(int x, int y, int frame, float rotation)
{
if(frame < 0 || frame > totalFrames)
{
std::string error = name;
error += " spriteframe was used outside range";
throw CL_Error(error);
}
surfaces[frame]->put_screen(x, y, rotation);
}
|