ombre: Pass `RuntimeCtx` to application methods
f8ee4687460207ce99d0af02d07cd088bd011cee
1 parent
731333fa
ombre/examples/button.rs
+2 -2
| 25 | 25 | type Error = T::Error; |
|
| 26 | 26 | ||
| 27 | 27 | fn event( |
|
| 28 | 28 | &mut self, |
|
| 29 | 29 | event: platform::WindowEvent, |
|
| 30 | - | win: &mut platform::Window, |
|
| 30 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 31 | 31 | ) -> Result<(), T::Error> { |
|
| 32 | - | self.ui.event(event, &mut (), &mut self.backend, win)?; |
|
| 32 | + | self.ui.event(event, &mut (), &mut self.backend, ctx.window)?; |
|
| 33 | 33 | ||
| 34 | 34 | Ok(()) |
|
| 35 | 35 | } |
|
| 36 | 36 | ||
| 37 | 37 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/examples/buttons.rs
+2 -2
| 73 | 73 | type Error = T::Error; |
|
| 74 | 74 | ||
| 75 | 75 | fn event( |
|
| 76 | 76 | &mut self, |
|
| 77 | 77 | event: platform::WindowEvent, |
|
| 78 | - | win: &mut platform::Window, |
|
| 78 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 79 | 79 | ) -> Result<(), T::Error> { |
|
| 80 | - | self.ui.event(event, &mut self.data, &mut self.backend, win)?; |
|
| 80 | + | self.ui.event(event, &mut self.data, &mut self.backend, ctx.window)?; |
|
| 81 | 81 | ||
| 82 | 82 | Ok(()) |
|
| 83 | 83 | } |
|
| 84 | 84 | ||
| 85 | 85 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/examples/canvas.rs
+2 -2
| 38 | 38 | type Error = T::Error; |
|
| 39 | 39 | ||
| 40 | 40 | fn event( |
|
| 41 | 41 | &mut self, |
|
| 42 | 42 | event: platform::WindowEvent, |
|
| 43 | - | win: &mut platform::Window, |
|
| 43 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 44 | 44 | ) -> Result<(), T::Error> { |
|
| 45 | - | self.ui.event(event, &mut self.state, &mut self.backend, win)?; |
|
| 45 | + | self.ui.event(event, &mut self.state, &mut self.backend, ctx.window)?; |
|
| 46 | 46 | ||
| 47 | 47 | Ok(()) |
|
| 48 | 48 | } |
|
| 49 | 49 | ||
| 50 | 50 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/examples/checkbox.rs
+3 -2
| 40 | 40 | type Error = T::Error; |
|
| 41 | 41 | ||
| 42 | 42 | fn event( |
|
| 43 | 43 | &mut self, |
|
| 44 | 44 | event: platform::WindowEvent, |
|
| 45 | - | win: &mut platform::Window, |
|
| 45 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 46 | 46 | ) -> Result<(), T::Error> { |
|
| 47 | - | self.ui.event(event, &mut self.checked, &mut self.backend, win)?; |
|
| 47 | + | self.ui |
|
| 48 | + | .event(event, &mut self.checked, &mut self.backend, ctx.window)?; |
|
| 48 | 49 | ||
| 49 | 50 | Ok(()) |
|
| 50 | 51 | } |
|
| 51 | 52 | ||
| 52 | 53 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/examples/circles.rs
+1 -1
| 62 | 62 | type Error = T::Error; |
|
| 63 | 63 | ||
| 64 | 64 | fn event( |
|
| 65 | 65 | &mut self, |
|
| 66 | 66 | event: platform::WindowEvent, |
|
| 67 | - | _win: &mut platform::Window, |
|
| 67 | + | _ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 68 | 68 | ) -> Result<(), Self::Error> { |
|
| 69 | 69 | match event { |
|
| 70 | 70 | platform::WindowEvent::Resized(size) => { |
|
| 71 | 71 | self.width = size.width; |
|
| 72 | 72 | self.height = size.height; |
ombre/examples/common/runtime.rs
+2 -0
| 1 | 1 | use ombre::math::Size; |
|
| 2 | 2 | use ombre::ui as gui; |
|
| 3 | 3 | use ombre::{gfx, platform}; |
|
| 4 | 4 | ||
| 5 | + | pub use ombre::runtime::RuntimeCtx; |
|
| 6 | + | ||
| 5 | 7 | pub fn run<R: ombre::runtime::Application>( |
|
| 6 | 8 | name: &str, |
|
| 7 | 9 | example: fn(gfx::gl::Context, &platform::Window) -> Result<R, gui::Error<gfx::gl::Context>>, |
|
| 8 | 10 | ) -> Result<(), Box<dyn std::error::Error>> { |
|
| 9 | 11 | ombre::logger::init(log::Level::Debug)?; |
ombre/examples/counter.rs
+2 -2
| 41 | 41 | type Error = R::Error; |
|
| 42 | 42 | ||
| 43 | 43 | fn event( |
|
| 44 | 44 | &mut self, |
|
| 45 | 45 | event: platform::WindowEvent, |
|
| 46 | - | win: &mut platform::Window, |
|
| 46 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 47 | 47 | ) -> Result<(), R::Error> { |
|
| 48 | - | self.ui.event(event, &mut self.state, &mut self.backend, win) |
|
| 48 | + | self.ui.event(event, &mut self.state, &mut self.backend, ctx.window) |
|
| 49 | 49 | } |
|
| 50 | 50 | ||
| 51 | 51 | fn frame(&mut self) -> Result<(), Self::Error> { |
|
| 52 | 52 | let mut frame = self.backend.frame(); |
|
| 53 | 53 | self.ui.frame(&self.state, &mut frame)?; |
ombre/examples/cursors.rs
+4 -4
| 62 | 62 | type Error = R::Error; |
|
| 63 | 63 | ||
| 64 | 64 | fn event( |
|
| 65 | 65 | &mut self, |
|
| 66 | 66 | event: platform::WindowEvent, |
|
| 67 | - | win: &mut platform::Window, |
|
| 67 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 68 | 68 | ) -> Result<(), R::Error> { |
|
| 69 | - | let cursor = win.get_cursor_pos(); |
|
| 70 | - | win.set_title(format!("{}, {}", cursor.x, cursor.y).as_str()); |
|
| 71 | - | self.ui.event(event, &mut self.state, &mut self.backend, win) |
|
| 69 | + | let cursor = ctx.window.get_cursor_pos(); |
|
| 70 | + | ctx.window.set_title(format!("{}, {}", cursor.x, cursor.y).as_str()); |
|
| 71 | + | self.ui.event(event, &mut self.state, &mut self.backend, ctx.window) |
|
| 72 | 72 | } |
|
| 73 | 73 | ||
| 74 | 74 | fn frame(&mut self) -> Result<(), Self::Error> { |
|
| 75 | 75 | let mut frame = self.backend.frame(); |
|
| 76 | 76 | self.ui.frame(&self.state, &mut frame)?; |
ombre/examples/fonts.rs
+2 -2
| 36 | 36 | type Error = T::Error; |
|
| 37 | 37 | ||
| 38 | 38 | fn event( |
|
| 39 | 39 | &mut self, |
|
| 40 | 40 | event: platform::WindowEvent, |
|
| 41 | - | win: &mut platform::Window, |
|
| 41 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 42 | 42 | ) -> Result<(), T::Error> { |
|
| 43 | - | self.ui.event(event, &mut (), &mut self.backend, win)?; |
|
| 43 | + | self.ui.event(event, &mut (), &mut self.backend, ctx.window)?; |
|
| 44 | 44 | ||
| 45 | 45 | Ok(()) |
|
| 46 | 46 | } |
|
| 47 | 47 | ||
| 48 | 48 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/examples/hover.rs
+2 -2
| 39 | 39 | type Error = T::Error; |
|
| 40 | 40 | ||
| 41 | 41 | fn event( |
|
| 42 | 42 | &mut self, |
|
| 43 | 43 | event: platform::WindowEvent, |
|
| 44 | - | win: &mut platform::Window, |
|
| 44 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 45 | 45 | ) -> Result<(), T::Error> { |
|
| 46 | - | self.ui.event(event, &mut (), &mut self.backend, win)?; |
|
| 46 | + | self.ui.event(event, &mut (), &mut self.backend, ctx.window)?; |
|
| 47 | 47 | ||
| 48 | 48 | Ok(()) |
|
| 49 | 49 | } |
|
| 50 | 50 | ||
| 51 | 51 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/examples/hstack.rs
+2 -2
| 36 | 36 | type Error = T::Error; |
|
| 37 | 37 | ||
| 38 | 38 | fn event( |
|
| 39 | 39 | &mut self, |
|
| 40 | 40 | event: platform::WindowEvent, |
|
| 41 | - | win: &mut platform::Window, |
|
| 41 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 42 | 42 | ) -> Result<(), T::Error> { |
|
| 43 | - | self.ui.event(event, &mut (), &mut self.backend, win)?; |
|
| 43 | + | self.ui.event(event, &mut (), &mut self.backend, ctx.window)?; |
|
| 44 | 44 | ||
| 45 | 45 | Ok(()) |
|
| 46 | 46 | } |
|
| 47 | 47 | ||
| 48 | 48 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/examples/image.rs
+2 -2
| 26 | 26 | type Error = T::Error; |
|
| 27 | 27 | ||
| 28 | 28 | fn event( |
|
| 29 | 29 | &mut self, |
|
| 30 | 30 | event: platform::WindowEvent, |
|
| 31 | - | win: &mut platform::Window, |
|
| 31 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 32 | 32 | ) -> Result<(), T::Error> { |
|
| 33 | - | self.ui.event(event, &mut (), &mut self.backend, win)?; |
|
| 33 | + | self.ui.event(event, &mut (), &mut self.backend, ctx.window)?; |
|
| 34 | 34 | ||
| 35 | 35 | Ok(()) |
|
| 36 | 36 | } |
|
| 37 | 37 | ||
| 38 | 38 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/examples/input.rs
+2 -2
| 46 | 46 | type Error = T::Error; |
|
| 47 | 47 | ||
| 48 | 48 | fn event( |
|
| 49 | 49 | &mut self, |
|
| 50 | 50 | event: platform::WindowEvent, |
|
| 51 | - | win: &mut platform::Window, |
|
| 51 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 52 | 52 | ) -> Result<(), T::Error> { |
|
| 53 | - | self.ui.event(event, &mut self.data, &mut self.backend, win)?; |
|
| 53 | + | self.ui.event(event, &mut self.data, &mut self.backend, ctx.window)?; |
|
| 54 | 54 | ||
| 55 | 55 | Ok(()) |
|
| 56 | 56 | } |
|
| 57 | 57 | ||
| 58 | 58 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/examples/latency.rs
+3 -3
| 26 | 26 | } |
|
| 27 | 27 | ||
| 28 | 28 | fn update( |
|
| 29 | 29 | &mut self, |
|
| 30 | 30 | delta: runtime::time::Duration, |
|
| 31 | - | win: &mut Window, |
|
| 31 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 32 | 32 | ) -> Result<(), Self::Error> { |
|
| 33 | - | let cursor = win.get_cursor_pos(); |
|
| 33 | + | let cursor = ctx.get_cursor_pos(); |
|
| 34 | 34 | if cursor != self.cursor { |
|
| 35 | 35 | self.cursor = cursor; |
|
| 36 | - | win.redraw(); |
|
| 36 | + | ctx.redraw(); |
|
| 37 | 37 | } |
|
| 38 | 38 | self.delta += delta.as_secs_f64(); |
|
| 39 | 39 | ||
| 40 | 40 | Ok(()) |
|
| 41 | 41 | } |
ombre/examples/offscreen.rs
+2 -2
| 26 | 26 | } |
|
| 27 | 27 | ||
| 28 | 28 | fn update( |
|
| 29 | 29 | &mut self, |
|
| 30 | 30 | _delta: runtime::time::Duration, |
|
| 31 | - | win: &mut platform::Window, |
|
| 31 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 32 | 32 | ) -> Result<(), Self::Error> { |
|
| 33 | - | win.redraw(); |
|
| 33 | + | ctx.redraw(); |
|
| 34 | 34 | ||
| 35 | 35 | Ok(()) |
|
| 36 | 36 | } |
|
| 37 | 37 | ||
| 38 | 38 | fn present(&mut self) -> Result<(), Self::Error> { |
ombre/examples/paint/basic.rs
+8 -5
| 1 | 1 | #![deny(clippy::all)] |
|
| 2 | 2 | #![allow(clippy::unwrap_used)] |
|
| 3 | 3 | use std::time::Duration; |
|
| 4 | 4 | ||
| 5 | 5 | use ombre::paint::{canvas, shape, Canvas, PaintSurface}; |
|
| 6 | - | use ombre::platform::Window; |
|
| 7 | 6 | use ombre::prelude::*; |
|
| 8 | 7 | use ombre::rng::Rng; |
|
| 9 | 8 | ||
| 10 | 9 | pub struct Example<T> { |
|
| 11 | 10 | canvas: Canvas, |
| 32 | 31 | type Error = R::Error; |
|
| 33 | 32 | ||
| 34 | 33 | fn event( |
|
| 35 | 34 | &mut self, |
|
| 36 | 35 | event: platform::WindowEvent, |
|
| 37 | - | _win: &mut platform::Window, |
|
| 36 | + | _ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 38 | 37 | ) -> Result<(), R::Error> { |
|
| 39 | 38 | if let platform::WindowEvent::Resized(size) = event { |
|
| 40 | 39 | self.canvas.resize(size, &mut self.backend)?; |
|
| 41 | 40 | } |
|
| 42 | 41 | Ok(()) |
|
| 43 | 42 | } |
|
| 44 | 43 | ||
| 45 | - | fn tick(&mut self, delta: Duration, win: &mut Window) -> Result<(), Self::Error> { |
|
| 44 | + | fn tick( |
|
| 45 | + | &mut self, |
|
| 46 | + | delta: Duration, |
|
| 47 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 48 | + | ) -> Result<(), Self::Error> { |
|
| 46 | 49 | self.delta += delta.as_secs_f64(); |
|
| 47 | - | let size = win.size(); |
|
| 50 | + | let size = ctx.size(); |
|
| 48 | 51 | let origin = Point2D::new( |
|
| 49 | 52 | (self.rng.fraction() * size.width) as f32, |
|
| 50 | 53 | (self.rng.fraction() * size.height) as f32, |
|
| 51 | 54 | ); |
|
| 52 | 55 | let color = Rgba::new( |
| 59 | 62 | if self.rng.flip() { |
|
| 60 | 63 | self.canvas.fill(shape::circle(origin, 32., 8), color); |
|
| 61 | 64 | } else { |
|
| 62 | 65 | self.canvas.fill(shape::rectangle(origin, Size::new(48., 48.)), color); |
|
| 63 | 66 | } |
|
| 64 | - | win.redraw(); |
|
| 67 | + | ctx.redraw(); |
|
| 65 | 68 | ||
| 66 | 69 | Ok(()) |
|
| 67 | 70 | } |
|
| 68 | 71 | ||
| 69 | 72 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/examples/paint/showcase.rs
+1 -1
| 33 | 33 | type Error = R::Error; |
|
| 34 | 34 | ||
| 35 | 35 | fn event( |
|
| 36 | 36 | &mut self, |
|
| 37 | 37 | event: platform::WindowEvent, |
|
| 38 | - | _win: &mut platform::Window, |
|
| 38 | + | _ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 39 | 39 | ) -> Result<(), R::Error> { |
|
| 40 | 40 | match event { |
|
| 41 | 41 | platform::WindowEvent::Resized(size) => { |
|
| 42 | 42 | self.canvas.resize(size, &mut self.backend)?; |
|
| 43 | 43 | } |
ombre/examples/particles.rs
+3 -3
| 142 | 142 | type Error = T::Error; |
|
| 143 | 143 | ||
| 144 | 144 | fn event( |
|
| 145 | 145 | &mut self, |
|
| 146 | 146 | event: platform::WindowEvent, |
|
| 147 | - | _win: &mut platform::Window, |
|
| 147 | + | _ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 148 | 148 | ) -> Result<(), Self::Error> { |
|
| 149 | 149 | match event { |
|
| 150 | 150 | platform::WindowEvent::Resized(size) => { |
|
| 151 | 151 | self.width = size.width; |
|
| 152 | 152 | self.height = size.height; |
| 162 | 162 | } |
|
| 163 | 163 | ||
| 164 | 164 | fn tick( |
|
| 165 | 165 | &mut self, |
|
| 166 | 166 | delta: ombre::runtime::time::Duration, |
|
| 167 | - | win: &mut platform::Window, |
|
| 167 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 168 | 168 | ) -> Result<(), Self::Error> { |
|
| 169 | 169 | if !self.paused { |
|
| 170 | 170 | let dt = delta.as_secs_f32(); |
|
| 171 | 171 | self.emit_particles(); |
|
| 172 | 172 | self.update_particles(dt); |
|
| 173 | 173 | } |
|
| 174 | - | win.redraw(); |
|
| 174 | + | ctx.redraw(); |
|
| 175 | 175 | ||
| 176 | 176 | Ok(()) |
|
| 177 | 177 | } |
|
| 178 | 178 | ||
| 179 | 179 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/examples/picker.rs
+2 -2
| 46 | 46 | type Error = T::Error; |
|
| 47 | 47 | ||
| 48 | 48 | fn event( |
|
| 49 | 49 | &mut self, |
|
| 50 | 50 | event: platform::WindowEvent, |
|
| 51 | - | win: &mut platform::Window, |
|
| 51 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 52 | 52 | ) -> Result<(), T::Error> { |
|
| 53 | - | self.ui.event(event, &mut self.state, &mut self.backend, win)?; |
|
| 53 | + | self.ui.event(event, &mut self.state, &mut self.backend, ctx.window)?; |
|
| 54 | 54 | ||
| 55 | 55 | Ok(()) |
|
| 56 | 56 | } |
|
| 57 | 57 | ||
| 58 | 58 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/examples/sprite.rs
+4 -4
| 81 | 81 | type Error = T::Error; |
|
| 82 | 82 | ||
| 83 | 83 | fn tick( |
|
| 84 | 84 | &mut self, |
|
| 85 | 85 | _delta: ombre::runtime::time::Duration, |
|
| 86 | - | win: &mut platform::Window, |
|
| 86 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 87 | 87 | ) -> Result<(), Self::Error> { |
|
| 88 | - | win.redraw(); |
|
| 88 | + | ctx.redraw(); |
|
| 89 | 89 | ||
| 90 | 90 | Ok(()) |
|
| 91 | 91 | } |
|
| 92 | 92 | ||
| 93 | 93 | fn event( |
|
| 94 | 94 | &mut self, |
|
| 95 | 95 | event: platform::WindowEvent, |
|
| 96 | - | win: &mut platform::Window, |
|
| 96 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 97 | 97 | ) -> Result<(), Self::Error> { |
|
| 98 | 98 | match event { |
|
| 99 | 99 | platform::WindowEvent::Resized(size) => { |
|
| 100 | 100 | self.state.width = size.width; |
|
| 101 | 101 | self.state.height = size.height; |
| 108 | 108 | self.state.scale = 1.; |
|
| 109 | 109 | } |
|
| 110 | 110 | } |
|
| 111 | 111 | _ => {} |
|
| 112 | 112 | } |
|
| 113 | - | self.ui.event(event, &mut self.state, &mut self.backend, win)?; |
|
| 113 | + | self.ui.event(event, &mut self.state, &mut self.backend, ctx.window)?; |
|
| 114 | 114 | ||
| 115 | 115 | Ok(()) |
|
| 116 | 116 | } |
|
| 117 | 117 | ||
| 118 | 118 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/examples/state.rs
+2 -2
| 33 | 33 | type Error = T::Error; |
|
| 34 | 34 | ||
| 35 | 35 | fn event( |
|
| 36 | 36 | &mut self, |
|
| 37 | 37 | event: platform::WindowEvent, |
|
| 38 | - | win: &mut platform::Window, |
|
| 38 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 39 | 39 | ) -> Result<(), T::Error> { |
|
| 40 | 40 | self.state += 1; |
|
| 41 | - | self.ui.event(event, &mut self.state, &mut self.backend, win)?; |
|
| 41 | + | self.ui.event(event, &mut self.state, &mut self.backend, ctx.window)?; |
|
| 42 | 42 | ||
| 43 | 43 | Ok(()) |
|
| 44 | 44 | } |
|
| 45 | 45 | ||
| 46 | 46 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/examples/text.rs
+2 -2
| 116 | 116 | type Error = T::Error; |
|
| 117 | 117 | ||
| 118 | 118 | fn event( |
|
| 119 | 119 | &mut self, |
|
| 120 | 120 | event: platform::WindowEvent, |
|
| 121 | - | win: &mut platform::Window, |
|
| 121 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 122 | 122 | ) -> Result<(), T::Error> { |
|
| 123 | - | self.ui.event(event, &mut self.text, &mut self.backend, win)?; |
|
| 123 | + | self.ui.event(event, &mut self.text, &mut self.backend, ctx.window)?; |
|
| 124 | 124 | ||
| 125 | 125 | Ok(()) |
|
| 126 | 126 | } |
|
| 127 | 127 | ||
| 128 | 128 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/examples/texture.rs
+2 -3
| 1 | 1 | #![allow(clippy::unwrap_used)] |
|
| 2 | 2 | use ombre::derive::VertexAttributes; |
|
| 3 | - | use ombre::platform::Window; |
|
| 4 | 3 | use ombre::{math::Zero, prelude::*}; |
|
| 5 | 4 | ||
| 6 | 5 | const PIXELS: [Rgba8; 4 * 4] = [ |
|
| 7 | 6 | Rgba8::new(0xFF, 0xFF, 0xFF, 0xFF), |
|
| 8 | 7 | Rgba8::new(0xFF, 0x00, 0x00, 0xFF), |
| 62 | 61 | } |
|
| 63 | 62 | ||
| 64 | 63 | fn update( |
|
| 65 | 64 | &mut self, |
|
| 66 | 65 | delta: runtime::time::Duration, |
|
| 67 | - | win: &mut Window, |
|
| 66 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 68 | 67 | ) -> Result<(), Self::Error> { |
|
| 69 | 68 | self.delta += delta.as_secs_f64(); |
|
| 70 | - | win.redraw(); |
|
| 69 | + | ctx.redraw(); |
|
| 71 | 70 | ||
| 72 | 71 | Ok(()) |
|
| 73 | 72 | } |
|
| 74 | 73 | ||
| 75 | 74 | fn present(&mut self) -> Result<(), Self::Error> { |
ombre/examples/vstack.rs
+2 -2
| 35 | 35 | type Error = T::Error; |
|
| 36 | 36 | ||
| 37 | 37 | fn event( |
|
| 38 | 38 | &mut self, |
|
| 39 | 39 | event: platform::WindowEvent, |
|
| 40 | - | win: &mut platform::Window, |
|
| 40 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 41 | 41 | ) -> Result<(), T::Error> { |
|
| 42 | - | self.ui.event(event, &mut (), &mut self.backend, win)?; |
|
| 42 | + | self.ui.event(event, &mut (), &mut self.backend, ctx.window)?; |
|
| 43 | 43 | ||
| 44 | 44 | Ok(()) |
|
| 45 | 45 | } |
|
| 46 | 46 | ||
| 47 | 47 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/examples/zstack.rs
+2 -2
| 33 | 33 | type Error = T::Error; |
|
| 34 | 34 | ||
| 35 | 35 | fn event( |
|
| 36 | 36 | &mut self, |
|
| 37 | 37 | event: platform::WindowEvent, |
|
| 38 | - | win: &mut platform::Window, |
|
| 38 | + | ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 39 | 39 | ) -> Result<(), T::Error> { |
|
| 40 | - | self.ui.event(event, &mut (), &mut self.backend, win)?; |
|
| 40 | + | self.ui.event(event, &mut (), &mut self.backend, ctx.window)?; |
|
| 41 | 41 | ||
| 42 | 42 | Ok(()) |
|
| 43 | 43 | } |
|
| 44 | 44 | ||
| 45 | 45 | fn frame(&mut self) -> Result<(), Self::Error> { |
ombre/src/runtime.rs
+54 -6
| 1 | 1 | pub mod default; |
|
| 2 | 2 | pub mod sim; |
|
| 3 | 3 | pub mod time; |
|
| 4 | 4 | ||
| 5 | + | use std::time::Duration; |
|
| 6 | + | ||
| 5 | 7 | use crate::platform::*; |
|
| 6 | 8 | use crate::prelude::*; |
|
| 9 | + | use crate::runtime::time::MovingAverageTimer; |
|
| 7 | 10 | ||
| 8 | 11 | /// Default UI scale. |
|
| 9 | 12 | pub const DEFAULT_SCALE: f32 = 2.; |
|
| 10 | 13 | /// Default target frames per second. |
|
| 11 | 14 | pub const DEFAULT_TARGET_FPS: f64 = 60.; |
|
| 12 | 15 | ||
| 16 | + | /// Timing information available to the running application. |
|
| 17 | + | #[derive(Default, Clone)] |
|
| 18 | + | pub struct Timings { |
|
| 19 | + | /// Measures average frame time. |
|
| 20 | + | pub avg_frame_time: Duration, |
|
| 21 | + | /// Measures average update time. |
|
| 22 | + | pub avg_tick_time: Duration, |
|
| 23 | + | } |
|
| 24 | + | ||
| 25 | + | /// Runtime context. |
|
| 26 | + | pub struct RuntimeCtx<'a> { |
|
| 27 | + | /// Timing information. |
|
| 28 | + | pub timings: Timings, |
|
| 29 | + | /// Reference to the platform window. |
|
| 30 | + | pub window: &'a mut platform::Window, |
|
| 31 | + | } |
|
| 32 | + | ||
| 33 | + | impl std::ops::Deref for RuntimeCtx<'_> { |
|
| 34 | + | type Target = platform::Window; |
|
| 35 | + | ||
| 36 | + | fn deref(&self) -> &Self::Target { |
|
| 37 | + | self.window |
|
| 38 | + | } |
|
| 39 | + | } |
|
| 40 | + | ||
| 41 | + | impl std::ops::DerefMut for RuntimeCtx<'_> { |
|
| 42 | + | fn deref_mut(&mut self) -> &mut Self::Target { |
|
| 43 | + | self.window |
|
| 44 | + | } |
|
| 45 | + | } |
|
| 46 | + | ||
| 47 | + | impl<'a> RuntimeCtx<'a> { |
|
| 48 | + | /// Create a new runtime context from a window. |
|
| 49 | + | fn new(window: &'a mut platform::Window) -> Self { |
|
| 50 | + | Self { |
|
| 51 | + | timings: Timings::default(), |
|
| 52 | + | window, |
|
| 53 | + | } |
|
| 54 | + | } |
|
| 55 | + | } |
|
| 56 | + | ||
| 13 | 57 | /// Simulation context. |
|
| 14 | 58 | pub struct SimulationCtx<'a> { |
|
| 15 | 59 | /// Target/maximum frames per second. |
|
| 16 | 60 | target_fps: f64, |
|
| 17 | 61 | /// Ticks per second. |
| 73 | 117 | type Error: std::error::Error + 'static; |
|
| 74 | 118 | ||
| 75 | 119 | /// Called when there are changes that haven't been rendered. |
|
| 76 | 120 | fn frame(&mut self) -> Result<(), Self::Error>; |
|
| 77 | 121 | /// Called on every event. |
|
| 78 | - | fn event(&mut self, event: WindowEvent, win: &mut Window) -> Result<(), Self::Error>; |
|
| 122 | + | fn event(&mut self, event: WindowEvent, ctx: &mut RuntimeCtx<'_>) -> Result<(), Self::Error>; |
|
| 79 | 123 | /// Called periodically to update the application state. Use this to implement animations. |
|
| 80 | - | fn tick(&mut self, delta: time::Duration, win: &mut Window) -> Result<(), Self::Error> { |
|
| 124 | + | fn tick(&mut self, delta: time::Duration, ctx: &mut RuntimeCtx<'_>) -> Result<(), Self::Error> { |
|
| 81 | 125 | Ok(()) |
|
| 82 | 126 | } |
|
| 83 | 127 | } |
|
| 84 | 128 | ||
| 85 | 129 | /// Simulation interface for runtime. |
| 108 | 152 | type Error: std::error::Error + 'static; |
|
| 109 | 153 | ||
| 110 | 154 | /// Called on every frame, after events have been processed. Drawing code goes here. |
|
| 111 | 155 | fn present(&mut self) -> Result<(), Self::Error>; |
|
| 112 | 156 | /// Called periodically to update the application state. Use this to implement animations. |
|
| 113 | - | fn update(&mut self, delta: time::Duration, win: &mut Window) -> Result<(), Self::Error> { |
|
| 157 | + | fn update( |
|
| 158 | + | &mut self, |
|
| 159 | + | delta: time::Duration, |
|
| 160 | + | ctx: &mut RuntimeCtx<'_>, |
|
| 161 | + | ) -> Result<(), Self::Error> { |
|
| 114 | 162 | Ok(()) |
|
| 115 | 163 | } |
|
| 116 | 164 | /// Called when the window size changes. |
|
| 117 | 165 | fn window_resized(&mut self, size: LogicalSize) -> Result<(), Self::Error>; |
|
| 118 | 166 | /// Called when the window moved. |
| 139 | 187 | where |
|
| 140 | 188 | A: ApplicationExt, |
|
| 141 | 189 | { |
|
| 142 | 190 | type Error = A::Error; |
|
| 143 | 191 | ||
| 144 | - | fn event(&mut self, event: WindowEvent, _window: &mut Window) -> Result<(), Self::Error> { |
|
| 192 | + | fn event(&mut self, event: WindowEvent, _ctx: &mut RuntimeCtx<'_>) -> Result<(), Self::Error> { |
|
| 145 | 193 | match event { |
|
| 146 | 194 | WindowEvent::CursorEntered => { |
|
| 147 | 195 | self.mouse_entered(); |
|
| 148 | 196 | } |
|
| 149 | 197 | WindowEvent::CursorLeft => { |
| 188 | 236 | ||
| 189 | 237 | fn frame(&mut self) -> Result<(), <A as ApplicationExt>::Error> { |
|
| 190 | 238 | self.present() |
|
| 191 | 239 | } |
|
| 192 | 240 | ||
| 193 | - | fn tick(&mut self, delta: time::Duration, win: &mut Window) -> Result<(), Self::Error> { |
|
| 194 | - | self.update(delta, win) |
|
| 241 | + | fn tick(&mut self, delta: time::Duration, ctx: &mut RuntimeCtx<'_>) -> Result<(), Self::Error> { |
|
| 242 | + | self.update(delta, ctx) |
|
| 195 | 243 | } |
|
| 196 | 244 | } |
ombre/src/runtime/default.rs
+18 -15
| 2 | 2 | use core::time; |
|
| 3 | 3 | use std::time::Instant; |
|
| 4 | 4 | ||
| 5 | 5 | use crate::platform; |
|
| 6 | 6 | use crate::platform::*; |
|
| 7 | - | use crate::runtime::time::MovingAverageTimer; |
|
| 8 | - | use crate::runtime::Application; |
|
| 7 | + | use crate::runtime::{Application, MovingAverageTimer, RuntimeCtx}; |
|
| 9 | 8 | ||
| 10 | 9 | /// Run the given application. |
|
| 11 | 10 | pub fn run<A: Application>( |
|
| 12 | 11 | mut win: platform::Window, |
|
| 13 | 12 | mut events: platform::Events, |
|
| 14 | 13 | mut app: A, |
|
| 15 | 14 | ) -> Result<(), A::Error> { |
|
| 16 | 15 | let mut resized = None; |
|
| 17 | 16 | let mut minimized = false; |
|
| 18 | - | let mut render_timer = MovingAverageTimer::default(); |
|
| 19 | 17 | let mut last = Instant::now(); |
|
| 18 | + | let mut frame_timer = MovingAverageTimer::default(); |
|
| 19 | + | let mut tick_timer = MovingAverageTimer::default(); |
|
| 20 | + | let mut ctx = RuntimeCtx::new(&mut win); |
|
| 20 | 21 | let mut delta; |
|
| 21 | 22 | ||
| 22 | - | log::info!(target: "run", "starting runtime, window size is {}", win.size()); |
|
| 23 | + | log::info!(target: "run", "starting runtime, window size is {}", ctx.size()); |
|
| 23 | 24 | ||
| 24 | - | 'main: while win.is_open() { |
|
| 25 | + | 'main: while ctx.window.is_open() { |
|
| 25 | 26 | events.wait_timeout(time::Duration::from_millis(8)); |
|
| 27 | + | ctx.timings.avg_frame_time = frame_timer.get(); |
|
| 28 | + | ctx.timings.avg_tick_time = tick_timer.get(); |
|
| 26 | 29 | ||
| 27 | 30 | // The redraw flag is set by the "request redraw" function on `Window`. |
|
| 28 | - | let mut redraw = win.is_ready(); |
|
| 31 | + | let mut redraw = ctx.window.is_ready(); |
|
| 29 | 32 | for event in events.flush() { |
|
| 30 | 33 | log::debug!(target: "run", "event: {event:?}"); |
|
| 31 | 34 | if event.is_input() { |
|
| 32 | 35 | // Input events force a redraw. |
|
| 33 | 36 | redraw = true; |
| 49 | 52 | redraw = true; |
|
| 50 | 53 | } |
|
| 51 | 54 | WindowEvent::Moved(_) => { |
|
| 52 | 55 | app.event( |
|
| 53 | 56 | WindowEvent::CursorMoved { |
|
| 54 | - | position: win.get_cursor_pos(), |
|
| 57 | + | position: ctx.get_cursor_pos(), |
|
| 55 | 58 | }, |
|
| 56 | - | &mut win, |
|
| 59 | + | &mut ctx, |
|
| 57 | 60 | )?; |
|
| 58 | 61 | } |
|
| 59 | 62 | WindowEvent::Minimized => { |
|
| 60 | 63 | minimized = true; |
|
| 61 | 64 | } |
| 68 | 71 | .. |
|
| 69 | 72 | }) => { |
|
| 70 | 73 | break 'main; |
|
| 71 | 74 | } |
|
| 72 | 75 | _ => { |
|
| 73 | - | app.event(event, &mut win)?; |
|
| 76 | + | app.event(event, &mut ctx)?; |
|
| 74 | 77 | } |
|
| 75 | 78 | }; |
|
| 76 | 79 | } |
|
| 77 | 80 | ||
| 78 | 81 | // If minimized, don't update or render. |
| 80 | 83 | continue; |
|
| 81 | 84 | } |
|
| 82 | 85 | // Time delta. |
|
| 83 | 86 | delta = last.elapsed(); |
|
| 84 | 87 | last += delta; |
|
| 85 | - | app.tick(delta, &mut win)?; |
|
| 88 | + | tick_timer.measure(|_| app.tick(delta, &mut ctx))?; |
|
| 86 | 89 | ||
| 87 | 90 | // Since we may receive multiple resize events at once, instead of responding to each |
|
| 88 | 91 | // resize event, we handle the resize only once. |
|
| 89 | 92 | if let Some(size) = resized.take() { |
|
| 90 | - | let position = win.get_cursor_pos(); |
|
| 93 | + | let position = ctx.get_cursor_pos(); |
|
| 91 | 94 | ||
| 92 | - | app.event(WindowEvent::Resized(size), &mut win)?; |
|
| 93 | - | app.event(WindowEvent::CursorMoved { position }, &mut win)?; |
|
| 95 | + | app.event(WindowEvent::Resized(size), &mut ctx)?; |
|
| 96 | + | app.event(WindowEvent::CursorMoved { position }, &mut ctx)?; |
|
| 94 | 97 | } |
|
| 95 | 98 | if redraw { |
|
| 96 | - | render_timer.measure(|_| { |
|
| 99 | + | frame_timer.measure(|_| { |
|
| 97 | 100 | app.frame()?; |
|
| 98 | - | win.present(); |
|
| 101 | + | ctx.present(); |
|
| 99 | 102 | ||
| 100 | 103 | Ok(()) |
|
| 101 | 104 | })?; |
|
| 102 | 105 | } |
|
| 103 | 106 | } |
reve/src/lib.rs
+1 -1
| 97 | 97 | type Error = Error<T>; |
|
| 98 | 98 | ||
| 99 | 99 | fn update( |
|
| 100 | 100 | &mut self, |
|
| 101 | 101 | _delta: runtime::time::Duration, |
|
| 102 | - | _win: &mut platform::Window, |
|
| 102 | + | _ctx: &mut runtime::RuntimeCtx<'_>, |
|
| 103 | 103 | ) -> Result<(), Self::Error> { |
|
| 104 | 104 | Ok(()) |
|
| 105 | 105 | } |
|
| 106 | 106 | ||
| 107 | 107 | fn window_resized(&mut self, size: platform::LogicalSize) -> Result<(), Error<T>> { |