Ref<Face> Face::read(ResourceCache& cache, const std::string& name) { if (Face* cached = cache.find<Face>(name)) return cached; const Path path = cache.findFile(name); if (path.isEmpty()) { logError("Failed to find face %s", name.c_str()); return nullptr; } std::ifstream stream(path.name(), std::ios::in | std::ios::binary); if (stream.fail()) { logError("Failed to open face file %s", path.name().c_str()); return nullptr; } std::vector<char> data; stream.seekg(0, std::ios::end); data.resize((size_t) stream.tellg()); stream.seekg(0, std::ios::beg); stream.read(data.data(), data.size()); return create(ResourceInfo(cache, name, path), data.data(), data.size()); }
void Terrain::load() { // Load terrain resources. ResourceList resourceList; // Heightmap resourceList.push_back( ResourceInfo( mHeightmapFile ) ); // Textures for( LayerInstances::iterator i = mLayerInstances.begin(); i != mLayerInstances.end(); ++i ) { for( std::vector<Path>::iterator j = (*i).mTextureFiles.begin(); j != (*i).mTextureFiles.end(); ++j ) { resourceList.push_back( ResourceInfo( *j ) ); } } mResourceManager.loadResources( resourceList, sigc::mem_fun( this, &Terrain::resourcesLoaded ) ); }
Ref<Material> Material::read(RenderContext& context, const std::string& name) { initializeMaps(); if (Material* cached = context.cache().find<Material>(name)) return cached; const Path path = context.cache().findFile(name); if (path.isEmpty()) { logError("Failed to find material %s", name.c_str()); return nullptr; } pugi::xml_document document; const pugi::xml_parse_result result = document.load_file(path.name().c_str()); if (!result) { logError("Failed to load material %s: %s", name.c_str(), result.description()); return nullptr; } pugi::xml_node root = document.child("material"); if (!root || root.attribute("version").as_uint() != MATERIAL_XML_VERSION) { logError("Material file format mismatch in %s", name.c_str()); return nullptr; } Ref<Material> material = Material::create(ResourceInfo(context.cache(), name, path), context); for (auto pn : root.children("pass")) { const std::string phaseName(pn.attribute("phase").value()); if (!phaseMap.hasKey(phaseName)) { logError("Invalid render phase %s in material %s", phaseName.c_str(), name.c_str()); return nullptr; } if (!parsePass(context, material->pass(phaseMap[phaseName]), pn)) { logError("Failed to parse pass for material %s", name.c_str()); return nullptr; } } return material; }
void ApplicationCacheHost::fillResourceList(ResourceInfoList* resources) { if (!m_internal) return; WebKit::WebVector<WebKit::WebApplicationCacheHost::ResourceInfo> webResources; m_internal->m_outerHost->getResourceList(&webResources); for (size_t i = 0; i < webResources.size(); ++i) { resources->append(ResourceInfo( webResources[i].url, webResources[i].isMaster, webResources[i].isManifest, webResources[i].isFallback, webResources[i].isForeign, webResources[i].isExplicit, webResources[i].size)); } }
void Audio::create() { mCreated = true; if( mFile.empty() ) return; try { mResourceManager.loadResource( ResourceInfo( mFile, RESOURCETYPE_AUDIO ), sigc::mem_fun( this, &Audio::resourceLoaded ) ); } catch( FileNotFoundException e ) { CLOGE << "Could not load resource for audio component: " << e.what(); Component::destroyComponentLocally(); } }
void ApplicationCacheHost::fillResourceList(ResourceInfoList* resources) { ApplicationCache* cache = applicationCache(); if (!cache || !cache->isComplete()) return; for (const auto& urlAndResource : *cache) { RefPtr<ApplicationCacheResource> resource = urlAndResource.value; unsigned type = resource->type(); bool isMaster = type & ApplicationCacheResource::Master; bool isManifest = type & ApplicationCacheResource::Manifest; bool isExplicit = type & ApplicationCacheResource::Explicit; bool isForeign = type & ApplicationCacheResource::Foreign; bool isFallback = type & ApplicationCacheResource::Fallback; resources->append(ResourceInfo(resource->url(), isMaster, isManifest, isFallback, isForeign, isExplicit, resource->estimatedSizeInStorage())); } }
Ref<Font> Font::read(RenderContext& context, const std::string& name) { if (Font* cached = context.cache().find<Font>(name)) return cached; const Path path = context.cache().findFile(name); if (path.isEmpty()) { logError("Failed to find font %s", name.c_str()); return nullptr; } pugi::xml_document document; const pugi::xml_parse_result result = document.load_file(path.name().c_str()); if (!result) { logError("Failed to load font %s: %s", name.c_str(), result.description()); return nullptr; } pugi::xml_node root = document.child("font"); if (!root || root.attribute("version").as_uint() != FONT_XML_VERSION) { logError("Font file format mismatch in %s", name.c_str()); return nullptr; } const std::string faceName(root.attribute("face").value()); if (faceName.empty()) { logError("No typeface specified for font %s", faceName.c_str()); return nullptr; } const uint height = root.attribute("height").as_uint(); Ref<Face> face = Face::read(context.cache(), faceName); if (!face) return nullptr; return create(ResourceInfo(context.cache(), name, path), context, *face, height); }
void ApplicationCacheHost::fillResourceList(ResourceInfoList* resources) { ApplicationCache* cache = applicationCache(); if (!cache || !cache->isComplete()) return; ApplicationCache::ResourceMap::const_iterator end = cache->end(); for (ApplicationCache::ResourceMap::const_iterator it = cache->begin(); it != end; ++it) { RefPtr<ApplicationCacheResource> resource = it->value; unsigned type = resource->type(); bool isMaster = type & ApplicationCacheResource::Master; bool isManifest = type & ApplicationCacheResource::Manifest; bool isExplicit = type & ApplicationCacheResource::Explicit; bool isForeign = type & ApplicationCacheResource::Foreign; bool isFallback = type & ApplicationCacheResource::Fallback; resources->append(ResourceInfo(resource->url(), isMaster, isManifest, isFallback, isForeign, isExplicit, resource->estimatedSizeInStorage())); } }
void ResourceStorage::AdjustCapacity( const HashedString& hResource, int iAdjustAmount ) { std::map< HashedString, ResourceInfo >::iterator pIter = m_Resources.find( hResource ); if ( pIter == m_Resources.end() ) { // Does not have from the resource m_Resources[ hResource ] = ResourceInfo( 0, iAdjustAmount ); } else { pIter->second.iCapacity += iAdjustAmount; if ( pIter->second.iCapacity < pIter->second.iAmount ) { pIter->second.iAmount = pIter->second.iCapacity; } } }
Ref<Font> FontReader::read(const String& name, const Path& path) { std::ifstream stream(path.name().c_str()); if (stream.fail()) { logError("Failed to open font %s", name.c_str()); return nullptr; } pugi::xml_document document; const pugi::xml_parse_result result = document.load(stream); if (!result) { logError("Failed to load font %s: %s", name.c_str(), result.description()); return nullptr; } pugi::xml_node root = document.child("font"); if (!root || root.attribute("version").as_uint() != FONT_XML_VERSION) { logError("Font file format mismatch in %s", name.c_str()); return nullptr; } const String faceName(root.attribute("face").value()); if (faceName.empty()) { logError("No typeface specified for font %s", faceName.c_str()); return nullptr; } const uint height = root.attribute("height").as_uint(); Ref<Face> face = Face::read(cache, faceName); if (!face) return nullptr; return Font::create(ResourceInfo(cache, name, path), *m_pool, *face, height); }
Ref<AudioBuffer> AudioBuffer::read(AudioContext& context, const std::string& sampleName) { ResourceCache& cache = context.cache(); std::string name; name += "sample:"; name += sampleName; if (Ref<AudioBuffer> buffer = cache.find<AudioBuffer>(name)) return buffer; Ref<Sample> data = Sample::read(cache, sampleName); if (!data) { logError("Failed to read sample for buffer %s", name.c_str()); return nullptr; } return create(ResourceInfo(cache, name), context, *data); }
void Audio::setAudioFile( const Path& rAudioFile ) { if( rAudioFile.empty() || mFile == rAudioFile ) return; mFile = rAudioFile; if( mCreated ) { // TODO: Store and load properties when reloading the audio file so all the properties from // previous audio component also get set onto the new audio component. try { mResourceManager.loadResource( ResourceInfo( mFile, RESOURCETYPE_AUDIO ), sigc::mem_fun( this, &Audio::resourceLoaded ) ); } catch( FileNotFoundException e ) { CLOGE << "Could not load resource for audio component: " << e.what(); } } }
Ref<Image> Face::glyph(int index, float scale) const { if (stbtt_IsGlyphEmpty(m_info, index)) return nullptr; int left, top, right, bottom; stbtt_GetGlyphBitmapBox(m_info, index, scale, scale, &left, &top, &right, &bottom); Ref<Image> glyph = Image::create(ResourceInfo(cache()), PixelFormat::L8, right - left, bottom - top); stbtt_MakeGlyphBitmap(m_info, (unsigned char*) glyph->pixels(), glyph->width(), glyph->height(), glyph->width(), scale, scale, index); glyph->flipHorizontal(); return glyph; }
Ref<Mesh> MeshReader::read(const String& name, const Path& path) { std::ifstream stream(path.name().c_str(), std::ios::in | std::ios::binary); if (stream.fail()) { logError("Failed to open mesh %s", name.c_str()); return nullptr; } String line; uint lineNumber = 0; std::vector<vec3> positions; std::vector<vec3> normals; std::vector<vec2> texcoords; std::vector<Triplet> triplets; std::vector<FaceGroup> groups; FaceGroup* group = nullptr; while (std::getline(stream, line)) { const char* text = line.c_str(); ++lineNumber; if (!interesting(&text)) continue; try { String command = parseName(&text); if (command == "g") { // Silently ignore group names } else if (command == "o") { // Silently ignore object names } else if (command == "s") { // Silently ignore smoothing } else if (command == "v") { vec3 vertex; vertex.x = parseFloat(&text); vertex.y = parseFloat(&text); vertex.z = parseFloat(&text); positions.push_back(vertex); } else if (command == "vt") { vec2 texcoord; texcoord.x = parseFloat(&text); texcoord.y = parseFloat(&text); texcoords.push_back(texcoord); } else if (command == "vn") { vec3 normal; normal.x = parseFloat(&text); normal.y = parseFloat(&text); normal.z = parseFloat(&text); normals.push_back(normalize(normal)); } else if (command == "usemtl") { String materialName = parseName(&text); group = nullptr; for (auto& g : groups) { if (g.name == materialName) group = &g; } if (!group) { groups.push_back(FaceGroup()); groups.back().name = materialName; group = &(groups.back()); } } else if (command == "mtllib") { // Silently ignore .mtl material files } else if (command == "f") { if (!group) throw Exception("Expected \'usemtl\' before \'f\'"); triplets.clear(); while (*text != '\0') { triplets.push_back(Triplet()); Triplet& triplet = triplets.back(); triplet.vertex = parseInteger(&text); triplet.texcoord = 0; triplet.normal = 0; if (*text == '/') { if (std::isdigit(*(++text))) triplet.texcoord = parseInteger(&text); if (*text == '/') { if (std::isdigit(*(++text))) triplet.normal = parseInteger(&text); } } while (std::isspace(*text)) text++; } for (size_t i = 2; i < triplets.size(); i++) { group->faces.push_back(Face()); Face& face = group->faces.back(); face.p[0] = triplets[0]; face.p[1] = triplets[i - 1]; face.p[2] = triplets[i]; } } else { logWarning("Unknown command %s in mesh %s line %d", command.c_str(), name.c_str(), lineNumber); } } catch (Exception& e) { logError("%s in mesh %s line %d", e.what(), name.c_str(), lineNumber); return nullptr; } } Ref<Mesh> mesh = new Mesh(ResourceInfo(cache, name, path)); mesh->vertices.resize(positions.size()); for (size_t i = 0; i < positions.size(); i++) mesh->vertices[i].position = positions[i]; VertexTool tool(mesh->vertices); for (auto& g : groups) { mesh->sections.push_back(MeshSection()); MeshSection& geometry = mesh->sections.back(); const FaceList& faces = g.faces; geometry.materialName = g.name; geometry.triangles.resize(faces.size()); for (size_t i = 0; i < faces.size(); i++) { const Face& face = faces[i]; MeshTriangle& triangle = geometry.triangles[i]; for (size_t j = 0; j < 3; j++) { const Triplet& point = face.p[j]; vec3 normal; if (point.normal) normal = normals[point.normal - 1]; vec2 texcoord; if (point.texcoord) texcoord = texcoords[point.texcoord - 1]; triangle.indices[j] = tool.addAttributeLayer(point.vertex - 1, normal, texcoord); } } } tool.realizeVertices(mesh->vertices); return mesh; }