diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e660fd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0cf7dfa --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +rect: rect.rs + rustc -L. -lraylib $< -o bin/$@ diff --git a/README.md b/README.md new file mode 100644 index 0000000..1782f2c --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Rust and raylib playground + +A sample project to play with rust, raylib, and rust/c interoperability diff --git a/main b/main new file mode 100755 index 0000000..0decc30 Binary files /dev/null and b/main differ diff --git a/main.rs b/main.rs deleted file mode 100644 index 8e73505..0000000 --- a/main.rs +++ /dev/null @@ -1,14 +0,0 @@ -mod raylib; - -fn main() { - raylib::init_window(800, 600, "Hello, raylib"); - while !raylib::window_should_close() { - raylib::begin_drawing(); - raylib::draw_rect(0, 0, 100, 200, raylib::Color(255, 0, 0, 255)); - if raylib::is_key_pressed(raylib::Key::Z) { - raylib::draw_rect(200, 200, 100, 100, raylib::Color(0, 255, 0, 0)); - } - raylib::end_drawing(); - } -} - diff --git a/raylib.rs b/raylib.rs index 7ea1e92..f9aa8cd 100644 --- a/raylib.rs +++ b/raylib.rs @@ -6,16 +6,27 @@ pub struct Color(pub u8, pub u8, pub u8, pub u8); #[link(name = "raylib")] extern { fn InitWindow(width: u32, height: u32, title: *const c_char); + fn ClearBackground(color: Color); fn WindowShouldClose() -> bool; + fn SetTargetFPS(fps: u32); fn BeginDrawing(); fn EndDrawing(); fn DrawRectangle(pos_x: u32, pos_y: u32, width: u32, height: u32, color: Color); - fn IsKeyPressed(key: u32) -> bool; + fn IsKeyDown(key: u32) -> bool; } #[derive(Copy, Clone)] pub enum Key { - Z = 89, + W = 87, + S = 83, + A = 65, + D = 68, +} + +pub mod colors { + use super::Color; + pub const RED: Color = Color(255, 0, 0, 255); + pub const BLACK: Color = Color(0, 0, 0, 0); } pub fn init_window(width: u32, height: u32, title: &str) { @@ -25,10 +36,18 @@ pub fn init_window(width: u32, height: u32, title: &str) { } } +pub fn clear_background(c: Color) { + unsafe { ClearBackground(c); } +} + pub fn window_should_close() -> bool { unsafe { WindowShouldClose() } } +pub fn set_target_fps(fps: u32) { + unsafe { SetTargetFPS(fps); } +} + pub fn begin_drawing() { unsafe { BeginDrawing(); } } @@ -41,14 +60,7 @@ 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 is_key_pressed(key: Key) -> bool { - let key = key as u32; - unsafe { - let ret = IsKeyPressed(key); - if ret { - println!("is_key_pressed({key})"); - } - ret - } +pub fn is_key_down(key: Key) -> bool { + unsafe { IsKeyDown(key as u32) } } diff --git a/readme b/readme deleted file mode 100644 index ba7bba6..0000000 --- a/readme +++ /dev/null @@ -1 +0,0 @@ -rustc -L. -lraylib test.rs && ./test diff --git a/rect.rs b/rect.rs new file mode 100644 index 0000000..b8c5825 --- /dev/null +++ b/rect.rs @@ -0,0 +1,35 @@ +mod raylib; + +pub struct Vector2 { + pub x: S, + pub y: S, +} + +fn main() { + let screen = Vector2:: { x: 800, y: 600 }; + let mut pos = Vector2:: { + x: screen.x / 2, + y: screen.y / 2 + }; + raylib::init_window(screen.x, screen.y, "Hello, raylib"); + raylib::set_target_fps(60); + + while !raylib::window_should_close() { + raylib::begin_drawing(); + raylib::clear_background(raylib::colors::BLACK); + + if raylib::is_key_down(raylib::Key::W) { + pos.y -= 10; + } else if raylib::is_key_down(raylib::Key::S) { + pos.y += 10; + } else if raylib::is_key_down(raylib::Key::A) { + pos.x -= 10; + } else if raylib::is_key_down(raylib::Key::D) { + pos.x += 10; + } + + raylib::draw_rect(pos.x, pos.y, 100, 100, raylib::colors::RED); + raylib::end_drawing(); + } +} +