From 27a01e4891d9072df1c388e75aa8f7fd925621f0 Mon Sep 17 00:00:00 2001 From: maxluli Date: Tue, 10 May 2022 11:19:41 +0200 Subject: [PATCH] Added a moving cube with drifty controls and borders --- .gitignore | 1 + Cargo.lock | 63 ++++++++++++++++ Cargo.toml | 9 +++ src/main.rs | 204 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 277 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..2de3ecb --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,63 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "Test_SDL" +version = "0.1.0" +dependencies = [ + "sdl2", +] + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.125" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b" + +[[package]] +name = "sdl2" +version = "0.35.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7959277b623f1fb9e04aea73686c3ca52f01b2145f8ea16f4ff30d8b7623b1a" +dependencies = [ + "bitflags", + "lazy_static", + "libc", + "sdl2-sys", +] + +[[package]] +name = "sdl2-sys" +version = "0.35.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3586be2cf6c0a8099a79a12b4084357aa9b3e0b0d7980e3b67aaf7a9d55f9f0" +dependencies = [ + "cfg-if", + "libc", + "version-compare", +] + +[[package]] +name = "version-compare" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe88247b92c1df6b6de80ddc290f3976dbdf2f5f5d3fd049a9fb598c6dd5ca73" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..be346ce --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "Test_SDL" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +sdl2 = "0.35.2" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..59dc428 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,204 @@ +use sdl2::pixels::Color; +use sdl2::event::Event; +use sdl2::keyboard::Keycode; +use std::time::Duration; +use sdl2::rect::Point; +use sdl2::rect::Rect; + +const WINDOW_HEIGHT:i32 = 600; +const WINDOW_WIDTH:i32 = 800; +const BLOC_SPEED:f32 = 3.0; +const MAX_SPEED:f32 = 20.0; + +pub struct Game{ + main_bloc:Entity, +} +impl Game{ + pub fn render(&mut self,ctx:&mut sdl2::render::Canvas){ + ctx.set_draw_color(Color::RGB(0x6D, 0x6D, 0x64)); + ctx.clear(); + + //Rendering main_bloc + ctx.set_draw_color(Color::RGB(0x00, 0x00, 0x00)); + ctx.fill_rect(self.main_bloc.shape).expect("Could not fill the rectangle"); + ctx.set_draw_color(Color::RGB(0xFF, 0xFF, 0xFF)); + ctx.draw_rect(self.main_bloc.shape).expect("Could not outline the rectangle"); + } + pub fn up(&mut self){ + // 'w' or the up arrow has been pressed + println!("DEBUG: Up is pressed"); + self.main_bloc.velocity.y -= BLOC_SPEED as i32; + if self.main_bloc.velocity.y < -MAX_SPEED as i32{ + self.main_bloc.velocity.y = -MAX_SPEED as i32; + } + } + pub fn down(&mut self){ + // 's' or the down arrow has been pressed + println!("DEBUG: Down is pressed"); + self.main_bloc.velocity.y += BLOC_SPEED as i32; + if self.main_bloc.velocity.y > MAX_SPEED as i32{ + self.main_bloc.velocity.y = MAX_SPEED as i32; + } + } + pub fn left(&mut self){ + // 'a' or the left arrow has been pressed + println!("DEBUG: Left is pressed"); + self.main_bloc.velocity.x -= BLOC_SPEED as i32; + if self.main_bloc.velocity.x < -MAX_SPEED as i32{ + self.main_bloc.velocity.x = -MAX_SPEED as i32; + } + } + pub fn right(&mut self){ + // 'd' or the right arrow has been pressed + println!("DEBUG: Right is pressed"); + self.main_bloc.velocity.x += BLOC_SPEED as i32; + if self.main_bloc.velocity.y > MAX_SPEED as i32{ + self.main_bloc.velocity.y = MAX_SPEED as i32; + } + } + pub fn update(&mut self){ + let mut posx = self.main_bloc.shape.x; + let mut posy = self.main_bloc.shape.y; + posx += self.main_bloc.velocity.x; + posy += self.main_bloc.velocity.y; + let bloc_width:i32 = self.main_bloc.shape.width() as i32; + let bloc_height:i32 = self.main_bloc.shape.height() as i32; + + if posx + bloc_width > WINDOW_WIDTH{ + posx = WINDOW_WIDTH - bloc_width as i32; + self.main_bloc.velocity.x = 0; + }else{ + if posx < 0{ + posx = 0; + self.main_bloc.velocity.x = 0; + } + } + if posy + bloc_height > WINDOW_HEIGHT{ + posy = WINDOW_HEIGHT - bloc_height as i32; + self.main_bloc.velocity.y = 0; + }else{ + if posy < 0{ + posy = 0; + self.main_bloc.velocity.y = 0; + } + } + + if self.main_bloc.velocity.x > 0{ + if self.main_bloc.velocity.x < BLOC_SPEED as i32{ + self.main_bloc.velocity.x = 0; + }else{ + self.main_bloc.velocity.x -= (BLOC_SPEED / 2.0) as i32; + } + }else{ + if self.main_bloc.velocity.x > -BLOC_SPEED as i32{ + self.main_bloc.velocity.x = 0; + }else{ + self.main_bloc.velocity.x += (BLOC_SPEED / 2.0) as i32; + } + } + + if self.main_bloc.velocity.y > 0{ + if self.main_bloc.velocity.y < BLOC_SPEED as i32{ + self.main_bloc.velocity.y = 0; + }else{ + self.main_bloc.velocity.y -= (BLOC_SPEED / 2.0) as i32; + } + }else{ + if self.main_bloc.velocity.y > -BLOC_SPEED as i32{ + self.main_bloc.velocity.y = 0; + }else{ + self.main_bloc.velocity.y += (BLOC_SPEED / 2.0) as i32; + } + } + + self.main_bloc.shape.set_x(posx); + self.main_bloc.shape.set_y(posy); + } +} + +pub struct Entity{ + shape:Rect, + velocity:Point, +} + +pub fn main_loop(canvas: &mut sdl2::render::Canvas,event_pump:&mut sdl2::EventPump,game:&mut Game){ + 'running: loop { + canvas.clear(); + for event in event_pump.poll_iter() { + match event { + Event::Quit {..} | + Event::KeyDown { keycode: Some(Keycode::Escape), .. } => { + break 'running + }, + Event::KeyDown { keycode: Some(Keycode::W), .. } => { + //pressed up + game.up(); + }, + Event::KeyDown { keycode: Some(Keycode::Up), .. } => { + //pressed up + game.up(); + }, + Event::KeyDown { keycode: Some(Keycode::S), .. } => { + //pressed up + game.down(); + }, + Event::KeyDown { keycode: Some(Keycode::Down), .. } => { + //pressed up + game.down(); + }, + Event::KeyDown { keycode: Some(Keycode::A), .. } => { + //pressed up + game.left(); + }, + Event::KeyDown { keycode: Some(Keycode::Left), .. } => { + //pressed up + game.left(); + }, + Event::KeyDown { keycode: Some(Keycode::D), .. } => { + //pressed up + game.right(); + }, + Event::KeyDown { keycode: Some(Keycode::Right), .. } => { + //pressed up + game.right(); + }, + _ => {} + } + } + + // MAIN LOOP + game.update(); + game.render(canvas); + // MAIN LOOP + + canvas.present(); + ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)); + } +} + +pub fn main() { + let sdl_context = sdl2::init().unwrap(); + let video_subsystem = sdl_context.video().unwrap(); + + let window = video_subsystem.window("Test SDL Rust", WINDOW_WIDTH as u32, WINDOW_HEIGHT as u32) + .position_centered() + .build() + .unwrap(); + + let mut canvas = window.into_canvas().build().unwrap(); + let mut event_pump = sdl_context.event_pump().unwrap(); + + canvas.set_draw_color(Color::RGB(0x6D, 0x6D, 0x64)); + + let main_bloc_width:i32 = 50; + let main_bloc_height:i32 = 50; + let main_bloc_position:Point = Point::new(WINDOW_WIDTH / 2 - main_bloc_width / 2,WINDOW_HEIGHT / 2 - main_bloc_height / 2); + let main_bloc_rectangle = Rect::new(main_bloc_position.x,main_bloc_position.y, main_bloc_width as u32, main_bloc_height as u32); + let main_bloc_velocity = Point::new(0,0); + let main_entity = Entity{ + shape:main_bloc_rectangle, + velocity:main_bloc_velocity + }; + let mut game = Game{main_bloc:main_entity}; + main_loop(&mut canvas,&mut event_pump,&mut game); +} \ No newline at end of file