|
-
Sep 21st, 2001, 01:04 PM
#1
Thread Starter
Lively Member
A confusing question
I have a question that I was wondering if someone could at least make a guess as to what the problem is with my program.
I'm designing an RPG with the bit blit function. The combat requires about 50 frames of animation (which means about 100 sprites and masks), between your character and the enemey.
I have all the masks and sprites in their own picture box on separate forms and use timers to change which frame should be displayed then another timer that redraws the combat screen and sprites every 10 miliseconds weather they have been changed or not by looking at a case statement then drawing the correct mask and sprite.
I also initialize all the drawing fuctions before the combat begins by drawing every single one onto the combat window then erasing them before the user can see it. This increases the performance, otherwise the first time you do a move it will be sluggish.
Now finally to the problem. I've found that one random frame always seems to get screwed up displaying a large black area, and will remain screwed up unless you turn off the computer entirely then turn it back on and try again. But this only happens sometimes other times not at all.
Can anyone guess what that problem might be what is going on?
I assume the hdc property or picture itself is somehow getting screwed up during the initilization process. To combat it I now replace every picture in the picture boxes with one from a resource file after the initalization takes place and I have never had that problem since. But I'd really like to know what is causing it, because I really don't understand.
All will fall before the might of the Black Sashi...
-
Sep 22nd, 2001, 12:16 AM
#2
Frenzied Member
I'll take a guess. Sounds like a resource problem. On the start menu there is the Resource Meter (Start/Accessories/System Tools/Resource Meter) Run it before you run your program and then check it when you start to have problems. If resources get too low all kinds of screwy things can start to happen.
Needless to say 50 pictureboxes are using up a lot of resources, and when you have a lot of BitBlts going on continuosly Windows can get backed up. You can try peppering your code with DoEvents and see if that helps, especially in loops.
Another thing you may want to consider is combining a lot of the pictures into one larger picture and store it in one picturebox or maybe 10 or 15 pictureboxes. Just cut down the number as much as you can. Then you can BitBlt sections of it as needed.
Just a thought.
Greg
Free VB Add-In - The Reference Librarian
Click Here for screen shot and download link.
-
Sep 22nd, 2001, 06:08 AM
#3
Retired VBF Adm1nistrator
You shouldnt be blitting from pictureboxes at all actually.
Create device contexts.
VB Code:
Option Explicit
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDc As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hDc As Long) As Long
Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hDc As Long, ByVal hObject As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Public Function GenerateDC(FileName As String) As Long
Dim DC As Long, picTemp As IPictureDisp
DC = CreateCompatibleDC(0)
If DC < 1 Then
Exit Function
End If
Set picTemp = LoadPicture(FileName)
SelectObject DC, picTemp
DeleteObject picTemp
Set picTemp = Nothing
GenerateDC = DC
End Function
Private Sub Form_Load()
Dim myDc As Long
myDc = GenerateDC("c:\jamie\jamie.bmp")
With Picture1
.AutoRedraw = True
BitBlt .hDc, 0, 0, .Width, .Height, myDc, 0, 0, vbSrcCopy
End With
End Sub
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Sep 22nd, 2001, 01:22 PM
#4
Frenzied Member
The way I always did it was to have one picturebox for each sprite. There's an excellent tutorial on that at Fox's web site - http://orion.spaceports.com/~mccloud/
-
Sep 22nd, 2001, 01:50 PM
#5
Thread Starter
Lively Member
?
Thanks plenderj but how do you suggest I implement that code so ALL the sprites are in memory at once. If I use that code you posted every time I bit blit something the performance is really going to go down. I know with Direct X (which I'm not using in this project) you can load all the sprites into an array. Can I do that with that code without loseing the hDC property? The reason I have picture boxes right now is because its proved to be the fastest performance, but I'm sure an array would be better.
All will fall before the might of the Black Sashi...
-
Sep 22nd, 2001, 01:54 PM
#6
Thread Starter
Lively Member
wait
actually at a second look that might work. I tryed to implement something similar once and it was totally not working, but let me give that a try thanks.
All will fall before the might of the Black Sashi...
-
Sep 22nd, 2001, 01:56 PM
#7
Frenzied Member
Well, in the site I gave there's an article on blting lots of frames from a single DC, and another one on loading an offscreen DC without wasting resources every time you blt...
-
Sep 24th, 2001, 02:34 AM
#8
Retired VBF Adm1nistrator
Zaknafein,
What you do is as follows.
You show a loading screen for example.
And what you do is create device contexts for all the sprites.
You can have an array of Longs, or just lots of variable names.
Anyway you create lots. And once you've created them, thats that.
Eg:
VB Code:
shipUp = GenerateDC("c:\jamie\up.bmp")
shipDown = GenerateDC("c:\jamie\down.bmp")
shipLeft = GenerateDC("c:\jamie\left.bmp")
shipRight = GenerateDC("c:\jamie\right.bmp")
Once those device contexts have been created, then each time you want to draw a picture you'd do :
VB Code:
Select Case True
Case goUp:
BitBlt destPBox.hDc, 0, 0, destPBox.Width, destPBox.Height, shipUp, 0, 0, vbSrcCopy
Case goDown:
BitBlt destPBox.hDc, 0, 0, destPBox.Width, destPBox.Height, shipDown, 0, 0, vbSrcCopy
Case goLeft:
BitBlt destPBox.hDc, 0, 0, destPBox.Width, destPBox.Height, shipLeft, 0, 0, vbSrcCopy
Case goRight:
BitBlt destPBox.hDc, 0, 0, destPBox.Width, destPBox.Height, shipRight, 0, 0, vbSrcCopy
End Select
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Sep 24th, 2001, 06:49 AM
#9
Junior Member
well if i was you, i would scrap bitblt and use DirectX. once you get past creating surfaces and all the start-up code, its alot easier than Bitblt + you don't have to make masks!
and if you want i will upload my graphics class which does alot of the work for you (although it was done with sports games inmide, i am sure it wouldnt take that much work to bring it up to RPG standards....)
JSG
Two Wrongs May Not Make A Right, But Three Rights Make A Left....
-
Sep 24th, 2001, 07:21 PM
#10
Thread Starter
Lively Member
VB
Thank you for all the replys. This is very helpful. Yes JSG I would like to see your direct X graphics class if you don't mind.
All will fall before the might of the Black Sashi...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|