Skip to content

Commit fc10658

Browse files
committed
Merge branch 'master' of https://github.com/dpx-infinity/rust-sdl2 into dpx-infinity-master
Conflicts: src/sdl2/render.rs
2 parents ef7c518 + 84c9573 commit fc10658

File tree

10 files changed

+103
-35
lines changed

10 files changed

+103
-35
lines changed

src/sdl2/audio.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl<'a> AudioSpec<'a> {
231231
let audio_buf = ptr::null::<u8>();
232232
let audio_len = 0u32;
233233
unsafe {
234-
let ret = ll::SDL_LoadWAV_RW(src.raw, 0, mem::transmute(&spec), &audio_buf, &audio_len);
234+
let ret = ll::SDL_LoadWAV_RW(src.raw(), 0, mem::transmute(&spec), &audio_buf, &audio_len);
235235
if ret.is_null() {
236236
Err(get_error())
237237
} else {
@@ -317,10 +317,13 @@ impl AudioDevice {
317317

318318
#[deriving(Eq)] #[allow(raw_pointer_deriving)]
319319
pub struct AudioCVT {
320-
pub raw: *mut ll::SDL_AudioCVT,
321-
pub owned: bool,
320+
raw: *mut ll::SDL_AudioCVT,
321+
owned: bool,
322322
}
323323

324+
impl_raw_accessors!(AudioCVT, *mut ll::SDL_AudioCVT)
325+
impl_owned_accessors!(AudioCVT, owned)
326+
324327
impl Drop for AudioCVT {
325328
fn drop(&mut self) {
326329
if self.owned {

src/sdl2/keyboard.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn get_keyboard_focus() -> Option<Window> {
7171
if raw == ptr::null() {
7272
None
7373
} else {
74-
Some(Window{ raw: raw, owned: false })
74+
unsafe { Some(Window::from_ll(raw, false)) }
7575
}
7676
}
7777

@@ -163,5 +163,5 @@ pub fn has_screen_keyboard_support() -> bool {
163163
}
164164

165165
pub fn is_screen_keyboard_shown(window: &Window) -> bool {
166-
unsafe { ll::SDL_IsScreenKeyboardShown(window.raw) == 1 }
166+
unsafe { ll::SDL_IsScreenKeyboardShown(window.raw()) == 1 }
167167
}

src/sdl2/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub mod keycode;
1616
#[path = "generated/scancode.rs"]
1717
pub mod scancode;
1818

19+
pub mod macros;
1920
pub mod event;
2021
pub mod gesture;
2122
pub mod touch;

src/sdl2/macros.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![macro_escape]
2+
3+
macro_rules! impl_raw_accessors(
4+
($($t:ty, $raw:ty);+) => (
5+
$(
6+
impl $t {
7+
#[inline]
8+
pub fn raw(&self) -> $raw { self.raw }
9+
}
10+
)+
11+
)
12+
)
13+
14+
macro_rules! impl_owned_accessors(
15+
($($t:ty, $owned:ident);+) => (
16+
$(
17+
impl $t {
18+
#[inline]
19+
pub fn $owned(&self) -> bool { self.$owned }
20+
}
21+
)+
22+
)
23+
)
24+
25+
macro_rules! impl_raw_constructor(
26+
($($t:ty -> $te:ident ($($r:ident:$rt:ty),+));+) => (
27+
$(
28+
impl $t {
29+
#[inline]
30+
pub unsafe fn from_ll($($r:$rt),+) -> $t {
31+
$te { $($r: $r),+ }
32+
}
33+
}
34+
)+
35+
)
36+
)

src/sdl2/mouse.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl Cursor {
105105
// TODO: figure out how to pass Surface in here correctly
106106
pub fn from_surface(surface: &surface::Surface, hot_x: int, hot_y: int) -> Result<Cursor, StrBuf> {
107107
unsafe {
108-
let raw = ll::SDL_CreateColorCursor(surface.raw, hot_x as i32,
108+
let raw = ll::SDL_CreateColorCursor(surface.raw(), hot_x as i32,
109109
hot_y as i32);
110110

111111
if raw == ptr::null() {
@@ -167,7 +167,7 @@ pub fn get_mouse_focus() -> Option<video::Window> {
167167
if raw == ptr::null() {
168168
None
169169
} else {
170-
Some(video::Window{ raw: raw, owned: false })
170+
unsafe { Some(video::Window::from_ll(raw, false)) }
171171
}
172172
}
173173

@@ -190,7 +190,7 @@ pub fn get_relative_mouse_state() -> (MouseState, int, int) {
190190
}
191191

192192
pub fn warp_mouse_in_window(window: &video::Window, x: i32, y: i32) {
193-
unsafe { ll::SDL_WarpMouseInWindow(window.raw, x, y); }
193+
unsafe { ll::SDL_WarpMouseInWindow(window.raw(), x, y); }
194194
}
195195

196196
pub fn set_relative_mouse_mode(on: bool) {

src/sdl2/pixels.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ pub mod ll {
8989
}
9090
#[deriving(Eq)] #[allow(raw_pointer_deriving)]
9191
pub struct Palette {
92-
pub raw: *ll::SDL_Palette
92+
raw: *ll::SDL_Palette
9393
}
9494

95+
impl_raw_accessors!(Palette, *ll::SDL_Palette)
96+
9597
#[deriving(Eq)]
9698
pub enum Color {
9799
RGB(u8, u8, u8),
@@ -132,9 +134,12 @@ impl rand::Rand for Color {
132134

133135
#[deriving(Eq)] #[allow(raw_pointer_deriving)]
134136
pub struct PixelFormat {
135-
pub raw: *ll::SDL_PixelFormat
137+
raw: *ll::SDL_PixelFormat
136138
}
137139

140+
impl_raw_accessors!(PixelFormat, *ll::SDL_PixelFormat)
141+
impl_raw_constructor!(PixelFormat -> PixelFormat (raw: *ll::SDL_PixelFormat))
142+
138143
#[deriving(Eq, Show, FromPrimitive)]
139144
pub enum PixelFormatFlag {
140145
Unknown = ll::SDL_PIXELFORMAT_UNKNOWN as int,

src/sdl2/render.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,9 @@ impl RendererInfo {
202202

203203
#[deriving(Eq)] #[allow(raw_pointer_deriving)]
204204
pub struct Renderer<S> {
205-
pub raw: *ll::SDL_Renderer,
205+
raw: *ll::SDL_Renderer,
206206
parent: Option<S>,
207-
pub owned: bool
207+
owned: bool
208208
}
209209

210210
#[unsafe_destructor]
@@ -226,7 +226,7 @@ impl Renderer<Window> {
226226
};
227227

228228
let raw = unsafe {
229-
ll::SDL_CreateRenderer(window.raw, index as c_int, renderer_flags.bits())
229+
ll::SDL_CreateRenderer(window.raw(), index as c_int, renderer_flags.bits())
230230
};
231231

232232
if raw == ptr::null() {
@@ -241,10 +241,7 @@ impl Renderer<Window> {
241241
let raw_renderer: *ll::SDL_Renderer = ptr::null();
242242
let result = unsafe { ll::SDL_CreateWindowAndRenderer(width as c_int, height as c_int, window_flags.bits(), &raw_window, &raw_renderer) == 0};
243243
if result {
244-
let window = Window {
245-
raw: raw_window,
246-
owned: true
247-
};
244+
let window = unsafe { Window::from_ll(raw_window, true) };
248245
Ok(Renderer {
249246
raw: raw_renderer,
250247
parent: Some(window),
@@ -258,7 +255,7 @@ impl Renderer<Window> {
258255

259256
impl Renderer<Surface> {
260257
pub fn from_surface(surface: surface::Surface) -> Result<Renderer<Surface>, StrBuf> {
261-
let result = unsafe { ll::SDL_CreateSoftwareRenderer(surface.raw) };
258+
let result = unsafe { ll::SDL_CreateSoftwareRenderer(surface.raw()) };
262259
if result == ptr::null() {
263260
Ok(Renderer {
264261
raw: result,
@@ -281,6 +278,12 @@ impl<S> Renderer<S> {
281278
mem::replace(&mut self.parent, None).unwrap()
282279
}
283280

281+
#[inline]
282+
pub fn raw(&self) -> *ll::SDL_Renderer { self.raw }
283+
284+
#[inline]
285+
pub fn owned(&self) -> bool { self.owned }
286+
284287
pub fn set_draw_color(&self, color: pixels::Color) -> Result<(), StrBuf> {
285288
let ret = match color {
286289
pixels::RGB(r, g, b) => {
@@ -340,7 +343,7 @@ impl<S> Renderer<S> {
340343
}
341344

342345
pub fn create_texture_from_surface(&self, surface: &surface::Surface) -> Result<Texture, StrBuf> {
343-
let result = unsafe { ll::SDL_CreateTextureFromSurface(self.raw, surface.raw) };
346+
let result = unsafe { ll::SDL_CreateTextureFromSurface(self.raw, surface.raw()) };
344347
if result == ptr::null() {
345348
Err(get_error())
346349
} else {

src/sdl2/rwops.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ pub mod ll {
4343

4444
#[deriving(Eq)] #[allow(raw_pointer_deriving)]
4545
pub struct RWops {
46-
pub raw: *ll::SDL_RWops,
47-
pub close_on_drop: bool
46+
raw: *ll::SDL_RWops,
47+
close_on_drop: bool
4848
}
4949

50+
impl_raw_accessors!(RWops, *ll::SDL_RWops)
51+
impl_owned_accessors!(RWops, close_on_drop)
52+
5053
/// A structure that provides an abstract interface to stream I/O.
5154
impl RWops {
5255
pub fn from_file(path: &Path, mode: &str) -> Result<RWops, StrBuf> {

src/sdl2/surface.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ bitflags!(flags SurfaceFlag: u32 {
8585
#[deriving(Eq)]
8686
#[allow(raw_pointer_deriving)]
8787
pub struct Surface {
88-
pub raw: *ll::SDL_Surface,
89-
pub owned: bool
88+
raw: *ll::SDL_Surface,
89+
owned: bool
9090
}
9191

9292
impl Drop for Surface {
@@ -99,6 +99,10 @@ impl Drop for Surface {
9999
}
100100
}
101101

102+
impl_raw_accessors!(Surface, *ll::SDL_Surface)
103+
impl_owned_accessors!(Surface, owned)
104+
impl_raw_constructor!(Surface -> Surface (raw: *ll::SDL_Surface, owned: bool))
105+
102106
impl Surface {
103107
pub fn new(surface_flags: SurfaceFlag, width: int, height: int, bpp: int,
104108
rmask: u32, gmask: u32, bmask: u32, amask: u32) -> Result<Surface, StrBuf> {
@@ -137,8 +141,8 @@ impl Surface {
137141
}
138142

139143
pub fn get_pixel_format(&self) -> pixels::PixelFormat {
140-
pixels::PixelFormat {
141-
raw: unsafe { (*self.raw).format }
144+
unsafe {
145+
pixels::PixelFormat::from_ll((*self.raw).format)
142146
}
143147
}
144148

@@ -164,7 +168,7 @@ impl Surface {
164168

165169
pub fn from_bmp(path: &Path) -> Result<Surface, StrBuf> {
166170
let raw = unsafe {
167-
ll::SDL_LoadBMP_RW(try!(rwops::RWops::from_file(path, "rb")).raw, 0)
171+
ll::SDL_LoadBMP_RW(try!(rwops::RWops::from_file(path, "rb")).raw(), 0)
168172
};
169173

170174
if raw.is_null() { Err(get_error()) }
@@ -173,15 +177,15 @@ impl Surface {
173177

174178
pub fn save_bmp(&self, path: &Path) -> Result<(), StrBuf> {
175179
let ret = unsafe {
176-
ll::SDL_SaveBMP_RW(self.raw, try!(rwops::RWops::from_file(path, "rb")).raw, 0)
180+
ll::SDL_SaveBMP_RW(self.raw, try!(rwops::RWops::from_file(path, "rb")).raw(), 0)
177181
};
178182
if ret == 0 { Ok(()) }
179183
else { Err(get_error()) }
180184
}
181185

182186
pub fn set_palette(&self, palette: &pixels::Palette) -> bool {
183187
unsafe {
184-
ll::SDL_SetSurfacePalette(self.raw, palette.raw) == 0
188+
ll::SDL_SetSurfacePalette(self.raw, palette.raw()) == 0
185189
}
186190
}
187191

src/sdl2/video.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ fn unwrap_windowpos (pos: WindowPos) -> ll::SDL_WindowPos {
297297

298298
#[deriving(Eq)]
299299
pub struct GLContext {
300-
pub raw: ll::SDL_GLContext,
301-
pub owned: bool
300+
raw: ll::SDL_GLContext,
301+
owned: bool
302302
}
303303

304304
impl Drop for GLContext {
@@ -311,14 +311,27 @@ impl Drop for GLContext {
311311
}
312312
}
313313

314-
315314
#[deriving(Eq)]
316315
#[allow(raw_pointer_deriving)]
317316
pub struct Window {
318-
pub raw: *ll::SDL_Window,
319-
pub owned: bool
317+
raw: *ll::SDL_Window,
318+
owned: bool
320319
}
321320

321+
impl_raw_accessors!(
322+
GLContext, ll::SDL_GLContext;
323+
Window, *ll::SDL_Window
324+
)
325+
326+
impl_owned_accessors!(
327+
GLContext, owned;
328+
Window, owned
329+
)
330+
331+
impl_raw_constructor!(
332+
Window -> Window (raw: *ll::SDL_Window, owned: bool)
333+
)
334+
322335
impl Drop for Window {
323336
fn drop(&mut self) {
324337
if self.owned {
@@ -427,7 +440,7 @@ impl Window {
427440
}
428441

429442
pub fn set_icon(&self, icon: &Surface) {
430-
unsafe { ll::SDL_SetWindowIcon(self.raw, icon.raw) }
443+
unsafe { ll::SDL_SetWindowIcon(self.raw, icon.raw()) }
431444
}
432445

433446
//pub fn SDL_SetWindowData(window: *SDL_Window, name: *c_char, userdata: *c_void) -> *c_void; //TODO: Figure out what this does
@@ -515,7 +528,7 @@ impl Window {
515528
if raw == ptr::null() {
516529
Err(get_error())
517530
} else {
518-
Ok(Surface {raw: raw, owned: false}) //Docs say that it releases with the window
531+
unsafe { Ok(Surface::from_ll(raw, false)) } //Docs say that it releases with the window
519532
}
520533
}
521534

0 commit comments

Comments
 (0)