Skip to content

Commit

Permalink
Fix femtovg example
Browse files Browse the repository at this point in the history
  • Loading branch information
prokopyl committed Mar 27, 2024
1 parent 999f97d commit e10382d
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions examples/render_femtovg/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use baseview::gl::GlConfig;
use baseview::gl::{GlConfig, GlContext};
use baseview::{
Event, EventStatus, MouseEvent, PhyPoint, Size, Window, WindowEvent, WindowHandler, WindowInfo,
WindowOpenOptions, WindowScalePolicy,
Expand All @@ -7,42 +7,45 @@ use femtovg::renderer::OpenGl;
use femtovg::{Canvas, Color};

struct FemtovgExample {
_window: Window,
gl_context: GlContext,
canvas: Canvas<OpenGl>,
current_size: WindowInfo,
current_mouse_position: PhyPoint,
damaged: bool,
}

impl FemtovgExample {
fn new(window: &mut Window) -> Self {
let context = window.gl_context().unwrap();
unsafe { context.make_current() };
fn new(window: Window) -> Self {
let gl_context = window.gl_context().unwrap();
unsafe { gl_context.make_current() };

let renderer =
unsafe { OpenGl::new_from_function(|s| context.get_proc_address(s)) }.unwrap();
unsafe { OpenGl::new_from_function(|s| gl_context.get_proc_address(s)) }.unwrap();

let mut canvas = Canvas::new(renderer).unwrap();
// TODO: get actual window width
canvas.set_size(512, 512, 1.0);

unsafe { context.make_not_current() };
unsafe { gl_context.make_not_current() };
Self {
canvas,
current_size: WindowInfo::from_logical_size(Size { width: 512.0, height: 512.0 }, 1.0),
current_mouse_position: PhyPoint { x: 256, y: 256 },
damaged: true,
_window: window,
gl_context,
}
}
}

impl WindowHandler for FemtovgExample {
fn on_frame(&mut self, window: &mut Window) {
fn on_frame(&mut self) {
if !self.damaged {
return;
}

let context = window.gl_context().unwrap();
unsafe { context.make_current() };
unsafe { self.gl_context.make_current() };

let screen_height = self.canvas.height();
let screen_width = self.canvas.width();
Expand Down Expand Up @@ -70,12 +73,12 @@ impl WindowHandler for FemtovgExample {

// Tell renderer to execute all drawing commands
self.canvas.flush();
context.swap_buffers();
unsafe { context.make_not_current() };
self.gl_context.swap_buffers();
unsafe { self.gl_context.make_not_current() };
self.damaged = false;
}

fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
fn on_event(&mut self, event: Event) -> EventStatus {
match event {
Event::Window(WindowEvent::Resized(size)) => {
let phy_size = size.physical_size();
Expand Down

0 comments on commit e10382d

Please sign in to comment.