|
| 1 | +use baseview::dpi::{LogicalSize, PhysicalPosition, PhysicalSize}; |
| 2 | +use baseview::gl::{GlConfig, GlContext}; |
| 3 | +use baseview::{ |
| 4 | + DamageArea, Event, EventStatus, HandlerError, Window, WindowContext, WindowHandler, |
| 5 | + WindowSettings, WindowSize, |
| 6 | +}; |
| 7 | +use color::{Hsl, OpaqueColor}; |
| 8 | +use femtovg::renderer::OpenGl; |
| 9 | +use femtovg::{Canvas, Color}; |
| 10 | +use std::cell::{Cell, RefCell}; |
| 11 | + |
| 12 | +struct FemtovgExample { |
| 13 | + gl_context: GlContext, |
| 14 | + canvas: RefCell<Canvas<OpenGl>>, |
| 15 | + damage_handler: DamageHandler, |
| 16 | +} |
| 17 | + |
| 18 | +impl FemtovgExample { |
| 19 | + fn new(window_context: WindowContext) -> Result<Self, HandlerError> { |
| 20 | + let Some(gl_context) = window_context.gl_context() else { unreachable!() }; |
| 21 | + unsafe { gl_context.make_current()? }; |
| 22 | + |
| 23 | + let renderer = |
| 24 | + unsafe { OpenGl::new_from_function_cstr(|s| gl_context.get_proc_address(s)) }?; |
| 25 | + |
| 26 | + let mut canvas = Canvas::new(renderer)?; |
| 27 | + let size = window_context.size(); |
| 28 | + |
| 29 | + canvas.set_size(size.physical.width, size.physical.height, size.scale_factor as f32); |
| 30 | + |
| 31 | + unsafe { gl_context.make_not_current()? }; |
| 32 | + Ok(Self { gl_context, canvas: canvas.into(), damage_handler: DamageHandler::new() }) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl WindowHandler for FemtovgExample { |
| 37 | + fn draw(&self) -> Result<(), HandlerError> { |
| 38 | + let context = &self.gl_context; |
| 39 | + unsafe { context.make_current()? }; |
| 40 | + |
| 41 | + let mut canvas = self.canvas.borrow_mut(); |
| 42 | + |
| 43 | + let screen_height = canvas.height(); |
| 44 | + let screen_width = canvas.width(); |
| 45 | + |
| 46 | + let color_of_the_day = new_random_color(); |
| 47 | + |
| 48 | + if self.damage_handler.is_full_screen_clear() { |
| 49 | + canvas.clear_rect(0, 0, screen_width, screen_height, color_of_the_day); |
| 50 | + } else { |
| 51 | + while let Some((pos, size)) = self.damage_handler.next_damage() { |
| 52 | + dbg!((pos, size)); |
| 53 | + canvas.clear_rect(pos.x, pos.y, size.width, size.height, color_of_the_day); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + self.damage_handler.clear(); |
| 58 | + |
| 59 | + // Tell renderer to execute all drawing commands |
| 60 | + canvas.flush(); |
| 61 | + context.swap_buffers()?; |
| 62 | + unsafe { context.make_not_current()? }; |
| 63 | + |
| 64 | + Ok(()) |
| 65 | + } |
| 66 | + |
| 67 | + fn damage(&self, area: DamageArea) { |
| 68 | + eprintln!("Damaged: {area:?}"); |
| 69 | + match area { |
| 70 | + DamageArea::FullWindow => self.damage_handler.set_full_screen_clear(), |
| 71 | + DamageArea::Rect { position, size } => { |
| 72 | + self.damage_handler.add_damaged_rect(position, size) |
| 73 | + } |
| 74 | + _ => {} |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + fn resized(&self, new_size: WindowSize) -> Result<(), HandlerError> { |
| 79 | + let size = new_size.physical; |
| 80 | + self.canvas.borrow_mut().set_size(size.width, size.height, new_size.scale_factor as f32); |
| 81 | + |
| 82 | + Ok(()) |
| 83 | + } |
| 84 | + |
| 85 | + fn on_event(&self, _event: Event) -> EventStatus { |
| 86 | + EventStatus::Ignored |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +fn main() -> Result<(), baseview::Error> { |
| 91 | + unsafe { baseview::assume_standalone_in_process() }; |
| 92 | + |
| 93 | + let window_open_options = WindowSettings::new() |
| 94 | + .with_title("Baseview Partial redraw example") |
| 95 | + .with_size(LogicalSize::new(512, 512)) |
| 96 | + .with_gl_config(GlConfig { alpha_bits: 8, ..GlConfig::default() }); |
| 97 | + |
| 98 | + let window = Window::create(window_open_options, |ctx| FemtovgExample::new(ctx))?; |
| 99 | + |
| 100 | + window.run_until_closed()?; |
| 101 | + Ok(()) |
| 102 | +} |
| 103 | + |
| 104 | +fn new_random_color() -> Color { |
| 105 | + let hue = rand::random_range(0.0..360.0); |
| 106 | + let hsv_color = OpaqueColor::<Hsl>::new([hue, 100.0, 15.0]); |
| 107 | + let color = hsv_color.to_rgba8(); |
| 108 | + |
| 109 | + Color::rgb(color.r, color.g, color.b) |
| 110 | +} |
| 111 | + |
| 112 | +struct DamageHandler { |
| 113 | + full_screen: Cell<bool>, |
| 114 | + damaged_rects: RefCell<Vec<(PhysicalPosition<u32>, PhysicalSize<u32>)>>, |
| 115 | +} |
| 116 | + |
| 117 | +impl DamageHandler { |
| 118 | + fn new() -> Self { |
| 119 | + Self { full_screen: false.into(), damaged_rects: RefCell::new(Vec::with_capacity(8)) } |
| 120 | + } |
| 121 | + |
| 122 | + fn is_full_screen_clear(&self) -> bool { |
| 123 | + self.full_screen.get() |
| 124 | + } |
| 125 | + |
| 126 | + fn set_full_screen_clear(&self) { |
| 127 | + self.full_screen.set(true) |
| 128 | + } |
| 129 | + |
| 130 | + fn clear(&self) { |
| 131 | + self.full_screen.set(false); |
| 132 | + self.damaged_rects.borrow_mut().clear(); |
| 133 | + } |
| 134 | + |
| 135 | + fn next_damage(&self) -> Option<(PhysicalPosition<u32>, PhysicalSize<u32>)> { |
| 136 | + self.damaged_rects.borrow_mut().pop() |
| 137 | + } |
| 138 | + |
| 139 | + fn add_damaged_rect(&self, position: PhysicalPosition<u32>, size: PhysicalSize<u32>) { |
| 140 | + self.damaged_rects.borrow_mut().push((position, size)); |
| 141 | + } |
| 142 | +} |
0 commit comments