diff --git a/Makefile b/Makefile index b454558..a847980 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,10 @@ +.PHONY: all + +all: rect bounce + rect: rect.rs rustc -L./raylib/ -lraylib $< -o bin/$@ + +bounce: bounce.rs + rustc -L./raylib/ -lraylib $< -o bin/$@ + diff --git a/bounce.rs b/bounce.rs new file mode 100644 index 0000000..29a4f91 --- /dev/null +++ b/bounce.rs @@ -0,0 +1,42 @@ +mod raylib; + +use raylib::*; + +fn main() { + let screen: Vector2 = Vector2 { x: 800, y: 600 }; + let ball_radius: f32 = 100.0; + let mut pos: Vector2 = Vector2 { + x: screen.x as f32 / 2.0, + y: screen.y as f32 / 2.0, + }; + let mut velocity: Vector2 = 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(); + } +} + diff --git a/raylib.rs b/raylib.rs index f9aa8cd..df48251 100644 --- a/raylib.rs +++ b/raylib.rs @@ -11,8 +11,12 @@ extern { fn SetTargetFPS(fps: u32); fn BeginDrawing(); fn EndDrawing(); + fn DrawRectangle(pos_x: u32, pos_y: u32, width: u32, height: u32, color: Color); + fn DrawCircleV(center: Vector2, radius: f32, color: Color); + fn IsKeyDown(key: u32) -> bool; + fn GetFrameTime() -> f32; } #[derive(Copy, Clone)] @@ -29,6 +33,13 @@ pub mod colors { pub const BLACK: Color = Color(0, 0, 0, 0); } +#[repr(C)] +#[derive(Copy, Clone)] +pub struct Vector2 { + pub x: S, + pub y: S, +} + pub fn init_window(width: u32, height: u32, title: &str) { unsafe { 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); } } +pub fn draw_circle_v(center: Vector2, radius: f32, color: Color) { + unsafe { DrawCircleV(center, radius, color) } +} + pub fn is_key_down(key: Key) -> bool { unsafe { IsKeyDown(key as u32) } } +pub fn get_frame_time() -> f32 { + unsafe { GetFrameTime() } +} + diff --git a/rect.rs b/rect.rs index b8c5825..5331e2a 100644 --- a/rect.rs +++ b/rect.rs @@ -1,13 +1,8 @@ mod raylib; -pub struct Vector2 { - pub x: S, - pub y: S, -} - fn main() { - let screen = Vector2:: { x: 800, y: 600 }; - let mut pos = Vector2:: { + let screen = raylib::Vector2:: { x: 800, y: 600 }; + let mut pos = raylib::Vector2:: { x: screen.x / 2, y: screen.y / 2 };