Hello, I apologize for the many questions lately, I am a bit of a beginner.
When I run the script:
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, Color4f, Paint,PaintStyle, Font, Typeface, FontStyle, Data, FontMgr, ColorType, ImageInfo, AlphaType, ColorSpace, SurfaceProps, SurfacePropsFlags,PixelGeometry};
use std::fs::{File, read};
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();
// Set pixel's color variables
let mut last_pixel = Color4f{r:0.0 , g:0.0 , b:0.0 , a:0.0};
let mut _current_pixel = Color4f{r:0.0 , g:0.0 , b:0.0 , a:0.0};
let (width, height) = {
let size = window.inner_size();
(size.width, size.height)
};
let ii = ImageInfo::new((width as i32,height as i32), ColorType::RGBA8888, AlphaType::Premul, ColorSpace::new_srgb());
let mut visible_buffer = surfaces::raster(&ii,(0) as usize, Some(&SurfaceProps::default())).expect("surface");
// let mut pixmap = visible_buffer.canvas().peek_pixels().unwrap().bytes().unwrap();
// Run event loop
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
// When window opened or changed its size, redraw its graphics content
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 ii = ImageInfo::new((width as i32,height as i32), ColorType::RGBA8888, AlphaType::Premul, ColorSpace::new_srgb());
let mut visible_buffer = surfaces::raster(&ii,(0) as usize, Some(&SurfaceProps::default())).expect("surface");
visible_buffer.canvas().clear(Color::WHITE);
let mut paint = Paint::default();
paint.set_color(Color::BLUE);
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::RED);
fpaint.set_style(PaintStyle::Fill);
// let font = Font::from_typeface(Typeface::from_name("Times", FontStyle::bold_italic()).unwrap(), 20.0);
// let font_mgr = FontMgr::new();
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("string works??", ((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 {
// Close the software when window closed
WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::CursorMoved { position,device_id , modifiers }
=> {
let x = position.x as i32;
let y = position.y as i32;
// thread 'main' panicked at 'assertion failed: p.x >= 0 && p.x < self.width()'
_current_pixel = visible_buffer.canvas().peek_pixels().unwrap().get_color_4f((x,y));
if last_pixel != _current_pixel {
last_pixel = _current_pixel;
println!("mouse pixel: {last_pixel:?}")
};
}
_ => {}
}_ => {}
};
});
}
I get an error message in the terminal:
thread 'main' panicked at 'assertion failed: p.x >= 0 && p.x < self.width()'
I think the problem is caused by this part of the script:
let x = position.x as i32;
let y = position.y as i32;
// thread 'main' panicked at 'assertion failed: p.x >= 0 && p.x < self.width()'
_current_pixel = visible_buffer.canvas().peek_pixels().unwrap().get_color_4f((x,y));
if last_pixel != _current_pixel {
last_pixel = _current_pixel;
println!("mouse pixel: {last_pixel:?}")
};
If there are any improvements or any other possible errors, I would really appreciate it if you let me know.