* fixed DSFML Sound3D example

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1434 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
trass3r 2010-03-02 13:45:46 +00:00
parent 914db8bce0
commit e394b6f35c

View file

@ -7,72 +7,73 @@ import dsfml.audio.all;
void main() void main()
{ {
//We create our window with a limit of 100 and a white backcolor. //We create our window with a limit of 100 and a white backcolor.
RenderWindow app = new RenderWindow (VideoMode(800, 600, 32), "Sound Spatialization Sample"); RenderWindow app = new RenderWindow (VideoMode(800, 600, 32), "Sound Spatialization Sample");
app.useVerticalSync(true); app.useVerticalSync(true);
Font f = new Font("Data/cheeseburger.ttf", 34); Font f = new Font("Data/cheeseburger.ttf");
//Some instructions //Some instructions
String s = new String("Click anywhere on screen to change listener position.\nPress + or - to modify the speed of the car."c, f); Text s = new Text("Click anywhere on screen to change listener position.\nPress + or - to modify the speed of the car."c, f);
s.setPosition(20, 30); s.setCharacterSize(34);
s.setColor(Color.BLACK); s.setPosition(20, 30);
s.setColor(Color.BLACK);
//We prepare our images and the sound //We prepare our images and the sound
char[][2] images = ["Data/bluerallyecarleft.bmp", "Data/bluerallyecarright.bmp"]; string[2] images = ["Data/bluerallyecarleft.bmp", "Data/bluerallyecarright.bmp"];
Car c = new Car(images, "Data/car_idle.wav"); Car c = new Car(images, "Data/car_idle.wav");
int carSpeed = 100; int carSpeed = 100;
//Set default position for the car and the listener //Set default position for the car and the listener
c.setPosition(Vector2f(0, 300)); c.setPosition(Vector2f(0, 300));
SoundListener.setPosition(Vector2f(400, 300)); SoundListener.setPosition(Vector2f(400, 300));
c.startPlaying(); c.startPlaying();
//Start the main loop //Start the main loop
while (app.isOpened()) while (app.isOpened())
{ {
app.clear(Color.WHITE); app.clear(Color.WHITE);
Event evt; Event evt;
//The event loop //The event loop
while (app.getEvent(evt)) while (app.getEvent(evt))
{ {
// if the window is closed, we can leave the game loop // if the window is closed, we can leave the game loop
if (evt.Type == Event.EventType.CLOSED) if (evt.Type == EventType.Closed)
app.close(); app.close();
// we handle the click event to change listener position // we handle the click event to change listener position
else if (evt.Type == Event.EventType.MOUSEBUTTONPRESSED && evt.MouseButton.Button == MouseButtons.LEFT) else if (evt.Type == EventType.MouseButtonPressed && evt.MouseButton.Button == MouseButtons.Left)
{ {
Input i = app.getInput(); Input i = app.getInput();
SoundListener.setPosition(Vector2f(i.getMouseX(), i.getMouseY())); SoundListener.setPosition(Vector2f(i.getMouseX(), i.getMouseY()));
} }
// and eventual keys press // and eventual keys press
else if (evt.Type == Event.EventType.KEYPRESSED) else if (evt.Type == EventType.KeyPressed)
{ {
//Change the car speed //Change the car speed
if (evt.Key.Code == KeyCode.ADD) if (evt.Key.Code == KeyCode.Add)
{ {
carSpeed += 25; carSpeed += 25;
} }
else if (evt.Key.Code == KeyCode.SUBTRACT) else if (evt.Key.Code == KeyCode.Substract)
{ {
carSpeed -= 25; carSpeed -= 25;
} }
} }
} }
//We move constantly our car. //We move constantly our car.
c.move(Vector2f(app.getFrameTime() * carSpeed, 0)); c.move(Vector2f(app.getFrameTime() * carSpeed, 0));
//Draw all the sprite and string on render window //Draw all the sprite and string on render window
app.draw(s); app.draw(s);
app.draw(c.getSprite()); app.draw(c.getSprite());
app.draw(SoundListener.getSprite()); app.draw(SoundListener.getSprite());
//And finally display the window //And finally display the window
app.display(); app.display();
} }
} }
@ -81,105 +82,105 @@ void main()
// There is only one listener so all the methods are statics. // There is only one listener so all the methods are statics.
class SoundListener class SoundListener
{ {
static Sprite s_crosshair; static Sprite s_crosshair;
static Vector2f s_p; static Vector2f s_p;
static this() static this()
{ {
Image crosshairImg = new Image("Data/crosshair.tga"); Image crosshairImg = new Image("Data/crosshair.tga");
crosshairImg.createMaskFromColor(Color.WHITE); crosshairImg.createMaskFromColor(Color.WHITE);
s_crosshair = new Sprite(crosshairImg); s_crosshair = new Sprite(crosshairImg);
s_crosshair.setCenter(Vector2f(s_crosshair.getSize().x / 2, s_crosshair.getSize().y / 2)); s_crosshair.setOrigin(Vector2f(s_crosshair.getSize().x / 2, s_crosshair.getSize().y / 2));
//Listener.setTarget(1.f, 0.f, 0.f); //Listener.setTarget(1.f, 0.f, 0.f);
} }
// Adjust position of the listener // Adjust position of the listener
static void setPosition(Vector2f p) static void setPosition(Vector2f p)
{ {
Listener.setPosition(p.x, p.y, 5.f); Listener.setPosition(p.x, p.y, 5.f);
s_crosshair.setPosition(p); s_crosshair.setPosition(p);
} }
static Sprite getSprite() static Sprite getSprite()
{ {
return s_crosshair; return s_crosshair;
} }
} }
// Class encapsulating all data for our car // Class encapsulating all data for our car
class Car class Car
{ {
Vector2f m_actual; Vector2f m_actual;
Sprite m_sprite; Sprite m_sprite;
Sound m_sound; Sound m_sound;
bool reverse; bool reverse;
Image[2] imgs; Image[2] imgs;
//Constructor with with a fixed size string array of image path, and a string for the sound path //Constructor with with a fixed size string array of image path, and a string for the sound path
this (char[][2] images, char[] soundFilename) this (string[2] images, string soundFilename)
{ {
//load images and create filter //load images and create filter
imgs[0] = new Image(images[0]); imgs[1] = new Image(images[1]); imgs[0] = new Image(images[0]); imgs[1] = new Image(images[1]);
foreach(img; imgs) foreach(img; imgs)
img.createMaskFromColor(Color(97, 68, 43)); img.createMaskFromColor(Color(97, 68, 43));
m_sprite = new Sprite(imgs[0]); m_sprite = new Sprite(imgs[0]);
m_sprite.setCenter(Vector2f(m_sprite.getSize().x / 2, m_sprite.getSize().y / 2)); m_sprite.setOrigin(Vector2f(m_sprite.getSize().x / 2, m_sprite.getSize().y / 2));
SoundBuffer buff = new SoundBuffer(soundFilename); SoundBuffer buff = new SoundBuffer(soundFilename);
//load our sound with loop enabled //load our sound with loop enabled
m_sound = new Sound(buff, true); m_sound = new Sound(buff, true);
m_sound.setAttenuation(.05f); m_sound.setAttenuation(.05f);
} }
// Begin the sound play // Begin the sound play
void startPlaying() void startPlaying()
{ {
m_sound.play(); m_sound.play();
} }
// Set the position of the car on the window // Set the position of the car on the window
// Used to setup the begin car window and sound location // Used to setup the begin car window and sound location
void setPosition(Vector2f p) void setPosition(Vector2f p)
{ {
m_sprite.setPosition(p); m_sprite.setPosition(p);
m_sound.setPosition(p.x, 0, p.y); m_sound.setPosition(p.x, 0, p.y);
} }
//Move the car (visual and sound position) //Move the car (visual and sound position)
//If the car leave the screen, we change the sprite image and reverse moving //If the car leave the screen, we change the sprite image and reverse moving
void move(Vector2f vec) void move(Vector2f vec)
{ {
// if the car is beyond the right screen limit // if the car is beyond the right screen limit
if (!reverse && m_sprite.getPosition().x > 850) if (!reverse && m_sprite.getPosition().x > 850)
{ {
m_sprite.setImage(imgs[1]); m_sprite.setImage(imgs[1]);
reverse = true; reverse = true;
} }
// same as above but for left limit // same as above but for left limit
else if (reverse && vec.x + m_sprite.getPosition().x < -50) else if (reverse && vec.x + m_sprite.getPosition().x < -50)
{ {
m_sprite.setImage(imgs[0]); m_sprite.setImage(imgs[0]);
reverse = false; reverse = false;
} }
if (reverse) if (reverse)
vec = -vec; vec = -vec;
m_sprite.move(vec); m_sprite.move(vec);
Vector2f pos = m_sprite.getPosition(); Vector2f pos = m_sprite.getPosition();
m_sound.setPosition(pos.x , pos.y, 0); m_sound.setPosition(pos.x , pos.y, 0);
} }
Sprite getSprite() Sprite getSprite()
{ {
return m_sprite; return m_sprite;
} }
} }