Sie sind auf Seite 1von 3

Computer animation is the process used for generating animated images by usingcomputer

graphics. The more general termcomputer generated imagery encompasses both static scenes and
dynamic images, while computer animation only refers to moving images.Modern computer
animation usually uses3D computer graphics, although 2D computer graphics are still used for
stylistic, low bandwidth, and faster real-time renderings. Sometimes the target of the animation is
the computer itself, but sometimes the target is another medium, such as film.
Each character is implemented as an object of a class CGraphin that has the following members
(in addition to the standard constructor and destructor).
protected:
CWinThread * m_pThread;
static UINT ThreadFunc(LPVOID pParam);
GParam m_param;
public:
void SetGraphin(GParam *p);
void GetGraphin(GParam *p);
void StartWork(BOOL resume);
void StopWork(BOOL terminal);
void Draw(CDC *pDC, HICON IconList[]);
A graphin is essentially a thread with an associated function and a parameter structureGParam.
This structure contains not only input parameters but also placeholders of the state variables of
the object used by the calling thread. Bellow is a partial listing of the structure with the essential
members. Additional members affect the "lifespan" of the object, details of its motion, etc.
typedef struct _GParam {
int id;
// index of the object. If -1, the thread is not running
int x0, y0; // initial position
int dx, dy; // speed
int type;
// determines how it is drawn
int x, y, ix; // state of the object
// ...
} GParam;
This structure begins life in the calling program and it is copied in the graphin object during
initialization. The SetGraphin(GParam *p) function contains only the statement
m_param = *p;

and the GetGraphin(GParam *p) function contains only the statement


*p = m_param;

In earlier version (of May 15, 2006) I had used the


methods SuspendThread() andResumeThread() to control the animation. However, one has to
be very careful in using these functions and the MFC documentation recommends to have
threads terminate themselves. In this implementation I have added a variable stop_thread that
is a member of the parameter structure passed to the thread.
The functions for starting and stopping the thread using that variable are listed next:
void CGraphin::StartWork(BOOL resume)
{
if(m_param.id < 0) return;
if(resume==TRUE) {
m_param.x0 = m_param.x;
m_param.y0 = m_param.y;
}
m_param.stop_thread = FALSE;
m_pThread = AfxBeginThread(&CGraphin::ThreadFunc, &m_param);
ASSERT(m_pThread != NULL);
}
void CGraphin::StopWork(BOOL terminal)
{
if(m_param.id < < 0) return;
m_param.stop_thread = TRUE;
if(terminal == TRUE) m_param.id = -1;
}
The thread work function and the drawing method require a bit more discussion. Below is a
minimal listing of the first. I omit statements that check the boundary of the playing area or any
static objects.
UINT CGraphin::ThreadFunc(LPVOID pParam)
{
GParam *p = (GParam *)pParam;
p->x = p->x0;
p->y = p->y0;
for(int i=0; i<p->iter; i++) {
if(p->stop_thread == TRUE) break;
::Sleep(p->delay);
p->x += p->dx;
p->x += p->dy;
p->ix = i;
}
return 0;

}
The program works equally well when the thread function is taken out of the class because all
the class instance variables are passed as argument. It has been included in the class for
"bookeeping purposes".

Das könnte Ihnen auch gefallen