Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use a canvas outside of EventHandler #1056

Open
nobbele opened this issue Jun 27, 2022 · 2 comments
Open

How to use a canvas outside of EventHandler #1056

nobbele opened this issue Jun 27, 2022 · 2 comments
Labels

Comments

@nobbele
Copy link
Member

nobbele commented Jun 27, 2022

Describe the bug
Canvas::finish returns RenderError("starting Canvas outside of a frame") when used inside your state constructor.

Hardware and Software:

@PSteinhaus
Copy link
Member

Hmm, do we really want to allow to create renderpasses outside of draw?

This led people down weird paths in the past.

But, it can be useful…

Ok, after a bit of searching I realized that this is already possible, if you respect the red tape:

What you need to do to draw outside of draw is, you have to create a new frame yourself and also end it yourself, by calling gfx.begin_frame and gfx.end_frame.

@PSteinhaus PSteinhaus changed the title Can't use canvas outside of EventHandler How to use a canvas outside of EventHandler Oct 18, 2022
@p9436
Copy link

p9436 commented Jan 18, 2024

Here is a small example based on this answer:

impl GameState {
    fn new(ctx: &mut Context) -> ggez::GameResult<GameState> {
        // Create a texture to hold the background
        ctx.gfx.begin_frame()?;
        let mut background_image = Image::new_canvas_image(ctx, ctx.gfx.surface_format(), 100, 100, 1);
        let mut background_canvas= Canvas::from_image(ctx, background_image.clone(), None);
        background_canvas.set_sampler(graphics::Sampler::nearest_clamp());
        draw_background(ctx, &mut background_canvas);
        background_canvas.finish(ctx)?;
        ctx.gfx.end_frame()?;
        let background_image = Some(background_image);

        Ok(GameState {
            background_image,
        })
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants