| Crates.io | emscripten_main_loop |
| lib.rs | emscripten_main_loop |
| version | 0.1.1 |
| created_at | 2020-10-02 05:14:07.721088+00 |
| updated_at | 2020-10-02 05:27:24.396357+00 |
| description | Main loop abstraction that works for both native and Emscripten builds. Gets around the issue of freezing up browser tabs when a naive main loop is used. |
| homepage | |
| repository | https://github.com/therocode/rust_emscripten_main_loop |
| max_upload_size | |
| id | 295255 |
| size | 11,432 |
Many interactive applications such as games will utilise the technique of having a main loop. This loop will represent one "tick" of the simulation and often also capture input and render output. However, the typical implementation of this technique (simple loop/while statement) breaks down when targeting Emscripten since naive looping will cause the browser tab to freeze up. This library provides a simple trait to use as a replacement for that loop statement and will make sure that the looping is happening in the way that Emscripten needs it to.
Add emscripten_main_loop as a dependency to your Cargo.toml.
Implement the emscripten_main_loop::MainLoop trait for your application object that contains all the data that should be accessible to the main loop. The trait will require you to implement the main_loop function which is where you are meant to put your looping logic. main_loop will be called once per loop iteration and must return either MainLoopEvent::Continue or MainLoopEvent::Terminate as appropriate.
When the trait is implemented, you can invoke the looping by passing your data object to emscripten_main_loop::run(). See the documentation for further information, or check out this example project as a reference, which uses this library.
For a comprehensive guide on how to use Emscripten to build a project using Rust+SDL2+OpenGL for the web, check out my blog post.
Feel free to raise issues or pull requests as desired, contributions would be appreciated.