Speedy2D icon indicating copy to clipboard operation
Speedy2D copied to clipboard

Getting an Instance of Graphics2D without Window

Open johannes-adr opened this issue 2 years ago • 2 comments

Hello people,

I need a way to get a Graphics2D object without windows for my application. I want to further process the image buffer itself using the built-in caputer function. Is this possible? If yes, how would it work?

    let mut renderer: GLRenderer = todo!();
    renderer.draw_frame(|graphics| {
        graphics.clear_screen(Color::WHITE);
        graphics.draw_circle((100.0, 100.0), 75.0, Color::BLUE);
        let img = graphics.capture(image::ImageDataType::RGB);
        // ...
    });

To be precise with my needs, i want to implement a hardware accelerated drawing backend for my plotting library. It should draw (with help of this library) and save the result to a bitmap file.

johannes-adr avatar Mar 02 '23 19:03 johannes-adr

Thanks for the question! Here's an example from the test framework using Glutin on Linux:

    let context_builder = glutin::ContextBuilder::new()
        .with_gl_debug_flag(true)
        .with_multisampling(0)
        .with_gl(glutin::GlRequest::Specific(glutin::Api::OpenGl, (2, 0)));

   let context = context_builder
        .with_vsync(false)
        .build_headless(&event_loop, PhysicalSize::new(width, height))
        .unwrap();

    let context = unsafe { context.make_current().unwrap() };

    let mut renderer = unsafe {
        GLRenderer::new_for_gl_context((width, height), |name| {
            context.get_proc_address(name) as *const _
        })
        .unwrap()
    };

I think this only works on Linux, so you may need to use something other than build_headless on Mac and Windows.

QuantumBadger avatar Apr 05 '23 17:04 QuantumBadger

@QuantumBadger I don't want to be rude but If your snippet was supposed to work on linux then it isn't working. Mainly because glutin::ContextBuilder doesn't exist.

bambooCZ avatar Nov 30 '24 18:11 bambooCZ