[Sprite and Animation with API BitBlt] A way to get this ???

  • Thread starter Thread starter matt
  • Start date Start date
M

matt

Hallo,

I have to create a simple sprite movement;
in VB6 I used the API BitBlt and all was very good.

Using the NET graphical methods (not API) ,
the result is slow.

Do you have a NET example about BitBlt and the sprite movement ,
because I'm a little in trouble in implementing it
(a simple example could be this ... a Picturebox1 as background
and a small squared Picturebox2 bouncing
inside Picturebox1 from the left to the right) ?



note:
I don't want to use DirectX
 
I don't know about the VB6 aspect of your query but you'll find a
comprehensive example of animation in Windows Forms Tips and Tricks.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Hi Matt,
because I'm a little in trouble in implementing it

Can you elaborate what trouble do you have?

BTW, BitBlt declaration is:

~
Friend Declare Function BitBlt Lib "gdi32.dll" ( _
ByVal hdcDest As IntPtr, _
ByVal nXDest As Integer, _
ByVal nYDest As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal hdcSrc As IntPtr, _
ByVal nXSrc As Integer, _
ByVal nYSrc As Integer, _
ByVal dwRop As Integer _
) As Boolean
~

Roman
 
The animations performed by the Net Framework are slow.
Only the old BitBlt API can assure the needed velocity.

But tthe management of the hdc devices (to pass
as parameter to the BitBlt) are more complex in NET.
This gets me angry (but this is not important);
it is so complex that doesn't allow me to build up
the old simple animation task.
I am stopped.
 
THe problem I have is about
the way to prepare and manage
the hdc devices .

I have two PictureBoxes (destination and source);
in VB6 I only had to write
PictureBox1.hdc and PictureBox2.hdc
and pass them to the BitBlt API.

In NET it seems I can't do this:
I think I have to
create the two devices from both the PictureBoxes,
then I have to create two compatible bmp,
select them as objects,
pass them to te BitBlt API,
set my destination PictureBox equal to the BitBltted image I got,
and finally Delete and Dispose devices.

Actually too complex for me.

Is anyone could help me...
 
Don't think it's *too* complex.

Simple sample:

~
REM Copy one PictureBox's content into another one.
Friend Declare Function GetDC Lib "user32.dll" ( _
ByVal hWnd As IntPtr _
) As IntPtr

Friend Declare Function ReleaseDC Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByVal hDC As IntPtr _
) As Boolean

Const SRCCOPY As Integer = &HCC0020

Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
MyBase.OnClick(e)
Dim hDC1 As IntPtr = GetDC(PictureBox1.Handle)
Dim hDC2 As IntPtr = GetDC(PictureBox2.Handle)
BitBlt(hDC2, 0, 0, PictureBox1.Width, PictureBox1.Height, hDC1,
0, 0, SRCCOPY)
ReleaseDC(PictureBox1.Handle, hDC1)
ReleaseDC(PictureBox2.Handle, hDC2)
End Sub
~

Roman
 
This works, thanks.


But I have few related questions
(for you or anyone else):



1)
In your opinion, have we also to

ReleaseHdc
or
DeleteDC
or
Dispose

the two hdc we used?

If Yes, how to do that?





2)
If I put only your five rows of code
Dim hDC1 As IntPtr = GetDC(PictureBox1.Handle)
Dim hDC2 As IntPtr = GetDC(PictureBox2.Handle)
BitBlt(hDC2, 0, 0, PictureBox1.Width, PictureBox1.Height, hDC1, 0, 0,
SRCCOPY)
ReleaseDC(PictureBox1.Handle, hDC1)
ReleaseDC(PictureBox2.Handle, hDC2)
(not the whole Protected Overrides Sub OnLoad...)
in the load event, they don't work,

While if I put them (in example) in Command1_Click event,
they work.

Do you know why?



3)
I knew that when declaring an API
we have to transform the Long value in Integer value,
you also transformed the Device parameter into a IntPtr parameter.
Where did you learn it (MSDN guide?) ?
Have you an API Viewer (I have Visual Studio 2005 and I didn't find
any API Viewer and I need it) ?
 
Hi again,
1)
In your opinion, have we also to

ReleaseHdc
or
DeleteDC
or
Dispose

the two hdc we used?

If Yes, how to do that?

Of course, you have to release all DC's you get. You can see I call
ReleaseDC in the sample.
It's not recommended to call DeleteDC on this kind of DC's, so don't.
You can't Dispose DC since it's unmanaged.

If you want to animate using BitBlt then you obviously shouldn't
create/release DC each time. Create one at the beginning and release it
after animation is done.
2)
If I put only your five rows of code
Dim hDC1 As IntPtr = GetDC(PictureBox1.Handle)
Dim hDC2 As IntPtr = GetDC(PictureBox2.Handle)
BitBlt(hDC2, 0, 0, PictureBox1.Width, PictureBox1.Height, hDC1, 0, 0,
SRCCOPY)
ReleaseDC(PictureBox1.Handle, hDC1)
ReleaseDC(PictureBox2.Handle, hDC2)
(not the whole Protected Overrides Sub OnLoad...)

It's OnClick actually 8=]
in the load event, they don't work,

While if I put them (in example) in Command1_Click event,
they work.

Do you know why?

It's probably because Load event occurs before Paint event, so every
painting you got is lost because Picturebox paints itself again with
it's background color.
3)
I knew that when declaring an API
we have to transform the Long value in Integer value,
you also transformed the Device parameter into a IntPtr parameter.
Where did you learn it (MSDN guide?) ?
Have you an API Viewer (I have Visual Studio 2005 and I didn't find
any API Viewer and I need it) ?

Funny, but I can't remember when and where I learned about IntPtr's 8=]
I just recall that at some point I said to myself: "Now, give up these
Int32's & move on to IntPtr, dude!".
I can show you benefits of IntPtr though:
1. It's platform-dependent, so when you move on to 64-bit, you'll
probably have less work to do, since IntPtr's will be 64-bit already.
2. All .NET framework methods that give you (take from you) a handle,
pointer or something like this, they take IntPtr's (Control.Handle,
Marshal.* etc), so you wouldn't have to use .ToInt32 and New IntPtr(...)
stuff.

AFAIK VS'es don't have built-in API viewer. You can try ApiViewer 2004
(http://www.activevb.de/rubriken/apiviewer/index-apiviewereng.html),
however I prefer to make API declarations myself.

Roman
 
Back
Top