Git Product home page Git Product logo

Comments (5)

schell avatar schell commented on August 15, 2024

@dalaing - I've added the ability to get timer events, the details are here on the host side and here on the guest side.

It works well enough but it eats quite a bit of cpu (~120%). Oddly enough, when there are lots of sdl events the cpu use falls down to ~13%. I haven't done any profiling yet to see which functions spend the most time on top of the call stack but maybe you have some simpler ideas about how to accomplish these timers?

Previously I had implemented them with getAsyncEvent but those fire on another thread, which makes them incompatible with GL calls. &shrug;

from reflex-sdl2.

schell avatar schell commented on August 15, 2024

@ocharles or @polarina - do you guys have any novel ideas of how to create a recurring timer event? Is this appropriate for the registerEvent/UserDataEvent pipeline? My hope is that I can use waitEvent in my main loop and then use delayThread in another thread to manage the timing delay, and push a user defined "timer event" to wake the main sdl loop.

from reflex-sdl2.

schell avatar schell commented on August 15, 2024

This works pretty well, actually:

--------------------------------------------------------------------------------
data TimerData = TimerData { timerDataCode      :: Int32
                           , timerDataTimestamp :: Timestamp
                           }


toTimerData :: Int32 -> RegisteredEventData -> Timestamp -> IO (Maybe TimerData)
toTimerData eventCode rdat ts
  | eventCode == registeredEventCode rdat =
    return $ Just $ TimerData eventCode ts
  | otherwise = return Nothing


fromTimerData :: TimerData -> IO RegisteredEventData
fromTimerData (TimerData code _) =
  return $ emptyRegisteredEvent{ registeredEventCode = code }


getRecurringTimerEventWithEventCode
  :: ReflexSDL2 r t m => Int32 -> Int -> m (Event t ())
getRecurringTimerEventWithEventCode eventCode millis = do
  -- Register the timer event as a user event so it will wake `waitEvent` when
  -- pushed into the queue.
  let toData = toTimerData eventCode
  mayReg <- registerEvent toData fromTimerData
  case mayReg of
    Nothing -> return ()
    Just (RegisteredEventType push _) -> liftIO $ void $ async $ fix $ \loop -> do
      threadDelay $ millis * 1000
      ts <- ticks
      push (TimerData eventCode ts) >>= \case
        EventPushSuccess   -> return ()
        EventPushFiltered  -> putStrLn "push filtered"
        EventPushFailure t -> print t
      loop
  -- Filter the user event to only fire when the incoming code matches.
  evUser <- getUserEvent
  return $ fmapMaybe (guard . (eventCode ==) . userEventCode) evUser

from reflex-sdl2.

schell avatar schell commented on August 15, 2024

I may attempt to use this pattern with async events as well, since it would be great if those come back and fire on the main thread. Doing this would also eliminate all the MonadReflexCreateTrigger and TriggerEvent constraints along with the associated housekeeping.

from reflex-sdl2.

schell avatar schell commented on August 15, 2024

Kk, I think this is done - I've used this pattern for async events as well and it seems to work!

from reflex-sdl2.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.