Skip to content

Commit eb87055

Browse files
[WIP] Use extern types instead of empty enums
1 parent 47f1536 commit eb87055

File tree

5 files changed

+167
-136
lines changed

5 files changed

+167
-136
lines changed

src/audio/sound_buffer.rs

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,64 @@ use std::io::{Read, Seek};
77
use std::ops::Deref;
88
use std::slice;
99
use system::Time;
10+
use std::fmt::{self, Debug};
1011

11-
/// Storage for audio samples defining a sound.
12-
///
13-
/// A sound buffer holds the data of a sound, which is an array of audio samples.
14-
///
15-
/// A sample is a 16 bits signed integer that defines the amplitude of the sound at a given time.
16-
/// The sound is then reconstituted by playing these samples at a high rate
17-
/// (for example, 44100 samples per second is the standard rate used for playing CDs).
18-
/// In short, audio samples are like texture pixels, and a `SoundBuffer` is similar to a `Texture`.
19-
///
20-
/// A sound buffer can be loaded from a file (see `from_file()` for the complete list of
21-
/// supported formats), from memory, from a custom stream or directly from an array of samples.
22-
/// It can also be saved back to a file.
23-
///
24-
/// Sound buffers alone are not very useful: they hold the audio data but cannot be played.
25-
/// To do so, you need to use the `Sound` type, which provides functions to play/pause/stop
26-
/// the sound as well as changing the way it is outputted (volume, pitch, 3D position, ...).
27-
/// This separation allows more flexibility and better performances: indeed a `SoundBuffer` is
28-
/// a heavy resource, and any operation on it is slow (often too slow for real-time applications).
29-
/// On the other side, a `Sound` is a lightweight object, which can use the audio data of a sound
30-
/// buffer and change the way it is played without actually modifying that data.
31-
/// Note that it is also possible to bind several `Sound` instances to the same `SoundBuffer`.
32-
///
33-
/// It is important to note that the `Sound` instance doesn't copy the buffer that it uses,
34-
/// it only keeps a reference to it. Thus, a `SoundBuffer` can not be destructed while it is
35-
/// borrowed by a `Sound`.
36-
///
37-
/// # Usage example
38-
///
39-
/// ```no_run
40-
/// use sfml::audio::{Sound, SoundBuffer, SoundSource};
41-
///
42-
/// // Load a new sound buffer
43-
/// let buffer = SoundBuffer::from_file("sound.wav").unwrap();
44-
///
45-
/// // Create a sound source and bind it to the buffer
46-
/// let mut sound_1 = Sound::with_buffer(&buffer);
47-
///
48-
/// // Play the sound
49-
/// sound_1.play();
50-
///
51-
/// // Create another sound source bound to the same buffer
52-
/// let mut sound_2 = Sound::with_buffer(&buffer);
53-
///
54-
/// // Play it with a higher pitch -- the first sound remains unchanged
55-
/// sound_2.set_pitch(2.0);
56-
/// sound_2.play();
57-
/// ```
58-
#[derive(Debug)]
59-
#[allow(missing_copy_implementations)]
60-
pub enum SoundBuffer {}
12+
extern "C" {
13+
/// Storage for audio samples defining a sound.
14+
///
15+
/// A sound buffer holds the data of a sound, which is an array of audio samples.
16+
///
17+
/// A sample is a 16 bits signed integer that defines the amplitude of the sound at a given time.
18+
/// The sound is then reconstituted by playing these samples at a high rate
19+
/// (for example, 44100 samples per second is the standard rate used for playing CDs).
20+
/// In short, audio samples are like texture pixels, and a `SoundBuffer` is similar to a `Texture`.
21+
///
22+
/// A sound buffer can be loaded from a file (see `from_file()` for the complete list of
23+
/// supported formats), from memory, from a custom stream or directly from an array of samples.
24+
/// It can also be saved back to a file.
25+
///
26+
/// Sound buffers alone are not very useful: they hold the audio data but cannot be played.
27+
/// To do so, you need to use the `Sound` type, which provides functions to play/pause/stop
28+
/// the sound as well as changing the way it is outputted (volume, pitch, 3D position, ...).
29+
/// This separation allows more flexibility and better performances: indeed a `SoundBuffer` is
30+
/// a heavy resource, and any operation on it is slow (often too slow for real-time applications).
31+
/// On the other side, a `Sound` is a lightweight object, which can use the audio data of a sound
32+
/// buffer and change the way it is played without actually modifying that data.
33+
/// Note that it is also possible to bind several `Sound` instances to the same `SoundBuffer`.
34+
///
35+
/// It is important to note that the `Sound` instance doesn't copy the buffer that it uses,
36+
/// it only keeps a reference to it. Thus, a `SoundBuffer` can not be destructed while it is
37+
/// borrowed by a `Sound`.
38+
///
39+
/// # Usage example
40+
///
41+
/// ```no_run
42+
/// use sfml::audio::{Sound, SoundBuffer, SoundSource};
43+
///
44+
/// // Load a new sound buffer
45+
/// let buffer = SoundBuffer::from_file("sound.wav").unwrap();
46+
///
47+
/// // Create a sound source and bind it to the buffer
48+
/// let mut sound_1 = Sound::with_buffer(&buffer);
49+
///
50+
/// // Play the sound
51+
/// sound_1.play();
52+
///
53+
/// // Create another sound source bound to the same buffer
54+
/// let mut sound_2 = Sound::with_buffer(&buffer);
55+
///
56+
/// // Play it with a higher pitch -- the first sound remains unchanged
57+
/// sound_2.set_pitch(2.0);
58+
/// sound_2.play();
59+
/// ```
60+
pub type SoundBuffer;
61+
}
62+
63+
impl Debug for SoundBuffer {
64+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65+
write!(f, "SoundBuffer at {:p}", self)
66+
}
67+
}
6168

6269
impl SoundBuffer {
6370
/// Save a sound buffer to an audio file

src/graphics/font.rs

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,56 @@ use std::borrow::{Borrow, ToOwned};
77
use std::ffi::{CStr, CString};
88
use std::io::{Read, Seek};
99
use std::ops::{Deref, DerefMut};
10+
use std::fmt::{self, Debug};
1011

11-
/// Type for loading and manipulating character fonts.
12-
///
13-
/// Fonts can be loaded from a file, from memory or from a custom stream,
14-
/// and supports the most common types of fonts.
15-
///
16-
/// See the `from_file` function for the complete list of supported formats.
17-
///
18-
/// Once it is loaded, a `Font` instance provides three types of information about the font:
19-
///
20-
/// - Global metrics, such as the line spacing
21-
/// - Per-glyph metrics, such as bounding box or kerning
22-
/// - Pixel representation of glyphs
23-
///
24-
/// Fonts alone are not very useful: they hold the font data but cannot make anything useful of it.
25-
/// To do so you need to use the `Text` type, which is able to properly output text with
26-
/// several options such as character size, style, color, position, rotation, etc.
27-
/// This separation allows more flexibility and better performances:
28-
/// indeed a `Font` is a heavy resource, and any operation on it is
29-
/// slow (often too slow for real-time applications).
30-
/// On the other side, a `Text` is a lightweight object which can combine the
31-
/// glyphs data and metrics of a `Font` to display any text on a render target.
32-
/// Note that it is also possible to bind several `Text` instances to the same `Font`.
33-
///
34-
/// It is important to note that the `Text` instance doesn't copy the font that it uses,
35-
/// it only keeps a reference to it.
36-
/// Thus, a `Font` must not be destructed while it is used by a
37-
/// `Text` (i.e. never write a function that uses a local `Font` instance for creating a text).
38-
///
39-
/// Apart from loading font files, and passing them to instances of `Text`,
40-
/// you should normally not have to deal directly with this type.
41-
/// However, it may be useful to access the font metrics or rasterized glyphs for advanced usage.
42-
///
43-
/// Note that if the font is a bitmap font, it is not scalable,
44-
/// thus not all requested sizes will be available to use.
45-
/// This needs to be taken into consideration when using `Text`.
46-
/// If you need to display text of a certain size, make sure the corresponding bitmap font that
47-
/// supports that size is used.
48-
#[derive(Debug)]
49-
#[allow(missing_copy_implementations)]
50-
pub enum Font {}
12+
extern "C" {
13+
/// Type for loading and manipulating character fonts.
14+
///
15+
/// Fonts can be loaded from a file, from memory or from a custom stream,
16+
/// and supports the most common types of fonts.
17+
///
18+
/// See the `from_file` function for the complete list of supported formats.
19+
///
20+
/// Once it is loaded, a `Font` instance provides three types of information about the font:
21+
///
22+
/// - Global metrics, such as the line spacing
23+
/// - Per-glyph metrics, such as bounding box or kerning
24+
/// - Pixel representation of glyphs
25+
///
26+
/// Fonts alone are not very useful:
27+
/// they hold the font data but cannot make anything useful of it.
28+
/// To do so you need to use the `Text` type, which is able to properly output text with
29+
/// several options such as character size, style, color, position, rotation, etc.
30+
/// This separation allows more flexibility and better performances:
31+
/// indeed a `Font` is a heavy resource, and any operation on it is
32+
/// slow (often too slow for real-time applications).
33+
/// On the other side, a `Text` is a lightweight object which can combine the
34+
/// glyphs data and metrics of a `Font` to display any text on a render target.
35+
/// Note that it is also possible to bind several `Text` instances to the same `Font`.
36+
///
37+
/// It is important to note that the `Text` instance doesn't copy the font that it uses,
38+
/// it only keeps a reference to it.
39+
/// Thus, a `Font` must not be destructed while it is used by a
40+
/// `Text` (i.e. never write a function that uses a local `Font` instance for creating a text).
41+
///
42+
/// Apart from loading font files, and passing them to instances of `Text`,
43+
/// you should normally not have to deal directly with this type.
44+
/// However, it may be useful to access the font metrics or rasterized glyphs for
45+
/// advanced usage.
46+
///
47+
/// Note that if the font is a bitmap font, it is not scalable,
48+
/// thus not all requested sizes will be available to use.
49+
/// This needs to be taken into consideration when using `Text`.
50+
/// If you need to display text of a certain size, make sure the corresponding bitmap font that
51+
/// supports that size is used.
52+
pub type Font;
53+
}
54+
55+
impl Debug for Font {
56+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57+
write!(f, "Font at {:p}", self)
58+
}
59+
}
5160

5261
impl Font {
5362
/// Get the kerning value corresponding to a given pair of characters in a font

src/graphics/texture.rs

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,54 @@ use std::ffi::CString;
88
use std::io::{Read, Seek};
99
use std::ops::{Deref, DerefMut};
1010
use std::ptr;
11+
use std::fmt::{self, Debug};
1112
use system::Vector2u;
1213
use window::Window;
1314

14-
/// `Image` living on the graphics card that can be used for drawing.
15-
///
16-
/// `Texture` stores pixels that can be drawn, with a sprite for example.
17-
///
18-
/// A texture lives in the graphics card memory, therefore it is very fast to draw a
19-
/// texture to a render target,
20-
/// or copy a render target to a texture (the graphics card can access both directly).
21-
///
22-
/// Being stored in the graphics card memory has some drawbacks.
23-
/// A texture cannot be manipulated as freely as a `Image`,
24-
/// you need to prepare the pixels first and then upload them to the texture in a
25-
/// single operation (see `Texture::update`).
26-
///
27-
/// `Texture` makes it easy to convert from/to `Image`,
28-
/// but keep in mind that these calls require transfers between the graphics card and
29-
/// the central memory, therefore they are slow operations.
30-
///
31-
/// A texture can be loaded from an image, but also directly from a file/memory/stream.
32-
/// The necessary shortcuts are defined so that you don't need an image first for the
33-
/// most common cases.
34-
/// However, if you want to perform some modifications on the pixels before creating the
35-
/// final texture, you can load your file to a `Image`, do whatever you need with the pixels,
36-
/// and then call `Texture::from_image`.
37-
///
38-
/// Since they live in the graphics card memory,
39-
/// the pixels of a texture cannot be accessed without a slow copy first.
40-
/// And they cannot be accessed individually.
41-
/// Therefore, if you need to read the texture's pixels (like for pixel-perfect collisions),
42-
/// it is recommended to store the collision information separately,
43-
/// for example in an array of booleans.
44-
///
45-
/// Like `Image`, `Texture` can handle a unique internal representation of pixels,
46-
/// which is RGBA 32 bits.
47-
/// This means that a pixel must be composed of
48-
/// 8 bits red, green, blue and alpha channels – just like a `Color`.
49-
#[derive(Debug)]
50-
#[allow(missing_copy_implementations)]
51-
pub enum Texture {}
15+
extern "C" {
16+
/// `Image` living on the graphics card that can be used for drawing.
17+
///
18+
/// `Texture` stores pixels that can be drawn, with a sprite for example.
19+
///
20+
/// A texture lives in the graphics card memory, therefore it is very fast to draw a
21+
/// texture to a render target,
22+
/// or copy a render target to a texture (the graphics card can access both directly).
23+
///
24+
/// Being stored in the graphics card memory has some drawbacks.
25+
/// A texture cannot be manipulated as freely as a `Image`,
26+
/// you need to prepare the pixels first and then upload them to the texture in a
27+
/// single operation (see `Texture::update`).
28+
///
29+
/// `Texture` makes it easy to convert from/to `Image`,
30+
/// but keep in mind that these calls require transfers between the graphics card and
31+
/// the central memory, therefore they are slow operations.
32+
///
33+
/// A texture can be loaded from an image, but also directly from a file/memory/stream.
34+
/// The necessary shortcuts are defined so that you don't need an image first for the
35+
/// most common cases.
36+
/// However, if you want to perform some modifications on the pixels before creating the
37+
/// final texture, you can load your file to a `Image`, do whatever you need with the pixels,
38+
/// and then call `Texture::from_image`.
39+
///
40+
/// Since they live in the graphics card memory,
41+
/// the pixels of a texture cannot be accessed without a slow copy first.
42+
/// And they cannot be accessed individually.
43+
/// Therefore, if you need to read the texture's pixels (like for pixel-perfect collisions),
44+
/// it is recommended to store the collision information separately,
45+
/// for example in an array of booleans.
46+
///
47+
/// Like `Image`, `Texture` can handle a unique internal representation of pixels,
48+
/// which is RGBA 32 bits.
49+
/// This means that a pixel must be composed of
50+
/// 8 bits red, green, blue and alpha channels – just like a `Color`.
51+
pub type Texture;
52+
}
53+
54+
impl Debug for Texture {
55+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56+
write!(f, "Texture at {:p}", self)
57+
}
58+
}
5259

5360
impl Texture {
5461
/// Return the size of the texture

src/graphics/view.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@ use graphics::csfml_graphics_sys as ffi;
33
use std::borrow::{Borrow, ToOwned};
44
use std::ops::{Deref, DerefMut};
55
use system::Vector2f;
6+
use std::fmt::{self, Debug};
67

7-
/// 2D camera that defines what region is shown on screen
8-
///
9-
/// This is a very powerful concept: you can scroll,
10-
/// rotate or zoom the entire scene without altering
11-
/// the way that your drawable objects are drawn.
12-
#[derive(Debug)]
13-
#[allow(missing_copy_implementations)]
14-
pub enum View {}
8+
extern "C" {
9+
/// 2D camera that defines what region is shown on screen
10+
///
11+
/// This is a very powerful concept: you can scroll,
12+
/// rotate or zoom the entire scene without altering
13+
/// the way that your drawable objects are drawn.
14+
pub type View;
15+
}
16+
17+
impl Debug for View {
18+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19+
write!(f, "View at {:p}", self)
20+
}
21+
}
1522

1623
impl View {
1724
/// Get the current orientation of a view

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//! license.
2020
//!
2121
22+
#![feature(extern_types)]
2223
#![warn(missing_docs, trivial_numeric_casts, missing_copy_implementations,
2324
missing_debug_implementations, unused_results, trivial_casts)]
2425

0 commit comments

Comments
 (0)