-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.rs
227 lines (203 loc) · 6.3 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#![no_std]
#![no_main]
use core::ffi::*;
use core::panic::PanicInfo;
use core::mem::zeroed;
use libc::*;
#[panic_handler]
unsafe fn panic(info: &PanicInfo) -> ! {
// TODO: What's the best way to implement the panic handler within the Crust spirit
// PanicInfo must be passed by reference.
if let Some(location) = info.location() {
fprintf!(stderr, c"%.*s:%d: ", location.file().len(), location.file().as_ptr(), location.line());
}
fprintf!(stderr, c"panicked");
if let Some(message) = info.message().as_str() {
fprintf!(stderr, c": %.*s", message.len(), message.as_ptr());
}
fprintf!(stderr, c"\n");
abort();
}
pub mod raymath {
use core::ffi::{c_float};
#[repr(C)]
#[derive(Clone, Copy)]
pub struct Vector2 {
pub x: c_float,
pub y: c_float,
}
}
pub mod raylib {
use core::ffi::{c_int, c_float, c_char};
use crate::raymath::*;
#[repr(C)]
#[derive(Clone, Copy)]
pub struct Color {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}
extern "C" {
#[link_name="InitWindow"]
pub fn init_window(width: c_int, height: c_int, title: *const c_char);
#[link_name="WindowShouldClose"]
pub fn window_should_close() -> bool;
#[link_name="ClearBackground"]
pub fn clear_background(color: Color);
#[link_name="BeginDrawing"]
pub fn begin_drawing();
#[link_name="EndDrawing"]
pub fn end_drawing();
#[link_name="CloseWindow"]
pub fn close_window();
#[link_name="DrawRectangleV"]
pub fn draw_rectangle_v(position: Vector2, size: Vector2, color: Color);
#[link_name="GetFrameTime"]
pub fn get_frame_time() -> c_float;
#[link_name="GetScreenWidth"]
pub fn get_screen_width() -> c_int;
#[link_name="GetScreenHeight"]
pub fn get_screen_height() -> c_int;
}
}
#[macro_use]
pub mod libc {
use core::ffi::*;
pub type FILE = c_void;
extern "C" {
pub static stdin: *mut FILE;
pub static stdout: *mut FILE;
pub static stderr: *mut FILE;
}
#[macro_export]
macro_rules! fprintf {
($stream:expr, $fmt:literal $($args:tt)*) => {{
use core::ffi::c_int;
extern "C" {
#[link_name = "fprintf"]
pub fn fprintf_raw(stream: *mut libc::FILE, fmt: *const c_char, ...) -> c_int;
}
fprintf_raw($stream, $fmt.as_ptr() $($args)*)
}};
}
#[macro_export]
macro_rules! printf {
($fmt:literal $($args:tt)*) => {{
use core::ffi::c_int;
extern "C" {
#[link_name = "printf"]
pub fn printf_raw(fmt: *const u8, ...) -> c_int;
}
printf_raw($fmt.as_ptr() $($args)*)
}};
}
extern "C" {
pub fn abort() -> !;
}
pub unsafe fn realloc_items<T>(ptr: *mut T, count: usize) -> *mut T {
extern "C" {
#[link_name = "realloc"]
fn realloc_raw(ptr: *mut c_void, size: usize) -> *mut c_void;
}
realloc_raw(ptr as *mut c_void, size_of::<T>()*count) as *mut T
}
pub unsafe fn free<T>(ptr: *mut T) {
extern "C" {
#[link_name = "free"]
fn free_raw(ptr: *mut c_void);
}
free_raw(ptr as *mut c_void);
}
}
pub mod da { // Dynamic Arrays in Crust
use crate::libc;
use core::ptr;
#[repr(C)]
#[derive(Clone, Copy)]
pub struct Array<T> {
pub items: *mut T,
pub count: usize,
pub capacity: usize,
}
pub unsafe fn da_append<T>(da: *mut Array<T>, item: T) {
if (*da).count >= (*da).capacity {
if (*da).capacity == 0 {
(*da).capacity = 256;
} else {
(*da).capacity *= 2;
}
(*da).items = libc::realloc_items((*da).items, (*da).capacity);
}
*((*da).items.add((*da).count)) = item;
(*da).count += 1;
}
pub unsafe fn da_destroy<T>(da: *mut Array<T>) {
libc::free((*da).items);
(*da).items = ptr::null_mut();
(*da).count = 0;
(*da).capacity = 0;
}
}
#[derive(Copy, Clone)]
pub struct Rect {
pub position: raymath::Vector2,
pub velocity: raymath::Vector2,
pub color: raylib::Color,
}
#[no_mangle]
pub unsafe extern "C" fn main(_argc: i32, _argv: *mut *mut u8) -> i32 {
use core::ffi::c_float;
use raylib::*;
use raymath::*;
use da::*;
const BACKGROUND: Color = Color {r: 0x18, g: 0x18, b: 0x18, a: 255};
const RECT_SIZE: Vector2 = Vector2 { x: 100.0, y: 100.0 };
let mut rects: Array<Rect> = zeroed();
da_append(&mut rects, Rect {
position: Vector2 { x: 0.0, y: 0.0 },
velocity: Vector2 { x: 100.0, y: 100.0 },
color: Color {r: 0xFF, g: 0x18, b: 0x18, a: 255},
});
da_append(&mut rects, Rect {
position: Vector2 { x: 300.0, y: 20.0 },
velocity: Vector2 { x: 100.0, y: 100.0 },
color: Color {r: 0x18, g: 0xFF, b: 0x18, a: 255},
});
da_append(&mut rects, Rect {
position: Vector2 { x: 20.0, y: 300.0 },
velocity: Vector2 { x: 100.0, y: 100.0 },
color: Color {r: 0x18, g: 0x18, b: 0xFF, a: 255},
});
init_window(800, 600, c"Hello from Crust".as_ptr());
while !window_should_close() {
let dt = get_frame_time();
let w = get_screen_width() as c_float;
let h = get_screen_height() as c_float;
for i in 0..rects.count {
let rect = rects.items.add(i);
let nx = (*rect).position.x + (*rect).velocity.x*dt;
if nx < 0.0 || nx + RECT_SIZE.x >= w {
(*rect).velocity.x *= -1.0;
} else {
(*rect).position.x = nx;
}
let ny = (*rect).position.y + (*rect).velocity.y*dt;
if ny < 0.0 || ny + RECT_SIZE.y >= h {
(*rect).velocity.y *= -1.0;
} else {
(*rect).position.y = ny;
}
}
begin_drawing();
clear_background(BACKGROUND);
for i in 0..rects.count {
let rect = *rects.items.add(i);
draw_rectangle_v(rect.position, RECT_SIZE, rect.color);
}
end_drawing();
}
close_window();
da_destroy(&mut rects);
0
}