Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED]Cant Pass a sound to a function to play  (Read 3685 times)

0 Members and 2 Guests are viewing this topic.

Zidane

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
[SOLVED]Cant Pass a sound to a function to play
« on: March 30, 2015, 10:42:39 pm »
i want to pass my sound to collision_check but when i try program compiles fine and everything works but (when the circle collides which happens every 5 seconds or so) it wont play a sound when that happens

Here my Code
#include <iostream>
#include <string>
#include <conio.h>
#include <cmath>
#include <ctime>
#include <Windows.h>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

using namespace std;
using namespace sf;

int sleeper, b,t;
float cx = 500, cy = 1, X = 1, Y = 1, sy = 1, sx = 1, rx = 1200, ry = 768;
Event event;

void collision_check(sf::Sound sound){
        //left
        if (sx - cx <= 100 && sx - cx > 50 && sy - cy >= -50 && sy - cy <= 50) {
                sound.play();
                t = 1;
                cx -= 102 - sx + cx;
                if (X == 1){ X = 0; }
                else X = 1;
        }
        //right
        if (cx - sx <= 100 && cx - sx > 50 && sy - cy >= -50 && sy - cy <= 50){
                cx += 102 - (cx - sx);
                t = 1;
                sound.play();
                if (X == 1){ X = 0; }
                else X = 1;
        }
        //bottom
        if (cy - sy <= 100 && cy - sy > 50 && sx - cx >= -50 && sx - cx <= 50){
                cy += 102 - (cy - sy);
                t = 1;
                sound.play();
                if (Y == 1){ Y = 0; }
                else Y = 1;
        }
        //top
        if (sy - cy <= 100 && sy - cy > 50 && sx - cx >= -50 && sx - cx <= 50){
                cy -= 102 - (sy - cy);
                t = 1;
                sound.play();
                if (Y == 1){ Y = 0; }
                else Y = 1;
        }
}
void circle_movement(){
        if (GetAsyncKeyState(VK_SPACE) & 1){
                b++;
        }
        if (X == 1 && b % 2 == 0){
                cx++;
        }
        if (X == 0 && b % 2 == 0){
                cx--;
        }
        if (cx >= rx - 101){
                X = 0;
        }
        if (cx <= 1){
                X = 1;
        }

        if (Y == 1 && b % 2 == 0){
                cy++;
        }

        if (Y == 0 && b % 2 == 0){
                cy--;
        }
        if (cy >= ry - 101){
                Y = 0;
        }
        if (cy <= 1){
                Y = 1;
        }
}
void square_movement(){
        if (GetAsyncKeyState(VK_RIGHT) & 0x8000 && sx < rx - 101 || GetAsyncKeyState(68) & 0x8000 && sx < rx - 101){
                sx += 2;
        }
        if (GetAsyncKeyState(VK_LEFT) & 0x8000 && sx >= 2 || GetAsyncKeyState(65) & 0x8000 && sx >= 2){
                sx -= 2;
        }
        if (GetAsyncKeyState(VK_UP) & 0x8000 && sy >= 2 || GetAsyncKeyState(87) & 0x8000 && sy >= 2){
                sy -= 2;
        }
        if (GetAsyncKeyState(VK_DOWN) & 0x8000 && sy < ry - 101 || GetAsyncKeyState(83) & 0x8000 && sy < ry - 101){
                sy += 2;
        }
        if (GetAsyncKeyState(VK_LSHIFT) != 0){
                sx = 1;
                sy = 1;
        }
        if (GetAsyncKeyState(VK_RSHIFT) != 0){
                sx = rx - 101;
                sy = ry - 101;
        }
}

int main(){


        RenderWindow window(VideoMode(rx, ry), "SFML Window", Style::None);
        window.setFramerateLimit(500);
        RectangleShape shape3(Vector2f(rx - 2, ry - 2));
        shape3.setPosition(1.f, 1.f);
        shape3.setFillColor(Color::Black);
        shape3.setOutlineThickness(1);
        shape3.setOutlineColor(Color::Green);


        CircleShape shape1;
        shape1.setRadius(50.f);
        shape1.setFillColor(Color::White);

        sf::Texture texture;
        if (!texture.loadFromFile("C:/Users/Mackenzie/Pictures/photo.jpg", sf::IntRect(0, 0, 101, 101)))
        {
                // error...
        }

        sf::Music music;
        if (!music.openFromFile("C:/Windows/Media/town.ogg"))
                return -1; // error
        music.play();
        music.setVolume(6);

        sf::SoundBuffer buffer;
        if (!buffer.loadFromFile("C:/Windows/Media/Windows Default.wav"))
                return -1;
        sf::Sound sound;
        sound.setBuffer(buffer);
       

        while (window.isOpen())
        {
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                sf::Sprite shape2;
                shape2.setTexture(texture);
                window.clear(sf::Color::White);



                shape1.setPosition(cx + 0.f, cy + 0.f);
                shape2.setPosition(sx + 0.f, sy + 0.f);
                window.draw(shape3);
                window.draw(shape1);
                window.draw(shape2);
                window.display();
                collision_check(sound);
                if (t == 1){
                        sound.play();
                        t = 0;
                }
                circle_movement();
                square_movement();

        }
}

 

I used Int "t" as a trigger and the sound will only play inside of main but i want it to play inside my function that i pass it to.
« Last Edit: March 31, 2015, 03:07:38 am by Zidane »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Cant Pass a sound to a function to play
« Reply #1 on: March 30, 2015, 10:45:07 pm »
Pass the sound by reference, so it is not copied.

But I'd rather return a bool and play the sound outside, as it's not the collision detection's task. And why are you using GetAsyncKeyState()? Isn't the idea of SFML to get rid of OS-specific functions? Furthermore, avoid global variables. And you can reduce your code by outsourcing similar lines to functions.
« Last Edit: March 30, 2015, 10:50:02 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Zidane

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: [SOLVED]Cant Pass a sound to a function to play
« Reply #2 on: March 31, 2015, 03:08:57 am »
Alright thanks for the tips ya i got alot to work on my coding habit thanks for the help

 

anything