rust-raylib-playground/raylib.rs

86 lines
1.8 KiB
Rust
Raw Normal View History

2024-02-16 21:06:00 +00:00
use std::ffi::{c_char,CString};
#[repr(C)]
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);
2024-02-16 21:06:00 +00:00
fn WindowShouldClose() -> bool;
fn SetTargetFPS(fps: u32);
2024-02-16 21:06:00 +00:00
fn BeginDrawing();
fn EndDrawing();
2024-02-18 11:08:29 +00:00
2024-02-16 21:06:00 +00:00
fn DrawRectangle(pos_x: u32, pos_y: u32, width: u32, height: u32, color: Color);
2024-02-18 11:08:29 +00:00
fn DrawCircleV(center: Vector2<f32>, radius: f32, color: Color);
fn IsKeyDown(key: u32) -> bool;
2024-02-18 11:08:29 +00:00
fn GetFrameTime() -> f32;
2024-02-16 21:06:00 +00:00
}
#[derive(Copy, Clone)]
pub enum Key {
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);
2024-02-16 21:06:00 +00:00
}
2024-02-18 11:08:29 +00:00
#[repr(C)]
#[derive(Copy, Clone)]
pub struct Vector2<S> {
pub x: S,
pub y: S,
}
2024-02-16 21:06:00 +00:00
pub fn init_window(width: u32, height: u32, title: &str) {
unsafe {
let title = CString::new(title).unwrap();
InitWindow(width, height, title.as_ptr());
}
}
pub fn clear_background(c: Color) {
unsafe { ClearBackground(c); }
}
2024-02-16 21:06:00 +00:00
pub fn window_should_close() -> bool {
unsafe { WindowShouldClose() }
}
pub fn set_target_fps(fps: u32) {
unsafe { SetTargetFPS(fps); }
}
2024-02-16 21:06:00 +00:00
pub fn begin_drawing() {
unsafe { BeginDrawing(); }
}
pub fn end_drawing() {
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); }
}
2024-02-18 11:08:29 +00:00
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 {
unsafe { IsKeyDown(key as u32) }
2024-02-16 21:06:00 +00:00
}
2024-02-18 11:08:29 +00:00
pub fn get_frame_time() -> f32 {
unsafe { GetFrameTime() }
}