Game Troubles

  • 16 years ago

    I am making my first 'real' video game (back buffer, sound, etc.) and have come acrossed a very weird bug.  When the game loads the first thing it does is to create the back buffer and then blit the background picture to it (the background always appears), the it does the same for 3 sprites.  Once the game gets going tho (againonly on my machine it seems) sometimes a sprite will be invisible (it still intereacts with everything but its just not visibley there).  I think the problem is in this code:


    'Load ball image into the back buffer and create the ball object
       Set csBall = New clsSprite
       csBall.Transparent = True
       If Not csBall.LoadFrames(App.Path & "\Ball.bmp", 0, 0, 26, 26, 1) Then
           MsgBox "Error loading Ball image.", vbOKOnly, "Error"    'This isnt called even when I cant see the ball
           Shutdown
       End If
       csBall.X = (Picture1.Width / 2) - (csBall.Width / 2)
       csBall.Y = ((Picture1.Height + COMP) / 2) - (csBall.Height / 2) - 3


    Public Function LoadFrames(ByVal sFilename As String, _
       ByVal lStartX As Long, ByVal lStartY As Long, _
       ByVal lWidth As Long, ByVal lHeight As Long, _
       ByVal lNumFrames As Long, _
       Optional bGenMasks As Boolean = True) As Boolean


       On Error GoTo errorout
       Dim cbSource As New clsBitmap
       Dim lFrame As Long
       Dim lCopyX As Long
       Dim lCopyY As Long
       Dim bFailed As Boolean
       
       LoadFrames = False
       
       'load the source bitmap file
       If Not cbSource.Load(sFilename) Then Exit Function
           
       'copy each frame out of the source image
       For lFrame = 0 To lNumFrames - 1
           'calculate the upper left corner
           'lCopyX = lStartX + lFrame * (lWidth + 1)
           lCopyX = lStartX + lFrame * lWidth
           lCopyY = lStartY
           'set the current frame
           lCurrentFrame = lFrameCount + lFrame
           'redimension the bitmap array to hold the new frame
           ReDim Preserve cbFrames(0 To lCurrentFrame)
           'instantiate the new sprite frame
           Set cbFrames(lCurrentFrame) = New clsBitmap
           'create the bitmap image based on sprite dimensions
           If Not cbFrames(lCurrentFrame).Create(cbSource.hdc, _
               lWidth, lHeight) Then
               LoadFrames = False
               Exit For
           End If
           'transfer frame from source image to sprite array
           If BitBlt(cbFrames(lCurrentFrame).hdc, 0, 0, _
               lWidth, lHeight, cbSource.hdc, lCopyX, lCopyY, _
               vbSrcCopy) = 0 Then
               LoadFrames = False
               Exit For
           End If
       Next lFrame
       
       'increment the frame count
       lFrameCount = lFrameCount + lNumFrames
       
       'generate masks
       If bGenMasks Then
           'only gen masks if hardware support is false
           If Not HardwareTransparency Then
               If Not GenerateAllMasks() Then Exit Function
           End If
       End If
       
       'successful operation
       Set cbSource = Nothing
       LoadFrames = True
       Exit Function
    error
    out:
    End Function


    my OS : XP Pro SP1 & graphics card: NVIDIA GEFORCE2 MX Version: 3.11.01.30.99
    the thing i dont get is that sometimes it loads like a ball and one paddle, other times just a ball, none at all, or all 3 (yes the game is similar to pong).  y would such a sporadic behavior exist? im so lost...

  • 16 years ago

    whoops, forgot to explain that.  All bitmaps are stored as some sort of rectangle, so the bitmap of the ball has a black background around the round ball.  what that transaprent function does is 'erase' the black so you can see the background.  games wouldnt be much fun if all u had to work with were rectangles.  Heres teh code that does that:  


    Public Sub Draw(ByVal hdcDest As Long)
       Dim lOldColor As Long
       Dim lOldMode As Long
       
       'make sure current frame is valid
       If cbFrames(lCurrentFrame).hdc < 1 Then Exit Sub
       
       'draw sprite with transparency?
       If Transparent Then
           'check for hardware transparent blitter
           If HardwareTransparency Then
               'configure for hardware transparent blitting
               lOldMode = SetBkMode(hdcDest, NEWTRANSPARENT)
               lOldColor = SetBkColor(hdcDest, RGB(0, 0, 0))
               
               'draw image using hardware transparent blit
               BitBlt hdcDest, X, Y, Width, Height, _
                   cbFrames(lCurrentFrame).hdc, 0, 0, vbSrcCopy
               
               'restore background mode
               SetBkColor hdcDest, lOldColor
               SetBkMode hdcDest, lOldMode
           Else
               'blit the mask using AND
               BitBlt hdcDest, X, Y, Width, Height, _
                   cbMasks(lCurrentFrame).hdc, 0, 0, vbSrcAnd
               'blit the image using XOR
               BitBlt hdcDest, X, Y, Width, Height, _
                   cbFrames(lCurrentFrame).hdc, 0, 0, vbSrcPaint
           End If
       Else
           'blit the image without transparency
           BitBlt hdcDest, X, Y, Width, Height, _
               cbFrames(lCurrentFrame).hdc, 0, 0, vbSrcCopy
       End If
    End Sub

  • 16 years ago

    Also, the error appears randomly, some times there is just nothing there... theres no pattern or anything

  • 14 years ago

    The trouble may lie in this line of code:

    If cbFrames(lCurrentFrame).hdc < 1 Then Exit Sub

    I disabled it in similar code I am studying in the book "Microsoft Visual Basic Programming with DirectX".  Apparently, negative device contexts were ok, and my disappearing sprites went away.

     :)

Post a reply

Enter your message below

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

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only two kinds of languages: the ones people complain about and the ones nobody uses” - Bjarne Stroustrup