Get sound file path for SDL_mixer with VC++ (VS2017)

282 Views Asked by At

I managed to install the SDL and integrate it into my Visual Studio 2017 project for Windows 10.

My main problem is the playback of audio files. Indeed, when I put the absolute path of the audio file, everything works perfectly well.

#include "framework.h"
#include "Pokemon.h"
#include <iostream>
#include <SDL.h>
#include <SDL_mixer.h>

#define WAV_PATH "C:\\Users\\quent\\Documents\\Polytech\\Annee3\\projetPerso\\Sons\\musique.mp3" //WORK TOTALY FINE
#define WAV2_PATH ".\\Resource Files\\musique.mp3" //Mix_LoadMUS(WAV2_PATH) return NULL, so the file is not played

const int WIDTH = 800, HEIGHT = 600;

int main(int argc, char *argv[])
{

    Mix_Music *wave = NULL;
    Mix_Music *wave2 = NULL;

    if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) != 0) {
        std::cout << "Impossible d'ouvrir mixer " << SDL_GetError() << std::endl;
        return 1;
    }

    wave = Mix_LoadMUS(WAV2_PATH);
    wave2 = Mix_LoadMUS(WAV2_PATH);

    if (wave == NULL || wave2 == NULL) {
        std::cout << "Impossible de chargé un fichier .ogg " << SDL_GetError() << std::endl;
        return 2;
    }

    if (Mix_PlayMusic(wave, 1) != 0) {
        std::cout << "Impossible de jouer le fichier " << SDL_GetError() << std::endl;
        return 1;
    }

    SDL_Delay(5000);

    if (Mix_PlayMusic(wave2, 1) != 0) {
        std::cout << "Impossible de jouer le fichier " << SDL_GetError() << std::endl;
        return 1;
    }

    while (Mix_PlayingMusic());

    Mix_FreeMusic(wave);
    Mix_FreeMusic(wave2);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return EXIT_SUCCESS;
}

Where it gets complicated is when I import files into my project, in the "Resource Files" folder. Not knowing Visual Studio, I imagine that this is the folder to use for the external resources you would like to use (sorry if I'm wrong).

all sound file are into Resources Files

Once my files are imported, I can't figure out how to recover their path to give it to the SDL function (Mix_LoadMUS()) that will access the audio file to play.

So I would like to know how resources are managed in a Visual Studio project.

1

There are 1 best solutions below

1
On

You want to place your resources somewhere where their path is known relative to your executable at runtime. Then all you need to do is; work out the runtime path of your executable (all operating systems have a way to do that - for example, on Linux; read the /proc/self/exe symlink) and then transform that path into a path to your resource.

For example: If you always install your program so that it is in some/random/install/location/bin and resources then also go in some/random/install/location/resources. Then once you've obtained some/random/install/location/bin/myexecutable as the current runtime path to your program, all you need to do is strip off the "myexecutable" bit and replace it with "../resources/myresource.file".

This will work regardless of where your program is installed, as long as the directory structure beneath the installation directory, relative to your executable, is fixed (and you should be able to control that). Don't use absolute paths - that breaks as soon as someone installs your program in an unexpected location. Make everything relative to where the executable happens to be.