Time traveling balls.

Mac OSX specific forum
whizza
User
User
Posts: 46
Joined: Tue Nov 13, 2007 4:41 pm
Location: England

Time traveling balls.

Post by whizza »

Why does this program behave differently on OSX and Windows when spacebar is pressed?

On Windows the balls resume on the expected path.

On OSX the balls appear to leap forward in time.

Is there a way to make balls behave the same way on both OS?

Using PureBasic 5.72 Beta 2 for MacOS X (64-bit) on Catalina 10.15.3
and PureBasic 5.72 Beta 2 for Windows (64-bit) on Win10x64

Code: Select all


If InitSprite() = 0 Or InitKeyboard() = 0
  MessageRequester("Error", "Sprite system can't be initialized", 0)
  End
EndIf

If OpenWindow(0, 0, 0, 800, 600, "", #PB_Window_BorderLess | #PB_Window_ScreenCentered) = 0
  MessageRequester("Error", "Can't open main window")
  End
EndIf

If OpenWindowedScreen(WindowID(0), 100, 100, 600, 400, 0, 0, 0) = 0
  MessageRequester("Error", "Can't open main windowed screen")
  End
EndIf

  SetWindowColor(0, RGB(0,0,127))

  TextGadget(0, 100, 10, 600, 80, "")

  CreateSprite(0, 50, 50)
  StartDrawing(SpriteOutput(0))
  DrawingMode(#PB_2DDrawing_AllChannels)
  Box(0,0,50, 50, RGB(255,0,0))
  Circle(25, 25, 25, RGB(255,255,255))
  StopDrawing()
  
  CreateSprite(1, 50, 50, #PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(1))
  DrawingMode(#PB_2DDrawing_AllChannels)
  Box(0,0,50, 50, RGBA(255,0,0,255))
  Circle(25, 25, 25, RGBA(0,255,0,255))
  StopDrawing()
  
  x = 0
  move = 10
 
  Repeat
   
    Repeat
    Until WindowEvent() = 0
  
    FlipBuffers()
    
    ClearScreen(RGB(0,0,255))
     
    DisplaySprite(0, x, x)
    DisplaySprite(1, x+60, x)
    SetGadgetText(0, Str(x))
   
    x = x + move
   
    If x < 10
      move = 10
    EndIf
   
    If x > 340
      move = -5
    EndIf
 
    ExamineKeyboard()
    
    If KeyboardPushed(#PB_Key_Space)
      Delay(1000)
    EndIf
        
  Until KeyboardPushed(#PB_Key_Escape)
 
  End
  
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Time traveling balls.

Post by Mijikai »

Try:

Code: Select all

Global wait_for.q

If InitSprite() = 0 Or InitKeyboard() = 0
  MessageRequester("Error", "Sprite system can't be initialized", 0)
  End
EndIf

If OpenWindow(0, 0, 0, 800, 600, "", #PB_Window_BorderLess | #PB_Window_ScreenCentered) = 0
  MessageRequester("Error", "Can't open main window")
  End
EndIf

If OpenWindowedScreen(WindowID(0), 100, 100, 600, 400, 0, 0, 0) = 0
  MessageRequester("Error", "Can't open main windowed screen")
  End
EndIf

  SetWindowColor(0, RGB(0,0,127))

  TextGadget(0, 100, 10, 600, 80, "")

  CreateSprite(0, 50, 50)
  StartDrawing(SpriteOutput(0))
  DrawingMode(#PB_2DDrawing_AllChannels)
  Box(0,0,50, 50, RGB(255,0,0))
  Circle(25, 25, 25, RGB(255,255,255))
  StopDrawing()
 
  CreateSprite(1, 50, 50, #PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(1))
  DrawingMode(#PB_2DDrawing_AllChannels)
  Box(0,0,50, 50, RGBA(255,0,0,255))
  Circle(25, 25, 25, RGBA(0,255,0,255))
  StopDrawing()
 
  x = 0
  move = 10
 
  Repeat
   
    Repeat
    Until WindowEvent() = 0
 
    FlipBuffers()
   
    ClearScreen(RGB(0,0,255))
     
    DisplaySprite(0, x, x)
    DisplaySprite(1, x+60, x)
    SetGadgetText(0, Str(x))
    
    If Not wait_for
    
    x = x + move
   
    If x < 10
      move = 10
    EndIf
   
    If x > 340
      move = -5
    EndIf
    
    Else
      
      If ElapsedMilliseconds() >= wait_for
        wait_for = 0
      EndIf
      
    EndIf
    
    ExamineKeyboard()
   
    If KeyboardPushed(#PB_Key_Space)
      wait_for = ElapsedMilliseconds() + 1000
    EndIf
       
  Until KeyboardPushed(#PB_Key_Escape)
 
  End
U also might want to add some timing code for your game loop!
whizza
User
User
Posts: 46
Joined: Tue Nov 13, 2007 4:41 pm
Location: England

Re: Time traveling balls.

Post by whizza »

A short delay after flipobuffers() helps.


Code: Select all


Procedure Nap(MS)  
  StartTime.q = ElapsedMilliseconds()
  While ElapsedMilliseconds() - StartTime < MS    
    WaitWindowEvent(1)
  Wend
EndProcedure

If InitSprite() = 0 Or InitKeyboard() = 0
  MessageRequester("Error", "Sprite system can't be initialized", 0)
  End
EndIf

If OpenWindow(0, 0, 0, 800, 600, "", #PB_Window_BorderLess | #PB_Window_ScreenCentered) = 0
  MessageRequester("Error", "Can't open main window")
  End
EndIf

If OpenWindowedScreen(WindowID(0), 100, 100, 600, 400, 0, 0, 0) = 0
  MessageRequester("Error", "Can't open main windowed screen")
  End
EndIf

  SetWindowColor(0, RGB(0,0,127))

  TextGadget(0, 100, 10, 600, 80, "")

  CreateSprite(0, 50, 50)
  StartDrawing(SpriteOutput(0))
  DrawingMode(#PB_2DDrawing_AllChannels)
  Box(0,0,50, 50, RGB(255,0,0))
  Circle(25, 25, 25, RGB(255,255,255))
  StopDrawing()
  
  CreateSprite(1, 50, 50, #PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(1))
  DrawingMode(#PB_2DDrawing_AllChannels)
  Box(0,0,50, 50, RGBA(255,0,0,255))
  Circle(25, 25, 25, RGBA(0,255,0,255))
  StopDrawing()
  
  x = 0
  move = 10
  iters = 0
  
  Repeat
      
    If x < 10
      move = 4
    EndIf
   
    If x > 340
      move = -2
    EndIf
    
    x = x + move
    iters = iters + 1
    SetGadgetText(0, Str(x) + " " + Str(iters))
    
    ClearScreen(RGB(0,0,255))
    DisplaySprite(0, x, x)
    DisplaySprite(1, x+60, x)
         
    Repeat
    Until WindowEvent() = 0
    FlipBuffers()
    Nap(2)
    
    ExamineKeyboard()
    
    If KeyboardPushed(#PB_Key_Space)
      Nap(2000)
    EndIf
        
  Until KeyboardPushed(#PB_Key_Escape)
 
  End
  
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Time traveling balls.

Post by Mijikai »

I pers. would never ever use more than one WindowEvent function (even if it would work).
You are still missing timing code to ensure the game runs the same across different systems -> u could use SetFrameRate().
Post Reply