We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 31,216 times

Related Categories

How to Create a Sprite using MFC

davepamn

Objectives

  1. Create a Bitmap
  2. Create a DC in MFC VC++
  3. Tell the DC to draw the bitmap

Technical Points

  1. Load the Bitmap into a Bitmap class
  2. The Bitmap class stores the Original DC Bitmap which will be restored when the class is destoried.
  3. The BitBlt function performs a 1:1 copy of pixels in the Source DC to the Target DC.
  4. StretchBlt function maps the Source DC to fit the dimensions of the Target DC.

OnCreate Event

  pDC = new CClientDC(this);
   
   HDC hdcWindow = pDC->GetSafeHdc();
                     
    // let our class handle the details of loading the bitmaps!
       g_bmpBackground.Load(hdcWindow, "ch2p7_bg.bmp");
   g_bmpSprite.Load(hdcWindow, "ch2p7_purball.bmp");
   g_bmpSpriteMask.Load(hdcWindow, "ch2p7_purball_mask.bmp");

   // release the DC we obtained
   ReleaseDC(pDC);
   delete pDC;
   
   //Set the Milliseconds for each timer event
   SetTimer(1,500,0);


OnDraw Event

 StretchBlt(hdcWindow,
   0, 0, 640, 480,
   g_bmpBackground.GetBitmapDC(), 0, 0,
   g_bmpBackground.GetWidth(),
   g_bmpBackground.GetHeight(), SRCCOPY);


On Timer Event

    CDC *pDC;

     int randomx = RandomNumber(0,640);
     int randomy = RandomNumber(0,480);

    pDC = new CClientDC(this);

    HDC hdcWindow = pDC->GetSafeHdc();

    //redraw the background
    StretchBlt(hdcWindow,0, 0, 640, 480,
    g_bmpBackground.GetBitmapDC(), 0, 0, 640, 480, SRCCOPY);

     // first blit the mask, using SRCAND.
     BitBlt(hdcWindow, randomx, randomy, 640, 480,
       g_bmpSpriteMask.GetBitmapDC(), 0, 0, SRCAND);

     // then blit the image using the OR operator (SRCPAINT).
     BitBlt(hdcWindow, randomx, randomy, 640, 480,
       g_bmpSprite.GetBitmapDC(), 0, 0, SRCPAINT);

      ReleaseDC(pDC);
     delete pDC;

NishiSoft provides Part I of the Information Technology Project collaboration. Sign up and list your IT project tasks, assign task too friends, and get percent complete task. Part will include a work order system with NishiSoft acting as the middle man between software task order, verification of requirements meet and services delivered, and generation of the voucher for payment between parties.

Comments