Skip to content

Commit 70e7af6

Browse files
authored
Add a new example for OpenGL setup using femtovg (#176)
1 parent f5ae585 commit 70e7af6

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ uuid = { version = "0.8", features = ["v4"] }
4040
[dev-dependencies]
4141
rtrb = "0.2"
4242
softbuffer = "0.3.4"
43+
44+
[workspace]
45+
members = ["examples/render_femtovg"]

examples/render_femtovg/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "render_femtovg"
3+
version = "0.1.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[dependencies]
8+
baseview = { path = "../..", features = ["opengl"] }
9+
femtovg = "0.9.0"

examples/render_femtovg/src/main.rs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
use baseview::gl::GlConfig;
2+
use baseview::{
3+
Event, EventStatus, MouseEvent, PhyPoint, Size, Window, WindowEvent, WindowHandler, WindowInfo,
4+
WindowOpenOptions, WindowScalePolicy,
5+
};
6+
use femtovg::renderer::OpenGl;
7+
use femtovg::{Canvas, Color};
8+
9+
struct FemtovgExample {
10+
canvas: Canvas<OpenGl>,
11+
current_size: WindowInfo,
12+
current_mouse_position: PhyPoint,
13+
damaged: bool,
14+
}
15+
16+
impl FemtovgExample {
17+
fn new(window: &mut Window) -> Self {
18+
let context = window.gl_context().unwrap();
19+
unsafe { context.make_current() };
20+
21+
let renderer =
22+
unsafe { OpenGl::new_from_function(|s| context.get_proc_address(s)) }.unwrap();
23+
24+
let mut canvas = Canvas::new(renderer).unwrap();
25+
// TODO: get actual window width
26+
canvas.set_size(512, 512, 1.0);
27+
28+
unsafe { context.make_not_current() };
29+
Self {
30+
canvas,
31+
current_size: WindowInfo::from_logical_size(Size { width: 512.0, height: 512.0 }, 1.0),
32+
current_mouse_position: PhyPoint { x: 256, y: 256 },
33+
damaged: true,
34+
}
35+
}
36+
}
37+
38+
impl WindowHandler for FemtovgExample {
39+
fn on_frame(&mut self, window: &mut Window) {
40+
if !self.damaged {
41+
return;
42+
}
43+
44+
let context = window.gl_context().unwrap();
45+
unsafe { context.make_current() };
46+
47+
let screen_height = self.canvas.height();
48+
let screen_width = self.canvas.width();
49+
50+
// Clear
51+
self.canvas.clear_rect(0, 0, screen_width, screen_height, Color::rgb(0xAA, 0xAA, 0xAA));
52+
53+
// Make big blue rectangle
54+
self.canvas.clear_rect(
55+
(screen_width as f32 * 0.1).floor() as u32,
56+
(screen_height as f32 * 0.1).floor() as u32,
57+
(screen_width as f32 * 0.8).floor() as u32,
58+
(screen_height as f32 * 0.8).floor() as u32,
59+
Color::rgbf(0., 0.3, 0.9),
60+
);
61+
62+
// Make smol orange rectangle
63+
self.canvas.clear_rect(
64+
(self.current_mouse_position.x - 15).clamp(0, screen_width as i32 - 30) as u32,
65+
(self.current_mouse_position.y - 15).clamp(0, screen_height as i32 - 30) as u32,
66+
30,
67+
30,
68+
Color::rgbf(0.9, 0.3, 0.),
69+
);
70+
71+
// Tell renderer to execute all drawing commands
72+
self.canvas.flush();
73+
context.swap_buffers();
74+
unsafe { context.make_not_current() };
75+
self.damaged = false;
76+
}
77+
78+
fn on_event(&mut self, _window: &mut Window, event: Event) -> EventStatus {
79+
match event {
80+
Event::Window(WindowEvent::Resized(size)) => {
81+
let phy_size = size.physical_size();
82+
self.current_size = size;
83+
self.canvas.set_size(phy_size.width, phy_size.height, size.scale() as f32);
84+
self.damaged = true;
85+
}
86+
Event::Mouse(MouseEvent::CursorMoved { position, .. }) => {
87+
self.current_mouse_position = position.to_physical(&self.current_size);
88+
self.damaged = true;
89+
}
90+
_ => {}
91+
};
92+
log_event(&event);
93+
EventStatus::Captured
94+
}
95+
}
96+
97+
fn main() {
98+
let window_open_options = WindowOpenOptions {
99+
title: "Femtovg on Baseview".into(),
100+
size: Size::new(512.0, 512.0),
101+
scale: WindowScalePolicy::SystemScaleFactor,
102+
103+
gl_config: Some(GlConfig { alpha_bits: 8, ..GlConfig::default() }),
104+
};
105+
106+
Window::open_blocking(window_open_options, FemtovgExample::new);
107+
}
108+
109+
fn log_event(event: &Event) {
110+
match event {
111+
Event::Mouse(e) => println!("Mouse event: {:?}", e),
112+
Event::Keyboard(e) => println!("Keyboard event: {:?}", e),
113+
Event::Window(e) => println!("Window event: {:?}", e),
114+
}
115+
}

0 commit comments

Comments
 (0)