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
use std::ffi::{CStr, CString};
use std::marker::PhantomData;
use sys::sdl as ll;
use event::EventPump;
use video::WindowBuilder;
use util::CStringExt;
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum Error {
NoMemError = ll::SDL_ENOMEM as isize,
ReadError = ll::SDL_EFREAD as isize,
WriteError = ll::SDL_EFWRITE as isize,
SeekError = ll::SDL_EFSEEK as isize,
UnsupportedError = ll::SDL_UNSUPPORTED as isize
}
pub type SdlResult<T> = Result<T, String>;
use std::sync::atomic::{AtomicBool, ATOMIC_BOOL_INIT};
static IS_SDL_CONTEXT_ALIVE: AtomicBool = ATOMIC_BOOL_INIT;
pub struct Sdl {
_nosyncsend: PhantomData<*mut ()>
}
impl Sdl {
pub fn was_init(&self, flags: u32) -> u32 {
unsafe {
ll::SDL_WasInit(flags)
}
}
pub fn event_pump(&mut self) -> EventPump {
EventPump::new(self)
}
pub fn window(&self, title: &str, width: u32, height: u32) -> WindowBuilder {
WindowBuilder::new(self, title, width, height)
}
}
impl Drop for Sdl {
fn drop(&mut self) {
use std::sync::atomic::Ordering;
let was_alive = IS_SDL_CONTEXT_ALIVE.swap(false, Ordering::Relaxed);
assert!(was_alive);
unsafe { ll::SDL_Quit(); }
}
}
pub struct Subsystem<'sdl> {
flags: u32,
_marker: PhantomData<&'sdl Sdl>
}
impl<'sdl> Drop for Subsystem<'sdl> {
fn drop(&mut self) {
unsafe { ll::SDL_QuitSubSystem(self.flags); }
}
}
pub struct InitBuilder {
flags: u32
}
impl InitBuilder {
pub fn new() -> InitBuilder {
InitBuilder { flags: 0 }
}
pub fn build(&self) -> SdlResult<Sdl> {
unsafe {
use std::sync::atomic::Ordering;
let was_alive = IS_SDL_CONTEXT_ALIVE.swap(true, Ordering::Relaxed);
if was_alive {
Err(format!("Cannot have more than one `Sdl` in use at the same time"))
} else {
if ll::SDL_Init(self.flags) == 0 {
Ok(Sdl {
_nosyncsend: PhantomData
})
} else {
IS_SDL_CONTEXT_ALIVE.swap(false, Ordering::Relaxed);
Err(get_error())
}
}
}
}
pub fn unwrap(&self) -> Sdl { self.build().unwrap() }
pub fn build_subsystem(&self, _sdl: &Sdl) -> SdlResult<Subsystem> {
unsafe {
if ll::SDL_InitSubSystem(self.flags) == 0 {
Ok(Subsystem {
flags: self.flags,
_marker: PhantomData
})
} else {
Err(get_error())
}
}
}
pub fn everything(&mut self) -> &mut InitBuilder {
self.flags |= ll::SDL_INIT_EVERYTHING as u32;
self
}
pub fn timer(&mut self) -> &mut InitBuilder {
self.flags |= ll::SDL_INIT_TIMER as u32;
self
}
pub fn audio(&mut self) -> &mut InitBuilder {
self.flags |= ll::SDL_INIT_AUDIO as u32;
self
}
pub fn video(&mut self) -> &mut InitBuilder {
self.flags |= ll::SDL_INIT_VIDEO as u32;
self
}
pub fn joystick(&mut self) -> &mut InitBuilder {
self.flags |= ll::SDL_INIT_JOYSTICK as u32;
self
}
pub fn haptic(&mut self) -> &mut InitBuilder {
self.flags |= ll::SDL_INIT_HAPTIC as u32;
self
}
pub fn game_controller(&mut self) -> &mut InitBuilder {
self.flags |= ll::SDL_INIT_GAMECONTROLLER as u32;
self
}
pub fn events(&mut self) -> &mut InitBuilder {
self.flags |= ll::SDL_INIT_EVENTS as u32;
self
}
}
pub fn init() -> InitBuilder { InitBuilder::new() }
pub fn get_error() -> String {
unsafe {
let err = ll::SDL_GetError();
String::from_utf8_lossy(CStr::from_ptr(err).to_bytes()).to_string()
}
}
pub fn set_error(err: &str) {
let err = CString::new(err).remove_nul();
unsafe { ll::SDL_SetError(err.as_ptr()); }
}
pub fn set_error_from_code(err: Error) {
unsafe { ll::SDL_Error(err as ll::SDL_errorcode); }
}
pub fn clear_error() {
unsafe { ll::SDL_ClearError(); }
}
pub fn get_ticks() -> u32 {
unsafe { ll::SDL_GetTicks() as u32 }
}