Rendering

<
>
December 13, 2020

I was able to reuse a lot of the render code of Blaze3D (using the glutin and gl crates) and finally managed to render a Blueprint with it. Every Structure is rendered as a rectangle.
I also ported the text rendering from Blaze3D since this will be required later on and tested it by displaying a ‘Hello World’.
Looking like this:

With the following Blueprint definition:

<<<<<<<   
      a   
c>>>>eFE<<
         ^
))))>v   ^
     v   ^
     U   a
     >>)>>

As you might haved noticed: The rendering is currently flipped compared to the definition. I’ll have to fix that in the future.
The first version of the rendering code where the Blueprint is stepped every frame looks like this:

let el = glutin::event_loop::EventLoop::new();

let title = "Factor Y";

let wb = glutin::window::WindowBuilder::new()
    .with_title(title)
    .with_min_inner_size(glutin::dpi::PhysicalSize::new(
        WINDOW_SIZE[0],
        WINDOW_SIZE[1],
    ))
    .with_inner_size(glutin::dpi::PhysicalSize::new(
        MIN_WINDOW_SIZE[0],
        MIN_WINDOW_SIZE[1],
    ));

let windowed_context = glutin::ContextBuilder::new()
    .with_gl_profile(glutin::GlProfile::Core)
    .with_gl(glutin::GlRequest::Specific(glutin::Api::OpenGl, (3, 3)))
    .build_windowed(wb, &el)
    .unwrap();

let windowed_context = Rc::new(unsafe { windowed_context.make_current() }.unwrap());

gl::load_with(|symbol| windowed_context.get_proc_address(symbol) as *const _);

unsafe {
    gl::Enable(gl::DEPTH_TEST);
    gl::Enable(gl::BLEND);
    gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);
    gl::ClearColor(0.1, 0.1, 0.1, 1.0);
}

let font_renderer = Rc::new(FontRenderer::new(
    font,
    windowed_context.window().scale_factor(),
    FONT_SIZE,
    CACHE_GLYPH_SIZE,
    DEPTH_FONT,
));

el.run(move |_event, _, control_flow| {
    let w = windowed_context.window();
    let sf = w.scale_factor();
    let ps = w.inner_size();

    unsafe {
        gl::Clear(gl::COLOR_BUFFER_BIT);
        gl::Clear(gl::DEPTH_BUFFER_BIT);
    }

    bp.step();

    render(ps.width, ps.height, &bp);

    unsafe { gl::Viewport(0, 0, ps.width as i32, ps.height as i32) }

    font_renderer.render(
        sf,
        [ps.width as usize, ps.height as usize],
        "Hello world",
        true,
        true,
    );

    windowed_context.swap_buffers().unwrap();

    if *control_flow != glutin::event_loop::ControlFlow::Exit {
        *control_flow =
            glutin::event_loop::ControlFlow::WaitUntil(std::time::Instant::now() + FRAME_WAIT);
    }
});