sobota, 9 października 2010

Animacja

Ostatnio tworzyłem klasy odpowiedzialne za animacje. Które są zawarte w jednym obrazie(tzw. atlas), każda klatka ma określony wymiar , oto przykład:

Jak widać ma 4 klatki(każda o wymiarach 64x64). Dobra ale jak narysować odpowiednią ??
Otóż potrzebna nam jest klasa, która będzie zawierać numer komórki(w poziomie i pionie):

class CellID
{
public:
uint X;
uint Y;
};


Teraz szablon komórki(zawiera obraz z którego pobieramy dane, wysokość i szerokość jednej komórki), dzięki jednej z jej metod możemy pobrać interesującą nas klatkę animacji(wystarczy że podamy numer w poziomie i pionie):

class CTemplateSet
{
protected:
             // wskaźnik na obraz
GraphicsCore::ImagePtr m_Image;
            //  szerokość komórki
uint m_CellWidth;
           // jej wyskość
uint m_CellHeight;
public:
CTemplateSet()
{
m_Image = 0;
m_CellWidth = 0;
m_CellHeight = 0;
}
~CTemplateSet()
{
m_Image = 0;
m_CellWidth = 0;
m_CellHeight = 0;
}

void Create(GraphicsCore::ImagePtr& Image, uint CellWidth, uint CellHeight)
{
m_Image = Image;
m_CellWidth = CellWidth;
m_CellHeight = CellHeight;
}
                // zwraca nam współrzędne wybranej klatki
void GetRect(CellID Cell, CRect* Rect)
{
Rect->m_Top = Cell.Y* m_CellHeight;
Rect->m_Left = Cell.X* m_CellWidth;

Rect->m_Right = Cell.X* m_CellWidth + m_CellWidth;
Rect->m_Bottom = Cell.Y* m_CellHeight + m_CellHeight;
}
uint GetCellWidth() {return m_CellWidth;}
uint GetCellHeight() {return m_CellHeight;}
GraphicsCore::ImagePtr& GetImage() {return m_Image;}
};




Teraz czas na klasę animacji, 

class  CAnimation
{
protected:
CTemplateSet m_TemplateSet; // szablon katki
uint m_Frames; // liczba klatek
uint m_CurrentFrame; // obecna klatka
CellID* m_FramesID; // tablica numerów(w poziomie i pionie) naszych klatek
public:
CAnimation();
~CAnimation();

void Create(CTemplateSet & TemplateSet, uint Frames, CellID* FramesID);
void Update(); // zmienia klatkę na następną
void Render(float x, float y);
void SetCurrentFrame(uint Frame);
uint GetCurrentFrame();
};

-------------------------------------------------------------------------------------------------------------
// anim.cpp

#include "CAnimation.h"

CAnimation::CAnimation()
{
m_Frames = 0;
m_CurrentFrame = 0;
m_FramesID = 0;
}

CAnimation::~CAnimation()
{
m_Frames = 0;
m_CurrentFrame = 0;
if ( m_FramesID != 0)
{
delete [] m_FramesID;
m_FramesID = 0;
}
}

void CAnimation::Create(CTemplateSet & TemplateSet, uint Frames, CellID* FramesID)
{
m_TemplateSet.Create(TemplateSet.GetImage(), TemplateSet.GetCellWidth(),TemplateSet.GetCellHeight());

m_FramesID = new CellID [Frames];

memcpy(m_FramesID,FramesID,sizeof(CellID)*Frames);
m_Frames = Frames;
m_CurrentFrame = 0;
}

void CAnimation::Update()
{
m_CurrentFrame++;
if (m_CurrentFrame>= m_Frames)
m_CurrentFrame = 0;
}

void CAnimation::Render(float X, float Y)
{
CRect Rect;
m_TemplateSet.GetRect(m_FramesID[m_CurrentFrame],&Rect);
m_TemplateSet.GetImage()->DrawRect(X,Y,&Rect);
}

void CAnimation::SetCurrentFrame(uint Frame)
{
m_CurrentFrame = Frame;
}

uint CAnimation::GetCurrentFrame()
{
return m_CurrentFrame;
}



Dzięki tym klasą można tworzyć i wyświetlać animacje.
Można rozszerzyć klasę animacji o interwał czasowy zmiany klatki(klatka zmieni się po upływie określonego czasu np. po 0.5 sekundy)

Brak komentarzy:

Prześlij komentarz