Playing a sound function

Collisions with enemies and items are processed in the main game class. So, it's here that we will place the function to play sound effects. Add the following function declaration to Game.h:

 * Plays the given sound effect, with randomized parameters./
void PlaySound(sf::Sound& sound, sf::Vector2f position = { 0.f, 0.f });

This function takes two parameters: we take the sound that we want to play as a reference, to avoid an expensive copy, and we also include a parameter for the position where we want to play the sound. Note that we've given the position parameter a default value of { 0.f, 0.f }. Therefore, it can be ignored should we wish to do so. We'll cover exactly what this parameter does when we create spatialized ...

Get Procedural Content Generation for C++ Game Development now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.