rust-raylib-playground/raylib.rs

67 lines
1.4 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();
fn DrawRectangle(pos_x: u32, pos_y: u32, width: u32, height: u32, color: Color);
fn IsKeyDown(key: u32) -> bool;
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
}
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); }
}
pub fn is_key_down(key: Key) -> bool {
unsafe { IsKeyDown(key as u32) }
2024-02-16 21:06:00 +00:00
}