Beispiel #1
0
TextureHandle TextureManager::get(Common::UString name) {
	Common::StackLock lock(_mutex);

	if (_bogusTextures.find(name) != _bogusTextures.end())
		return TextureHandle();

	TextureMap::iterator texture = _textures.find(name);
	if (texture == _textures.end()) {
		std::pair<TextureMap::iterator, bool> result;

		ManagedTexture *managedTexture = new ManagedTexture(Texture::create(name));

		if (managedTexture->texture->isDynamic())
			name = name + "#" + Common::generateIDRandomString();

		result = _textures.insert(std::make_pair(name, managedTexture));

		texture = result.first;
	}

	if (_recordNewTextures)
		_newTextureNames.push_back(name);

	return TextureHandle(texture);
}
Beispiel #2
0
TextureHandle TextureManager::add(Texture *texture, Common::UString name) {
	Common::StackLock lock(_mutex);

	if (_bogusTextures.find(name) != _bogusTextures.end()) {
		delete texture;
		return TextureHandle();
	}

	ManagedTexture *managedTexture = 0;
	TextureMap::iterator textureIterator = _textures.end();

	if (name.empty())
		name = Common::generateIDRandomString();

	try {
		managedTexture = new ManagedTexture(texture);

		std::pair<TextureMap::iterator, bool> result;

		result = _textures.insert(std::make_pair(name, managedTexture));
		if (!result.second)
			throw Common::Exception("Texture \"%s\" already exists", name.c_str());

		textureIterator = result.first;

	} catch (...) {
		delete managedTexture;
		throw;
	}

	if (_recordNewTextures)
		_newTextureNames.push_back(name);

	return TextureHandle(textureIterator);
}
Beispiel #3
0
// Gets a pointer to the texture specified.
// @param path The path of the texture.
TextureHandle TextureManager::Load(const std::string path)
{
  auto fileIter = textures.find(path);

  if (fileIter != textures.end())
  {
    return TextureHandle(fileIter->second.lock());
  }
  else
  {
    Texture* texture = new Texture(fileSystem, path);

    renderVisitor.AddTexture(texture);

    std::shared_ptr<Texture> texturePointer{ texture, [this](Texture* texture) {
      this->Release(texture->path);

      renderVisitor.RemoveTexture(texture);

      delete texture;
    } };

    textures.insert({ path, texturePointer });

    return TextureHandle(texturePointer);
  }
}
Beispiel #4
0
TextureHandle TextureManager::getIfExist(const Common::UString &name) {
	Common::StackLock lock(_mutex);

	if (_bogusTextures.find(name) != _bogusTextures.end())
		return TextureHandle();

	TextureMap::iterator texture = _textures.find(name);
	if (texture != _textures.end())
		return TextureHandle(texture);

	return TextureHandle();
}
Beispiel #5
0
TextureHandle TextureManager::get(const Common::UString &name) {
	Common::StackLock lock(_mutex);

	if (ResMan.hasResource(name, ::Aurora::kFileTypePLT)) {
		_plts.push_back(new ManagedPLT(name));

		_newPLTs.push_back(PLTHandle(--_plts.end()));

		return _newPLTs.back().getPLT().getTexture();
	}

	TextureMap::iterator texture = _textures.find(name);
	if (texture == _textures.end()) {
		std::pair<TextureMap::iterator, bool> result;

		ManagedTexture *t = new ManagedTexture(name);

		result = _textures.insert(std::make_pair(name, t));

		texture = result.first;

		texture->second->reloadable = true;
	}

	return TextureHandle(texture);
}
Beispiel #6
0
TextureHandle TextureManager::add(Texture *texture, Common::UString name) {
	Common::StackLock lock(_mutex);

	bool reloadable = true;
	if (name.empty()) {
		reloadable = false;

		name = Common::generateIDRandomString();
	}

	TextureMap::iterator text = _textures.find(name);
	if (text != _textures.end())
		throw Common::Exception("Texture \"%s\" already exists", name.c_str());

	std::pair<TextureMap::iterator, bool> result;

	ManagedTexture *t = new ManagedTexture(name, texture);

	result = _textures.insert(std::make_pair(name, t));

	text = result.first;

	text->second->reloadable = reloadable;

	return TextureHandle(text);
}
Beispiel #7
0
 static void release_all(ResourcePool<TextureDX9, TextureHandle, COUNT>& arr)
 {
     for( uint32 i=0; i<arr.capacity(); ++i )
     {
         TextureHandle h = TextureHandle(UntypedResourceHandle(i));
         SafeRelease( arr[h].surface );
         SafeRelease( arr[h].native2D );
         SafeRelease( arr[h].native3D );
         SafeRelease( arr[h].nativeCUBE);
     }
 }
TextureHandle Texture::textureHandle(Sampler * sampler) const
{
    return TextureHandle(this, sampler);
}
TextureHandle Texture::textureHandle() const
{
    return TextureHandle(this);
}
Beispiel #10
0
TextureHandle TextureManager::loadTexture(const std::string& name) {
	// Check if texture has already been loaded.
	TextureHandle existing = getLoadedResource(name);
	if (existing.valid())
		return existing;

	std::string texture_filename;
	Texture::FilterMode filter_mode = Texture::FilterMode::LINEAR;
	Texture::RepeatMode repeat_mode = Texture::RepeatMode::WRAP;
	int channels = 4;

	HSQUIRRELVM vm = ::hw::engine::script_engine->vm;
	sq_pushroottable(vm);
	loadVariablePath(vm, "Resources^TextureInfo^" + name);

	const SQChar* tex_filename_cstr;

	CSQ(getKey(vm, "filename"));
	sq_getstring(vm, -1, &tex_filename_cstr);
	sq_poptop(vm);
	texture_filename = tex_filename_cstr;

	if (SQ_SUCCEEDED(getKey(vm, "channels"))) {
		sq_getinteger(vm, -1, &channels);
		sq_poptop(vm);
	}

	if (SQ_SUCCEEDED(getKey(vm, "filter_mode"))) {
		SQInteger tmp;
		sq_getinteger(vm, -1, &tmp);
		sq_poptop(vm);

		filter_mode = Texture::FilterMode(tmp);
	}

	if (SQ_SUCCEEDED(getKey(vm, "repeat_mode"))) {
		SQInteger tmp;
		sq_getinteger(vm, -1, &tmp);
		sq_poptop(vm);

		repeat_mode = Texture::RepeatMode(tmp);
	}

	texture_filename = "data/" + texture_filename;

	if (texture_filename.empty()) {
		return TextureHandle();
	}

	gl::Texture new_tex;
	glGenTextures(1, &new_tex.name);

	glBindTexture(GL_TEXTURE_2D, new_tex.name);

	setTextureParameters(filter_mode, repeat_mode);

	assert(channels >= 1 && channels <= 4);
	static const GLint internal_formats[4] = {GL_RED, GL_RG, GL_RGB, GL_RGBA};
	GLint gl_format = internal_formats[channels-1];

	int width, height, comp;
	unsigned char* data = stbi_load(texture_filename.c_str(), &width, &height, &comp, channels);
	if (data == nullptr) {
		return TextureHandle();
	}

	glTexImage2D(GL_TEXTURE_2D, 0, gl_format, width, height, 0, gl_format, GL_UNSIGNED_BYTE, data);

	return constructResource(name, Texture(std::move(new_tex), name, width, height));
}