darthdeus / comfy

Comfy is a fun 2D game engine built in Rust. It's designed to be opinionated, productive, and easy to use.
https://comfyengine.org
Apache License 2.0
717 stars 35 forks source link

Update ASSETS_LOADED one frame later so users can skip the placeholder. #84

Open yaymukund opened 8 months ago

yaymukund commented 8 months ago

When textures are rendered, there is a placeholder image that flashes for a single frame before the actual image. Here is an example of how it looks:

Screenshot 2024-01-13 at 5 59 11 PM

Example reproduction:

impl GameLoop for Game {
    fn new(_c: &mut EngineState) -> Self {
        load_multiple_textures(
            // Includes the "monkey" texture.
            crate::constants::TEXTURES
                .iter()
                .map(|(a, b)| (a.to_string(), b.to_string()))
                .collect_vec(),
        );

        clear_background(BLACK);
        Self
    }

    fn update(&mut self, c: &mut EngineContext) {
        if assets_loaded() < assets_queued_total() {
            println!("{}/{} loaded", assets_loaded(), assets_queued_total());
            return;
        }

        draw_sprite(
            texture_id("monkey"),
            vec2(5.0, 0.0),
            WHITE,
            0,
            vec2(2.60, 3.15),
        );
    }
}

Here's my attempt to track it down:

  1. run_early_update_stages() processes the asset queues...
  2. ...and then spawns an async task that does inc_assets_loaded() saying it is "loaded." It is not actually loaded yet.
  3. The renderer update can't find the texture and instead defaults to the "error" texture id, which is very similar to that image I linked above.
  4. Sometime between the renderer update and the next frame, the texture gets loaded. I am still not sure where this hapens.

Fix in this branch. We just put off the rendering by 1 frame.

yaymukund commented 8 months ago

This doesn't seem to fix things for Sprite::new