-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Update softbuffer source, fix example files and add 2 new axample files #4513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yagizgil
wants to merge
32
commits into
rust-windowing:master
Choose a base branch
from
yagizgil:branch1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 24 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
af28d87
Update fill.rs
yagizgil 52e6428
add new example
yagizgil ade77e9
Update win_with_soft.rs
yagizgil 48ded92
Update win_with_soft.rs
yagizgil 4cfeac9
Update fill.rs
yagizgil ce40fe5
Update fill.rs
yagizgil 38f85d2
Update fill.rs
yagizgil 858e04b
Update win_with_soft.rs
yagizgil ecf0a0a
Update win_with_soft.rs
yagizgil 109f1e2
Update win_with_soft.rs
yagizgil 85de3cd
Create win_with_soft_2.rs
yagizgil c242f59
Update win_with_soft_2.rs
yagizgil 1030024
Update win_with_soft_2.rs
yagizgil 60d5848
Update win_with_soft_2.rs
yagizgil 44c2b3f
Update win_with_soft_2.rs
yagizgil 6310dc0
Update win_with_soft_2.rs
yagizgil 74fabc5
Update win_with_soft_2.rs
yagizgil 2a3a14a
complate keywrods and switch softbuffer source
yagizgil e2297b7
Update unreleased.md
yagizgil 9838ef9
Update event_loop.rs
yagizgil e10fcb2
fix
yagizgil c6bd46c
fix: format
yagizgil 909e079
fix example file named "application.rs"
yagizgil cfe55b4
format example named applicatio.rs
yagizgil 284be22
remove keyboard mappings as requested
yagizgil bc3a2a6
Update unreleased.md
yagizgil d2bc9fe
Update fill.rs
yagizgil 776f639
fmt fill.rs and exclude android for new examples
yagizgil 5d01646
fix examples
yagizgil 2afb39f
fmt examples
yagizgil 6ff12eb
fix examples
yagizgil 5feb83f
.
yagizgil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| use std::error::Error; | ||
| use std::num::NonZeroU32; | ||
| use std::sync::Arc; | ||
| use std::time::{Duration, Instant}; | ||
|
|
||
| use softbuffer::{Context, Surface}; | ||
| use winit::application::ApplicationHandler; | ||
| use winit::event::WindowEvent; | ||
| use winit::event_loop::{ActiveEventLoop, EventLoop}; | ||
| use winit::window::{Window, WindowAttributes, WindowId}; | ||
|
|
||
| struct App { | ||
| window: Option<Arc<dyn Window>>, | ||
| surface: Option<Surface<Arc<dyn Window>, Arc<dyn Window>>>, | ||
| frame_count: u32, | ||
| last_fps_print: Instant, | ||
| } | ||
|
|
||
| impl App { | ||
| fn new() -> Self { | ||
| Self { window: None, surface: None, frame_count: 0, last_fps_print: Instant::now() } | ||
| } | ||
| } | ||
|
|
||
| impl ApplicationHandler for App { | ||
| fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) { | ||
| let attributes = WindowAttributes::default().with_title("framebuffer test"); | ||
| let window: Arc<dyn Window> = Arc::from(event_loop.create_window(attributes).unwrap()); | ||
|
|
||
| let context = Context::new(window.clone()).expect("context except"); | ||
| let surface = Surface::new(&context, window.clone()).expect("surface except"); | ||
|
|
||
| self.window = Some(window); | ||
| self.surface = Some(surface); | ||
| } | ||
|
|
||
| fn about_to_wait(&mut self, _event_loop: &dyn ActiveEventLoop) { | ||
| if let Some(window) = self.window.as_ref() { | ||
| window.request_redraw(); | ||
| } | ||
| } | ||
|
|
||
| fn window_event( | ||
| &mut self, | ||
| event_loop: &dyn ActiveEventLoop, | ||
| _id: WindowId, | ||
| event: WindowEvent, | ||
| ) { | ||
| match event { | ||
| WindowEvent::CloseRequested => event_loop.exit(), | ||
|
|
||
| WindowEvent::SurfaceResized(size) => { | ||
| if let Some(surface) = self.surface.as_mut() { | ||
| let w = NonZeroU32::new(size.width.max(1)).unwrap(); | ||
| let h = NonZeroU32::new(size.height.max(1)).unwrap(); | ||
| surface.resize(w, h).unwrap(); | ||
| } | ||
| }, | ||
|
|
||
| WindowEvent::RedrawRequested => { | ||
| if let Some(surface) = self.surface.as_mut() { | ||
| let mut buffer = surface.next_buffer().expect("buffer except"); | ||
|
|
||
| for (x, y, pixel) in buffer.pixels_iter() { | ||
| let red = (x % 255) as u8; | ||
| let green = (y % 255) as u8; | ||
| let blue = ((x * y) % 255) as u8; | ||
|
|
||
| *pixel = softbuffer::Pixel::new_rgb(red, green, blue); | ||
| } | ||
|
|
||
| buffer.present().unwrap(); | ||
|
|
||
| let now = Instant::now(); | ||
| self.frame_count += 1; | ||
|
|
||
| if now.duration_since(self.last_fps_print) >= Duration::from_secs(1) { | ||
| let fps = self.frame_count; | ||
| println!("FPS: {}", fps); | ||
|
|
||
| self.frame_count = 0; | ||
| self.last_fps_print = now; | ||
| } | ||
| } | ||
| }, | ||
| _ => (), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn main() -> Result<(), Box<dyn Error>> { | ||
| let event_loop = EventLoop::new()?; | ||
|
|
||
| let app = Box::leak(Box::new(App::new())); | ||
|
|
||
| event_loop.run_app(app)?; | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| use std::error::Error; | ||
| use std::num::NonZeroU32; | ||
| use std::sync::Arc; | ||
| use std::time::{Duration, Instant}; | ||
|
|
||
| use softbuffer::{Context, Pixel, Surface}; | ||
| use winit::application::ApplicationHandler; | ||
| use winit::event::WindowEvent; | ||
| use winit::event_loop::{ActiveEventLoop, EventLoop}; | ||
| use winit::window::{Window, WindowAttributes, WindowId}; | ||
|
|
||
| struct App { | ||
| window: Option<Arc<dyn Window>>, | ||
| surface: Option<Surface<Arc<dyn Window>, Arc<dyn Window>>>, | ||
| start_time: Instant, | ||
| frame_count: u32, | ||
| last_fps_print: Instant, | ||
| total_frame_count: u64, | ||
| } | ||
|
|
||
| impl ApplicationHandler for App { | ||
| fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) { | ||
| let window: Arc<dyn Window> = | ||
| Arc::from(event_loop.create_window(WindowAttributes::default()).unwrap()); | ||
| self.surface = | ||
| Some(Surface::new(&Context::new(window.clone()).unwrap(), window.clone()).unwrap()); | ||
| self.window = Some(window); | ||
| } | ||
|
|
||
| fn about_to_wait(&mut self, _: &dyn ActiveEventLoop) { | ||
| if let Some(w) = self.window.as_ref() { | ||
| w.request_redraw(); | ||
| } | ||
| } | ||
|
|
||
| fn window_event( | ||
| &mut self, | ||
| event_loop: &dyn ActiveEventLoop, | ||
| _id: WindowId, | ||
| event: WindowEvent, | ||
| ) { | ||
| match event { | ||
| WindowEvent::CloseRequested => event_loop.exit(), | ||
| WindowEvent::SurfaceResized(size) => { | ||
| if let Some(s) = self.surface.as_mut() { | ||
| s.resize( | ||
| NonZeroU32::new(size.width.max(1)).unwrap(), | ||
| NonZeroU32::new(size.height.max(1)).unwrap(), | ||
| ) | ||
| .unwrap(); | ||
| } | ||
| }, | ||
| WindowEvent::RedrawRequested => { | ||
| if let (Some(window), Some(surface)) = (&self.window, self.surface.as_mut()) { | ||
| let mut buffer = surface.next_buffer().unwrap(); | ||
| let size = window.outer_size(); | ||
| let elapsed = self.start_time.elapsed(); | ||
| let t = elapsed.as_secs_f32(); | ||
| let total_secs = elapsed.as_secs(); | ||
|
|
||
| let ball_x = ((t * 1.5).sin() * 0.5 + 0.5) * size.width as f32; | ||
| let ball_y = ((t * 2.1).cos() * 0.5 + 0.5) * size.height as f32; | ||
|
|
||
| for (x, y, pixel) in buffer.pixels_iter() { | ||
| let dx = x as f32 - ball_x; | ||
| let dy = y as f32 - ball_y; | ||
| let dist = (dx * dx + dy * dy).sqrt(); | ||
|
|
||
| let p1 = (x as f32 * 0.01 + t).sin(); | ||
| let p2 = (y as f32 * 0.01 + t * 0.5).cos(); | ||
| let p3 = ((x as f32 + y as f32) * 0.005 + t).sin(); | ||
| let plasma = (p1 + p2 + p3) * 0.33; | ||
|
|
||
| if dist < 50.0 { | ||
| *pixel = Pixel::new_rgb(255, 255, 255); | ||
| } else if (y as f32 + t * 50.0) % 20.0 < 2.0 { | ||
| *pixel = Pixel::new_rgb(0, 0, 0); | ||
| } else { | ||
| let r = ((plasma + 1.0) * 127.0) as u8; | ||
| let g = (x % 255) as u8; | ||
| let b = (y % 255).wrapping_add((t * 20.0) as u32) as u8; | ||
| *pixel = Pixel::new_rgb(r, g, b); | ||
| } | ||
| } | ||
| buffer.present().unwrap(); | ||
|
|
||
| self.frame_count += 1; | ||
| self.total_frame_count += 1; | ||
| if self.last_fps_print.elapsed() >= Duration::from_secs(1) { | ||
| let avg = self.total_frame_count as f64 / elapsed.as_secs_f64(); | ||
| window.set_title(&format!( | ||
| "FPS: {} | AVG: {:.2} | TIME: {}s | {}x{}", | ||
| self.frame_count, avg, total_secs, size.width, size.height | ||
| )); | ||
| self.frame_count = 0; | ||
| self.last_fps_print = Instant::now(); | ||
| } | ||
| } | ||
| }, | ||
| _ => (), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn main() -> Result<(), Box<dyn Error>> { | ||
| let app = App { | ||
| window: None, | ||
| surface: None, | ||
| start_time: Instant::now(), | ||
| frame_count: 0, | ||
| last_fps_print: Instant::now(), | ||
| total_frame_count: 0, | ||
| }; | ||
| EventLoop::new()?.run_app(app).map_err(Into::into) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding keyboard mappings is a simple change that should be done in another PR, in my opinion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed the keyboard mappings from this PR to keep it focused. I will open a separate PR