@@ -4,10 +4,14 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
build = "build.rs"
|
||||
|
||||
[lib]
|
||||
name = "my_raylib"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "rect"
|
||||
path = "src/rect.rs"
|
||||
path = "src/demos/rect.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "bounce"
|
||||
path = "src/bounce.rs"
|
||||
path = "src/demos/bounce.rs"
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
39
src/demos/bounce.rs
Normal file
39
src/demos/bounce.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use my_raylib::{colors, Vector2};
|
||||
|
||||
fn main() {
|
||||
let screen = Vector2 { x: 800.0, y: 600.0 };
|
||||
let ball_radius: f32 = 100.0;
|
||||
let mut pos = Vector2 {
|
||||
x: screen.x / 2.0,
|
||||
y: screen.y / 2.0,
|
||||
};
|
||||
let mut velocity = Vector2 { x: 200.0, y: 200.0 };
|
||||
|
||||
my_raylib::init_window(screen.x as u32, screen.y as u32, "Bouncing ball");
|
||||
my_raylib::set_target_fps(60);
|
||||
|
||||
while !my_raylib::window_should_close() {
|
||||
my_raylib::begin_drawing();
|
||||
my_raylib::clear_background(colors::BLACK);
|
||||
|
||||
let dt = my_raylib::get_frame_time();
|
||||
velocity.y += dt * 1000.0;
|
||||
|
||||
let x = pos.x + velocity.x * dt;
|
||||
if x - ball_radius < 0.0 || x + ball_radius >= screen.x {
|
||||
velocity.x *= -1.0;
|
||||
} else {
|
||||
pos.x = x;
|
||||
}
|
||||
|
||||
let y = pos.y + velocity.y * dt;
|
||||
if y - ball_radius < 0.0 || y + ball_radius >= screen.y {
|
||||
velocity.y *= -1.0;
|
||||
} else {
|
||||
pos.y = y;
|
||||
}
|
||||
|
||||
my_raylib::draw_circle_v(pos, ball_radius, colors::RED);
|
||||
my_raylib::end_drawing();
|
||||
}
|
||||
}
|
||||
29
src/demos/rect.rs
Normal file
29
src/demos/rect.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use my_raylib as raylib;
|
||||
|
||||
fn main() {
|
||||
let screen = raylib::Vector2 { x: 800.0, y: 600.0 };
|
||||
let mut pos = raylib::Vector2 {
|
||||
x: screen.x / 2.0,
|
||||
y: screen.y / 2.0,
|
||||
};
|
||||
raylib::init_window(screen.x as u32, screen.y as u32, "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.0;
|
||||
} else if raylib::is_key_down(raylib::Key::S) {
|
||||
pos.y += 10.0;
|
||||
} else if raylib::is_key_down(raylib::Key::A) {
|
||||
pos.x -= 10.0;
|
||||
} else if raylib::is_key_down(raylib::Key::D) {
|
||||
pos.x += 10.0;
|
||||
}
|
||||
|
||||
raylib::draw_rect(pos.x as u32, pos.y as u32, 100, 100, raylib::colors::RED);
|
||||
raylib::end_drawing();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
use std::ffi::{c_char,CString};
|
||||
use std::ffi::{c_char, CString};
|
||||
|
||||
#[repr(C)]
|
||||
pub struct Color(pub u8, pub u8, pub u8, pub u8);
|
||||
|
||||
#[link(name = "raylib")]
|
||||
extern {
|
||||
extern "C" {
|
||||
fn InitWindow(width: u32, height: u32, title: *const c_char);
|
||||
fn ClearBackground(color: Color);
|
||||
fn WindowShouldClose() -> bool;
|
||||
@@ -13,7 +13,7 @@ extern {
|
||||
fn EndDrawing();
|
||||
|
||||
fn DrawRectangle(pos_x: u32, pos_y: u32, width: u32, height: u32, color: Color);
|
||||
fn DrawCircleV(center: Vector2<f32>, radius: f32, color: Color);
|
||||
fn DrawCircleV(center: Vector2, radius: f32, color: Color);
|
||||
|
||||
fn IsKeyDown(key: u32) -> bool;
|
||||
fn GetFrameTime() -> f32;
|
||||
@@ -35,20 +35,22 @@ pub mod colors {
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Vector2<S> {
|
||||
pub x: S,
|
||||
pub y: S,
|
||||
pub struct Vector2 {
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
}
|
||||
|
||||
pub fn init_window(width: u32, height: u32, title: &str) {
|
||||
unsafe {
|
||||
unsafe {
|
||||
let title = CString::new(title).unwrap();
|
||||
InitWindow(width, height, title.as_ptr());
|
||||
InitWindow(width, height, title.as_ptr());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear_background(c: Color) {
|
||||
unsafe { ClearBackground(c); }
|
||||
unsafe {
|
||||
ClearBackground(c);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn window_should_close() -> bool {
|
||||
@@ -56,22 +58,30 @@ pub fn window_should_close() -> bool {
|
||||
}
|
||||
|
||||
pub fn set_target_fps(fps: u32) {
|
||||
unsafe { SetTargetFPS(fps); }
|
||||
unsafe {
|
||||
SetTargetFPS(fps);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn begin_drawing() {
|
||||
unsafe { BeginDrawing(); }
|
||||
unsafe {
|
||||
BeginDrawing();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn end_drawing() {
|
||||
unsafe { EndDrawing(); }
|
||||
unsafe {
|
||||
EndDrawing();
|
||||
}
|
||||
}
|
||||
|
||||
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_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<f32>, radius: f32, color: Color) {
|
||||
pub fn draw_circle_v(center: Vector2, radius: f32, color: Color) {
|
||||
unsafe { DrawCircleV(center, radius, color) }
|
||||
}
|
||||
|
||||
@@ -82,4 +92,3 @@ pub fn is_key_down(key: Key) -> bool {
|
||||
pub fn get_frame_time() -> f32 {
|
||||
unsafe { GetFrameTime() }
|
||||
}
|
||||
|
||||
30
src/rect.rs
30
src/rect.rs
@@ -1,30 +0,0 @@
|
||||
mod raylib;
|
||||
|
||||
fn main() {
|
||||
let screen = raylib::Vector2::<u32> { x: 800, y: 600 };
|
||||
let mut pos = raylib::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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user