WSound::~WSound(void) { alDeleteBuffers(m_numBuffers, m_buffers); alDeleteSources(1, &m_source); W_SAFE_DELETE_ARRAY(m_buffers); for (uint i = 0; i < m_dataV.size(); i++) { W_SAFE_FREE(m_dataV[i].data); } m_dataV.clear(); m_valid = false; //unregister GetCorePtr()->SoundComponent->m_UnRegisterSound(this); }
WError WImage::LoadFromStream(WFile* file, std::istream& inputStream) { bool bDynamic = false; unsigned int width, height, numComponents, componentSize; VkFormat format; inputStream.read((char*)&width, sizeof(m_width)); inputStream.read((char*)&height, sizeof(m_height)); inputStream.read((char*)&numComponents, sizeof(m_numComponents)); inputStream.read((char*)&componentSize, sizeof(m_componentSize)); inputStream.read((char*)&format, sizeof(m_format)); unsigned int dataSize = width * height * numComponents * componentSize; void* pixels = W_SAFE_ALLOC(dataSize); if (!pixels) return WError(W_OUTOFMEMORY); inputStream.read((char*)pixels, dataSize); WError err = CreateFromPixelsArray(pixels, width, height, bDynamic, numComponents, format, componentSize); W_SAFE_FREE(pixels); return err; }
WError WSound::LoadFromWS(basic_filebuf<char>* buff, uint pos, bool bSaveData) { //use the given stream fstream file; if (!buff) return WError(W_INVALIDPARAM); file.set_rdbuf(buff); file.seekg(pos); float temp[3]; //read pitch & volume file.read((char*)temp, 2 * sizeof(float)); alSourcef(m_source, AL_PITCH, temp[0]); alSourcef(m_source, AL_GAIN, temp[1]); //read position & direction file.read((char*)temp, 3 * sizeof(float)); alSource3f(m_source, AL_POSITION, temp[0], temp[1], temp[2]); file.read((char*)temp, 3 * sizeof(float)); alSource3f(m_source, AL_DIRECTION, temp[0], temp[1], temp[2]); char numBuffers = 0; file.read((char*)&numBuffers, 1); for (uint i = 0; i < numBuffers; i++) { __SAVEDATA data; file.read((char*)&data.buffer, 4); //buffer index file.read((char*)&data.format, sizeof ALenum); //buffer format file.read((char*)&temp[0], 4); //frequency file.read((char*)&data.dataSize, 4); //size of data data.data = W_SAFE_ALLOC(data.dataSize); file.read((char*)&data.data, m_dataV[i].dataSize); //data WError err = LoadFromMemory(data.buffer, data.data, data.dataSize, data.format, temp[0], bSaveData); W_SAFE_FREE(data.data); return WError(err); } return WError(W_SUCCEEDED); }
WError WSound::LoadFromWS(std::string filename, bool bSaveData) { fstream file; file.open(filename, ios::in | ios::binary); if (file.fail()) return WError(W_FILENOTFOUND); float temp[3]; //read pitch & volume file.read((char*)temp, 2 * sizeof(float)); alSourcef(m_source, AL_PITCH, temp[0]); alSourcef(m_source, AL_GAIN, temp[1]); //read position & direction file.read((char*)temp, 3 * sizeof(float)); alSource3f(m_source, AL_POSITION, temp[0], temp[1], temp[2]); file.read((char*)temp, 3 * sizeof(float)); alSource3f(m_source, AL_DIRECTION, temp[0], temp[1], temp[2]); char numBuffers = 0; file.read((char*)&numBuffers, 1); for (uint i = 0; i < numBuffers; i++) { __SAVEDATA data; file.read((char*)&data.buffer, 4); //buffer index file.read((char*)&data.format, sizeof ALenum); //buffer format file.read((char*)&temp[0], 4); //frequency file.read((char*)&data.dataSize, 4); //size of data data.data = W_SAFE_ALLOC(data.dataSize); file.read((char*)&data.data, m_dataV[i].dataSize); //data LoadFromMemory(data.buffer, data.data, data.dataSize, data.format, temp[0], bSaveData); W_SAFE_FREE(data.data); } file.close(); return WError(W_SUCCEEDED); }
WError WSound::LoadWAV(std::string Filename, uint buffer, bool bSaveData) { if (!m_bCheck(true)) return WError(W_ERRORUNK); fstream file(Filename, ios::in | ios::binary); if (!file.is_open()) return WError(W_FILENOTFOUND); ALenum format; uint dataSize; uint freq; bool bLittleEndian = true; char buffer4Bytes[4] = { 0 }; char buffer2Bytes[2] = { 0 }; //frequency at offset 24 (4 bytes long) file.seekg(0); file.read(buffer4Bytes, 4); if (buffer4Bytes[3] == 'F') //RIFF bLittleEndian = true; else if (buffer4Bytes[3] == 'X') //RIFX bLittleEndian = false; else return WError(W_INVALIDFILEFORMAT); //frequency at offset 24 (4 bytes long) file.seekg(24); file.read(buffer4Bytes, 4); freq = BytesTo<__int32>((BYTE*)buffer4Bytes, bLittleEndian); //data size at offset 22 (2 bytes long) file.seekg(22); file.read(buffer2Bytes, 2); uint numChannels = BytesTo<__int16>((BYTE*)buffer2Bytes, bLittleEndian); //bits per sample at offset 34 (2 bytes long) file.seekg(34); file.read(buffer2Bytes, 2); uint bps = BytesTo<__int16>((BYTE*)buffer2Bytes, bLittleEndian); if (numChannels == 1) format = bps == 8 ? AL_FORMAT_MONO8 : AL_FORMAT_MONO16; else if (numChannels == 2) format = bps == 8 ? AL_FORMAT_STEREO8 : AL_FORMAT_STEREO16; else { file.close(); return WError(W_INVALIDFILEFORMAT); } //find "data" word int offset = 36; while (offset < 260) { file.seekg(offset); file.read(buffer4Bytes, 4); if (buffer4Bytes[0] == 'd' && buffer4Bytes[1] == 'a' && buffer4Bytes[2] == 't' && buffer4Bytes[3] == 'a') { offset += 4; break; } offset++; } if (offset == 60) { file.close(); return WError(W_INVALIDFILEFORMAT); } //data size at offset (4 bytes long) file.seekg(offset); file.read(buffer4Bytes, 4); dataSize = BytesTo<__int32>((BYTE*)buffer4Bytes, bLittleEndian); //data at offset+4 (dataSize bytes long) file.seekg(offset + 4); void* data = W_SAFE_ALLOC(dataSize); file.read((char*)data, dataSize); //set buffer data alBufferData(m_buffers[buffer], format, data, dataSize, freq); if (alGetError() != AL_NO_ERROR) return WError(W_INVALIDFILEFORMAT); //release read bytes if (!bSaveData) { W_SAFE_FREE(data); } else { __SAVEDATA saveData; saveData.buffer = buffer; saveData.dataSize = dataSize; saveData.format = format; saveData.data = W_SAFE_ALLOC(dataSize); memcpy(saveData.data, data, dataSize); m_dataV.push_back(saveData); } file.close(); // Attach buffer to source alSourcei(m_source, AL_BUFFER, m_buffers[buffer]); m_valid = true; return WError(W_SUCCEEDED); }
WError WMaterial::LoadFromStream(WFile* file, std::istream& inputStream) { _DestroyResources(); VkDevice device = m_app->GetVulkanDevice(); // read the UBO data uint numUBOs; vector<std::pair<VkDeviceSize, void*>> uboData; inputStream.read((char*)&numUBOs, sizeof(numUBOs)); for (uint i = 0; i < numUBOs; i++) { VkDeviceSize size; inputStream.read((char*)&size, sizeof(size)); uboData.push_back(std::pair<VkDeviceSize, void*>(size, W_SAFE_ALLOC(size))); inputStream.read((char*)uboData[uboData.size() - 1].second, size); } // read the texture data uint numTextures; vector<std::pair<uint, uint>> textureData; inputStream.read((char*)&numTextures, sizeof(numTextures)); for (uint i = 0; i < numTextures; i++) { uint tid, index; inputStream.read((char*)&tid, sizeof(tid)); inputStream.read((char*)&index, sizeof(index)); textureData.push_back(std::pair<uint, uint>(tid, index)); } uint fxId; inputStream.read((char*)&fxId, sizeof(fxId)); // load dependencies WEffect* fx; WError err = file->LoadAsset<WEffect>(fxId, &fx); if (err) { err = SetEffect(fx); fx->RemoveReference(); if (err) { for (uint i = 0; i < textureData.size() && err; i++) { WImage* tex; err = file->LoadAsset<WImage>(textureData[i].first, &tex); if (err) { err = SetTexture(textureData[i].second, tex); tex->RemoveReference(); } } } } if (err) { if (m_uniformBuffers.size() != uboData.size()) err = WError(W_INVALIDFILEFORMAT); else { for (uint i = 0; i < m_uniformBuffers.size() && err; i++) { UNIFORM_BUFFER_INFO* UBO = &m_uniformBuffers[i]; if (uboData[i].first != UBO->descriptor.range) err = WError(W_INVALIDFILEFORMAT); else { void* pData; VkResult vkRes = vkMapMemory(device, m_uniformBuffers[i].memory, 0, UBO->descriptor.range, 0, (void **)&pData); if (vkRes) err = WError(W_UNABLETOMAPBUFFER); else { memcpy(pData, uboData[i].second, UBO->descriptor.range); vkUnmapMemory(device, m_uniformBuffers[0].memory); } } } } } for (uint i = 0; i < uboData.size(); i++) W_SAFE_FREE(uboData[i].second); if (!err) _DestroyResources(); return err; }