Created
March 24, 2023 03:21
-
-
Save randrews/d01b27fa657c97bafbc4e466027ab50c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::f32::consts::PI; | |
use pixels::{Pixels, PixelsBuilder}; | |
use pixels::wgpu::TextureFormat; | |
use raqote::{DrawOptions, DrawTarget, Path, PathBuilder, SolidSource, Source}; | |
use winit::dpi::LogicalSize; | |
use winit::event::{Event, WindowEvent}; | |
use winit::event_loop::{ControlFlow, EventLoop}; | |
use winit::window::WindowBuilder; | |
fn main() -> ! { | |
// Make an event loop and a window | |
// (start up winit, in other words) | |
let mut event_loop = EventLoop::new(); | |
let window = WindowBuilder::new() | |
.with_inner_size(LogicalSize { width: 640, height: 640 }) | |
.with_title("Bouncy!").build(&event_loop) | |
.expect("Failed to create window! :("); | |
// Create a raqote draw target | |
// This is what we'll draw geometry on | |
let mut draw_target = DrawTarget::new(640, 640); | |
// Create a surface texture for pixels to render | |
let mut texture = pixels::SurfaceTexture::new( | |
(640.0 * window.scale_factor()) as u32, | |
(640.0 * window.scale_factor()) as u32, | |
&window); | |
// Create a pixels, specifying that the texture it's rendering will be in bgra / srgb | |
// because that's what raqote's draw_target will be in | |
let mut pixels = PixelsBuilder::new(640, 640, texture) | |
.texture_format(TextureFormat::Bgra8UnormSrgb) | |
.build().expect("Failed to open drawing context D:"); | |
// Fill the draw target with clear and slap a red circle on it | |
draw_target.clear(SolidSource::from_unpremultiplied_argb(0xff, 0x20, 0xa0, 0xb0)); | |
let mut pb = PathBuilder::new(); | |
pb.arc(100.0, 100.0, 30.0, 0.0, PI * 2.0); | |
draw_target.fill(&pb.finish(), | |
&Source::Solid(SolidSource::from_unpremultiplied_argb(0xff, 0xc0, 0, 0)), | |
&DrawOptions::new()); | |
// Copy the raqote target to the pixels buffer and thence to the GPU with render | |
pixels.frame_mut().copy_from_slice(draw_target.get_data_u8()); | |
pixels.render().expect("Failed to render frame :O"); | |
// Run an event loop that just waits for a CloseRequested and then bails. | |
event_loop.run(|event, target, control_flow| { | |
match event { | |
Event::WindowEvent { event, .. } => { | |
match event { | |
WindowEvent::Resized(_) => {} | |
WindowEvent::CloseRequested => { *control_flow = ControlFlow::ExitWithCode(0) } | |
WindowEvent::KeyboardInput { .. } => {} | |
WindowEvent::CursorMoved { .. } => {} | |
WindowEvent::CursorEntered { .. } => {} | |
WindowEvent::CursorLeft { .. } => {} | |
WindowEvent::MouseWheel { .. } => {} | |
WindowEvent::MouseInput { .. } => {} | |
_ => {} | |
} | |
} | |
_ => {} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment