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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Moonglum

Pages: [1]
1
Audio / multiple concurrent sound effects
« on: May 17, 2023, 09:27:04 am »
Hi All.

I am writing a game which requires the player to launch up to 9 missiles in quick succession. There is a sound effect for the launch of the missile and a sound effect for each explosion. The only way I can avoid the next sound effect cutting off the previous sound effect is for each missile to have two sound definitions, one for shoot and one for explosion. I cant use arrays as this will stop the sound playing and I cant use functions for the same reason. Relevant sections of code are as follows: This is for the explosions ...

    sf::SoundBuffer missileBuffer0;
    sf::SoundBuffer missileBuffer1;
    sf::SoundBuffer missileBuffer2;
    sf::SoundBuffer missileBuffer3;
    sf::SoundBuffer missileBuffer4;
    sf::SoundBuffer missileBuffer5;
    sf::SoundBuffer missileBuffer6;
    sf::SoundBuffer missileBuffer7;
    sf::SoundBuffer missileBuffer8;
    sf::SoundBuffer missileBuffer9;
    sf::Sound missile0;
    sf::Sound missile1;
    sf::Sound missile2;
    sf::Sound missile3;
    sf::Sound missile4;
    sf::Sound missile5;
    sf::Sound missile6;
    sf::Sound missile7;
    sf::Sound missile8;
    sf::Sound missile9;

... and this is the explosion hit sound effect playback.

                                    switch (i)
                                    {
                                    case 0:
                                        if (!missileBuffer0.loadFromFile("rockhit.flac"))
                                        {
                                            cout << "No sound file";
                                        }
                                        missile0.setBuffer(missileBuffer0);
                                        missile0.play();
                                        break;

                                    case 1:
                                        if (!missileBuffer1.loadFromFile("rockhit.flac"))
                                        {
                                            cout << "No sound file";
                                        }
                                        missile1.setBuffer(missileBuffer1);
                                        missile1.play();
                                        break;

                                    case 2:
                                        if (!missileBuffer2.loadFromFile("rockhit.flac"))
                                        {
                                            cout << "No sound file";
                                        ...

... and so on, you get the idea. It does work but seems to be terrible code. I feel I have missed something very important which will avoid the need for this. Please can someone tell me if there is a better way to get this done?

Many thanks

Moon.

2
Audio / Re: unable to hear sound
« on: May 16, 2023, 04:43:00 pm »
Oh wow! Thanks for coming back to me. Yes that worked. Removed the 'Else' and I can here the sound now, but only for that one play.  Do you really have to load the file in to the buffer every time you want to play the sound?  Does seem excessive, but thanks anyway.

Moon.

3
Audio / unable to hear sound
« on: May 16, 2023, 10:57:09 am »
Hi All.  Trying to play sound. Seems to load the file ok but cannot hear playback. App is not muted and I can hear the playback from Windows directly. I have included the Audio.hpp and it can find the flac file. I tried .wav file but again, no sound. Everything else from SFML works ok. Source code is ...

    sf::SoundBuffer shootBuffer;
    if (!shootBuffer.loadFromFile("shoot.flac"))
    {
        return -1;
    }
    else
    {
        sf::Sound shoot;
        shoot.setBuffer(shootBuffer);
        shoot.play();
    }

Any clues? Much appreciated as always.

Moon.

4
Graphics / Re: Cant show text
« on: April 25, 2023, 11:29:41 am »
Apologies, just spotted the missing font statement.  Cancel this one. Thanks.

5
Graphics / Cant show text
« on: April 25, 2023, 11:10:58 am »
Hi all,
Would appreciate some guidance on fonts please. I have tried to follow guides to put some text on the render window but just get nothing.  My code is:

    sf::Text text;
    sf::Font font;
   
    if (font.loadFromFile("C:\\Windows\\Fonts\\Arial.ttf"))
    {
        cout<< "Font Is Ok";
    }
    else
    {
        cout << "Font is not found";
    }

    text.setString("Message");
    text.setFillColor(sf::Color::White);
    window.draw(text);
    window.display();

Any clues would be appreciated.

Many thanks

Moon.



6
Graphics / Re: Changing vertices
« on: March 18, 2023, 01:32:32 pm »
Awesome. Thanks Stauricus. That works a treat. Your points are noted. Just learning at the moment. This is fab info. Thank you.

Moon.

7
Graphics / Changing vertices
« on: March 17, 2023, 10:02:30 am »
I am trying to draw a line in SFML. I use the ...

sf::Vertex line[]=
        {
            sf::Vertex(sf::Vector2f(sx,sy)),
            sf::Vertex(sf::Vector2f(ex,ey))
        };


... command, where sx,sy,ex,ey are previously defined as 'float' and given appropriate values. However, I understand that this defines a new variable called 'line' with two vertices. Great but I now want to draw the line in a new position. How do change the position of the line? If I just run that command again then it tells me that 'line' is already used. This is no good for a game where line drawing is in a loop.

Please can someone tell me how to change the line position.

Many thanks

Moon.

8
Window / Re: sf::keyboard::key seems to be missing
« on: March 04, 2023, 06:09:12 pm »
Hi Kojack

Good spot, that's done the trick. I think there is also a few minor syntax differences with older versions but this works fine now.  In the original code, it would only look at the keyboard when the window was being resized or closed. Moving the brace resolved this.

Many thanks for your help.  Much appreciated.

Moon.

9
Window / Re: sf::keyboard::key seems to be missing
« on: March 04, 2023, 12:13:35 pm »
Thanks for coming back to me.  I am following a tutorial. I have created a render window, defined a rectangle shape which I called "player" and shaded red. Set up a while loop whilst the render window is open and poll for a windows close or resize. I want to check if a key is pressed and change player position accordingly.

The tutorial is SFML 2.4 For Beginners - 5: Keyboard Input.
by Hilze Vonck on YouTube.


He shows sf::keyboard:key in his tutorial but that doesn't work for me.  I have tried to enter left and right as show below but the object does not move with any keys.

Code is as follows:

#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
   
        sf::RenderWindow window(sf::VideoMode(512, 512), "SFML Tutorial", sf::Style::Close | sf::Style::Resize);
        sf::RectangleShape player(sf::Vector2f(100.0f, 100.0f));
        player.setFillColor(sf::Color::Red);


        while (window.isOpen())
        {
                sf::Event evnt;

                while (window.pollEvent(evnt))
                {
                       
                        switch (evnt.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        case sf::Event::Resized:
                                printf ("Width: %i, Height: %i\n", evnt.size.width, evnt.size.height);
                                break;
                        case sf::Event::TextEntered:
                                if (evnt.text.unicode < 128)
                                {
                                        printf("%c", evnt.text.unicode);
                                }
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                player.move(-0.1f, 0.0f);
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                player.move(0.1f, 0.0f);
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                        {
                                player.move(0.0f, -0.1f);
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                        {
                                player.move(0.0f, 0.1f);
                        }



                        window.clear();

                        window.draw(player);
                        window.display();



                }
        }


        return 0;

}

Many thanks for your time.

10
Window / sf::keyboard::key seems to be missing
« on: March 04, 2023, 09:03:55 am »
Trying to use sf::keyboard:key to read keyboard state. This doesn't seem to work for me. sf::keyboard is there but sf::keyboard::key seems to be missing and the compiler complains if I tried to just type this in. without the ::key I can compile ok, but the keyboard state does not work.  I assume I have missed something. Any help would be appreciated.

Thanks

Moon.

Pages: [1]