Results 1 to 9 of 9

Thread: Issue with close button in a 2d gaming window.

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    1

    Angry Issue with close button in a 2d gaming window.

    Hello building a small game here and was having issues making a proper close button for in game windows.. I will post a chunk of code and maybe you can fix my issue..

    Code:
            Case MailboxWindow
                With GameWindow.Mailbox
                    Engine_Render_Grh .SkinGrh, .Screen.X, .Screen.Y, 0, 1, True, GUIColorValue, GUIColorValue, GUIColorValue, GUIColorValue
                    Engine_Render_Text Font_Default, MailboxListBuffer, .Screen.X + .List.X, .Screen.Y + .List.Y, -1
                    Engine_Render_Text Font_Default, "Read", .Screen.X + .ReadLbl.X, .Screen.Y + .ReadLbl.Y, -1
                    Engine_Render_Text Font_Default, "Write", .Screen.X + .WriteLbl.X, .Screen.Y + .WriteLbl.Y, -1
                    Engine_Render_Text Font_Default, "Delete", .Screen.X + .DeleteLbl.X, .Screen.Y + .DeleteLbl.Y, -1
                    If SelMessage > 0 Then Engine_Render_Rectangle .Screen.X + .List.X, .Screen.Y + .List.Y + ((SelMessage - 1) * Font_Default.CharHeight), .List.Width, Font_Default.CharHeight, 1, 1, 1, 1, 1, 1, 0, 0, 2097217280, 2097217280, 2097217280, 2097217280, , False   'ARGB: 125/0/255/0
                        If Engine_Collision_Rect(MousePos.X, MousePos.Y, 1, 1, .Screen.X + 325, .Screen.Y + 5, 10, 17) Then
            If MouseLeftDown > 0 Then
                ShowGameWindow(LastClickedWindow) = 0
                LastClickedWindow = 0
                Exit Sub
            End If
        End If
                End With
    Now this is what I added extra in that is in the above code...
    Code:
      If Engine_Collision_Rect(MousePos.X, MousePos.Y, 1, 1, .Screen.X + 325, .Screen.Y + 5, 10, 17) Then
            If MouseLeftDown > 0 Then
                ShowGameWindow(LastClickedWindow) = 0
                LastClickedWindow = 0
                Exit Sub
            End If
        End If
    This where I have issues.. It works fine closes no problem.. But when I click somewhere on the screen and hold down the mouse button right to this X in this window in the main game window it crashes...

    So I get a error 9 and debugs goes to this:
    Code:
    LastClickedWindow = 0
    I am a n99b and learning.. If you can give me a snip of code to try or know how to fix the issue that would be great..

  2. #2
    Lively Member
    Join Date
    Mar 2008
    Posts
    115

    Re: Issue with close button in a 2d gaming window.

    To stop the program you should use "END" or "Unload Me"

    You could also use the ESC to end game.

    Code:
            If MouseLeftDown = True Then
               Unload me        ' or END
    
    End If
    Unless you are just trying to stop the game and and show results then you should use a Boolean to pause the game loop.

    Are you using a Timer or game Loop?

    .

  3. #3
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Issue with close button in a 2d gaming window.

    Jabber, did you say use End.
    End is not considered a good way to stop a program it's brutal and can lead to memory leaks (akin to ending a process with task manager). Unload is much better.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Issue with close button in a 2d gaming window.

    Moved To Games Programming

  5. #5
    Lively Member
    Join Date
    Mar 2008
    Posts
    115

    Re: Issue with close button in a 2d gaming window.

    Quote Originally Posted by Milk
    Jabber, did you say use End.
    End is not considered a good way to stop a program it's brutal and can lead to memory leaks (akin to ending a process with task manager). Unload is much better.
    OK. I will remember that but in many DX game examples they set everything to nothing and then often use END instead of "unload me."

    .

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Issue with close button in a 2d gaming window.

    In that case, I'd recommend that you don't trust the other parts of the examples, and find a better source instead.

    If somebody thinks that End is perfectly fine, the chances are that they don't have much experience - and so in other parts of the code are likely to have made errors and/or done things in inappropriate ways.

    If they don't think it is fine, but have used it without saying so (such as a brief comment on that line to say "use a valid method instead"), what else have they not bothered to tell you?


    For an explanation (and example code) for how you should be closing a form/program (part of which includes "Unload Me"), see the article How should I close my form/program/class? from our Classic VB FAQs (in the FAQ forum, which is shown near the top of our home page)

  7. #7
    Lively Member
    Join Date
    Mar 2008
    Posts
    115

    Re: Issue with close button in a 2d gaming window.

    Code:
    Sub RestoreWindowsGUI()
    
    'When you bail out of DX7 you must destroy your surfaces or they remain in Sys_Memory
    ReDim AutoZ_Sprite(0) As DirectDrawSurface7: Set AutoZ_Sprite(0) = Nothing
    ReDim StaticB(0) As DirectDrawSurface7: Set StaticB(0) = Nothing
    
    'clearing other DX7 sufaces
    Set StaticC = Nothing: Set StaticD = Nothing: Set StaticE = Nothing: Set Primary = Nothing: Set BackBuffer = Nothing
    
    'End the MainLoop Bool
    LoopUntilWeDrop = False
    
    'RESTORING DISPLAY MODE
        Call d7.RestoreDisplayMode
        Call d7.SetCooperativeLevel(AutoZ_Order.hWnd, DDSCL_NORMAL)
        
        End 'WE'RE OUTTA' HERE
    End Sub
    si_the_geek, So you think Johnathan Drake And Jack Hoxley don't know what they are doing?

    .

  8. #8
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Issue with close button in a 2d gaming window.

    It might well be that they do know what they are doing, they have kept track of all the objects, been very careful to ensure that everything is properly dereferenced. In this case End probably tidies things up quite nicely, it's unnecessary, but it won't cause an unseen problem.

    The problem arises when a less experienced programmer upon reading that code learns that End is how to terminate a program, but does not learn that they might have to clean up a bit first, and does not learn that the unload / queryunload events are bypassed. They could be leaking memory never realise it.

    The End statement never has to be used, it can always be avoided.

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Issue with close button in a 2d gaming window.

    In addition to what Milk posted, the use of : to join multiple lines is debatable at the best of times (as it generally makes the code harder to read), and not really apt in examples/tutorials - especially when it is used to join several almost unrelated items ("other DX7 sufaces").

    Based on the comments to go with it, some of the code does not seem right:
    Code:
    'When you bail out of DX7 you must destroy your surfaces or they remain in Sys_Memory
    ReDim AutoZ_Sprite(0) As DirectDrawSurface7: Set AutoZ_Sprite(0) = Nothing
    To me it sounds as if you should be doing something more than setting them to Nothing, or perhaps set all elements to Nothing. If the array is initially defined without a size (which it needs to be in order to ReDim), this code is not ideal anyway - using just Erase would be better (which sets all elements to Nothing, and resets the array to have no elements at all).


    There is also the room for serious error, depending on how the rest of the program is structured. There is code for "End the MainLoop Bool", but it is run after the surfaces have been destroyed. If there is an attempt to use them, some kind of error will occur (I can't remember DX well enough to know how bad that would be).


    I am by no means saying that they don't know what they are doing with DX - but they haven't presented things as well as they probably should have (see my previous post), and I suspect there are better articles to learn from.
    Last edited by si_the_geek; May 26th, 2008 at 01:51 PM.

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