fixed some bugs in SoundStream

property style functions for SoundStream and SoundSource
fixed pong and soundstream samples: thx AndrejM!

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1552 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
Trass3r 2010-08-25 20:37:00 +00:00
parent dda69de7e9
commit 64b4bd8472
4 changed files with 79 additions and 70 deletions

View File

@ -173,7 +173,7 @@ public:
* Returns: * Returns:
* Position of the sound * Position of the sound
*/ */
Vector3f getPosition() Vector3f position()
{ {
Vector3f ret; Vector3f ret;
sfSoundSource_GetPosition(m_ptr, &ret.x, &ret.y, &ret.z); sfSoundSource_GetPosition(m_ptr, &ret.x, &ret.y, &ret.z);
@ -279,7 +279,7 @@ public:
{ {
return sfSoundSource_GetAttenuation(m_ptr); return sfSoundSource_GetAttenuation(m_ptr);
} }
} } // of @property
private: private:

View File

@ -112,13 +112,15 @@ abstract class SoundStream : SoundSource!("sfSoundStream")
delete m_dummy; delete m_dummy;
} }
@property
{
/** /**
* Get number of channels of the stream * Get number of channels of the stream
* *
* Returns: * Returns:
* number of channels * number of channels
*/ */
uint getChannelsCount() uint channelsCount()
{ {
return m_channelsCount; return m_channelsCount;
} }
@ -129,7 +131,7 @@ abstract class SoundStream : SoundSource!("sfSoundStream")
* Returns: * Returns:
* sample rate * sample rate
*/ */
uint getSampleRate() uint sampleRate()
{ {
return m_sampleRate; return m_sampleRate;
} }
@ -140,7 +142,7 @@ abstract class SoundStream : SoundSource!("sfSoundStream")
* Returns: * Returns:
* current playing offset, in seconds. * current playing offset, in seconds.
*/ */
float getPlayingOffset() float playingOffset()
{ {
return sfSoundStream_GetPlayingOffset(m_ptr); return sfSoundStream_GetPlayingOffset(m_ptr);
} }
@ -151,7 +153,7 @@ abstract class SoundStream : SoundSource!("sfSoundStream")
* Params: * Params:
* timeOffset = New playing position, expressed in seconds * timeOffset = New playing position, expressed in seconds
*/ */
void setPlayingOffset(float timeOffset) void playingOffset(float timeOffset)
{ {
sfSoundStream_SetPlayingOffset(m_ptr, timeOffset); sfSoundStream_SetPlayingOffset(m_ptr, timeOffset);
} }
@ -162,7 +164,7 @@ abstract class SoundStream : SoundSource!("sfSoundStream")
* Returns: * Returns:
* True if the music is looping, false otherwise * True if the music is looping, false otherwise
*/ */
bool getLoop() bool loop()
{ {
if (m_ptr !is null) if (m_ptr !is null)
return cast(bool)sfSoundStream_GetLoop(m_ptr); return cast(bool)sfSoundStream_GetLoop(m_ptr);
@ -177,12 +179,12 @@ abstract class SoundStream : SoundSource!("sfSoundStream")
* Params: * Params:
* loop = true to play in loop, false to play once * loop = true to play in loop, false to play once
*/ */
void setLoop(bool loop) void loop(bool loop)
{ {
if (m_ptr !is null) if (m_ptr !is null)
sfSoundStream_SetLoop(m_ptr, loop); sfSoundStream_SetLoop(m_ptr, loop);
} }
} // of @property
protected: protected:
/** /**
@ -213,7 +215,7 @@ protected:
/** /**
* Called each time the stream is seeked * Called each time the stream is seeked
*/ */
abstract void onSeek(); abstract void onSeek(float timeOffset);
/** /**
* Called each time the stream needs new data. * Called each time the stream needs new data.
@ -236,7 +238,7 @@ private:
if ((id = *cast(int*) user) in s_instances) if ((id = *cast(int*) user) in s_instances)
{ {
SoundStream temp = s_instances[id]; SoundStream temp = s_instances[id];
return (temp.onSeek()); return (temp.onSeek(t));
} }
} }

View File

@ -27,7 +27,7 @@ void main()
app.useVerticalSync(false); app.useVerticalSync(false);
Input i = app.getInput(); Input i = app.input;
// Load the sounds used in the game // Load the sounds used in the game
Sound BallSound = new Sound(new SoundBuffer("Data/ball.wav")); Sound BallSound = new Sound(new SoundBuffer("Data/ball.wav"));
@ -40,23 +40,23 @@ void main()
// Initialize the end text // Initialize the end text
Text End = new Text(""c); Text End = new Text(""c);
Font font = new Font("Data/cheeseburger.ttf"); Font font = new Font("Data/cheeseburger.ttf");
End.setFont(font); End.font = font;
End.setCharacterSize(60); End.characterSize = 60;
End.move(150.f, 200.f); End.move(150.f, 200.f);
End.setColor(Color(50, 50, 250)); End.color = Color(50, 50, 250);
Text fps = new Text(""c, font, 30); Text fps = new Text(""c, font, 30);
fps.move(50.f, 50.f); fps.move(50.f, 50.f);
fps.setColor(Color.BLACK); fps.color = Color.BLACK;
// Create the sprites of the background, the paddles and the ball // Create the sprites of the background, the paddles and the ball
Sprite LeftPaddle = new Sprite(PaddleImage); Sprite LeftPaddle = new Sprite(PaddleImage);
Sprite RightPaddle = new Sprite(PaddleImage); Sprite RightPaddle = new Sprite(PaddleImage);
Sprite Ball = new Sprite(BallImage); Sprite Ball = new Sprite(BallImage);
LeftPaddle.move(10, (app.getView().getHeight() - LeftPaddle.getSize().y) / 2); LeftPaddle.move(10, (app.view.getHeight() - LeftPaddle.size.y) / 2);
RightPaddle.move(app.getView().getWidth() - RightPaddle.getSize().x - 10, (app.getView().getHeight() - RightPaddle.getSize().y) / 2); RightPaddle.move(app.view.getWidth() - RightPaddle.size.x - 10, (app.view.getHeight() - RightPaddle.size.y) / 2);
Ball.move((app.getView().getWidth() - Ball.getSize().x) / 2, (app.getView().getHeight() - Ball.getSize().y) / 2); Ball.move((app.view.getWidth() - Ball.size.x) / 2, (app.view.getHeight() - Ball.size.y) / 2);
// Define the paddles properties // Define the paddles properties
auto AITimer = new PerformanceCounter(); auto AITimer = new PerformanceCounter();
@ -78,11 +78,12 @@ void main()
Event evt; Event evt;
uint iFps = 0; uint iFps = 0;
auto fpsClock = new PerformanceCounter(); auto fpsClock = new PerformanceCounter();
while (app.isOpened()) while (app.isOpened())
{ {
app.clear(Color(255, 255, 255, 255)); app.clear(Color(255, 255, 255, 255));
// Handle events
// Handle events
while (app.getEvent(evt)) while (app.getEvent(evt))
{ {
// Window closed or escape key pressed : exit // Window closed or escape key pressed : exit
@ -97,16 +98,16 @@ void main()
if (IsPlaying) if (IsPlaying)
{ {
// Move the player's paddle // Move the player's paddle
if (i.isKeyDown(KeyCode.Up) && (LeftPaddle.getPosition().y > 5.f)) if (i.isKeyDown(KeyCode.Up) && (LeftPaddle.position.y > 5.f))
LeftPaddle.move(0.f, -LeftPaddleSpeed * app.getFrameTime()); LeftPaddle.move(0.f, -LeftPaddleSpeed * app.frameTime);
if (i.isKeyDown(KeyCode.Down) && (LeftPaddle.getPosition().y < app.getView().getHeight() - LeftPaddle.getSize().y - 5.f)) if (i.isKeyDown(KeyCode.Down) && (LeftPaddle.position.y < app.view.getHeight() - LeftPaddle.size.y - 5.f))
LeftPaddle.move(0.f, LeftPaddleSpeed * app.getFrameTime()); LeftPaddle.move(0.f, LeftPaddleSpeed * app.frameTime);
// Move the computer's paddle // Move the computer's paddle
if (((RightPaddleSpeed < 0.f) && (RightPaddle.getPosition().y > 5.f)) || if (((RightPaddleSpeed < 0.f) && (RightPaddle.position.y > 5.f)) ||
((RightPaddleSpeed > 0.f) && (RightPaddle.getPosition().y < app.getView().getHeight() - RightPaddle.getSize().y - 5.f))) ((RightPaddleSpeed > 0.f) && (RightPaddle.position.y < app.view.getHeight() - RightPaddle.size.y - 5.f)))
{ {
RightPaddle.move(0.f, RightPaddleSpeed * app.getFrameTime()); RightPaddle.move(0.f, RightPaddleSpeed * app.frameTime);
} }
// Update the computer's paddle direction according to the ball position // Update the computer's paddle direction according to the ball position
@ -114,65 +115,65 @@ void main()
if (AITimer.milliseconds > AITime) if (AITimer.milliseconds > AITime)
{ {
AITimer.start(); AITimer.start();
if ((RightPaddleSpeed < 0) && (Ball.getPosition().y + Ball.getSize().y > RightPaddle.getPosition().y + RightPaddle.getSize().y)) if ((RightPaddleSpeed < 0) && (Ball.position.y + Ball.size.y > RightPaddle.position.y + RightPaddle.size.y))
RightPaddleSpeed = -RightPaddleSpeed; RightPaddleSpeed = -RightPaddleSpeed;
if ((RightPaddleSpeed > 0) && (Ball.getPosition().y < RightPaddle.getPosition().y)) if ((RightPaddleSpeed > 0) && (Ball.position.y < RightPaddle.position.y))
RightPaddleSpeed = -RightPaddleSpeed; RightPaddleSpeed = -RightPaddleSpeed;
} }
// Move the ball // Move the ball
float Factor = BallSpeed * app.getFrameTime(); float Factor = BallSpeed * app.frameTime;
Ball.move(cos(BallAngle) * Factor, sin(BallAngle) * Factor); Ball.move(cos(BallAngle) * Factor, sin(BallAngle) * Factor);
// Check collisions between the ball and the screen // Check collisions between the ball and the screen
if (Ball.getPosition().x < 0.f) if (Ball.position.x < 0.f)
{ {
IsPlaying = false; IsPlaying = false;
End.setString("You lost !\n(press escape to exit)"c); End.text("You lost !\n(press escape to exit)"c);
} }
if (Ball.getPosition().x + Ball.getSize().x > app.getView().getWidth()) if (Ball.position.x + Ball.size.x > app.view.getWidth())
{ {
IsPlaying = false; IsPlaying = false;
End.setString("You won !\n(press escape to exit)"c); End.text("You won !\n(press escape to exit)"c);
} }
if (Ball.getPosition().y < 0.f) if (Ball.position.y < 0.f)
{ {
BallSound.play(); BallSound.play();
BallAngle = -BallAngle; BallAngle = -BallAngle;
Ball.setY(0.1f); Ball.y = 0.1f;
} }
if (Ball.getPosition().y + Ball.getSize().y > app.getView().getHeight()) if (Ball.position.y + Ball.size.y > app.view.getHeight())
{ {
BallSound.play(); BallSound.play();
BallAngle = -BallAngle; BallAngle = -BallAngle;
Ball.setY(app.getView().getHeight() - Ball.getSize().y - 0.1f); Ball.y = app.view.getHeight() - Ball.size.y - 0.1f;
} }
// Check the collisions between the ball and the paddles // Check the collisions between the ball and the paddles
// Left Paddle // Left Paddle
if (Ball.getPosition().x < LeftPaddle.getPosition().x + LeftPaddle.getSize().x && if (Ball.position.x < LeftPaddle.position.x + LeftPaddle.size.x &&
Ball.getPosition().x > LeftPaddle.getPosition().x + (LeftPaddle.getSize().x / 2.0f) && Ball.position.x > LeftPaddle.position.x + (LeftPaddle.size.x / 2.0f) &&
Ball.getPosition().y + Ball.getSize().y >= LeftPaddle.getPosition().y && Ball.position.y + Ball.size.y >= LeftPaddle.position.y &&
Ball.getPosition().y <= LeftPaddle.getPosition().y + LeftPaddle.getSize().y) Ball.position.y <= LeftPaddle.position.y + LeftPaddle.size.y)
{ {
BallSound.play(); BallSound.play();
BallAngle = PI - BallAngle; BallAngle = PI - BallAngle;
Ball.setX(LeftPaddle.getPosition().x + LeftPaddle.getSize().x + 0.1f); Ball.x = LeftPaddle.position.x + LeftPaddle.size.x + 0.1f;
} }
// Right Paddle // Right Paddle
if (Ball.getPosition().x + Ball.getSize().x > RightPaddle.getPosition().x && if (Ball.position.x + Ball.size.x > RightPaddle.position.x &&
Ball.getPosition().x + Ball.getSize().x < RightPaddle.getPosition().x + (RightPaddle.getSize().x / 2.0f) && Ball.position.x + Ball.size.x < RightPaddle.position.x + (RightPaddle.size.x / 2.0f) &&
Ball.getPosition().y + Ball.getSize().y >= RightPaddle.getPosition().y && Ball.position.y + Ball.size.y >= RightPaddle.position.y &&
Ball.getPosition().y <= RightPaddle.getPosition().y + RightPaddle.getSize().y) Ball.position.y <= RightPaddle.position.y + RightPaddle.size.y)
{ {
BallSound.play(); BallSound.play();
BallAngle = PI - BallAngle; BallAngle = PI - BallAngle;
Ball.setX(RightPaddle.getPosition().x - Ball.getSize().x - 0.1f); Ball.x = RightPaddle.position.x - Ball.size.x - 0.1f;
} }
} }
@ -185,7 +186,7 @@ void main()
fpsClock.stop(); fpsClock.stop();
if(fpsClock.seconds >= 1) if(fpsClock.seconds >= 1)
{ {
fps.setString(std.string.format("%d fps", iFps)); fps.text = std.string.format("%d fps", iFps);
iFps = 0; iFps = 0;
fpsClock.start(); fpsClock.start();
} }

View File

@ -29,28 +29,34 @@ class MySoundStream : SoundStream
// We create a sound buffer to load samples from files // We create a sound buffer to load samples from files
m_buff = new SoundBuffer("Data/car_idle.wav"); m_buff = new SoundBuffer("Data/car_idle.wav");
m_data = m_buff.getSamples[0..m_buff.getSamplesCount]; m_data = m_buff.samples[0..m_buff.samplesCount];
} }
protected: protected:
/*
bool onStart() bool onStart()
{ {
// No specifics things to do, just return true. // No specifics things to do, just return true.
return true; return true;
} }
*/
bool onGetData(out short[] data) override bool onGetData(out short[] data)
{ {
// We ensure that we have enough data to send // We ensure that we have enough data to send
if (m_cursor + this.getSampleRate > m_data.length) if (m_cursor + this.sampleRate > m_data.length)
return false; return false;
// Assign data in the buffer ... // Assign data in the buffer ...
data = m_data[m_cursor..m_cursor + this.getSampleRate]; data = m_data[m_cursor..m_cursor + this.sampleRate];
// ... and increment the cursor // ... and increment the cursor
m_cursor += this.getSampleRate; m_cursor += this.sampleRate;
return true; return true;
} }
override void onSeek(float timeOffset)
{
}
} }
void main() void main()
@ -59,20 +65,20 @@ void main()
display("Playing sound !\n Press enter to stop playback."); display("Playing sound !\n Press enter to stop playback.");
stream.play(); stream.play();
read(); read(); // prevent console from closing
stream.stop(); stream.stop();
} }
void display(char[] c) void display(string c)
{ {
version (Tango) version (Tango)
{ {
Stdout(c).newline; Stdout(c).newline;
} }
else else
{ {
writefln("%s", c); writeln(c);
} }
} }
/** /**
@ -81,11 +87,11 @@ void display(char[] c)
void read() void read()
{ {
version (Tango) version (Tango)
{ {
Cin.get(); Cin.get();
} }
else else
{ {
readln(); readln();
} }
} }