-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.rs
138 lines (116 loc) · 3.41 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
extern crate minifb;
use std::cell::RefCell;
use std::collections::HashMap;
// use minifb::Key;
use minifb::Window;
use minifb::WindowOptions;
pub static SUCCESS: u32 = 0;
pub static ERR_WINDOW_NOT_FOUND: u32 = 2;
pub static ERR_UPDATE_WITH_BUFFER_FAILED: u32 = 3;
thread_local! {
static WINDOWS: RefCell<HashMap<u32, Window>> = RefCell::new(HashMap::new());
}
#[no_mangle]
pub extern "C" fn window_new(title: *const i8, width: usize, height: usize) -> u32 {
WINDOWS.with(|map| {
let mut map = map.borrow_mut();
let mut id = 0u32;
while map.contains_key(&id) {
id += 1;
}
let title = unsafe { std::ffi::CStr::from_ptr(title) };
let window = Window::new(
title.to_str().unwrap(),
width,
height,
WindowOptions::default()
).unwrap();
map.insert(id, window);
id
})
}
#[no_mangle]
pub extern "C" fn window_close(id: u32) -> u32 {
WINDOWS.with(|map| {
let mut map = map.borrow_mut();
if map.contains_key(&id) {
map.remove(&id);
SUCCESS
} else {
ERR_WINDOW_NOT_FOUND
}
})
}
#[no_mangle]
pub extern "C" fn window_is_open(id: u32) -> u32 {
WINDOWS.with(|map| {
let map = map.borrow();
if let Some(window) = map.get(&id) {
if window.is_open() { 1 } else { 0 }
} else {
ERR_WINDOW_NOT_FOUND
}
})
}
#[no_mangle]
pub extern "C" fn window_is_active(id: u32) -> u32 {
WINDOWS.with(|map| {
let mut map = map.borrow_mut();
if let Some(window) = map.get_mut(&id) {
if window.is_active() { 1 } else { 0 }
} else {
ERR_WINDOW_NOT_FOUND
}
})
}
#[no_mangle]
pub extern "C" fn window_limit_update_rate(id: u32, ms: u64) -> u32 {
WINDOWS.with(|map| {
let mut map = map.borrow_mut();
if let Some(window) = map.get_mut(&id) {
window.limit_update_rate(if ms == 0 { None } else {
Some(std::time::Duration::from_micros(ms))
});
SUCCESS
} else {
ERR_WINDOW_NOT_FOUND
}
})
}
#[no_mangle]
pub extern "C" fn window_update_with_buffer(id: u32, buffer: *const u8, width: usize, height: usize) -> u32 {
WINDOWS.with(|map| {
let mut map = map.borrow_mut();
if let Some(window) = map.get_mut(&id) {
let bufu8: &[u8] = unsafe { std::slice::from_raw_parts(buffer, width * height * 4) };
let mut buffer = vec![0u32; width * height];
let mut idx = 0;
while idx < buffer.len() {
let r = bufu8[idx * 4 + 0] as u32;
let g = bufu8[idx * 4 + 1] as u32;
let b = bufu8[idx * 4 + 2] as u32;
buffer[idx] = (r << 16) | (g << 8) | b;
idx += 1;
}
if let Ok(()) = window.update_with_buffer(&buffer, width, height) {
SUCCESS
} else {
ERR_UPDATE_WITH_BUFFER_FAILED
}
} else {
ERR_WINDOW_NOT_FOUND
}
})
}
#[no_mangle]
pub extern "C" fn window_update(id: u32) -> u32 {
WINDOWS.with(|map| {
let mut map = map.borrow_mut();
if let Some(window) = map.get_mut(&id) {
window.update();
SUCCESS
} else {
ERR_WINDOW_NOT_FOUND
}
})
}