addTextureFromSColor -Making an ITexture out of a SColor

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

addTextureFromSColor -Making an ITexture out of a SColor

Post by MasterGod »

Credit to arras for explaining to me how to do that.

Prototype:

Code: Select all

ITexture* addTextureFromSColor(IVideoDriver* driver, SColor& color, c8* name, dimension2di& size = dimension2di(512,512));
Implementation:

Code: Select all

ITexture* CResourceManager::addTextureFromSColor(IVideoDriver* driver, SColor& color, c8* name, dimension2di& size)
{
	ITexture* texture = driver->addTexture(size, name, ECF_A8R8G8B8);

	// Get pointer to texture data which in this case is basicly array of u32 values, 
	// a cast is needed since the method returns void pointer, however we already 
	// know the color format and thus what to cast to.
	u32* p = (u32*)texture->lock();

	// Now use that pointer to acces array
	for(s32 i=0; i < texture->getSize().Width * texture->getSize().Height; i++)
	{
		p[i] = color.color; // color member is the color in A8R8G8B8 Format 
	}

	// Now unlock texture
	texture->unlock();

	return texture;
}
Usage Example:

Code: Select all

ITexture* blackTexture = addTextureFromSColor(m_pVideoDriver ,SColor(255,0,0,0), "BlackTexture");
		m_pSceneManager->addSkyBoxSceneNode(
			blackTexture,
			blackTexture,
			blackTexture,
			blackTexture,
			blackTexture,
			blackTexture);
I'm using revision 1224 but should work with official problem-less.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Strong99
Admin
Posts: 687
Joined: Fri Mar 31, 2006 7:06 pm
Location: Netherlands
Contact:

Post by Strong99 »

Whats the advantage of this than not just making the background for example black or using black textures?
Halifax
Posts: 1424
Joined: Sun Apr 29, 2007 10:40 pm
Location: $9D95

Post by Halifax »

I have to agree this adds no clear functionality over just using a texture with the colors. Why, you may ask? Well for the simple fact that textures are independent of the program. Hypothetically, I don't want to recompile my program to make the background white instead of black if it takes a long time to compile, when I could just have the textures independent of the program and change them as I want with no time constraint.
TheQuestion = 2B || !2B
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

I used this because I need an ITexture with a specific color and I don't want to add a separate jpg image to do this.
Just thought of sharing it..
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Post Reply