QuantumBadger / Speedy2D

Rust library for hardware accelerated drawing of 2D shapes, images, and text, with an easy to use API.
Apache License 2.0
386 stars 42 forks source link

Getting an Instance of Graphics2D without Window #75

Open johannes-adr opened 1 year ago

johannes-adr commented 1 year ago

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.

QuantumBadger commented 1 year ago

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.