Results 1 to 10 of 10

Thread: A confusing question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Posts
    74

    Unhappy 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...

  2. #2
    Frenzied Member
    Join Date
    Aug 2001
    Posts
    1,075
    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.

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    You shouldnt be blitting from pictureboxes at all actually.
    Create device contexts.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDc As Long) As Long
    4. Private Declare Function DeleteDC Lib "gdi32" (ByVal hDc As Long) As Long
    5. 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
    6. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    7. Private Declare Function SelectObject Lib "gdi32" (ByVal hDc As Long, ByVal hObject As Long) As Long
    8. 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
    9.  
    10. Public Function GenerateDC(FileName As String) As Long
    11.     Dim DC As Long, picTemp As IPictureDisp
    12.     DC = CreateCompatibleDC(0)
    13.     If DC < 1 Then
    14.         Exit Function
    15.     End If
    16.     Set picTemp = LoadPicture(FileName)
    17.     SelectObject DC, picTemp
    18.     DeleteObject picTemp
    19.     Set picTemp = Nothing
    20.     GenerateDC = DC
    21. End Function
    22.  
    23. Private Sub Form_Load()
    24.     Dim myDc As Long
    25.     myDc = GenerateDC("c:\jamie\jamie.bmp")
    26.     With Picture1
    27.         .AutoRedraw = True
    28.         BitBlt .hDc, 0, 0, .Width, .Height, myDc, 0, 0, vbSrcCopy
    29.     End With
    30. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    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/
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Posts
    74

    ?

    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...

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Posts
    74

    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...

  7. #7
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    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...
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  8. #8
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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:
    1. shipUp = GenerateDC("c:\jamie\up.bmp")
    2. shipDown = GenerateDC("c:\jamie\down.bmp")
    3. shipLeft = GenerateDC("c:\jamie\left.bmp")
    4. 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:
    1. Select Case True
    2.     Case goUp:
    3.         BitBlt destPBox.hDc, 0, 0, destPBox.Width, destPBox.Height, shipUp, 0, 0, vbSrcCopy
    4.     Case goDown:
    5.         BitBlt destPBox.hDc, 0, 0, destPBox.Width, destPBox.Height, shipDown, 0, 0, vbSrcCopy
    6.     Case goLeft:
    7.         BitBlt destPBox.hDc, 0, 0, destPBox.Width, destPBox.Height, shipLeft, 0, 0, vbSrcCopy
    8.     Case goRight:
    9.         BitBlt destPBox.hDc, 0, 0, destPBox.Width, destPBox.Height, shipRight, 0, 0, vbSrcCopy
    10. End Select
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  9. #9
    Junior Member
    Join Date
    Sep 2001
    Posts
    22
    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....

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2001
    Posts
    74

    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
  •  



Click Here to Expand Forum to Full Width