Added debug mode, space now doubles the vector amount and added a ReadMe
This commit is contained in:
14
Cargo.lock
generated
14
Cargo.lock
generated
@@ -2,13 +2,6 @@
|
||||
# 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"
|
||||
@@ -56,6 +49,13 @@ dependencies = [
|
||||
"version-compare",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "test_sdl"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"sdl2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "version-compare"
|
||||
version = "0.1.0"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
[package]
|
||||
name = "Test_SDL"
|
||||
name = "test_sdl"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
|
||||
31
ReadMe.md
Normal file
31
ReadMe.md
Normal file
@@ -0,0 +1,31 @@
|
||||
# test_sdl
|
||||
|
||||
It is just a playground for me to test SDL2 features
|
||||
|
||||
## Run it
|
||||
|
||||
You need to download Rust
|
||||
|
||||
Then you clone this repository
|
||||
|
||||
And laucnh it with :
|
||||
|
||||
```Bash
|
||||
cargo build --release
|
||||
|
||||
cargo run --release
|
||||
```
|
||||
|
||||
## Controls
|
||||
|
||||
W,A,S,D and Arrow Keys are for moving around the cube.
|
||||
|
||||
Left Click teleports the block the cursors location
|
||||
|
||||
Right Click cancels any velocity of the cube
|
||||
|
||||
Tab toggles a debug mode where you can see the actual vector applied to the cube (maybe even more in the futur)
|
||||
|
||||
Spacebar doubles the acceleration (to use with moderation)
|
||||
|
||||
Bonus: Maintain the left click and moving(s) key(s) at the same time and it will store some momentum wich you can use when you release the click. Enjoy :)
|
||||
80
src/main.rs
80
src/main.rs
@@ -2,7 +2,6 @@ use sdl2::pixels::Color;
|
||||
use sdl2::event::Event;
|
||||
use sdl2::keyboard::Keycode;
|
||||
use sdl2::keyboard::Scancode;
|
||||
use std::collections::HashSet;
|
||||
use std::time::Duration;
|
||||
use sdl2::rect::Point;
|
||||
use sdl2::rect::Rect;
|
||||
@@ -10,57 +9,68 @@ use sdl2::rect::Rect;
|
||||
const WINDOW_HEIGHT:i32 = 800;
|
||||
const WINDOW_WIDTH:i32 = 1000;
|
||||
const BLOC_SPEED:f32 = 3.0;
|
||||
const MAX_SPEED:f32 = 20.0;
|
||||
const GRAVITY:f32 = 10.0;
|
||||
const MAX_SPEED:f32 = 60.0;
|
||||
const DEBUG_FX_SIZE_RATIO:i32 = 2;
|
||||
|
||||
const BOUNCE_RATIO:f32 = 80.0;
|
||||
|
||||
pub struct Game{
|
||||
main_bloc:Entity,
|
||||
draw_debug:bool,
|
||||
}
|
||||
impl Game{
|
||||
pub fn render(&mut self,ctx:&mut sdl2::render::Canvas<sdl2::video::Window>){
|
||||
ctx.set_draw_color(Color::RGB(0x6D, 0x6D, 0x64));
|
||||
ctx.clear();
|
||||
|
||||
//Rendering main_bloc
|
||||
ctx.set_draw_color(Color::RGB(0x33, 0x33, 0x33));
|
||||
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");
|
||||
if self.draw_debug {
|
||||
ctx.set_draw_color(Color::RGB(0x8b,0x00,0x00));
|
||||
let center:Point = Point::new(self.main_bloc.shape.x + (self.main_bloc.shape.width() / 2) as i32,self.main_bloc.shape.y + (self.main_bloc.shape.width() / 2) as i32);
|
||||
let apex:Point = Point::new(center.x + (self.main_bloc.velocity.x * DEBUG_FX_SIZE_RATIO) as i32,center.y + (self.main_bloc.velocity.y * DEBUG_FX_SIZE_RATIO) as i32);
|
||||
ctx.draw_line(center,apex).expect("Could not render the Debug FX");
|
||||
}
|
||||
}
|
||||
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 toggle_debug(&mut self){
|
||||
self.draw_debug = !self.draw_debug;
|
||||
}
|
||||
pub fn double_speed(&mut self){
|
||||
self.main_bloc.velocity.x *= 2;
|
||||
self.main_bloc.velocity.y *= 2;
|
||||
}
|
||||
pub fn left_click(&mut self,mouse_position:Point){
|
||||
// moves the bloc at the cursor position
|
||||
self.main_bloc.shape.set_x(mouse_position.x - (self.main_bloc.shape.width() / 2) as i32);
|
||||
self.main_bloc.shape.set_y(mouse_position.y - (self.main_bloc.shape.height() / 2) as i32);
|
||||
}
|
||||
pub fn right_click(&mut self,mouse_position:Point){
|
||||
// Removes any vectors
|
||||
self.main_bloc.velocity = Point::new(0,0);
|
||||
}
|
||||
pub fn update(&mut self){
|
||||
let bloc = &mut self.main_bloc;
|
||||
let mut posx = bloc.shape.x;
|
||||
@@ -125,25 +135,55 @@ fn is_left_pressed(e: &sdl2::EventPump)-> bool{
|
||||
fn is_right_pressed(e: &sdl2::EventPump)-> bool{
|
||||
e.keyboard_state().is_scancode_pressed(Scancode::D) || e.keyboard_state().is_scancode_pressed(Scancode::Right)
|
||||
}
|
||||
fn is_tab_pressed(e: &sdl2::EventPump)-> bool{
|
||||
e.keyboard_state().is_scancode_pressed(Scancode::Tab)
|
||||
}
|
||||
fn is_space_pressed(e: &sdl2::EventPump)-> bool{
|
||||
e.keyboard_state().is_scancode_pressed(Scancode::Space)
|
||||
}
|
||||
fn check_mouse_left_pressed(e: &sdl2::EventPump) -> Point{
|
||||
if e.mouse_state().left(){
|
||||
return Point::new(e.mouse_state().x(),e.mouse_state().y());
|
||||
}else{
|
||||
return Point::new(-1,-1);
|
||||
}
|
||||
}
|
||||
fn check_mouse_right_pressed(e: &sdl2::EventPump) -> Point{
|
||||
if e.mouse_state().right(){
|
||||
return Point::new(e.mouse_state().x(),e.mouse_state().y());
|
||||
}else{
|
||||
return Point::new(-1,-1);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main_loop(canvas: &mut sdl2::render::Canvas<sdl2::video::Window>,event_pump:&mut sdl2::EventPump,game:&mut Game){
|
||||
'running: loop {
|
||||
if is_up_pressed(event_pump){
|
||||
//println!("Hello");
|
||||
game.up();
|
||||
}
|
||||
if is_down_pressed(event_pump){
|
||||
//println!("Hello");
|
||||
game.down();
|
||||
}
|
||||
if is_left_pressed(event_pump){
|
||||
//println!("Hello");
|
||||
game.left();
|
||||
}
|
||||
if is_right_pressed(event_pump){
|
||||
//println!("Hello");
|
||||
game.right();
|
||||
}
|
||||
if is_tab_pressed(event_pump){
|
||||
game.toggle_debug();
|
||||
}
|
||||
if is_space_pressed(event_pump){
|
||||
game.double_speed();
|
||||
}
|
||||
let mut mouse_position:Point = check_mouse_left_pressed(event_pump);
|
||||
if mouse_position.x >=0 && mouse_position.y >= 0{
|
||||
game.left_click(mouse_position);
|
||||
}
|
||||
mouse_position = check_mouse_right_pressed(event_pump);
|
||||
if mouse_position.x >=0 && mouse_position.y >= 0{
|
||||
game.right_click(mouse_position);
|
||||
}
|
||||
|
||||
for event in event_pump.poll_iter() {
|
||||
match event {
|
||||
@@ -188,6 +228,6 @@ pub fn main() {
|
||||
shape:main_bloc_rectangle,
|
||||
velocity:main_bloc_velocity
|
||||
};
|
||||
let mut game = Game{main_bloc:main_entity};
|
||||
let mut game = Game{main_bloc:main_entity,draw_debug:false};
|
||||
main_loop(&mut canvas,&mut event_pump,&mut game);
|
||||
}
|
||||
Reference in New Issue
Block a user