computer animation

Upload: sujathalavi

Post on 04-Mar-2016

215 views

Category:

Documents


0 download

DESCRIPTION

Computer Animation

TRANSCRIPT

Computer animationis the process used for generatinganimated imagesby usingcomputer graphics. The more general termcomputer generated imageryencompasses both static scenes and dynamic images, whilecomputer animationonly refers to moving images.Modern computer animation usually uses3Dcomputer graphics, although2Dcomputer graphicsare 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 anothermedium, such asfilm.Each character is implemented as an object of a classCGraphinthat 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[]);Agraphinis 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 thegraphinobject during initialization. TheSetGraphin(GParam *p)function contains only the statementm_param = *p;and theGetGraphin(GParam *p)function contains only the statement*p = m_param;In earlier version (of May 15, 2006) I had used the methodsSuspendThread()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 variablestop_threadthat 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; iiter; 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".