// pathfinder/examples/canvas_minimal/src/main.rs // // Copyright © 2019 The Pathfinder Project Developers. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. use pathfinder_canvas::{Canvas, CanvasFontContext, Path2D}; use pathfinder_color::ColorF; use pathfinder_geometry::rect::RectF; use pathfinder_geometry::vector::{vec2f, vec2i}; use pathfinder_gl::{GLDevice, GLVersion}; use pathfinder_renderer::concurrent::rayon::RayonExecutor; use pathfinder_renderer::concurrent::scene_proxy::SceneProxy; use pathfinder_renderer::gpu::options::{DestFramebuffer, RendererOptions}; use pathfinder_renderer::gpu::renderer::Renderer; use pathfinder_renderer::options::BuildOptions; use pathfinder_resources::embedded::EmbeddedResourceLoader; use sdl2::event::Event; use sdl2::keyboard::Keycode; use sdl2::video::GLProfile; fn main() { // Set up SDL2. let sdl_context = sdl2::init().unwrap(); let video = sdl_context.video().unwrap(); // Make sure we have at least a GL 3.0 context. Pathfinder requires this. let gl_attributes = video.gl_attr(); gl_attributes.set_context_profile(GLProfile::Core); gl_attributes.set_context_version(3, 3); // Open a window. let window_size = vec2i(640, 480); let window = video.window("Minimal example", window_size.x() as u32, window_size.y() as u32) .opengl() .build() .unwrap(); // Create the GL context, and make it current. let gl_context = window.gl_create_context().unwrap(); gl::load_with(|name| video.gl_get_proc_address(name) as *const _); window.gl_make_current(&gl_context).unwrap(); // Create a Pathfinder renderer. let mut renderer = Renderer::new(GLDevice::new(GLVersion::GL3, 0), &EmbeddedResourceLoader::new(), DestFramebuffer::full_window(window_size), RendererOptions { background_color: Some(ColorF::white()) }); // Make a canvas. We're going to draw a house. let font_context = CanvasFontContext::from_system_source(); let mut canvas = Canvas::new(window_size.to_f32()).get_context_2d(font_context); // Set line width. canvas.set_line_width(10.0); // Draw walls. canvas.stroke_rect(RectF::new(vec2f(75.0, 140.0), vec2f(150.0, 110.0))); // Draw door. canvas.fill_rect(RectF::new(vec2f(130.0, 190.0), vec2f(40.0, 60.0))); // Draw roof. let mut path = Path2D::new(); path.move_to(vec2f(50.0, 140.0)); path.line_to(vec2f(150.0, 60.0)); path.line_to(vec2f(250.0, 140.0)); path.close_path(); canvas.stroke_path(path); // Render the canvas to screen. let scene = SceneProxy::from_scene(canvas.into_canvas().into_scene(), RayonExecutor); scene.build_and_render(&mut renderer, BuildOptions::default()); window.gl_swap_window(); // Wait for a keypress. let mut event_pump = sdl_context.event_pump().unwrap(); loop { match event_pump.wait_event() { Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => return, _ => {} } } }