Fix issue in Rocket.lua script.
[Cafu-Engine.git] / Libs / SoundSystem / SoundSysFMOD3 / BufferManager.cpp
blob46fe90209960bc54338447ec2694a1975c4fa05e
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
7 #include "BufferManager.hpp"
8 #include "StaticBuffer.hpp"
9 #include "StreamingBuffer.hpp"
11 #include "FileSys/FileMan.hpp"
12 #include "FileSys/File.hpp"
13 #include "String.hpp"
15 #include <iostream>
18 static const unsigned long StreamFileSize=50000; ///< Filesize (in bytes) at which GetBuffer uses streams instead of static files.
21 BufferManagerT* BufferManagerT::GetInstance()
23 static BufferManagerT BufferManagerInstance;
25 return &BufferManagerInstance;
29 BufferManagerT::BufferManagerT()
34 BufferManagerT::~BufferManagerT()
36 ReleaseAll();
40 BufferT* BufferManagerT::GetBuffer(const std::string& AudioFile, SoundShaderT::LoadTypeE LoadType, bool Is3DSound)
42 // We need to differentiate between 2D and 3D sounds since they are created differently.
43 std::string BufferName=AudioFile;
45 if (Is3DSound) BufferName.append("3D");
46 else BufferName.append("2D");
48 switch (LoadType)
50 case SoundShaderT::AUTO:
52 if (cf::String::EndsWith(AudioFile, ".wav"))
53 return GetBuffer(AudioFile, SoundShaderT::STATIC, Is3DSound); // Always use static buffer for .wav files.
55 cf::FileSys::InFileI* FileHandle=cf::FileSys::FileMan->OpenRead(AudioFile);
57 if (FileHandle!=NULL && FileHandle->GetSize()>StreamFileSize)
59 cf::FileSys::FileMan->Close(FileHandle);
60 return GetBuffer(AudioFile, SoundShaderT::STREAM, Is3DSound);
63 cf::FileSys::FileMan->Close(FileHandle);
64 return GetBuffer(AudioFile, SoundShaderT::STATIC, Is3DSound); // Note: non existent files are handled in the StaticBufferT constructor.
67 case SoundShaderT::STATIC:
69 // If audio file has not yet been loaded into a buffer.
70 if (StaticBuffers.find(BufferName)==StaticBuffers.end())
71 StaticBuffers[BufferName]=new StaticBufferT(AudioFile, Is3DSound);
73 StaticBuffers.find(BufferName)->second->References++;
75 return StaticBuffers.find(BufferName)->second;
78 case SoundShaderT::STREAM:
80 // If audio file has already been loaded into a static buffer we don't need to create a
81 // streaming buffer anymore and just use the static one.
82 if (StaticBuffers.find(BufferName)!=StaticBuffers.end())
83 return StaticBuffers.find(BufferName)->second;
85 StreamingBufferT* NewBuffer=new StreamingBufferT(AudioFile, Is3DSound);
86 NewBuffer->References++;
88 StreamingBuffers.PushBack(NewBuffer);
90 return NewBuffer;
94 case SoundShaderT::COMPRESSED:
95 std::cout << "OpenAL: COMPRESSED creation is not yet supported, switching to AUTO\n";
96 return GetBuffer(AudioFile, SoundShaderT::AUTO, Is3DSound);
99 return NULL;
103 void BufferManagerT::CleanUp()
105 for (std::map<std::string, StaticBufferT*>::iterator It=StaticBuffers.begin(); It!=StaticBuffers.end();)
107 if (It->second->References<1)
109 // Store current position and increment BEFORE erasing the iterator position.
110 std::map<std::string, StaticBufferT*>::iterator Current=It;
111 It++;
113 delete Current->second;
114 StaticBuffers.erase(Current);
115 continue;
118 It++;
121 for (unsigned long i=0; i<StreamingBuffers.Size();)
123 if (StreamingBuffers[i]->References<1)
125 delete StreamingBuffers[i];
126 StreamingBuffers.RemoveAtAndKeepOrder(i);
127 continue;
130 i++;
135 void BufferManagerT::ReleaseBuffer(BufferT* Buffer)
137 Buffer->References--;
139 if (Buffer->References<1)
140 CleanUp();
144 void BufferManagerT::ReleaseAll()
146 for (std::map<std::string, StaticBufferT*>::iterator It=StaticBuffers.begin(); It!=StaticBuffers.end(); It++)
147 delete It->second;
149 StaticBuffers.clear();
151 for (unsigned long i=0; i<StreamingBuffers.Size(); i++)
152 delete StreamingBuffers[i];
154 StreamingBuffers.Clear();