Implement rect.rs: move a rectangle using keyboard keys

This commit is contained in:
Thibaud Gasser 2024-02-18 11:38:05 +01:00
parent 9e86f4eeb8
commit 3f2220265f
8 changed files with 64 additions and 26 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
bin/

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
rect: rect.rs
rustc -L. -lraylib $< -o bin/$@

3
README.md Normal file
View File

@ -0,0 +1,3 @@
# Rust and raylib playground
A sample project to play with rust, raylib, and rust/c interoperability

BIN
main Executable file

Binary file not shown.

14
main.rs
View File

@ -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();
}
}

View File

@ -6,16 +6,27 @@ pub struct Color(pub u8, pub u8, pub u8, pub u8);
#[link(name = "raylib")] #[link(name = "raylib")]
extern { extern {
fn InitWindow(width: u32, height: u32, title: *const c_char); fn InitWindow(width: u32, height: u32, title: *const c_char);
fn ClearBackground(color: Color);
fn WindowShouldClose() -> bool; fn WindowShouldClose() -> bool;
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 IsKeyPressed(key: u32) -> bool; fn IsKeyDown(key: u32) -> bool;
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub enum Key { 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) { 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 { pub fn window_should_close() -> bool {
unsafe { WindowShouldClose() } unsafe { WindowShouldClose() }
} }
pub fn set_target_fps(fps: u32) {
unsafe { SetTargetFPS(fps); }
}
pub fn begin_drawing() { pub fn begin_drawing() {
unsafe { BeginDrawing(); } 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); } unsafe { DrawRectangle(pos_x, pos_y, width, height, color); }
} }
pub fn is_key_pressed(key: Key) -> bool { pub fn is_key_down(key: Key) -> bool {
let key = key as u32; unsafe { IsKeyDown(key as u32) }
unsafe {
let ret = IsKeyPressed(key);
if ret {
println!("is_key_pressed({key})");
}
ret
}
} }

1
readme
View File

@ -1 +0,0 @@
rustc -L. -lraylib test.rs && ./test

35
rect.rs Normal file
View File

@ -0,0 +1,35 @@
mod raylib;
pub struct Vector2<S> {
pub x: S,
pub y: S,
}
fn main() {
let screen = Vector2::<u32> { x: 800, y: 600 };
let mut pos = Vector2::<u32> {
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();
}
}