Comments (5)
@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.
@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.
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.
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.
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)
- Trigger Event Support HOT 12
- getRecurringTimerEventWithEventCode causes 'Video subsystem has not been initialised' HOT 15
- Rethink user data and SystemEvents. HOT 1
- Update reflex to the version that includes `tickLosyFromPostBuildTime`. HOT 14
- Fix CI
- Reflex 0.6.1 support
- Turn `app/Main.hs` into a literate haskell tutorial.
- GHC 8.8 support HOT 1
- Failing build plans from reflex-sdl2 0.1.0.0
- GHC 9.2 (and above) support HOT 1
- Docs should link to proper external sources.
- Wrap up more of sdl2's functions.
- Release tag missing from Git repository HOT 4
- Updated nix to pull sdl2 2.3.0
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reflex-sdl2.