text: #include <iostream>
  #include <cmath>
  #include "SDL/SDL_rotozoom.h"
  #include "scaledSprite.h"
  #include "gamedata.h"
  #include<stdlib.h>
  #include<time.h>
  
  ScaledSprite::ScaledSprite(const std::string& name, 
                             SDL_Surface* surface) :
    Drawable(name,
             Vector2f(0, rand()%380 ), 
             Vector2f( (rand()%2?1:-1)*getRandom(
                      Gamedata::getInstance().getXmlInt(name+"/cushion"),
                      Gamedata::getInstance().getXmlInt(name+"/speed/x")),
                      (rand()%2?1:-1)*getRandom(
                      Gamedata::getInstance().getXmlInt(name+"/cushion"),
                      Gamedata::getInstance().getXmlInt(name+"/speed/y"))
                     )
    ), 
    scale(getRandFloat(Gamedata::getInstance().getXmlFloat(name+"/scale/min"),
                       Gamedata::getInstance().getXmlFloat(name+"/scale/max"))
    ),
    scaledSurface( rotozoomSurface(surface, 0, scale, SMOOTHING_ON) ),
    frame( new Frame(scaledSurface, scaledSurface->w, scaledSurface->h,
                     Gamedata::getInstance().getXmlInt(name+"/src/x"), 
                     Gamedata::getInstance().getXmlInt(name+"/src/y"))
    ),
    frameWidth(frame->getWidth()),
    frameHeight(frame->getHeight()),
    worldWidth(Gamedata::getInstance().getXmlInt("world/width")),
    worldHeight(Gamedata::getInstance().getXmlInt("world/height"))
  { }
  
  ScaledSprite::ScaledSprite(const ScaledSprite& s) :
    Drawable(s.getName(), s.getPosition(), s.getVelocity()), 
    scale(s.scale),
    scaledSurface(s.scaledSurface),
    frame(s.frame),
    frameWidth(s.frameWidth),
    frameHeight(s.frameHeight),
    worldWidth(s.worldWidth),
    worldHeight(s.worldHeight)
  {  }
  
  ScaledSprite& ScaledSprite::operator=(const ScaledSprite& rhs) {
    setName( rhs.getName() );
    setPosition(rhs.getPosition());
    setVelocity(rhs.getVelocity());
    scale = rhs.scale;
    scaledSurface = rhs.scaledSurface;
    frame = rhs.frame;
    frameWidth = rhs.frameWidth;
    frameHeight = rhs.frameHeight;
    worldWidth = rhs.worldWidth;
    worldHeight = rhs.worldHeight;
    return *this;
  }
  
  ScaledSprite::~ScaledSprite() {
    SDL_FreeSurface( scaledSurface );
    delete frame;
  }
  
  void ScaledSprite::draw() const { 
    Uint32 x = static_cast<Uint32>(X());
    Uint32 y = static_cast<Uint32>(Y());
    frame->draw(x, y); 
  }
  
  unsigned ScaledSprite::getPixel(Uint32 i, Uint32 j) const { 
    Uint32 x = static_cast<Uint32>(X());
    Uint32 y = static_cast<Uint32>(Y());
    x = i - x;
    y = j - y;
    Uint32 *pixels = static_cast<Uint32 *>(frame->getSurface()->pixels);
    return pixels[ ( y * frame->getWidth() ) + x ];
  }
  
  
  void ScaledSprite::update(Uint32 ticks) { 
    //Vector2f incr = getVelocity() * static_cast<float>(ticks) * 0.001;
    //setPosition(getPosition() + incr);
    //float incrY = velocityY() * static_cast<float>(ticks) * 0.001;
  	float incrX = velocityX() * static_cast<float>(ticks) * (0.001/4);
  //	Y( Y() + incrY );
  	X(X() + incrX);  
  
    if ( Y() < 0) {
      velocityY( abs( velocityY() ) );
    }
    if ( Y() > worldHeight-frameHeight) {
  	Vector2f XPos(((rand()%1280)+1),0);
  	setPosition( XPos );
  	 velocityY( abs( velocityY() ) );
    }
  
    if ( X() < 0) {
      Vector2f XPos(0, ((rand()%380)+1));
  	setPosition( XPos );
  	 velocityY( abs( velocityY() ) );
    }
    if ( X() > worldWidth-frameWidth) {
   Vector2f XPos(0, ((rand()%380)+1));
  	setPosition( XPos );
  	 velocityY( abs( velocityY() ) );
    }  
  }
  