Animating the Appearance of Sprites - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Animating the Appearance of Sprites

Description:

Image backgrounds. Use a bitmap image to represent the background. Animated backgrounds ... Animated Background Class. class StarryBackground : Background ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 10
Provided by: kay57
Category:

less

Transcript and Presenter's Notes

Title: Animating the Appearance of Sprites


1
Animating the Appearance of Sprites Creating
Backgrounds
2
Incorporate Frame Animation Into Sprite Animation
  • Rely on a series of bitmap images to represent
    more than one appearance for the sprite
  • Store a series of frame images vertically/horizont
    ally in a single image
  • Then only draw the current frame as part of an
    image
  • Additional information for animated sprite
  • Total number of frames
  • Current frame
  • Frame delay
  • Frame trigger //count down delay and indicate
    when its time to move to the next frame

3
Changes in Bitmap class
  • void DrawPart(HDC hDC, int x, int y, int xPart,
    int yPart, int wPart, int hPart, BOOL bTrans,
    COLORREF crTransColor) // a New method in the
    Bitmap class
  • if (m_hBitmap ! NULL)
  • // Create a memory device context for the
    bitmap
  • HDC hMemDC CreateCompatibleDC(hDC)
  • // Select the bitmap into the device
    context
  • HBITMAP hOldBitmap (HBITMAP)SelectObject(h
    MemDC, m_hBitmap)
  • // Draw the bitmap to the destination
    device context
  • if (bTrans)
  • TransparentBlt(hDC, x, y, wPart, hPart,
    hMemDC, xPart, yPart, wPart, hPart,
  • crTransColor)
  • else
  • BitBlt(hDC, x, y, wPart, hPart, hMemDC,
    xPart, yPart, SRCCOPY)
  • // Restore and delete the memory device
    context
  • SelectObject(hMemDC, hOldBitmap)
  • DeleteDC(hMemDC)

4
Changes in Sprite Class
  • //turns a normal sprite into an animated sprite
    by setting its
  • number of frames
  • void SpriteSetNumFrames(int iNumFrames)
  • // Set the number of frames
  • m_iNumFrames iNumFrames
  • // Recalculate the position
  • RECT rect GetPosition()
  • rect.bottom rect.top ((rect.bottom -
    rect.top) / iNumFrames)
  • SetPosition(rect)

5
Changes in Sprite Class Cont.
  • //updates the sprites current animation frame
  • void SpriteUpdateFrame()
  • if ((m_iFrameDelay gt 0) (--m_iFrameTrigger
    lt 0))
  • // Reset the frame trigger
  • m_iFrameTrigger m_iFrameDelay
  • // Increment the frame
  • if (m_iCurFrame gt m_iNumFrames)
  • m_iCurFrame 0

6
Changes in Sprite Class Cont.
  • // draw the sprite differently depending on
    whther or not it is an animated sprite
  • void SpriteDraw(HDC hDC)
  • // Draw the sprite if it isn't hidden
  • if (m_pBitmap ! NULL !m_bHidden)
  • // Draw the appropriate frame, if necessary
  • if (m_iNumFrames 1)
  • m_pBitmap-gtDraw(hDC, m_rcPosition.left,
  • m_rcPosition.top, TRUE)
  • else
  • m_pBitmap-gtDrawPart(hDC,
    m_rcPosition.left, m_rcPosition.top, 0,
    m_iCurFrame
  • GetHeight(), GetWidth(), GetHeight(),
    TRUE)

7
Types of Game Backgrounds
  • Solid backgrounds
  • Consist only of a solid color
  • Image backgrounds
  • Use a bitmap image to represent the background
  • Animated backgrounds
  • Changes its appearance over time
  • Write custom code to update and draw the
    background
  • Scrolling backgrounds
  • Involves an image or set of graphical objects
    capable of being shifted or scrolled around on
    the screen

8
A Basic Background Class
  • class Background
  • protected
  • // Member Variables
  • int m_iWidth, m_iHeight
  • COLORREF m_crColor
  • Bitmap m_pBitmap
  • public
  • // Constructor(s)/Destructor
  • Background(int iWidth, int iHeight,
    COLORREF crColor)
  • Background(Bitmap pBitmap)
  • virtual Background()
  • // General Methods
  • virtual void Update()
  • virtual void Draw(HDC hDC)
  • // Accessor Methods
  • int GetWidth() return m_iWidth
  • int GetHeight() return m_iHeight

9
A Derived Animated Background Class
  • class StarryBackground Background
  • protected
  • // Member Variables
  • int m_iNumStars
  • int m_iTwinkleDelay
  • POINT m_ptStars100
  • COLORREF m_crStarColors100
  • public
  • // Constructor(s)/Destructor
  • StarryBackground(int iWidth, int iHeight,
    int iNumStars 100,
  • int iTwinkleDelay 50)
  • virtual StarryBackground()
  • // General Methods
  • virtual void Update()
  • virtual void Draw(HDC hDC)
Write a Comment
User Comments (0)
About PowerShow.com