implement bounce.rs

This commit is contained in:
Thibaud Gasser 2024-02-18 12:08:29 +01:00
parent 372d3d1137
commit e3c86e0e37
4 changed files with 71 additions and 7 deletions

View File

@ -1,2 +1,10 @@
.PHONY: all
all: rect bounce
rect: rect.rs rect: rect.rs
rustc -L./raylib/ -lraylib $< -o bin/$@ rustc -L./raylib/ -lraylib $< -o bin/$@
bounce: bounce.rs
rustc -L./raylib/ -lraylib $< -o bin/$@

42
bounce.rs Normal file
View File

@ -0,0 +1,42 @@
mod raylib;
use raylib::*;
fn main() {
let screen: Vector2<u32> = Vector2 { x: 800, y: 600 };
let ball_radius: f32 = 100.0;
let mut pos: Vector2<f32> = Vector2 {
x: screen.x as f32 / 2.0,
y: screen.y as f32 / 2.0,
};
let mut velocity: Vector2<f32> = Vector2 { x: 200.0, y: 200.0 };
init_window(screen.x, screen.y, "Bouncing ball");
set_target_fps(60);
while !window_should_close() {
begin_drawing();
clear_background(colors::BLACK);
let dt: f32 = get_frame_time();
velocity.y += dt * 1000.0;
let x: f32 = pos.x + velocity.x * dt;
if x - ball_radius < 0.0 || x + ball_radius >= screen.x as f32{
velocity.x *= -1.0;
} else {
pos.x = x;
}
let y: f32 = pos.y + velocity.y * dt;
if y - ball_radius < 0.0 || y + ball_radius >= screen.y as f32 {
velocity.y *= -1.0;
} else {
pos.y = y;
}
draw_circle_v(pos, ball_radius, colors::RED);
end_drawing();
}
}

View File

@ -11,8 +11,12 @@ extern {
fn SetTargetFPS(fps: u32); fn SetTargetFPS(fps: u32);
fn BeginDrawing(); fn BeginDrawing();
fn EndDrawing(); fn EndDrawing();
fn DrawRectangle(pos_x: u32, pos_y: u32, width: u32, height: u32, color: Color); fn DrawRectangle(pos_x: u32, pos_y: u32, width: u32, height: u32, color: Color);
fn DrawCircleV(center: Vector2<f32>, radius: f32, color: Color);
fn IsKeyDown(key: u32) -> bool; fn IsKeyDown(key: u32) -> bool;
fn GetFrameTime() -> f32;
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
@ -29,6 +33,13 @@ pub mod colors {
pub const BLACK: Color = Color(0, 0, 0, 0); pub const BLACK: Color = Color(0, 0, 0, 0);
} }
#[repr(C)]
#[derive(Copy, Clone)]
pub struct Vector2<S> {
pub x: S,
pub y: S,
}
pub fn init_window(width: u32, height: u32, title: &str) { pub fn init_window(width: u32, height: u32, title: &str) {
unsafe { unsafe {
let title = CString::new(title).unwrap(); let title = CString::new(title).unwrap();
@ -60,7 +71,15 @@ pub fn draw_rect(pos_x: u32, pos_y: u32, width:u32, height: u32, color: Color) {
unsafe { DrawRectangle(pos_x, pos_y, width, height, color); } unsafe { DrawRectangle(pos_x, pos_y, width, height, color); }
} }
pub fn draw_circle_v(center: Vector2<f32>, radius: f32, color: Color) {
unsafe { DrawCircleV(center, radius, color) }
}
pub fn is_key_down(key: Key) -> bool { pub fn is_key_down(key: Key) -> bool {
unsafe { IsKeyDown(key as u32) } unsafe { IsKeyDown(key as u32) }
} }
pub fn get_frame_time() -> f32 {
unsafe { GetFrameTime() }
}

View File

@ -1,13 +1,8 @@
mod raylib; mod raylib;
pub struct Vector2<S> {
pub x: S,
pub y: S,
}
fn main() { fn main() {
let screen = Vector2::<u32> { x: 800, y: 600 }; let screen = raylib::Vector2::<u32> { x: 800, y: 600 };
let mut pos = Vector2::<u32> { let mut pos = raylib::Vector2::<u32> {
x: screen.x / 2, x: screen.x / 2,
y: screen.y / 2 y: screen.y / 2
}; };