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

Author Topic: System and logic questions  (Read 1018 times)

0 Members and 1 Guest are viewing this topic.

spoty76

  • Newbie
  • *
  • Posts: 21
    • View Profile
System and logic questions
« on: March 25, 2017, 10:55:37 am »
Hello, I got windows 10, vis. studio 2013, sfml 2.4.2: so, I have just made a test map using massives (the code is bellow). I've made some interaction with map in class Player and the thing is that I want to play a sound when the player touches an object, but the problem with that is I make a sound variable in the Int main, and the interaction with map is above the int main. I tried to creat sound variable right there, in class player, but this didn't work. Then the other thing I would like to understand is how to play sound when I press a keyboard button, for example when the player walks I want him to say smth, but the sound only starts and starts because the keyboard key is pressed, and I want the sound to play once the key WAS pressed, plz help!

I want to put sound here:
if (TileMap[i][j] == '6') {x = 120; y = 920; [b]teleport.play();[/b] TileMap[i][j] == ' '}
and here:
if (Keyboard::isKeyPressed(Keyboard::A)) {p.dir = 0; p.speed = 2;teleport.play();}

P.s. i got three source file: view.h, map.h, main.cpp:
main.cpp
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "map.h"
#include "view.h"
using namespace sf;

class Player {
private: float x, y;
public:
        float w, h, dx, dy, speed = 0;
        int dir = 0;
        String File;
        Image image;
        Texture texture;
        Sprite sprite;

        Player(String F, int X, int Y, float W, float H) {
                File = F;
                w = W; h = H;
                image.loadFromFile("images/" + File);
                image.createMaskFromColor(Color(41, 33, 59));
                texture.loadFromImage(image);
                sprite.setTexture(texture);
                x = X; y = Y;
                sprite.setTextureRect(IntRect(w, h, w, h));

        }
        void update(float time)
        {
                switch (dir)
                {
                case 0: dx = speed; dy = 0; break;
                case 1: dx = -speed; dy = 0; break;
                case 2: dx = 0; dy = speed; break;
                case 3: dx = 0; dy = -speed; break;
                }

                x += dx*time;
                y += dy*time;

                speed = 0;
                sprite.setPosition(x, y);
                interactionWithMap();

        }


        float getplayercoordinateX(){
                return x;
        }
        float getplayercoordinateY(){
                return y;
        }


        void interactionWithMap()
        {
                for (int i = y / 32; i < (y + h) / 32; i++)
                for (int j = x / 32; j < (x + w) / 32; j++)
                {
                        if (TileMap[i][j] == '3')
                        {
                                if (dy>0)
                                {
                                        y = i * 32 - h;
                                }
                                if (dy < 0)
                                {
                                        y = i * 32 + 32;
                                }
                                if (dx>0)
                                {
                                        x = j * 32 - w;
                                }
                                if (dx < 0)
                                {
                                        x = j * 32 + 32;
                                }
                        }

                        if (TileMap[i][j] == '1') {
                                x = 100; y = 675;
                                sprite.setScale(Vector2f(4, 4));
                                y = 392;
                                view.setSize(1380, 820);
                                TileMap[i][j] = ' ';
                        }
                }

                for (int i = y / 32; i < (y + h) / 32; i++)
                for (int j = x / 32; j < (x + w) / 32; j++)
                {
                        if (TileMap[i][j] == '3')
                        {
                                if (dy>0)
                                {
                                        y = i * 32 - h;
                                }
                                if (dy < 0)
                                {
                                        y = i * 32 + 32;
                                }
                                if (dx>0)
                                {
                                        x = j * 32 - w;
                                }
                                if (dx < 0)
                                {
                                        x = j * 32 + 32;
                                }
                        }

                        if (TileMap[i][j] == '6') {
                                x = 120; y = 920;
                                TileMap[i][j] = ' ';
                        }
                }
        }
};


int main()
{
        sf::RenderWindow window(sf::VideoMode(1980, 1080), "Glinskii Adventures");
    view.reset(sf::FloatRect(0, 0, 640, 400));

        Image map_image;
        map_image.loadFromFile("images/map.png");
        Texture map;
        map.loadFromImage(map_image);
        Sprite map_sprite;
        map_sprite.setTexture(map);
       
        Image house_image;
        house_image.loadFromFile("images/house.png");
        house_image.createMaskFromColor(Color(255, 255, 255));
        Texture house;
        house.loadFromImage(house_image);
        Sprite house_sprite;
        house_sprite.setTexture(house);
        house_sprite.setPosition(100, 539);

        SoundBuffer teleportBuffer;
        teleportBuffer.loadFromFile("sounds/teleport.ogg");
        Sound teleport(teleportBuffer);

        SoundBuffer claySongBuffer;
        claySongBuffer.loadFromFile("sounds/claySong.ogg");
        Sound claySong(claySongBuffer);
        claySong.play();

        Player p("hero.png", 100, 675, 94.0, 96.0);

        float CurrentFrame = 0;

        Clock clock;

        while (window.isOpen())
        {
                float time = clock.getElapsedTime().asMicroseconds();
                clock.restart();
                time = time / 800;

                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
       
                if (Keyboard::isKeyPressed(Keyboard::D)) {
                        p.dir = 0; p.speed = 2;
                        CurrentFrame += 0.005*time;
                        if (CurrentFrame > 3) CurrentFrame -= 3;
                        p.sprite.setTextureRect(IntRect(97 * int(CurrentFrame), 192, 94, 96));
                        getplayercoordinateforview(p.getplayercoordinateX(), p.getplayercoordinateY());
                }

                if (Keyboard::isKeyPressed(Keyboard::A)) {
                        p.dir = 1; p.speed = 2;
                        CurrentFrame += 0.005*time;
                        if (CurrentFrame > 3) CurrentFrame -= 3;
                        p.sprite.setTextureRect(IntRect(96 * int(CurrentFrame), 96, 94, 96));
                        getplayercoordinateforview(p.getplayercoordinateX(), p.getplayercoordinateY());
                }
               
                        p.update(time);

                        viewmap(time);
                        changeview();
                        window.setView(view);

                window.clear();

                for (int i = 0; i < HEIGHT_MAP; i++)
                for (int j = 0; j < WIDTH_MAP; j++)
                {
                        if (TileMap[i][j] == '3') map_sprite.setTextureRect(IntRect(96, 0, 32, 32));
                        if (TileMap[i][j] == '0') map_sprite.setTextureRect(IntRect(128, 0, 32, 32));
                        if (TileMap[i][j] == '2') map_sprite.setTextureRect(IntRect(160, 0, 32, 32));
                        if (TileMap[i][j] == ' ') map_sprite.setTextureRect(IntRect(192, 0, 32, 32));
                        if (TileMap[i][j] == '1') map_sprite.setTextureRect(IntRect(0, 0, 32, 32));
                        if (TileMap[i][j] == 'g') map_sprite.setTextureRect(IntRect(64, 0, 32, 32));
                        if (TileMap[i][j] == '6') map_sprite.setTextureRect(IntRect(0, 0, 32, 32));
                        if (TileMap[i][j] == '7') map_sprite.setTextureRect(IntRect(0, 0, 32, 32));

            map_sprite.setPosition(j * 32, i * 32);

                        window.draw(map_sprite);
                }

                window.draw(p.sprite);
                window.draw(house_sprite);
                window.display();
        }

        return 0;
}
[b]map.h[/b]
#include <SFML\Graphics.hpp>
const int HEIGHT_MAP = 33;
const int WIDTH_MAP = 194;
sf::String TileMap[HEIGHT_MAP] = {
        "                                                                                                                                                                                                 0",
        " 7777 7   777 7  7                                                                                                                                                                               3",
        " 7    7    7  7  7                                                                                                                                                                               3",
        " 7    7    7  7 77                                                                                                                                                                               3",
        " 7 7  7    7  77 7                                                                                                                                                                               3",
        " 77 7 777 777 7  7                                                                                                                                                                               3",
        "                                                                                                                                                                                                 3",
        "   7   777  7   7                                                                                                                                                                                3",
        "  7 7  7  7 7   7                                                                                                                                                                                3",
        "  777  7  7  7 7                                                                                                                                                                                 3",
        " 7   7 7  7  7 7                                                                                                                                                                                 3",
        " 7   7 777    7   7                                                                                                                                                                              3",
        "                                                                                                                                                                                                 3",
        "                                                                                                                                                                                                 3",
        "                                                                                                                                                                                                 3",
        "3                                                                                                                                                                                                3",
        "3                                                                                                                                                                                                3",
        "3                                                                                                                                                                                                3",
        "3                                                                                                                                                                                                3",
        "3                                                                                                                                                                                                3",
        "3                                                                                                                                                                                                3",
        "3                                                                                                                                                                                                3",
        "3                                                                                                                                                                                                3",
        "3                                                                                                             6                                                                                  3",
        "22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222",
        "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg",
        "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg",
        "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg",
        "3                                                3                                                                                                                                                ",  
        "3                                                3                                                                                                                                                ",                                      
    "3                                                3                                                                                                                                                ",                                      
    "3                                           1    3                                                                                                                                                ",
    "33333333333333333333333333333333333333333333333333                                                                                                                                                ",                                      
};
 
« Last Edit: March 25, 2017, 04:36:28 pm by eXpl0it3r »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: System and logic questions
« Reply #1 on: March 25, 2017, 11:39:24 am »
You should place code inside [code=cpp] [/code] tags for clarity.

There is a lot of code that isn't relevant to the problem but not enough to test it.

I tried to creat sound variable right there, in class player, but this didn't work.
Didn't work in what way?

the other thing I would like to understand is how to play sound when I press a keyboard button, for example when the player walks I want him to say smth, but the sound only starts and starts because the keyboard key is pressed, and I want the sound to play once the key WAS pressed
I'm not sure I fully understand what this means but it sounds like you want the sound to start when you first press a key but not keep starting as long as you have the key still pressed. Is this correct?
If so:

If you use events to track these keypresses, you can get an event only for the keypress and then deal with it (start the sound):
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::A)
{
    sound.play();
    // stuff that happens when you press "A"
}

If you need to use real-time key states (isKeyPressed), you can set up booleans to track the previous state of the key and compare is to the current state. If they differ, the key has just changed (obviously, the previous state would then need to be updated to match):
bool previousKeypressedStateKeyA = false; // initialisation

// normal loop
{
    const bool currentKeypressedStateKeyA = sf::isKeyPressed(sf::Keyboard::A);
    if (currentKeypressedStateKeyA != previousKeypressedStateKeyA) // state of "A" key has changed
    {
        if (currentKeypressedStateKeyA)
        {
            // "A" key was just pressed
        }
        else
        {
            // "A" key was just released
        }
        previousKeypressedStateKeyA = currentKeypressedStateKeyA;
    }
}

However, the other option specifically for sound would be to check to see if the sound is playing and don't start it if it is:
const bool soundIsAlreadyPlaying = (sound.getStatus() == sf::Sound::Status::Playing);
Note that although using this can prevent the sound from starting if it's already playing but it wouldn't help if after the sound has finished; it's no longer playing so it may start again if the key is still pressed.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

spoty76

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: System and logic questions
« Reply #2 on: March 25, 2017, 03:00:23 pm »
I tried to do it like this:
 void interactionWithMap()
   {
      for (int i = y / 32; i < (y + h) / 32; i++)
      for (int j = x / 32; j < (x + w) / 32; j++)
      {
         if (TileMap[j] == '3')
         {
            if (dy>0)
            {
               y = i * 32 - h;
            }
            if (dy < 0)
            {
               y = i * 32 + 32;
            }
            if (dx>0)
            {
               x = j * 32 - w;
            }
            if (dx < 0)
            {
               x = j * 32 + 32;
            }
         }

         if (TileMap[j] == '1') {
            x = 100; y = 675;
            sprite.setScale(Vector2f(4, 4));
            y = 392;
            view.setSize(1380, 820);


            SoundBuffer teleportBuffer;
            teleportBuffer.loadFromFile("sounds/teleport.ogg");
            Sound teleport(teleportBuffer);
            teleport.play();


            TileMap[j] = ' ';
         }
      }
The sound didn't play when I faced block 1.
And with the keyboard you were totally right, that was what I wanted. Thank you
« Last Edit: March 25, 2017, 04:37:10 pm by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: System and logic questions
« Reply #3 on: March 25, 2017, 04:43:52 pm »
As pointed out by Hapax you should really use the [code=cpp] [/code] tags!
You can find them here in the editor or type them manually:


A SoundBuffer must exist as long as you play the sound.
The play function is non-blocking, which means it will return immediately and the sound will play in a separate thread, but since the function returns immediately, the execution will move forward, leaving the if-body and thus deleting the SoundBuffer.
As such, you should load resources in a place that has a longer living span. But don't use global variables, as that will most likely lead to crashes. Better pack your functions into a class and then use class member variables.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything