Page 1 of 1

Trouble with ElapsedMilliseconds()

Posted: Wed Oct 14, 2015 11:06 am
by Vera
Hello,
I simply don't understand ElapsedMilliseconds() :?

I am trying to develope a small fun prog wherein a number of moving sprites could be hovered and during this event should be allowed to be clicked to start the follow-up gimmik.

Well at first I had tried to implement ExamineMouse() to be only active during this hoverevent but I couldn't exclude the later called ReleaseMouse() being already processed and thus corrupting ExamineMouse().

So I moved to a different way to handle it hoping I could catch the duration time while the mouse is continuously hovering over a targeted sprite. And if a certain amount of time is tresspassed it should trigger that follow-up gimmik.

But nothing I've tried would work reliable ... only just occasionally.
Can you give me a hand please how I could determine the duration time of a hover event?

greets ~ Vera

Code: Select all

InitSprite()
InitKeyboard()

OpenWindow(0, 0, 0, 300, 200, "hover duration ?", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(0, 20, 0, 45, 20, "Quit")
OpenWindowedScreen(WindowID(0), 0, 20, 300, 180, 0, 0, 0)

CreateSprite(0, 20, 20)
If StartDrawing(SpriteOutput(0))
  Box(5, 5, 20, 20, RGB(255, 0, 155))
  StopDrawing()
EndIf

Repeat

  Repeat
    Event = WindowEvent()

    Select Event
      Case #PB_Event_Gadget
        If EventGadget() = 0
          End
        EndIf
      Case #PB_Event_CloseWindow
        Exit = #True
    EndSelect
  Until Event = 0

  FlipBuffers()
  ClearScreen(RGB(0, 0, 0))
  DisplaySprite(0, 50, 50)

  startTime = ElapsedMilliseconds() ;: Debug startTime

  MouseX = WindowMouseX(0)
  MouseY = WindowMouseY(0)
  If MouseX > 50 And MouseX < 70 And MouseY > 50 + 20 And MouseY < 70 + 20

    ; Debug "hover box"

    Repeat
      Delay(1)
      ; Debug ElapsedMilliseconds() - startTime
      If ElapsedMilliseconds() - startTime > 10

        Debug "duration reached"
        Debug ElapsedMilliseconds() - startTime
      Else
        stop = 1
      EndIf
    Until stop


  EndIf

  ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape) Or Exit

Re: Trouble with ElapsedMilliseconds()

Posted: Wed Oct 14, 2015 11:19 am
by Keya
i cant run it (the Sprite stuff doesnt seem to like my XP, too old directx/opengl or something i guess), so im just stabbing in the dark here sorry! but im just wondering if the problem is in the innermost loop:

Code: Select all

    Repeat
      Delay(1)
      ; Debug ElapsedMilliseconds() - startTime
      If ElapsedMilliseconds() - startTime > 10
        Debug "duration reached"
        Debug ElapsedMilliseconds() - startTime
      Else
        stop = 1
      EndIf
    Until stop
So the first time, lets just assume Delay(1) results in exactly a 1ms delay, you're asking "If 1 > 10", which it isnt, so you're setting stop=1 straight away and exiting the loop before "duration reached" ?

The other thing that might be a problem is that you're checking the mouse co-ords, and then going into that check-for-hover loop, but never again actually checking if the mouse co-ords are within the hover area?

wish i could test though, i might just be wasting your time!

ps. if this is for Windows-only you might be better off reacting to dialog messages to detect the hover (WM_MOUSEHOVER? WM_MOUSEMOVE?) then Windows will be telling you exactly when, rather than you having to sit in a loop constantly polling

Re: Trouble with ElapsedMilliseconds()

Posted: Wed Oct 14, 2015 12:48 pm
by kenmo
Keya is right, this check will fail (probably) every time, since you update startTime a few lines of code above this.

Code: Select all

If ElapsedMilliseconds() - startTime > 10
Also I wouldn't recommend putting a sub-loop in there to handle hovering, because
(a) you don't handle window events in the sub-loop
(b) the rest of the program "freezes" while hovering
(c) ... more reasons?

How about something like this?

Code: Select all

InitSprite()
OpenWindow(0, 0, 0, 300, 200, "hover", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0))

Structure ColorBoxStruct
  x.i
  y.i
  w.i
  h.i
  c.i
EndStructure

#N = 10
Dim ColorBox.ColorBoxStruct(#N-1)

For i = 0 To #N-1
  ColorBox(i)\x = Random(WindowWidth(0))
  ColorBox(i)\y = Random(WindowHeight(0))
  ColorBox(i)\w = Random(30, 10)
  ColorBox(i)\h = Random(30, 10)
  ColorBox(i)\c = Random($FFFF) + $FF0000
Next i

HoverBox.i = -1
#HoverTimeout = 1000







Repeat
  
  ; Handle window events
  Event = WindowEvent()
  While Event
    If (Event = #PB_Event_CloseWindow)
      Exit = #True
    EndIf
    Event = WindowEvent()
  Wend
  
  ; Handle "game"
  i = Random(#N-1)
  ColorBox(i)\x + (Random(2) - 1)
  i = Random(#N-1)
  ColorBox(i)\y + (Random(2) - 1)
  
  mx = WindowMouseX(0)
  my = WindowMouseY(0)
  PreviousHover = HoverBox
  HoverBox = -1
  For i = 0 To #N-1
    With ColorBox(i)
      If (mx >= \x) And (mx < \x + \w)
        If (my >= \y) And (my < \y + \h)
          HoverBox = i
          Break
        EndIf
      EndIf
    EndWith
  Next i
  If (HoverBox >= 0)
    If (HoverBox <> PreviousHover)
      HoverStart = ElapsedMilliseconds()
      HoverDone = #False
    EndIf
    If (ElapsedMilliseconds() - HoverStart >= #HoverTimeout)
      If (Not HoverDone)
        MessageRequester("HOVER", Str(#HoverTimeout) + " ms")
        HoverDone = #True
      EndIf
    EndIf
  EndIf
  
  ; Draw screen
  ClearScreen($FFFFFF)
  If StartDrawing(ScreenOutput())
    For i = #N-1 To 0 Step -1
      With ColorBox(i)
        If (i = HoverBox)
          Box(\x - 2, \y - 2, \w + 4, \h + 4, $00FF00)
        EndIf
        Box(\x, \y, \w, \h, \c)
      EndWith
    Next i
    StopDrawing()
  EndIf
  FlipBuffers()
  
  Delay(1)
Until Exit

Re: Trouble with ElapsedMilliseconds()

Posted: Wed Oct 14, 2015 1:12 pm
by Vera
ups - I was just about to reply :
Thanks Keya ~ good suggestions :-)

I tried to implement it somehow but am running from deadlock to deadlock.
Here's where I am at the moment. It either doesn't commence counting the time or doesn't interrupt until the duration limit is reached. ....
when kenmo crossed my submit ;-)

w0w ~ kenmo
that works straight forward and already comes very close to where I am heading
Thanks for that :-)

I don't grasp at first sight what you're exactely doing but I'll sure understand when I dig into it.

Yes, I too already observed unwanted sideeffects with unusual delayed reactions on WindowEvents and the freezing I had overcome already ;-)