use std::num::NonZeroU32;
use softbuffer::{Context, Surface};
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
use skia_safe::{surfaces,Color, Paint,PaintStyle, Font, Typeface, FontStyle, Data};
fn main(){
let event_loop = EventLoop::new();
// event_loop.set_control_flow(ControlFlow::Wait);
let window = WindowBuilder::new().build(&event_loop).unwrap();
let context = unsafe { Context::new(&window) }.unwrap();
let mut surface = unsafe { Surface::new(&context, &window) }.unwrap();
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::RedrawRequested(window_id) if window_id == window.id() => {
let (width, height) = {
let size = window.inner_size();
(size.width, size.height)
};
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();
let mut visible_buffer = surfaces::raster_n32_premul((width as i32, height as i32)).expect("surface");
visible_buffer.canvas().clear(Color::WHITE);
let mut paint = Paint::default();
paint.set_color(Color::BLUE);
// paint.set_color(Color::from_rgb(55, 250, 80));
// paint.set_color(Color::from_argb(255, 0, 0,255));
paint.set_anti_alias(true);
paint.set_style(PaintStyle::Fill);
visible_buffer.canvas().draw_circle(((width/2) as f32, (height/2) as f32),100.0 , &paint);
let mut fpaint = Paint::default();
// fpaint.set_color(Color::from_rgb(80, 150, 250));
fpaint.set_color(Color::RED);
// fpaint.set_color(Color::from_argb(255, 0, 200,0));
fpaint.set_anti_alias(true);
fpaint.set_style(PaintStyle::Fill);
let font_raw = std::fs::read("src/fonts/ColombiaItalic-BWGZB.ttf").unwrap();
let font_data = unsafe { Data::new_bytes(font_raw.as_slice()) };
let font = Font::from_typeface(Typeface::from_data(font_data, None).unwrap(), 30.0);
visible_buffer.canvas().draw_str("my string", ((width/2) as f32, (height/2) as f32), &font, &fpaint);
let pixmap = visible_buffer.canvas().peek_pixels().unwrap().bytes().unwrap();
let mut buffer = surface.buffer_mut().unwrap();
for index in 0..(width * height) as usize {
buffer[index] = pixmap[index * 4 + 2] as u32
| (pixmap[index * 4 + 1] as u32) << 8
| (pixmap[index * 4] as u32) << 16;
}
buffer.present().unwrap();
}
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => control_flow.set_exit(),
// WindowEvent::CursorMoved { position,device_id , modifiers }
// => {}
_ => {}
}_ => {}
};
});
}
A window is supposed to appear containing a blue circle with red text inside it, but what happens is that the circle is red and the text is blue.