My game lags on Steam.

Advanced game related topics
miso
Enthusiast
Enthusiast
Posts: 514
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: My game lags on Steam.

Post by miso »

What if :

Code: Select all

Repeat : WaitWindowEvent(1) : ForEver
Joubarbe
Enthusiast
Enthusiast
Posts: 728
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: My game lags on Steam.

Post by Joubarbe »

miso wrote: Sun Nov 23, 2025 5:04 pm What if :

Code: Select all

Repeat : WaitWindowEvent(1) : ForEver
I believed in that one for some reason, but no. :D

UPDATE: If anyone wants to help, please download the demo, and try 1/ via the steam launcher 2/ via RH.exe. The world generation ("Generate hope") should take about 4 seconds via Steam, and be instant via RH.exe. If not, please report! :)
miso
Enthusiast
Enthusiast
Posts: 514
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: My game lags on Steam.

Post by miso »

I was hoping too...
Still belive, that the steam launcher somehow affects the os events. (It's a guess, and may be wrong assumption.)

Edit: What if you iterate trough all of the pending events in a loop? Is it possible in your code?
User avatar
mk-soft
Always Here
Always Here
Posts: 6393
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: My game lags on Steam.

Post by mk-soft »

Your event loop is wrong ...

If you use an OpenWindowedScreen, the event loop must look different.
See PB-Help

Code: Select all

...
Repeat
    ; It's very important to process all the events remaining in the queue at each frame
    ;
    Repeat
      Event = WindowEvent()
      
      Select Event 
        Case #PB_Event_Gadget
        
        Case #PB_Event_CloseWindow
           Break 2
      EndSelect
    Until Event = 0
    
    ...
Forever
    
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
miso
Enthusiast
Enthusiast
Posts: 514
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: My game lags on Steam.

Post by miso »

He uses canvas, I'm not particularly familiar with it. So no screen as far as I know.
Joubarbe
Enthusiast
Enthusiast
Posts: 728
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: My game lags on Steam.

Post by Joubarbe »

Image

This is a CPU trace from Visual Studio. You can see that the Steam overlay is doing something at a specific function. Is there a way in PureBasic to reproduce that and to have the name of the function? I'm in complete unknown territory there, so maybe there are other tools / methods to do that?
ricardo_sdl
Enthusiast
Enthusiast
Posts: 143
Joined: Sat Sep 21, 2019 4:24 pm

Re: My game lags on Steam.

Post by ricardo_sdl »

Nice game!
From what I read, your game doesn't use Direct X or Opengl, right? It's just a normal windows executable, which uses the Canvas widget to draw stuff to the window, correct?

And which compiler version and options are you using to generate the exe?
You can check my games at:
https://ricardo-sdl.itch.io/
User avatar
minimy
Enthusiast
Enthusiast
Posts: 731
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: My game lags on Steam.

Post by minimy »

But does the problem come from an update?
Did it work well before?
I think it's better to use WindowEvent() and put a Delay(1) before the end of the main loop. Instead of WaitWindowEvent(1). To process everything.
I don't know if this is the issue or not. :|
If translation=Error: reply="Sorry, Im Spanish": Endif
Joubarbe
Enthusiast
Enthusiast
Posts: 728
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: My game lags on Steam.

Post by Joubarbe »

ricardo_sdl wrote: Thu Nov 27, 2025 6:13 pm Nice game!
From what I read, your game doesn't use Direct X or Opengl, right? It's just a normal windows executable, which uses the Canvas widget to draw stuff to the window, correct?

And which compiler version and options are you using to generate the exe?
Thanks! This is correct.

I compile in C with optimised code. Version is current non-beta.

It never worked before. It has always been slower on Steam.

WindowEvent() with a delay sounds to do exactly the same as WaitWindowEvent(1) to me, no?
User avatar
idle
Always Here
Always Here
Posts: 6079
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: My game lags on Steam.

Post by idle »

maybe you need to generate output in a thread draw to image then post an event to set the image to canvas
it looks like it's hooking your window procedure by injecting a global hook via dll into your process.
So If it's hooking the window procedure to monitor events it may actually be tying to sync it at 60 FPS but if your code is taking to long, it could be adding a delta delay so it halves the frame rate to 30FPS, that doesn't really make any sense to me as a hook should return immediately but that could explain the observed results perhaps or perhaps its adding events to the event loop that PB normally eats?

So try threading the render to an image and post an event to grab the image to canvas or poll it so it grabs the image

Code: Select all

Global gmRender = CreateMutex() 
Global width = DesktopScaledX(800)
Global height = DesktopScaledY(600) 
Global gImg = CreateImage(#PB_Any,width,height,32) 
Global gQuit 

Procedure RenderGameFrame() 
  
  Static x,y,vx,vy 
  If vx = 0 : vx= 1 : EndIf  
  If vy = 0 : vy=1 : EndIf   
   
  If StartDrawing(ImageOutput(gimg)) 
  Box(0,0,width,height,$FFFFFFFF) 
  DrawText(x,y,"Hello",RGBA(0,255,0,255),$FFFFFFFF)
  StopDrawing() 
  EndIf 
  If x > width  Or x < 0 
    vx = -vx 
  EndIf 
  If y > height Or y < 0 
    vy = -vy 
  EndIf 
  x+vx 
  y+vy
  
EndProcedure   

Procedure RenderThread(*data)
    
  Repeat 
    
    LockMutex(gmRender) 
    RenderGameFrame() 
    UnlockMutex(gmRender) 
    Delay(16)    
  Until gQuit   
  
EndProcedure 

Procedure GetFrame(gadget) 
  
  If TryLockMutex(gmRender) 
    If StartDrawing(CanvasOutput(gadget)) 
       DrawImage(ImageID(gImg),0,0)  
       StopDrawing() 
    EndIf    
    UnlockMutex(gmRender)   
  Else 
    Debug "blocked" 
  EndIf
  
EndProcedure   

#timerDraw = 123  

OpenWindow(0,0,0,800,600,"test") 
CanvasGadget(1,0,0,800,600) 
AddWindowTimer(0,#timerDraw,16)
CreateThread(@RenderThread(),0) 

Repeat 
  event = WaitWindowEvent() 
  If Event = #PB_Event_Timer And EventTimer() = #timerDraw 
    GetFrame(1) 
  EndIf 
Until event = #PB_Event_CloseWindow 
gQuit = 1 

Also If your drawing anything on top of the canvas or overlapping any gadgets that could also be causing problems by generating to many invalidate rect events and flooding the event queue.

Main thing is to get your event loop free so it can process the events, if your blocking it by the time it takes to render a frame that could easily be the root of the problem
Toss it in a thread!
Joubarbe
Enthusiast
Enthusiast
Posts: 728
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: My game lags on Steam.

Post by Joubarbe »

Thanks a lot for your effort idle! My game only redraws when necessary, so I had to change your code a bit, as I cannot afford to redraw the whole UI at 60 FPS. So I did put all the rendering in a thread but unfortunately it still doesn't work.

The Steam support is apparently on it, but you never know with those guys... I'll update if they find a solution.

The only other solution I can think of at this point is to use a screen and make my app GPU friendly. But that's a serious refactoring...
User avatar
idle
Always Here
Always Here
Posts: 6079
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: My game lags on Steam.

Post by idle »

you could try this then using a semephore.

Code: Select all

Global gmRender = CreateMutex() 
Global gsRender = CreateSemaphore() 
Global width = DesktopScaledX(800)
Global height = DesktopScaledY(600) 
Global gImg = CreateImage(#PB_Any,width,height,32) 
Global gQuit 

Procedure RenderGameFrame() 
  
  Static x,y,vx,vy 
  If vx = 0 : vx= 1 : EndIf  
  If vy = 0 : vy=1 : EndIf   
   
  If StartDrawing(ImageOutput(gimg)) 
  Box(0,0,width,height,$FFFFFFFF) 
  DrawText(x,y,"Hello",RGBA(0,255,0,255),$FFFFFFFF)
  StopDrawing() 
  EndIf 
  If x > width  Or x < 0 
    vx = -vx 
  EndIf 
  If y > height Or y < 0 
    vy = -vy 
  EndIf 
  x+vx 
  y+vy
  
EndProcedure   

Procedure RenderThread(gadget)
  
  Repeat 
    WaitSemaphore(gSrender) 
    If Not gquit
      
      RenderGameFrame() 
      If StartDrawing(CanvasOutput(gadget)) 
        DrawImage(ImageID(gImg),0,0)  
        StopDrawing() 
      EndIf    
    EndIf   
  Until gQuit   
  
EndProcedure 

#timerDraw = 123  

OpenWindow(0,0,0,800,600,"test") 
CanvasGadget(1,0,0,800,600) 
AddWindowTimer(0,#timerDraw,30)
CreateThread(@RenderThread(),1) 

Repeat 
  event = WaitWindowEvent() 
  If Event = #PB_Event_Timer And EventTimer() = #timerDraw 
    SignalSemaphore(gsRender) 
  EndIf 
Until event = #PB_Event_CloseWindow 
gquit = 1 
SignalSemaphore(gsRender) 

Are you sure that your rendering is only happening on the intended events and isn't being called multiple times?

I hope the Steam guys take a look at it for you and it will be interesting to find out what the cause is.
Post Reply