-
Notifications
You must be signed in to change notification settings - Fork 24
/
lib.rs
495 lines (432 loc) · 13.7 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#![no_std]
#![allow(internal_features)]
#![feature(lang_items, alloc_error_handler, core_intrinsics)]
#![allow(unused_variables, dead_code, unused_imports)]
extern crate alloc;
pub mod display;
pub mod file;
pub mod geometry;
pub mod graphics;
pub mod lua;
pub mod sound;
pub mod sprite;
pub mod system;
use {
crate::{
display::Display,
file::FileSystem,
graphics::{Graphics, PDRect},
lua::Lua,
sound::Sound,
sprite::{
Sprite, SpriteCollideFunction, SpriteDrawFunction, SpriteManager, SpriteUpdateFunction,
},
system::System,
},
alloc::boxed::Box,
anyhow::Error,
core::{fmt, panic::PanicInfo},
crankstart_sys::{playdate_sprite, LCDRect, LCDSprite, SpriteCollisionResponseType},
};
pub struct Playdate {
playdate: *const crankstart_sys::PlaydateAPI,
}
impl Playdate {
pub fn new(
playdate: *const crankstart_sys::PlaydateAPI,
sprite_update: SpriteUpdateFunction,
sprite_draw: SpriteDrawFunction,
) -> Result<Self, Error> {
let playdate_api = unsafe { *playdate };
let system = playdate_api.system;
System::new(system);
let playdate_sprite = playdate_api.sprite;
SpriteManager::new(playdate_sprite, sprite_update, sprite_draw);
let file = playdate_api.file;
FileSystem::new(file);
let graphics = playdate_api.graphics;
Graphics::new(graphics);
let lua = playdate_api.lua;
Lua::new(lua);
let sound = playdate_api.sound;
Sound::new(sound)?;
let display = playdate_api.display;
Display::new(display);
Ok(Self { playdate })
}
}
#[macro_export]
macro_rules! log_to_console {
($($arg:tt)*) => ($crate::system::System::log_to_console(&alloc::format!($($arg)*)));
}
#[macro_export]
macro_rules! pd_func_caller {
($raw_fn_opt:expr, $($arg:tt)*) => {
unsafe {
use alloc::format;
let raw_fn = $raw_fn_opt
.ok_or_else(|| anyhow::anyhow!("{} did not contain a function pointer", stringify!($raw_fn_opt)))?;
Ok(raw_fn($($arg)*))
}
};
($raw_fn_opt:expr) => {
unsafe {
use alloc::format;
let raw_fn = $raw_fn_opt
.ok_or_else(|| anyhow::anyhow!("{} did not contain a function pointer", stringify!($raw_fn_opt)))?;
Ok(raw_fn())
}
};
}
#[macro_export]
macro_rules! pd_func_caller_log {
($raw_fn_opt:expr, $($arg:tt)*) => {
unsafe {
if let Some(raw_fn) = $raw_fn_opt {
raw_fn($($arg)*);
} else {
$crate::log_to_console!("{} did not contain a function pointer", stringify!($raw_fn_opt));
}
}
};
}
pub trait Game {
fn update_sprite(&mut self, sprite: &mut Sprite, playdate: &mut Playdate) -> Result<(), Error> {
use alloc::format;
Err(anyhow::anyhow!("Error: sprite {:?} needs update but this game hasn't implemented the update_sprite trait method", sprite))
}
fn draw_sprite(
&self,
sprite: &Sprite,
bounds: &PDRect,
draw_rect: &PDRect,
playdate: &Playdate,
) -> Result<(), Error> {
use alloc::format;
Err(anyhow::anyhow!("Error: sprite {:?} needs to draw but this game hasn't implemented the draw_sprite trait method", sprite))
}
fn update(&mut self, playdate: &mut Playdate) -> Result<(), Error>;
fn draw_fps(&self) -> bool {
false
}
fn draw_and_update_sprites(&self) -> bool {
true
}
}
pub type GamePtr<T> = Box<T>;
pub struct GameRunner<T: Game> {
game: Option<GamePtr<T>>,
init_failed: bool,
playdate: Playdate,
}
impl<T: 'static + Game> GameRunner<T> {
pub fn new(game: Option<GamePtr<T>>, playdate: Playdate) -> Self {
Self {
init_failed: false,
game,
playdate,
}
}
pub fn update(&mut self) {
if self.init_failed {
return;
}
if let Some(game) = self.game.as_mut() {
if let Err(err) = game.update(&mut self.playdate) {
log_to_console!("Error in update: {err:#}")
}
if game.draw_and_update_sprites() {
if let Err(err) = SpriteManager::get_mut().update_and_draw_sprites() {
log_to_console!("Error from sprite_manager.update_and_draw_sprites: {err:#}")
}
}
if game.draw_fps() {
if let Err(err) = System::get().draw_fps(0, 0) {
log_to_console!("Error from system().draw_fps: {err:#}")
}
}
} else {
log_to_console!("can't get game to update");
self.init_failed = true;
}
}
pub fn update_sprite(&mut self, sprite: *mut LCDSprite) {
if let Some(game) = self.game.as_mut() {
if let Some(mut sprite) = SpriteManager::get_mut().get_sprite(sprite) {
if let Err(err) = game.update_sprite(&mut sprite, &mut self.playdate) {
log_to_console!("Error in update_sprite: {err:#}")
}
} else {
log_to_console!("Can't find sprite {sprite:?} to update");
}
} else {
log_to_console!("can't get game to update_sprite");
}
}
pub fn draw_sprite(&mut self, sprite: *mut LCDSprite, bounds: PDRect, draw_rect: PDRect) {
if let Some(game) = self.game.as_ref() {
if let Some(sprite) = SpriteManager::get_mut().get_sprite(sprite) {
if let Err(err) = game.draw_sprite(&sprite, &bounds, &draw_rect, &self.playdate) {
log_to_console!("Error in draw_sprite: {err:#}")
}
} else {
log_to_console!("Can't find sprite {sprite:?} to draw");
}
} else {
log_to_console!("can't get game to draw_sprite");
}
}
pub fn playdate_sprite(&self) -> *const playdate_sprite {
SpriteManager::get_mut().playdate_sprite
}
}
#[macro_export]
macro_rules! crankstart_game {
($game_struct:tt) => {
crankstart_game!($game_struct, PDSystemEvent::kEventInit);
};
($game_struct:tt, $pd_system_event:expr) => {
pub mod game_setup {
extern crate alloc;
use super::*;
use {
alloc::{boxed::Box, format},
crankstart::{
graphics::PDRect, log_to_console, sprite::SpriteManager, system::System,
GameRunner, Playdate,
},
crankstart_sys::{
LCDRect, LCDSprite, PDSystemEvent, PlaydateAPI, SpriteCollisionResponseType,
},
};
static mut GAME_RUNNER: Option<GameRunner<$game_struct>> = None;
extern "C" fn sprite_update(sprite: *mut LCDSprite) {
let game_runner = unsafe { GAME_RUNNER.as_mut().expect("GAME_RUNNER") };
game_runner.update_sprite(sprite);
}
extern "C" fn sprite_draw(sprite: *mut LCDSprite, bounds: PDRect, drawrect: PDRect) {
let game_runner = unsafe { GAME_RUNNER.as_mut().expect("GAME_RUNNER") };
game_runner.draw_sprite(sprite, bounds, drawrect);
}
extern "C" fn update(_user_data: *mut core::ffi::c_void) -> i32 {
let game_runner = unsafe { GAME_RUNNER.as_mut().expect("GAME_RUNNER") };
game_runner.update();
1
}
#[no_mangle]
extern "C" fn eventHandler(
playdate: *mut PlaydateAPI,
event: PDSystemEvent,
_arg: u32,
) -> crankstart_sys::ctypes::c_int {
if event == $pd_system_event {
// This would only fail if PlaydateAPI has null pointers, which shouldn't happen.
let mut playdate = match Playdate::new(playdate, sprite_update, sprite_draw) {
Ok(playdate) => playdate,
Err(e) => {
log_to_console!("Failed to construct Playdate system: {e:#}");
return 1;
}
};
System::get()
.set_update_callback(Some(update))
.unwrap_or_else(|err| {
log_to_console!("Got error while setting update callback: {err:#}");
});
let game = match $game_struct::new(&mut playdate) {
Ok(game) => Some(game),
Err(err) => {
log_to_console!("Got error while creating game: {err:#}");
None
}
};
unsafe {
GAME_RUNNER = Some(GameRunner::new(game, playdate));
}
}
0
}
}
};
}
fn abort_with_addr(addr: usize) -> ! {
let p = addr as *mut i32;
unsafe {
*p = 0;
}
core::intrinsics::abort()
}
#[panic_handler]
fn panic(#[allow(unused)] panic_info: &PanicInfo) -> ! {
use arrayvec::ArrayString;
use core::fmt::Write;
if let Some(location) = panic_info.location() {
let mut output = ArrayString::<1024>::new();
let payload = if let Some(payload) = panic_info.payload().downcast_ref::<&str>() {
payload
} else {
"no payload"
};
write!(
output,
"panic: {} @ {}:{}\0",
payload,
location.file(),
location.line()
)
.expect("write");
System::log_to_console(output.as_str());
} else {
System::log_to_console("panic\0");
}
#[cfg(target_os = "macos")]
{
unsafe {
core::intrinsics::breakpoint();
}
abort_with_addr(0xdeadbeef);
}
#[cfg(not(target_os = "macos"))]
{
abort_with_addr(0xdeadbeef);
}
}
use core::alloc::{GlobalAlloc, Layout};
pub(crate) struct PlaydateAllocator;
unsafe impl Sync for PlaydateAllocator {}
unsafe impl GlobalAlloc for PlaydateAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let system = System::get();
system.realloc(core::ptr::null_mut(), layout.size()) as *mut u8
}
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
let system = System::get();
system.realloc(ptr as *mut core::ffi::c_void, 0);
}
unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, new_size: usize) -> *mut u8 {
System::get().realloc(ptr as *mut core::ffi::c_void, new_size) as *mut u8
}
}
#[global_allocator]
pub(crate) static mut A: PlaydateAllocator = PlaydateAllocator;
// define what happens in an Out Of Memory (OOM) condition
#[alloc_error_handler]
fn alloc_error(_layout: Layout) -> ! {
System::log_to_console("Out of Memory\0");
abort_with_addr(0xDEADFA11);
}
#[cfg(target_os = "macos")]
#[no_mangle]
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
let mut i = 0;
while i < n {
*dest.add(i) = *src.add(i);
i += 1;
}
dest
}
#[cfg(target_os = "macos")]
#[no_mangle]
pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
if src < dest as *const u8 {
// copy from end
let mut i = n;
while i != 0 {
i -= 1;
*dest.add(i) = *src.add(i);
}
} else {
// copy from beginning
let mut i = 0;
while i < n {
*dest.add(i) = *src.add(i);
i += 1;
}
}
dest
}
#[cfg(target_os = "macos")]
#[no_mangle]
pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
let mut i = 0;
while i < n {
let a = *s1.add(i);
let b = *s2.add(i);
if a != b {
return a as i32 - b as i32;
}
i += 1;
}
0
}
#[cfg(target_os = "macos")]
#[no_mangle]
pub unsafe extern "C" fn bcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
memcmp(s1, s2, n)
}
#[cfg(target_os = "macos")]
pub unsafe fn memset_internal(s: *mut u8, c: crankstart_sys::ctypes::c_int, n: usize) -> *mut u8 {
let mut i = 0;
while i < n {
*s.add(i) = c as u8;
i += 1;
}
s
}
#[cfg(target_os = "macos")]
#[no_mangle]
pub unsafe extern "C" fn memset(s: *mut u8, c: crankstart_sys::ctypes::c_int, n: usize) -> *mut u8 {
memset_internal(s, c, n)
}
#[cfg(target_os = "macos")]
#[no_mangle]
pub unsafe extern "C" fn __bzero(s: *mut u8, n: usize) {
memset_internal(s, 0, n);
}
#[no_mangle]
pub extern "C" fn _sbrk() {}
#[cfg(not(target_os = "windows"))]
#[no_mangle]
pub extern "C" fn _write() {}
#[cfg(not(target_os = "windows"))]
#[no_mangle]
pub extern "C" fn _close() {}
#[cfg(not(target_os = "windows"))]
#[no_mangle]
pub extern "C" fn _lseek() {}
#[cfg(not(target_os = "windows"))]
#[no_mangle]
pub extern "C" fn _read() {}
#[no_mangle]
pub extern "C" fn _fstat() {}
#[no_mangle]
pub extern "C" fn _isatty() {}
#[cfg(not(target_os = "windows"))]
#[no_mangle]
pub extern "C" fn _exit() {}
#[no_mangle]
pub extern "C" fn _open() {}
#[no_mangle]
pub extern "C" fn _kill() {}
#[no_mangle]
pub extern "C" fn _getpid() {}
#[no_mangle]
pub extern "C" fn rust_eh_personality() {
unimplemented!();
}
#[cfg(target_os = "macos")]
#[no_mangle]
extern "C" fn _Unwind_Resume() {
unimplemented!();
}
#[no_mangle]
extern "C" fn __exidx_start() {
unimplemented!();
}
#[no_mangle]
extern "C" fn __exidx_end() {
unimplemented!();
}
#[cfg(target_os = "macos")]
#[link(name = "System")]
extern "C" {}