void SpritesDemo::Load() { m_sprites[0] = new WSprite(m_app); m_sprites[0]->Load(); WImage* img = new WImage(m_app); img->Load("Media/dummy.bmp"); m_sprites[0]->SetImage(img); img->RemoveReference(); m_sprites[1] = new WSprite(m_app); m_sprites[1]->Load(); img = new WImage(m_app); img->Load("Media/checker.bmp"); m_sprites[1]->SetImage(img); img->RemoveReference(); m_sprites[1]->SetPosition(100, 100); m_sprites[1]->SetAlpha(0.5f); m_sprites[2] = new WSprite(m_app); WShader* pixel_shader = new CustomSpritePS(m_app); pixel_shader->Load(); WEffect* fx = m_app->SpriteManager->CreateSpriteEffect(pixel_shader); pixel_shader->RemoveReference(); WMaterial* mat = new WMaterial(m_app); mat->SetEffect(fx); fx->RemoveReference(); m_sprites[2]->SetMaterial(mat); mat->RemoveReference(); m_sprites[2]->Load(); m_sprites[2]->SetPosition(200, 200); m_sprites[2]->SetSize(200, 200); }
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; }