Delta timing problem [Solved]

Just starting out? Need help? Post your questions and find answers here.
neumanix
User
User
Posts: 11
Joined: Thu Aug 13, 2009 3:13 pm
Location: Finland

Delta timing problem [Solved]

Post by neumanix »

Hi, hope someone can help me out with this simple(I'm sure) problem.

Why is the square skipping along and not moving smoothly?
I made the same program in Blitzmax using the same technique, and there it's smooth.

Code: Select all

EnableExplicit

InitSprite()
Define.i win=OpenWindow(#PB_Any, 0, 0, 800, 600, "test")
OpenWindowedScreen(WindowID(win), 0, 0, 800, 600, 0, 0, 0, #PB_Screen_NoSynchronization) ; no Vsync
;OpenWindowedScreen(WindowID(win), 0, 0, 800, 600) ; Vsync

Define delta.f
Define frames.i
Define time.f
Define current_time.q
Define old_time.q

Structure sprite
  x.f
  y.f
  gfx.i
  speed.i
EndStructure

Define player_sprite.sprite

player_sprite\gfx=CreateSprite(#PB_Any,32,32)
player_sprite\speed=500

StartDrawing(SpriteOutput(player_sprite\gfx))
  Box(0,0,32,32,RGB(255,255,255))
StopDrawing()

current_time=ElapsedMilliseconds()

Repeat 
  old_time=current_time
  current_time=ElapsedMilliseconds()
  delta=(current_time-old_time)/1000.0
  
  ClearScreen(0)

  If GetAsyncKeyState_(#VK_LEFT)
    player_sprite\x-player_sprite\speed * delta
  EndIf
  
  If GetAsyncKeyState_(#VK_RIGHT)
      player_sprite\x+player_sprite\speed * delta
  EndIf
    
  If GetAsyncKeyState_(#VK_UP)
      player_sprite\y-player_sprite\speed * delta
  EndIf
    
  If GetAsyncKeyState_(#VK_DOWN)
      player_sprite\y+player_sprite\speed * delta
  EndIf
  
  DisplaySprite(player_sprite\gfx, player_sprite\x, player_sprite\y)
    
  frames+1
  time+delta
  
  If time>1
    Debug "FPS: "+Str(frames-1)
    time=0
    frames=0
  EndIf
  
  FlipBuffers()
  
Until WindowEvent()=#PB_Event_CloseWindow Or GetAsyncKeyState_(#VK_ESCAPE)

CloseScreen()
CloseWindow(win)
End
Last edited by neumanix on Wed Aug 07, 2013 8:45 pm, edited 1 time in total.
neumanix
User
User
Posts: 11
Joined: Thu Aug 13, 2009 3:13 pm
Location: Finland

Re: Delta timing problem

Post by neumanix »

I solved it by using timeGetTime_() instead of elapsedmilliseconds(). Working fine now :D
Post Reply