Game hanging when losing focus

Previous topic - Next topic

MrPlow

hi

Does anyone know what might cause Windows game to hang when losing focus...

My game runs okay but if I alt tab away i cant get back in?

Could it be a looping or call subs issue?

My game uses a lot of TYPES and function calls.

rgds
G
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Moru

If you create a simple project that just displays some image from your game on the screen with a while/wend loop and then try to alt-tab, do you get the same problem?

spacefractal

im have seen that in Greedy Mouse as well, but its does only hanging in few secs. Also even when the game is paused, then you should countinue to use SHOWSCREEN and eventuelly SLEEP. If you dont doing that, then the game might hang because its dosent answear Windwos back again.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

bigsofty

Try adding a "SLEEP 5" at the end of your main loop, it may give windows a better chance to talk to you program.
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

Schranz0r

Code (glbasic) Select
AUTOPAUSE FALSE


???
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

MrPlow

Thanks Guys

I dont have Autopause false in my code.!!! I'm such a Numpty!

Things is running much smoother now...and I've added a SLEEP 5 in the key press loop.

Cheers...





Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Schranz0r

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

MrPlow

My game is still hanging  in WHILE loops where no screen activity is processed...any ideas on how I can fix this?

(hangs)
SHOWSCREEN
WHILE TRUE
SLEEP 5
   IF KEY() THEN

   IF KEY() THEN
WEND

(doesnt hang but this is not ideal for me)
WHILE TRUE
SHOWSCREEN // dont want a showscreen every loop
   IF KEY() THEN

   IF KEY() THEN
WEND
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

erico

I´m not the best to help on that issue MR.Plow, but I think SHOWSCREEN does other stuff then only display.
It may be a necessary command call depending on what you are doing on the loops.

But better wait for a more pro answer. :-[

Schranz0r

#9
The "normal" GLBasic-way is:

Code (glbasic) Select
WHILE TRUE
    // DO STUFF HERE
    IF KEY(28) THEN PRINT "WOHOO does is hang anymore?!", 10,10
SHOWSCREEN
WEND
END



OHHhhhhh.... now i understand!

You can never go out of this WHILE-WEND-LOOP!
If you do NO SHOWSCREEN, the game/program is dead for your OS there are no callbacks anymore!   
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

MrPlow

Looks like I should update all my key press loops with show screens but that also means Adding screen populating to each loop .... Arrgghh

Is this what every one else has to do.? Best practice?
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Ian Price

Yep - you have to populate the screen each frame and then SHOWSCREEN (although there are alternatives, you still have to use SHOWSCREEN regularly or the program will hang).

At your use of SHOWSCREEN explains the problems with exiting the game now.
I came. I saw. I played.

Schranz0r

"WHILE TRUE" - Loop for keyevents?!

pls show me your code!
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

MrPlow

Quote from: Schranz0r on 2013-Aug-31
"WHILE TRUE" - Loop for keyevents?!

pls show me your code!

This is a function for getting my key event...but now i have to use showscreen...

Code (glbasic) Select
FUNCTION getdir:

WHILE TRUE
SLEEP 5
IF KEY(71)
xm = -16
xy = -16
BREAK
ENDIF

IF KEY(72)
xm = 0
xy = -16
BREAK
ENDIF

IF KEY(73)
xm = +16
xy = -16
BREAK
ENDIF
IF KEY(75)
xm = -16
xy = 0
BREAK
ENDIF
IF KEY(77)
xm = +16
xy = 0
BREAK
ENDIF
IF KEY(79)
xm = -16
xy = +16
BREAK
ENDIF
IF KEY(80)
xm = 0
xy = +16
BREAK
ENDIF
IF KEY(81)
xm = +16
xy = +16
BREAK
ENDIF
WEND
ENDFUNCTION
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Ian Price

That routine is quite odd - you'd generally use the WHILE/WEND in the main loop and not in a called function.

If you still wanted a similar function I'd use something like

Code (glbasic) Select

GLOBAL press, xm, xy

MAIN()

// Main function
FUNCTION MAIN:

WHILE TRUE

// Draw all your graphics etc.

  getdir()

  IF press>0 THEN DEC press

  PRINT xm+" "+xy,20,20

  PRINT press,20,40

  SHOWSCREEN

WEND
ENDFUNCTION


// Get direction function
FUNCTION getdir:
IF press=0

                IF KEY(71)
                        xm = -16
                        xy = -16
                        press=10
                 ENDIF

                 IF KEY(72)
                        xm = 0
                        xy = -16
                        press=10
                 ENDIF

                 IF KEY(73)
                        xm = +16
                        xy = -16
                        press=10
                 ENDIF

                 IF KEY(75)
                        xm = -16
                        xy = 0
                        press=10
                 ENDIF

                 IF KEY(77)
                        xm = +16
                        xy = 0
                        press=10
                 ENDIF

                 IF KEY(79)
                        xm = -16
                        xy = +16
                        press=10
                 ENDIF

                 IF KEY(80)
                        xm = 0
                        xy = +16
                        press=10
                 ENDIF

                 IF KEY(81)
                        xm = +16
                        xy = +16
                        press=10
                 ENDIF

  ENDIF

ENDFUNCTION


This will limit the key presses (ie if a key has been pressed within the last 10 frames then don't accept this next key press). It also allows your program to update everything it needs to all within the main loop.

There are ways to make this code more efficient/compact, but you need to understand this first. Obviously you're doing other things right because Viking Raiders is very playable, despite the odd bug :)
I came. I saw. I played.