~vcs-imports/aethyra/client

« back to all changes in this revision

Viewing changes to src/core/sound/music.cpp

  • Committer: Tametomo
  • Author(s): Thorbjørn Lindeijer
  • Date: 2012-01-26 20:44:35 UTC
  • Revision ID: git-v1:d3c8a201fb7e5d09046b9f46d645b26b3da8267b
Stream music files directly from the archives

Use Mix_LoadMUS_RW to stream music files directly from PhysFS. I kept
around ResourceManager:copyFile for now, since it may have other uses.

Reviewed-by: Yohann Ferreira

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
#include "../log.h"
26
26
 
27
 
Music::Music(Mix_Chunk *music):
28
 
    mChunk(music),
29
 
    mChannel(-1)
 
27
Music::Music(Mix_Music *music):
 
28
    mMusic(music)
30
29
{
31
30
}
32
31
 
33
32
Music::~Music()
34
33
{
35
 
    //Mix_FreeMusic(music);
36
 
    Mix_FreeChunk(mChunk);
 
34
    Mix_FreeMusic(mMusic);
37
35
}
38
36
 
39
 
Resource *Music::load(void *buffer, unsigned bufferSize)
 
37
 
 
38
Resource *Music::load(SDL_RWops *rw)
40
39
{
41
 
    // Load the raw file data from the buffer in an RWops structure
42
 
    SDL_RWops *rw = SDL_RWFromMem(buffer, bufferSize);
43
 
 
44
 
    // Use Mix_LoadMUS to load the raw music data
45
 
    //Mix_Music* music = Mix_LoadMUS_RW(rw); Need to be implemeted
46
 
    Mix_Chunk *tmpMusic = Mix_LoadWAV_RW(rw, 1);
47
 
 
48
 
    if (tmpMusic)
49
 
        return new Music(tmpMusic);
 
40
    if (Mix_Music *music = Mix_LoadMUS_RW(rw))
 
41
        return new Music(music);
50
42
    else
51
43
    {
52
44
        logger->log("Error, failed to load music: %s", Mix_GetError());
54
46
    }
55
47
}
56
48
 
57
 
bool Music::play(const int loops)
58
 
{
59
 
    /*
60
 
     * Warning: loops should be always set to -1 (infinite) with current
61
 
     * implementation to avoid halting the playback of other samples
62
 
     */
63
 
 
64
 
    /*if (Mix_PlayMusic(music, loops))
65
 
        return true;*/
66
 
    Mix_VolumeChunk(mChunk, 120);
67
 
    mChannel = Mix_PlayChannel(-1, mChunk, loops);
68
 
 
69
 
    return mChannel != -1;
70
 
}
71
 
 
72
 
void Music::stop()
73
 
{
74
 
    /*
75
 
     * Warning: very dangerous trick, it could try to stop channels occupied
76
 
     * by samples rather than the current music file
77
 
     */
78
 
 
79
 
    //Mix_HaltMusic();
80
 
 
81
 
    if (mChannel != -1)
82
 
        Mix_HaltChannel(mChannel);
83
 
}
 
49
bool Music::play(const int loops, const int fadeIn)
 
50
{
 
51
    if (fadeIn > 0)
 
52
        return Mix_FadeInMusic(mMusic, loops, fadeIn);
 
53
    else
 
54
        return Mix_PlayMusic(mMusic, loops);
 
55
}
 
56