From 9866af6a56f2a34e368619f723eb701d68dd8923 Mon Sep 17 00:00:00 2001 From: Sven Nilsen Date: Mon, 15 Dec 2025 03:09:11 +0100 Subject: [PATCH] Updated dependencies --- Cargo.toml | 21 +- assets/colored_cube.wgsl | 24 +++ assets/cube_120.glslf | 8 - assets/cube_120.glslv | 9 - assets/cube_150.glslf | 9 - assets/cube_150.glslv | 9 - examples/cube.rs | 355 ++++++++++++++++++--------------- examples/deform.rs | 22 +- examples/draw_state.rs | 9 +- examples/freetype/Cargo.toml | 15 +- examples/hello_world.rs | 13 +- examples/image.rs | 9 +- examples/multi_window.rs | 10 +- examples/paint.rs | 27 ++- examples/shapes.rs | 4 +- examples/snake.rs | 176 ++++++++-------- examples/sprite.rs | 15 +- examples/texture_swap.rs | 14 +- examples/user_input/Cargo.toml | 14 +- 19 files changed, 372 insertions(+), 391 deletions(-) create mode 100644 assets/colored_cube.wgsl delete mode 100644 assets/cube_120.glslf delete mode 100644 assets/cube_120.glslv delete mode 100644 assets/cube_150.glslf delete mode 100644 assets/cube_150.glslv diff --git a/Cargo.toml b/Cargo.toml index 78383f9..011a829 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,25 +1,16 @@ [package] name = "piston-examples" version = "0.0.0" -edition = "2018" +edition = "2024" authors = ["bvssvni "] keywords = [] +[dependencies.piston_window] +version = "0.138.2" +features = ["batteries"] + [dependencies] -piston = "1.0.0" -piston2d-graphics = "0.44.0" -pistoncore-glutin_window = "0.72.0" -piston2d-opengl_graphics = "0.85.0" -piston_window = "0.132.0" find_folder = "0.3.0" -piston-ai_behavior = "0.33.0" -piston2d-sprite = "0.68.0" -image = "0.25.1" piston2d-drag_controller = "0.30.0" -piston2d-deform_grid = "0.14.0" -gfx = "0.18.1" -gfx_device_gl = "0.16.2" -vecmath = "1.0.0" -camera_controllers = "0.34.0" +piston2d-deform_grid = "0.15.0" rand = "0.8.5" -shader_version = "0.7.0" diff --git a/assets/colored_cube.wgsl b/assets/colored_cube.wgsl new file mode 100644 index 0000000..d55b40d --- /dev/null +++ b/assets/colored_cube.wgsl @@ -0,0 +1,24 @@ + +@group(0) @binding(0) +var mvp: mat4x4; + +struct VertexOutput { + @builtin(position) clip_position: vec4, + @location(0) color: vec4, +} + +@vertex +fn vs_main( + @location(0) position: vec3, + @location(1) color: vec4, +) -> VertexOutput { + var out: VertexOutput; + out.clip_position = mvp * vec4(position, 1.0); + out.color = color; + return out; +} + +@fragment +fn fs_main(in: VertexOutput) -> @location(0) vec4 { + return in.color; +} diff --git a/assets/cube_120.glslf b/assets/cube_120.glslf deleted file mode 100644 index 352b8d3..0000000 --- a/assets/cube_120.glslf +++ /dev/null @@ -1,8 +0,0 @@ -#version 120 -varying vec2 v_TexCoord; -uniform sampler2D t_color; -void main() { - vec4 tex = texture2D(t_color, v_TexCoord); - float blend = dot(v_TexCoord-vec2(0.5,0.5), v_TexCoord-vec2(0.5,0.5)); - gl_FragColor = mix(tex, vec4(0.0,0.0,0.0,0.0), blend*1.0); -} diff --git a/assets/cube_120.glslv b/assets/cube_120.glslv deleted file mode 100644 index a19faa7..0000000 --- a/assets/cube_120.glslv +++ /dev/null @@ -1,9 +0,0 @@ -#version 120 -attribute ivec3 a_pos; -attribute ivec2 a_tex_coord; -varying vec2 v_TexCoord; -uniform mat4 u_model_view_proj; -void main() { - v_TexCoord = a_tex_coord; - gl_Position = u_model_view_proj * vec4(a_pos, 1.0); -} diff --git a/assets/cube_150.glslf b/assets/cube_150.glslf deleted file mode 100644 index 2b5c43f..0000000 --- a/assets/cube_150.glslf +++ /dev/null @@ -1,9 +0,0 @@ -#version 150 core -in vec2 v_TexCoord; -out vec4 o_Color; -uniform sampler2D t_color; -void main() { - vec4 tex = texture(t_color, v_TexCoord); - float blend = dot(v_TexCoord-vec2(0.5,0.5), v_TexCoord-vec2(0.5,0.5)); - o_Color = mix(tex, vec4(0.0,0.0,0.0,0.0), blend*1.0); -} diff --git a/assets/cube_150.glslv b/assets/cube_150.glslv deleted file mode 100644 index c065435..0000000 --- a/assets/cube_150.glslv +++ /dev/null @@ -1,9 +0,0 @@ -#version 150 core -in ivec3 a_pos; -in ivec2 a_tex_coord; -out vec2 v_TexCoord; -uniform mat4 u_model_view_proj; -void main() { - v_TexCoord = a_tex_coord; - gl_Position = u_model_view_proj * vec4(a_pos, 1.0); -} diff --git a/examples/cube.rs b/examples/cube.rs index b25d7bc..891523f 100644 --- a/examples/cube.rs +++ b/examples/cube.rs @@ -1,180 +1,203 @@ -extern crate piston_window; -extern crate vecmath; -extern crate camera_controllers; -#[macro_use] -extern crate gfx; -extern crate shader_version; - -//---------------------------------------- -// Cube associated data - -gfx_vertex_struct!( Vertex { - a_pos: [i8; 4] = "a_pos", - a_tex_coord: [i8; 2] = "a_tex_coord", -}); - -impl Vertex { - fn new(pos: [i8; 3], tc: [i8; 2]) -> Vertex { - Vertex { - a_pos: [pos[0], pos[1], pos[2], 1], - a_tex_coord: tc, - } - } -} - -gfx_pipeline!( pipe { - vbuf: gfx::VertexBuffer = (), - u_model_view_proj: gfx::Global<[[f32; 4]; 4]> = "u_model_view_proj", - t_color: gfx::TextureSampler<[f32; 4]> = "t_color", - out_color: gfx::RenderTarget<::gfx::format::Srgba8> = "o_Color", - out_depth: gfx::DepthTarget<::gfx::format::DepthStencil> = - gfx::preset::depth::LESS_EQUAL_WRITE, -}); - -//---------------------------------------- +use piston_window::*; +use turbine::scene3d::*; +use turbine_scene3d_wgpu::{utils, State}; +use turbine::scene3d::Command::*; +use vecmath::*; +use camera_controllers::*; fn main() { - use piston_window::*; - use gfx::traits::*; - use shader_version::Shaders; - use shader_version::glsl::GLSL; - use camera_controllers::{ - FirstPersonSettings, - FirstPerson, - CameraPerspective, - model_view_projection + println!("Toggle camera control by pressing C"); + + let mut capture_cursor = false; + let (mut window, mut scene, vs, fs) = { + let settings = WindowSettings::new("colored cube", [512; 2]) + .samples(4) + .exit_on_esc(true); + let mut window: PistonWindow = settings.build().unwrap(); + window.set_capture_cursor(capture_cursor); + + let depth_texture_view = utils::create_depth_texture_view( + &window.device, + &window.surface_config, + 1, + "depth_texture", + ); + + let mut scene: Scene = + Scene::new(SceneSettings::new(), State::new( + window.device.clone(), + window.queue.clone(), + window.surface_config.clone(), + depth_texture_view, + )); + let vs = scene.vertex_shader(include_str!("../assets/colored_cube.wgsl")).unwrap(); + let fs = scene.fragment_shader(include_str!("../assets/colored_cube.wgsl")).unwrap(); + (window, scene, vs, fs) }; - let opengl = OpenGL::V3_2; - - let mut window: PistonWindow = - WindowSettings::new("piston: cube", [640, 480]) - .exit_on_esc(true) - .samples(4) - .graphics_api(opengl) - .build() - .unwrap(); - window.set_capture_cursor(true); - - let mut factory = window.factory.clone(); - - let vertex_data = vec![ - //top (0, 0, 1) - Vertex::new([-1, -1, 1], [0, 0]), - Vertex::new([ 1, -1, 1], [1, 0]), - Vertex::new([ 1, 1, 1], [1, 1]), - Vertex::new([-1, 1, 1], [0, 1]), - //bottom (0, 0, -1) - Vertex::new([ 1, 1, -1], [0, 0]), - Vertex::new([-1, 1, -1], [1, 0]), - Vertex::new([-1, -1, -1], [1, 1]), - Vertex::new([ 1, -1, -1], [0, 1]), - //right (1, 0, 0) - Vertex::new([ 1, -1, -1], [0, 0]), - Vertex::new([ 1, 1, -1], [1, 0]), - Vertex::new([ 1, 1, 1], [1, 1]), - Vertex::new([ 1, -1, 1], [0, 1]), - //left (-1, 0, 0) - Vertex::new([-1, 1, 1], [0, 0]), - Vertex::new([-1, -1, 1], [1, 0]), - Vertex::new([-1, -1, -1], [1, 1]), - Vertex::new([-1, 1, -1], [0, 1]), - //front (0, 1, 0) - Vertex::new([-1, 1, -1], [0, 0]), - Vertex::new([ 1, 1, -1], [1, 0]), - Vertex::new([ 1, 1, 1], [1, 1]), - Vertex::new([-1, 1, 1], [0, 1]), - //back (0, -1, 0) - Vertex::new([ 1, -1, 1], [0, 0]), - Vertex::new([-1, -1, 1], [1, 0]), - Vertex::new([-1, -1, -1], [1, 1]), - Vertex::new([ 1, -1, -1], [0, 1]), - ]; - - let index_data: &[u16] = &[ - 0, 1, 2, 2, 3, 0, // top - 4, 6, 5, 6, 4, 7, // bottom - 8, 9, 10, 10, 11, 8, // right - 12, 14, 13, 14, 12, 15, // left - 16, 18, 17, 18, 16, 19, // front - 20, 21, 22, 22, 23, 20, // back - ]; - - let (vbuf, slice) = factory.create_vertex_buffer_with_slice - (&vertex_data, index_data); - - let texels = [ - [0xff, 0xff, 0xff, 0x00], - [0xff, 0x00, 0x00, 0x00], - [0x00, 0xff, 0x00, 0x00], - [0x00, 0x00, 0xff, 0x00] - ]; - let (_, texture_view) = factory.create_texture_immutable::( - gfx::texture::Kind::D2(2, 2, gfx::texture::AaMode::Single), - gfx::texture::Mipmap::Provided, - &[&texels]).unwrap(); - - let sinfo = gfx::texture::SamplerInfo::new( - gfx::texture::FilterMethod::Bilinear, - gfx::texture::WrapMode::Clamp); - - let glsl = opengl.to_glsl(); - let pso = factory.create_pipeline_simple( - Shaders::new() - .set(GLSL::V1_20, include_str!("../assets/cube_120.glslv")) - .set(GLSL::V1_50, include_str!("../assets/cube_150.glslv")) - .get(glsl).unwrap().as_bytes(), - Shaders::new() - .set(GLSL::V1_20, include_str!("../assets/cube_120.glslf")) - .set(GLSL::V1_50, include_str!("../assets/cube_150.glslf")) - .get(glsl).unwrap().as_bytes(), - pipe::new() - ).unwrap(); - - let get_projection = |w: &PistonWindow| { - let draw_size = w.window.draw_size(); - CameraPerspective { - fov: 90.0, near_clip: 0.1, far_clip: 1000.0, - aspect_ratio: (draw_size.width as f32) / (draw_size.height as f32) - }.projection() + let mut events = Events::new(EventSettings::new()); + let mut frame_graph = FrameGraph::new(); + + let cube = { + let program = scene.program_from_vertex_fragment(vs, fs); + let mvp = scene.matrix4_uniform(program, "mvp").unwrap(); + + let va = scene.vertex_array(); + let pos = scene.vertex_buffer3(va, 0, &vertex_buffer_data()); + let _col = scene.color_buffer(va, 1, &color_buffer_data()); + + frame_graph.command_list(vec![ + EnableCullFace, + CullFaceBack, + UseProgram(program), + SetModelViewProjection(mvp), + DrawTriangles(va, pos.len()), + ]) }; - let model = vecmath::mat4_id(); - let mut projection = get_projection(&window); + let cubes = frame_graph.command_list(vec![ + Draw(cube), + Translate([2.5, 0.0, 0.0]), + RotateAxisDeg(vec3_normalized([1.0, 0.0, 1.0]), 45.0), + Draw(cube) + ]); + let mut first_person = FirstPerson::new( [0.5, 0.5, 4.0], FirstPersonSettings::keyboard_wasd() ); - let mut data = pipe::Data { - vbuf, - u_model_view_proj: [[0.0; 4]; 4], - t_color: (texture_view, factory.create_sampler(sinfo)), - out_color: window.output_color.clone(), - out_depth: window.output_stencil.clone(), - }; - - while let Some(e) = window.next() { - first_person.event(&e); - - window.draw_3d(&e, |window| { - let args = e.render_args().unwrap(); - - window.encoder.clear(&window.output_color, [0.3, 0.3, 0.3, 1.0]); - window.encoder.clear_depth(&window.output_stencil, 1.0); - - data.u_model_view_proj = model_view_projection( - model, - first_person.camera(args.ext_dt).orthogonal(), - projection - ); - window.encoder.draw(&slice, &pso, &data); - }); - - if e.resize_args().is_some() { - projection = get_projection(&window); - data.out_color = window.output_color.clone(); - data.out_depth = window.output_stencil.clone(); + while let Some(e) = events.next(&mut window) { + if capture_cursor {first_person.event(&e)}; + + if let Some(args) = e.render_args() { + let surface_texture = window.surface.get_current_texture().unwrap(); + scene.state.surface_texture = Some(surface_texture); + + scene.clear([0.0, 0.0, 0.0, 1.0]); + + let proj = get_projection(&window); + scene.projection(proj); + scene.camera(first_person.camera(args.ext_dt).orthogonal()); + scene.model(mat4_id()); + + scene.draw(cubes, &frame_graph); + + scene.state.end_render_pass(); + if let Some(surface_texture) = std::mem::replace( + &mut scene.state.surface_texture, None + ) { + surface_texture.present(); + } + } + + if let Some(button) = e.press_args() { + if let Button::Keyboard(Key::C) = button { + capture_cursor = !capture_cursor; + window.set_capture_cursor(capture_cursor); + } } } } + +#[allow(unused)] +fn get_projection(w: &W) -> Matrix4 { + let draw_size = w.draw_size(); + CameraPerspective { + fov: 90.0, near_clip: 0.1, far_clip: 1000.0, + aspect_ratio: (draw_size.width as f32) / (draw_size.height as f32) + }.projection() +} + +fn vertex_buffer_data() -> Vec { + vec![ + -1.0, -1.0, -1.0, // triangle 1 : begin + -1.0, -1.0, 1.0, + -1.0, 1.0, 1.0, // triangle 1 : end + + 1.0, 1.0, -1.0, // triangle 2 : begin + -1.0, -1.0, -1.0, + -1.0, 1.0, -1.0, // triangle 2 : end + + 1.0, -1.0, 1.0, + -1.0, -1.0, -1.0, + 1.0, -1.0, -1.0, + + 1.0, 1.0, -1.0, + 1.0, -1.0, -1.0, + -1.0, -1.0, -1.0, + + -1.0, -1.0, -1.0, + -1.0, 1.0, 1.0, + -1.0, 1.0, -1.0, + + 1.0, -1.0, 1.0, + -1.0, -1.0, 1.0, + -1.0, -1.0, -1.0, + + -1.0, 1.0, 1.0, + -1.0, -1.0, 1.0, + 1.0, -1.0, 1.0, + + 1.0, 1.0, 1.0, + 1.0, -1.0, -1.0, + 1.0, 1.0, -1.0, + + 1.0, -1.0, -1.0, + 1.0, 1.0, 1.0, + 1.0, -1.0, 1.0, + + 1.0, 1.0, 1.0, + 1.0, 1.0, -1.0, + -1.0, 1.0, -1.0, + + 1.0, 1.0, 1.0, + -1.0, 1.0, -1.0, + -1.0, 1.0, 1.0, + + 1.0, 1.0, 1.0, + -1.0, 1.0, 1.0, + 1.0, -1.0, 1.0 + ] +} + +fn color_buffer_data() -> Vec { + vec![ + 0.583, 0.771, 0.014, 1.0, + 0.609, 0.115, 0.436, 1.0, + 0.327, 0.483, 0.844, 1.0, + 0.822, 0.569, 0.201, 1.0, + 0.435, 0.602, 0.223, 1.0, + 0.310, 0.747, 0.185, 1.0, + 0.597, 0.770, 0.761, 1.0, + 0.559, 0.436, 0.730, 1.0, + 0.359, 0.583, 0.152, 1.0, + 0.483, 0.596, 0.789, 1.0, + 0.559, 0.861, 0.639, 1.0, + 0.195, 0.548, 0.859, 1.0, + 0.014, 0.184, 0.576, 1.0, + 0.771, 0.328, 0.970, 1.0, + 0.406, 0.615, 0.116, 1.0, + 0.676, 0.977, 0.133, 1.0, + 0.971, 0.572, 0.833, 1.0, + 0.140, 0.616, 0.489, 1.0, + 0.997, 0.513, 0.064, 1.0, + 0.945, 0.719, 0.592, 1.0, + 0.543, 0.021, 0.978, 1.0, + 0.279, 0.317, 0.505, 1.0, + 0.167, 0.620, 0.077, 1.0, + 0.347, 0.857, 0.137, 1.0, + 0.055, 0.953, 0.042, 1.0, + 0.714, 0.505, 0.345, 1.0, + 0.783, 0.290, 0.734, 1.0, + 0.722, 0.645, 0.174, 1.0, + 0.302, 0.455, 0.848, 1.0, + 0.225, 0.587, 0.040, 1.0, + 0.517, 0.713, 0.338, 1.0, + 0.053, 0.959, 0.120, 1.0, + 0.393, 0.621, 0.362, 1.0, + 0.673, 0.211, 0.457, 1.0, + 0.820, 0.883, 0.371, 1.0, + 0.982, 0.099, 0.879, 1.0, + ] +} diff --git a/examples/deform.rs b/examples/deform.rs index b94f557..3c35ab2 100644 --- a/examples/deform.rs +++ b/examples/deform.rs @@ -1,22 +1,17 @@ -extern crate piston_window; -extern crate drag_controller; -extern crate find_folder; -extern crate deform_grid; - -use deform_grid::DeformGrid; use piston_window::*; +use deform_grid::DeformGrid; use drag_controller::{ DragController, Drag }; +use wgpu_graphics::{Texture, TextureSettings}; +use graphics::ImageSize; fn main() { println!("Click in the red square and drag."); println!("Toggle grid with G."); println!("Reset grid with R."); - let opengl = OpenGL::V3_2; let mut window: PistonWindow = WindowSettings::new("piston-example-deform", [300, 300]) .exit_on_esc(true) - .graphics_api(opengl) .samples(4) .build() .unwrap(); @@ -24,14 +19,13 @@ fn main() { let assets = find_folder::Search::ParentsThenKids(3, 3) .for_folder("assets").unwrap(); let image = assets.join("rust.png"); - let image = Texture::from_path( + let image_tex = Texture::from_path( &mut window.create_texture_context(), &image, - Flip::None, &TextureSettings::new() ).unwrap(); - let (width, height) = image.get_size(); + let (width, height) = image_tex.get_size(); let width = width as f64; let height = height as f64; let mut grid = DeformGrid::new( @@ -86,10 +80,12 @@ fn main() { } } window.draw_2d(&e, |c, g, _| { + use graphics::*; + clear(color::WHITE, g); // Draw deformed image. - grid.draw_image(&image, &c.draw_state, c.transform, g); + grid.draw_image(&image_tex, &c.draw_state, c.transform, g); if draw_grid { // Draw grid. @@ -115,7 +111,7 @@ fn main() { let original = Ellipse::new([1.0, 0.0, 0.0, 0.5]); let current = Ellipse::new([0.0, 0.0, 0.0, 0.5]); for i in 0..grid.ps.len() { - use piston_window::ellipse::circle; + use ellipse::circle; // Original positions. let x = grid.ps[i][0]; diff --git a/examples/draw_state.rs b/examples/draw_state.rs index d37fbfb..775169d 100644 --- a/examples/draw_state.rs +++ b/examples/draw_state.rs @@ -1,8 +1,6 @@ -extern crate piston_window; -extern crate find_folder; - -use piston_window::draw_state::Blend; use piston_window::*; +use graphics::draw_state::Blend; +use wgpu_graphics::{Texture, TextureSettings}; fn main() { println!("Press A to change blending"); @@ -24,11 +22,12 @@ fn main() { let mut clip_inside = true; let rust_logo = Texture::from_path(&mut window.create_texture_context(), assets.join("rust.png"), - Flip::None, &TextureSettings::new()).unwrap(); window.set_lazy(true); while let Some(e) = window.next() { window.draw_2d(&e, |c, g, _| { + use graphics::*; + clear([0.8, 0.8, 0.8, 1.0], g); g.clear_stencil(0); Rectangle::new([1.0, 0.0, 0.0, 1.0]) diff --git a/examples/freetype/Cargo.toml b/examples/freetype/Cargo.toml index 7444aa6..95cb7fa 100644 --- a/examples/freetype/Cargo.toml +++ b/examples/freetype/Cargo.toml @@ -10,21 +10,24 @@ path = "src/main.rs" [dependencies] piston = "1.0.0" -piston2d-opengl_graphics = "0.85.0" -piston2d-graphics = "0.44.0" -freetype-rs = "0.36.0" +piston2d-opengl_graphics = "0.89.0" +piston2d-graphics = "0.45.0" find_folder = "0.3.0" +[dependencies.freetype-rs] +version = "0.38.0" +features = ["bundled"] + [dependencies.pistoncore-sdl2_window] -version = "0.69.0" +version = "0.71.0" optional = true [dependencies.pistoncore-glfw_window] -version = "0.81.0" +version = "0.82.0" optional = true [dependencies.pistoncore-glutin_window] -version = "0.72.0" +version = "0.73.1" optional = true [features] diff --git a/examples/hello_world.rs b/examples/hello_world.rs index d7aa769..dc7811b 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -1,5 +1,3 @@ -extern crate piston_window; -extern crate find_folder; use piston_window::*; @@ -16,23 +14,22 @@ fn main() { let assets = find_folder::Search::ParentsThenKids(3, 3) .for_folder("assets").unwrap(); println!("{:?}", assets); - let mut glyphs = window.load_font(assets.join("FiraSans-Regular.ttf")).unwrap(); + let ref mut glyphs = window.load_font(assets.join("FiraSans-Regular.ttf")).unwrap(); window.set_lazy(true); while let Some(e) = window.next() { - window.draw_2d(&e, |c, g, device| { + window.draw_2d(&e, |c, g, _| { + use graphics::*; + let transform = c.transform.trans(10.0, 100.0); clear([1.0, 1.0, 1.0, 1.0], g); text::Text::new_color([0.0, 0.0, 0.0, 1.0], 32).draw( "Hello world!", - &mut glyphs, + glyphs, &c.draw_state, transform, g ).unwrap(); - - // Update glyphs before rendering. - glyphs.factory.encoder.flush(device); }); } } diff --git a/examples/image.rs b/examples/image.rs index 4dad864..c71f0bc 100644 --- a/examples/image.rs +++ b/examples/image.rs @@ -1,14 +1,10 @@ -extern crate piston_window; -extern crate find_folder; - use piston_window::*; +use wgpu_graphics::{Texture, TextureSettings}; fn main() { - let opengl = OpenGL::V3_2; let mut window: PistonWindow = WindowSettings::new("piston: image", [300, 300]) .exit_on_esc(true) - .graphics_api(opengl) .build() .unwrap(); @@ -18,12 +14,13 @@ fn main() { let rust_logo: G2dTexture = Texture::from_path( &mut window.create_texture_context(), &rust_logo, - Flip::None, &TextureSettings::new() ).unwrap(); window.set_lazy(true); while let Some(e) = window.next() { window.draw_2d(&e, |c, g, _| { + use graphics::*; + clear([1.0; 4], g); image(&rust_logo, c.transform, g); }); diff --git a/examples/multi_window.rs b/examples/multi_window.rs index e949ead..2c47eb5 100644 --- a/examples/multi_window.rs +++ b/examples/multi_window.rs @@ -1,4 +1,8 @@ -extern crate piston_window; + +/* + +// Multi-window is not supported for PistonWindow at the moment. +// To use multi-window, use another window backend or write a custom one. use piston_window::*; @@ -19,6 +23,7 @@ fn main() { if let Some(e) = window.next() { any_window_open = true; window.draw_2d(&e, |_c, g, _device| { + use graphics::*; clear(colors[i], g); }); } @@ -28,3 +33,6 @@ fn main() { if !any_window_open { break } } } +*/ + +fn main() {} diff --git a/examples/paint.rs b/examples/paint.rs index 3d839d1..e1226ad 100644 --- a/examples/paint.rs +++ b/examples/paint.rs @@ -1,26 +1,19 @@ -extern crate piston_window; -extern crate image as im; -extern crate vecmath; - use piston_window::*; use vecmath::*; +use image as im; +use wgpu_graphics::{Format, Texture, TextureSettings, UpdateTexture}; fn main() { - let opengl = OpenGL::V3_2; let (width, height) = (300, 300); let mut window: PistonWindow = WindowSettings::new("piston: paint", (width, height)) .exit_on_esc(true) - .graphics_api(opengl) .build() .unwrap(); let mut canvas = im::ImageBuffer::new(width, height); let mut draw = false; - let mut texture_context = TextureContext { - factory: window.factory.clone(), - encoder: window.factory.create_command_buffer().into() - }; + let mut texture_context = window.create_texture_context(); let mut texture: G2dTexture = Texture::from_image( &mut texture_context, &canvas, @@ -31,10 +24,16 @@ fn main() { while let Some(e) = window.next() { if e.render_args().is_some() { - texture.update(&mut texture_context, &canvas).unwrap(); - window.draw_2d(&e, |c, g, device| { - // Update texture before rendering. - texture_context.encoder.flush(device); + let (w, h) = canvas.dimensions(); + texture.update( + &mut texture_context, + Format::Rgba8, + &canvas, + [0, 0], + [w, h], + ).unwrap(); + window.draw_2d(&e, |c, g, _device| { + use graphics::*; clear([1.0; 4], g); image(&texture, c.transform, g); diff --git a/examples/shapes.rs b/examples/shapes.rs index 358c61d..8cdc463 100644 --- a/examples/shapes.rs +++ b/examples/shapes.rs @@ -3,16 +3,16 @@ extern crate piston_window; use piston_window::*; fn main() { - let opengl = OpenGL::V3_2; let mut window: PistonWindow = WindowSettings::new("shapes", [512; 2]) .exit_on_esc(true) - .graphics_api(opengl) .build() .unwrap(); window.set_lazy(true); while let Some(e) = window.next() { window.draw_2d(&e, |c, g, _| { + use graphics::*; + clear([1.0; 4], g); for i in 0..5 { let c = c.trans(0.0, i as f64 * 100.0); diff --git a/examples/snake.rs b/examples/snake.rs index c29fd03..74b7b35 100644 --- a/examples/snake.rs +++ b/examples/snake.rs @@ -1,40 +1,25 @@ -extern crate glutin_window; -extern crate graphics; -extern crate opengl_graphics; -extern crate piston; -extern crate rand; - -use glutin_window::GlutinWindow as Window; -use opengl_graphics::{GlGraphics, OpenGL}; -use crate::piston::EventLoop; -use piston::event_loop::{EventSettings, Events}; -use piston::input::{RenderArgs, RenderEvent, UpdateArgs, UpdateEvent, ButtonArgs, ButtonEvent, Button, ButtonState, Key}; -use piston::window::WindowSettings; -use rand::Rng; +use piston_window::*; +use graphics::{Context, Graphics}; +use rand::Rng; enum Direction{ - Up,Down,Left,Right -} - -pub struct Segment{ - x: i32, - y: i32 + Up, + Down, + Left, + Right, } pub struct App { - gl: GlGraphics, // OpenGL drawing backend. - segments: Vec, + segments: Vec<[i32; 2]>, direction: Direction, - applex: i32, - appley: i32, + apple: [i32; 2], score: u32, gameover: bool, - } impl App{ - fn render(&mut self, args: &RenderArgs){ + fn render(&mut self, c: &Context, gl: &mut G) { use graphics::*; const GREEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0]; @@ -43,62 +28,74 @@ impl App{ let mut square_segments: Vec<[f64; 4]> = Vec::new(); for i in &self.segments{ - let x = i.x as f64; - let y = i.y as f64; + let x = i[0] as f64; + let y = i[1] as f64; square_segments.push(rectangle::square(x, y, 10.0)); } - let apple = rectangle::square(self.applex as f64, self.appley as f64, 10.0); - - self.gl.draw(args.viewport(), |c, gl|{ - clear(WHITE, gl); - let transform = c.transform.trans(0.0,0.0).rot_deg(0.0); - for i in square_segments { - rectangle(BLUE, i, transform, gl); - } - rectangle(GREEN, apple, transform, gl); + let apple = rectangle::square(self.apple[0] as f64, self.apple[1] as f64, 10.0); - }); + clear(WHITE, gl); + let transform = c.transform.trans(0.0,0.0).rot_deg(0.0); + for i in square_segments { + rectangle(BLUE, i, transform, gl); + } + rectangle(GREEN, apple, transform, gl); } - fn update(&mut self, _args: &UpdateArgs, windowx: &u32, windowy: &u32){ + fn update(&mut self, _args: &UpdateArgs, size: [i32; 2]){ if self.gameover { return; } + + if self.segments[0] == self.apple { + self.gen_apple_coords(size); + self.score += 1; + } else { + self.segments.pop(); + } + if matches!(self.direction, Direction::Up) { - self.segments.insert(0, Segment{x: self.segments[0].x, y: self.segments[0].y - 10}); + self.segments.insert(0, [ + self.segments[0][0], + self.segments[0][1] - 10 + ]); } if matches!(self.direction, Direction::Down) { - self.segments.insert(0, Segment{x: self.segments[0].x, y: self.segments[0].y + 10}); + self.segments.insert(0, [ + self.segments[0][0], + self.segments[0][1] + 10 + ]); } if matches!(self.direction, Direction::Left) { - self.segments.insert(0, Segment{x: self.segments[0].x - 10, y: self.segments[0].y}); + self.segments.insert(0, [ + self.segments[0][0] - 10, + self.segments[0][1] + ]); } if matches!(self.direction, Direction::Right) { - self.segments.insert(0, Segment{x: self.segments[0].x + 10, y: self.segments[0].y}); + self.segments.insert(0, [self.segments[0][0] + 10, self.segments[0][1]]); } - if self.check_if_collision(&windowx, &windowy) { + if self.check_if_collision(size) { self.gameover = true; return; } - if self.segments[0].x == self.applex && self.segments[0].y == self.appley { - self.gen_apple_coords(&windowx, &windowy); - self.score += 1; - } else { - self.segments.pop(); - } } - fn check_if_collision(&mut self, windowx: &u32, windowy: &u32) -> bool{ - if (self.segments[0].x < 0 || self.segments[0].y < 0) || (self.segments[0].x > *windowx as i32 || self.segments[0].y > *windowy as i32) { + fn check_if_collision(&mut self, size: [i32; 2]) -> bool { + if (self.segments[0][0] < 0 || + self.segments[0][1] < 0) || + (self.segments[0][0] >= size[0] || + self.segments[0][1] > size[1]) + { return true; } for i in 1..self.segments.len() { - if self.segments[0].x == self.segments[i].x && self.segments[0].y == self.segments[i].y { + if self.segments[i] == self.segments[0] { return true; } } return false; } - fn change_directions(&mut self, args: &ButtonArgs){ + fn change_directions(&mut self, args: &ButtonArgs) { if args.state == ButtonState::Press { if args.button == Button::Keyboard(Key::Up) && check_directions(&self.direction, Direction::Up) { self.direction = Direction::Up; @@ -113,61 +110,51 @@ impl App{ self.direction = Direction::Right; } - } } - fn gen_apple_coords(&mut self, windowx: &u32, windowy: &u32){ - self.applex = round_to_nearest_10(rand::thread_rng().gen_range(0..*windowx as i32)); - self.appley = round_to_nearest_10(rand::thread_rng().gen_range(0..*windowy as i32)); - for i in &self.segments{ - if i.x == self.applex && i.y == self.appley { - self.gen_apple_coords(windowx, windowy); + fn gen_apple_coords(&mut self, size: [i32; 2]){ + let w: i32 = size[0] / 10; + let h: i32 = size[1] / 10; + let mut rng = rand::thread_rng(); + self.apple = [10_i32 * rng.gen_range(0..w), 10_i32 * rng.gen_range(0..h)]; + for pos in &self.segments{ + if *pos == self.apple { + self.gen_apple_coords(size); break; } } } - - - } fn main() { - // Change this to OpenGL::V2_1 if not working. - let opengl = OpenGL::V3_2; - let windowx: u32 = 300; - let windowy: u32 = 300; - - // Create a Glutin window. - let mut window: Window = WindowSettings::new("Snake", [windowx, windowy]) - .graphics_api(opengl) + let size: [i32; 2] = [300, 300]; + + let mut window: PistonWindow = WindowSettings::new("Snake", [size[0] as u32, size[1] as u32]) .exit_on_esc(true) .resizable(false) .build() .unwrap(); // Create a new game and run it. - + let mut app = App { - gl: GlGraphics::new(opengl), - segments: vec![Segment{x:50, y:30}, Segment{x:40, y:30}, Segment{x:30, y:30}], + segments: vec![[50, 30], [40, 30], [30, 30]], direction: Direction::Right, - applex: 200, - appley: 30, + apple: [200, 30], score: 0, gameover: false, }; - + let event_settings = EventSettings::new().ups(15); let mut events = Events::new(event_settings); let mut already_pressed = true; while let Some(e) = events.next(&mut window) { - if let Some(args) = e.render_args() { - app.render(&args); - } + window.draw_2d(&e, |c, g, _| { + app.render(&c, g); + }); if let Some(args) = e.update_args() { - let newwindowy = windowy + 50; already_pressed = false; - app.update(&args, &windowx, &newwindowy); + app.update(&args, size); } if app.gameover { println!("Game over! Your score is: {}", app.score); @@ -178,22 +165,23 @@ fn main() { already_pressed = true; app.change_directions(&args); } - + } } } -fn round_to_nearest_10(n: i32) -> i32{ - let a = (n/10) * 10 as i32; - let b = a + 10; - if n - a > b - n { - return b; - } - return a; -} fn check_directions(dir1: &Direction, dir2: Direction) -> bool{ - if (matches!(dir1, Direction::Down) && matches!(dir2, Direction::Up)) || (matches!(dir1 ,Direction::Up) && matches!(dir2 ,Direction::Down)) || (matches!(dir1, Direction::Left) && matches!(dir2, Direction::Right)) || (matches!(dir1, Direction::Right) && matches!(dir2, Direction::Left)) { - return false; - } + if (matches!(dir1, Direction::Down) && + matches!(dir2, Direction::Up)) || + (matches!(dir1 ,Direction::Up) && + matches!(dir2 ,Direction::Down)) || + (matches!(dir1, Direction::Left) && + matches!(dir2, Direction::Right)) || + (matches!(dir1, Direction::Right) && + matches!(dir2, Direction::Left)) + { + return false; + } else { return true; + } } diff --git a/examples/sprite.rs b/examples/sprite.rs index e754d7a..688a462 100644 --- a/examples/sprite.rs +++ b/examples/sprite.rs @@ -1,7 +1,3 @@ -extern crate piston_window; -extern crate ai_behavior; -extern crate sprite; -extern crate find_folder; use std::rc::Rc; @@ -14,14 +10,13 @@ use ai_behavior::{ WaitForever, While, }; +use wgpu_graphics::{Texture, TextureSettings}; fn main() { let (width, height) = (300, 300); - let opengl = OpenGL::V3_2; let mut window: PistonWindow = WindowSettings::new("piston: sprite", (width, height)) .exit_on_esc(true) - .graphics_api(opengl) .build() .unwrap(); @@ -29,14 +24,10 @@ fn main() { .for_folder("assets").unwrap(); let id; let mut scene = Scene::new(); - let mut texture_context = TextureContext { - factory: window.factory.clone(), - encoder: window.factory.create_command_buffer().into() - }; + let mut texture_context = window.create_texture_context(); let tex = Rc::new(Texture::from_path( &mut texture_context, assets.join("rust.png"), - Flip::None, &TextureSettings::new() ).unwrap()); let mut sprite = Sprite::from_texture(tex); @@ -71,6 +62,8 @@ fn main() { scene.event(&e); window.draw_2d(&e, |c, g, _| { + use graphics::*; + clear([1.0, 1.0, 1.0, 1.0], g); scene.draw(c.transform, g); }); diff --git a/examples/texture_swap.rs b/examples/texture_swap.rs index d84191a..2b7742a 100644 --- a/examples/texture_swap.rs +++ b/examples/texture_swap.rs @@ -1,8 +1,7 @@ -extern crate rand; -extern crate piston_window; -extern crate image as im; use piston_window::*; +use image as im; +use wgpu_graphics::{Texture, TextureSettings}; fn main() { let texture_count = 1024; @@ -11,10 +10,7 @@ fn main() { let mut window: PistonWindow = WindowSettings::new("piston", [1024; 2]).build().unwrap(); - let mut texture_context = TextureContext { - factory: window.factory.clone(), - encoder: window.factory.create_command_buffer().into() - }; + let mut texture_context = window.create_texture_context(); let textures = { (0..texture_count).map(|_| { let mut img = im::ImageBuffer::new(2, 2); @@ -29,7 +25,7 @@ fn main() { &img, &TextureSettings::new() ).unwrap() - }).collect::>>() + }).collect::>() }; let mut positions = (0..texture_count) @@ -44,6 +40,8 @@ fn main() { if counter > frames { break; } } window.draw_2d(&e, |c, g, _| { + use graphics::*; + clear([0.0, 0.0, 0.0, 1.0], g); for p in &mut positions { let (x, y) = *p; diff --git a/examples/user_input/Cargo.toml b/examples/user_input/Cargo.toml index 4092136..803bdaa 100644 --- a/examples/user_input/Cargo.toml +++ b/examples/user_input/Cargo.toml @@ -10,24 +10,24 @@ path = "src/main.rs" [dependencies] piston = "1.0.0" -piston2d-opengl_graphics = "0.85.0" -piston2d-graphics = "0.44.0" -piston2d-touch_visualizer = "0.34.0" +piston2d-opengl_graphics = "0.89.0" +piston2d-graphics = "0.45.0" +piston2d-touch_visualizer = "0.35.0" [dependencies.pistoncore-sdl2_window] -version = "0.69.0" +version = "0.71.0" optional = true [dependencies.pistoncore-glfw_window] -version = "0.81.0" +version = "0.82.0" optional = true [dependencies.pistoncore-glutin_window] -version = "0.72.0" +version = "0.73.1" optional = true [dependencies.pistoncore-winit_window] -version = "0.18.0" +version = "0.20.1" optional = true [features]