virtual void compileResource(const char* fileName, std::map<std::string, std::string>& options) {
		TiXmlDocument document;
		document.LoadFile(fileName);

		if(document.Error()) {
			throw Exception(document.ErrorDesc());
		}

		TiXmlElement* rootMaterial = document.RootElement();
		if(strcmp("material", rootMaterial->Value()) != 0) {
			throw Exception("");
		}

		TiXmlElement* xmlEffect = rootMaterial->FirstChildElement("effect");
		if(!xmlEffect)
			throw Exception("");

		const char* effectSrc = xmlEffect->Attribute("src");
		std::string effectName = file::getFilename(effectSrc);

		compiler->compile(effectSrc, options);
		Effect* effect = (Effect*)manager->loadResource(EffectKey(effectName));

		Material* material = new Material(file::getFilename(fileName), manager, effect);

		TiXmlElement* xmlSampler = xmlEffect->FirstChildElement("sampler");
		while(xmlSampler) {
			const char* samplerName = xmlSampler->Attribute("name");
			const char* textureSrc = xmlSampler->Attribute("src");
			std::string textureName = file::getFilename(textureSrc);

			compiler->compile(textureSrc, options);

			Image* image = (Image*)manager->loadResource(ImageKey(textureName));
			material->addSampler(samplerName, image);

			xmlSampler = xmlSampler->NextSiblingElement("sampler");
		}

		std::string outputName = file::getPath(fileName) + "/" + file::getFilename(fileName) + ".material";
		FileStream fileStream(outputName);
		ResourceBinStream resourceStream(fileStream);
		MaterialUtils::write(resourceStream, *manager, material);

		delete material;
	}
Exemple #2
0
void Sprite::load(ResourceManager & resourceManager)
{
	if(this->texture) {
		/* texture is already loaded */
		return;
	}

	Texture2DLoader loader(resourceManager);
	uint64_t textureId = resourceManager.loadResource(this->imageName, loader);
	this->texture = static_cast<Texture2D *>(resourceManager.getResourceById(textureId));
	SDL_assert(this->texture);
	
	const Rectangle & r = this->texture->getBounds();
	this->source = Rectangle(
		r.x / r.width,
		r.y / r.height,
		1,
		1
	);
	
	if(!(this->bounds.width) || !(this->bounds.height)) {
		this->bounds = this->texture->getBounds();
	}
}
ANKI_TEST(Resource, ResourceManager)
{
    // Create
    Config config;

    HeapAllocator<U8> alloc(allocAligned, nullptr);

    ResourceManagerInitInfo rinit;
    rinit.m_gr = nullptr;
    rinit.m_config = &config;
    rinit.m_cacheDir = "/tmp/";
    rinit.m_allocCallback = allocAligned;
    rinit.m_allocCallbackData = nullptr;
    ResourceManager* resources = alloc.newInstance<ResourceManager>();
    ANKI_TEST_EXPECT_NO_ERR(resources->create(rinit));

    // Very simple
    {
        DummyResourcePtr a;
        ANKI_TEST_EXPECT_NO_ERR(resources->loadResource("blah", a));
    }

    // Load a resource
    {
        DummyResourcePtr a;

        ANKI_TEST_EXPECT_NO_ERR(resources->loadResource("blah", a));

        {
            DummyResourcePtr b = a;
            a = b;
            b = a;
        }
    }

    // Load and load again
    {
        DummyResourcePtr a;
        ANKI_TEST_EXPECT_NO_ERR(resources->loadResource("blah", a));
        auto refcount = a->getRefcount().load();

        DummyResourcePtr b;
        ANKI_TEST_EXPECT_NO_ERR(resources->loadResource("blah", b));
        ANKI_TEST_EXPECT_EQ(b->getRefcount().load(), a->getRefcount().load());
        ANKI_TEST_EXPECT_EQ(a->getRefcount().load(), refcount + 1);

        ANKI_TEST_EXPECT_EQ(b.get(), a.get());

        // Again
        DummyResourcePtr c;
        ANKI_TEST_EXPECT_NO_ERR(resources->loadResource("blah", c));
        ANKI_TEST_EXPECT_EQ(a->getRefcount().load(), refcount + 2);

        // Load something else
        DummyResourcePtr d;
        ANKI_TEST_EXPECT_NO_ERR(resources->loadResource("blih", d));
        ANKI_TEST_EXPECT_EQ(a->getRefcount().load(), refcount + 2);
    }

    // Error
    {
        {
            DummyResourcePtr a;
            ANKI_TEST_EXPECT_EQ(resources->loadResource("error", a), ErrorCode::USER_DATA);
        }

        {
            DummyResourcePtr a;
            ANKI_TEST_EXPECT_EQ(resources->loadResource("error", a), ErrorCode::USER_DATA);
        }
    }

    // Delete
    alloc.deleteInstance(resources);
}