Sie sind auf Seite 1von 4

/***************************************************************************

Below is implementation for the sprite class used in our recent game. The
class is used for setting sprite textures as well as putting it through the
render pipeline. Objects in the game like the players, nodes, and background
would be sprites. For each sprite we wanted to be able to figure out if the
sprite was visible to player, and get and set the sprite properites. Some
properties include scale, rotation, and position of the sprite. We put the
sprites into an enum list so it had easy access when we wanted to call the
sprites ID. Since the game was component based architecture, the sprites are
a component.
****************************************************************************/
/****************************************************************************
Default Constructor. Sets the sprites initial rotation, visiblity,
****************************************************************************/
Sprite::Sprite() : m_rotation(0.0f), m_isVisible(true), m_blendAmount(1.0f)
{
// Sets the component type.
componentType = typeSprite;
}
/***************************************************************************
Default Constructor. Sets the sprites initial rotation, visiblity, and
transparency.
****************************************************************************/
Sprite::Sprite(enum SpriteIndex type) : m_rotation(0.0f), m_isVisible(true),
m_blendAmount(1.0f)
{
// Set component type.
componentType = typeSprite;
// Set Sprite type.
m_index = type;
}
/***************************************************************************
Removes this sprite from the graphics list.
****************************************************************************/
Sprite::~Sprite()
{
GRAPHICS->RemoveSprite(this);
}
/***************************************************************************
Calls the sprite initialize function.
****************************************************************************/
void Sprite::Start()
{
InitializeSprite();

}
/***************************************************************************
Initializes each sprite. Sets their scale and layer values.
****************************************************************************/
void Sprite::InitializeSprite()
{
SetScale(myGameObject->scale);
//give the object the specified z-order
SetLayer(myGameObject->zOrder);
//add the sprite to the graphics system
GRAPHICS->AddSprite(this);
}
/***************************************************************************
Calculates the model to world matrix for the sprite and returns that
matrix.
****************************************************************************/
D3DXMATRIX Sprite::GetWorldMatrix()
{
//we need to pass the sprite through the render pipeline. To do so we must
follow the order of Translate*Rotate*Scale
//The resulting matrix will be the transformation matrix that the sprite
will use
//First we create the 3 matrices we will need
D3DXMATRIX translation;
D3DXMATRIX rotationZ;
D3DXMATRIX scale;
// Creates the translation matrix.
D3DXMatrixTranslation(&translation, myGameObject->position.x, myGameObject>position.y,
0.0f);
// Creates the rotation matrix.
D3DXMatrixRotationZ(&rotationZ, myGameObject->rotation_);
// Creates the scale matrix.
D3DXMatrixScaling(&scale, myGameObject->scale.x, myGameObject->scale.y,
1.0f);
// Multiplies the matix together to create the world matrix.
return scale * rotationZ * translation;
}

/***************************************************************************
Sets the position of the sprite.
****************************************************************************/
void Sprite::SetPosition(float newX, float newY)
{
//set the x and y position of the sprite
myGameObject->position.x = newX;
myGameObject->position.y = newY;
}
/***************************************************************************
Sets the scale of the sprite.
****************************************************************************/
void Sprite::SetScale(Point2D scale)
{
m_scale = scale;
}
/***************************************************************************
Sets the rotation of the sprite.
****************************************************************************/
void Sprite::SetRotation(float rotation)
{
m_rotation = rotation;
}
/***************************************************************************
Returns the index to the texture the sprite will use
****************************************************************************/
int Sprite::GetTexture() const
{
return m_index;
}
/***************************************************************************
Returns the layer the sprite is on (Z-Order)
****************************************************************************/
int Sprite::GetLayer() const
{
return m_layer;
}
/***************************************************************************
Sets the texture of the sprite.
****************************************************************************/
void Sprite::SetTexture(int tex_index)
{
m_index = tex_index;
}

/***************************************************************************
Sets the layer of the sprite.
****************************************************************************/
void Sprite::SetLayer(float layer)
{
m_layer = (int)layer;
}
/***************************************************************************
Returns wheither the sprite is visible or not.
****************************************************************************/
bool Sprite::GetVisible() const
{
return myGameObject->isVisable_;
}
/***************************************************************************
Sets the component index.
****************************************************************************/
void Sprite::DeSerialize(std::ifstream & component)
{
// grab the index and the blend amount from the specified file and apply it
to the sprite
component >> m_index;
component >> m_blendAmount;
}
/***************************************************************************
Sets the transparency value. This is how visible the object will be.
****************************************************************************/
void Sprite::SetTransparency(float blendAmount)
{
m_blendAmount = blendAmount;
}
/***************************************************************************
Gets the transparency value.
****************************************************************************/
float Sprite::GetTransparency() const
{
return m_blendAmount;
}

Das könnte Ihnen auch gefallen