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
#![cfg_attr(not(test), no_std)]
extern crate alloc;

#[allow(unused_imports)]
#[macro_use]
pub extern crate sys;
pub extern crate menu;
pub extern crate display;
pub extern crate ctrl as controls;
pub extern crate gfx as graphics;


// macro re-export
pub use sys::{println, ll_symbols, api, api_opt, api_ok};


pub mod system {
	pub use system::*;
	pub use menu;
}

pub mod sound {
	pub use sound::*;
	pub use sound::prelude::*;
}

pub mod sprite {
	pub use sprite::*;
	pub use sprite::prelude::*;
}

pub mod fs {
	pub use fs::*;
	pub use fs::prelude::*;
}

pub mod lua {
	pub use lua::*;
}


pub mod ext {
	use core::ptr::NonNull;


	/// Main Playdate API entry point.
	pub trait PlaydateAPIExt {
		/// Playdate System API.
		fn system(&self) -> system::System<system::api::Cache>;

		/// Playdate Peripherals API.
		fn peripherals(&self) -> controls::peripherals::Peripherals<controls::api::Cache>;

		/// Playdate File-system API.
		fn file(&self) -> fs::Fs<fs::api::Cache>;

		/// Playdate Graphics API.
		fn graphics(&self) -> graphics::Graphics<graphics::api::Cache>;

		// fn sprite() -> sprite::Sprite;

		/// Playdate Display API.
		fn display(&self) -> display::Display<display::api::Cache>;

		/// Playdate Sound API.
		fn sound(&self) -> sound::Sound<sound::api::Cache>;

		/// Playdate Lua API.
		fn lua(&self) -> lua::Lua<lua::api::Cache>;

		// fn json() -> json::Json;

		fn scoreboards(&self) -> scoreboards::Scoreboards<scoreboards::api::Cache>;
	}


	impl PlaydateAPIExt for NonNull<sys::ffi::PlaydateAPI> {
		fn system(&self) -> system::System<system::api::Cache> {
			system::System::new_with(system::api::Cache::from(unsafe { self.as_ref() }.system))
		}

		fn peripherals(&self) -> controls::peripherals::Peripherals<controls::api::Cache> {
			let api = system::api::Cache::from(unsafe { self.as_ref() }.system);
			controls::peripherals::Peripherals::new_with(api.into())
		}

		fn file(&self) -> fs::Fs<fs::api::Cache> {
			fs::Fs::new_with(fs::api::Cache::from(unsafe { self.as_ref() }.file))
		}

		fn graphics(&self) -> graphics::Graphics<graphics::api::Cache> {
			graphics::Graphics::new_with(graphics::api::Cache::from(unsafe { self.as_ref() }.graphics))
		}

		fn display(&self) -> display::Display<display::api::Cache> {
			display::Display::new_with(display::api::Cache::from(unsafe { self.as_ref() }.display))
		}

		fn sound(&self) -> sound::Sound<sound::api::Cache> {
			sound::Sound::new_with(sound::api::Cache::from(unsafe { self.as_ref() }.sound))
		}

		fn scoreboards(&self) -> scoreboards::Scoreboards<scoreboards::api::Cache> {
			scoreboards::Scoreboards::new_with(scoreboards::api::Cache::from(unsafe { self.as_ref() }.scoreboards))
		}

		fn lua(&self) -> lua::Lua<lua::api::Cache> {
			lua::Lua::new_with(lua::api::Cache::from(unsafe { self.as_ref() }.lua))
		}
	}

	impl PlaydateAPIExt for *const sys::ffi::PlaydateAPI {
		fn system(&self) -> system::System<system::api::Cache> {
			system::System::new_with(system::api::Cache::from(unsafe { self.as_ref() }.expect("api").system))
		}

		fn peripherals(&self) -> controls::peripherals::Peripherals<controls::api::Cache> {
			let api = system::api::Cache::from(unsafe { self.as_ref() }.expect("api").system);
			controls::peripherals::Peripherals::new_with(api.into())
		}

		fn file(&self) -> fs::Fs<fs::api::Cache> {
			fs::Fs::new_with(fs::api::Cache::from(unsafe { self.as_ref() }.expect("api").file))
		}

		fn graphics(&self) -> graphics::Graphics<graphics::api::Cache> {
			graphics::Graphics::new_with(graphics::api::Cache::from(unsafe { self.as_ref() }.expect("api").graphics))
		}

		fn display(&self) -> display::Display<display::api::Cache> {
			display::Display::new_with(display::api::Cache::from(unsafe { self.as_ref() }.expect("api").display))
		}

		fn sound(&self) -> sound::Sound<sound::api::Cache> {
			sound::Sound::new_with(sound::api::Cache::from(unsafe { self.as_ref() }.expect("api").sound))
		}

		fn scoreboards(&self) -> scoreboards::Scoreboards<scoreboards::api::Cache> {
			let api = scoreboards::api::Cache::from(unsafe { self.as_ref() }.expect("api").scoreboards);
			scoreboards::Scoreboards::new_with(api)
		}

		fn lua(&self) -> lua::Lua<lua::api::Cache> {
			lua::Lua::new_with(lua::api::Cache::from(unsafe { self.as_ref() }.expect("api").lua))
		}
	}
}