Jump to content
  • Advertisement
Sign in to follow this  
TheGangster

Problem with Textues

This topic is 4790 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I've created a new class under my very newbie small engine and I wanted it to load the textures into an LPDIRECT3DTEXTURE9 array of textures. I've created the header file:
#ifndef _H_Textures_
#define _H_Textures_

#include "Engine.h"
#include <vector>

using namespace std;

struct FILES
{
	char Header[30];
	int Size;
	char FileName[128];
};

class Textures
{
public:
	Textures();
	~Textures();

	int m_VectorDimension;
	vector<LPDIRECT3DTEXTURE9> m_Textures;

	bool LoadTextureFile( LPDIRECT3DDEVICE9 pd3dDevice, const char *TextureFile );
	void SetTextureState( LPDIRECT3DDEVICE9 pd3dDevice, int Indice );
};

#endif

And the Source file:
#include "Textures.h"


//-----------------------------------------------------------------------------
// Name: Textures()
// Desc: Textures constructor
//-----------------------------------------------------------------------------
Textures::Textures()
{
}




//-----------------------------------------------------------------------------
// Name: ~Textures()
// Desc: Textures destructor
//-----------------------------------------------------------------------------
Textures::~Textures()
{
	for ( int i = 0; i <= m_VectorDimension; i++ )
	{
		SAFE_RELEASE( m_Textures );
	}
}




//-----------------------------------------------------------------------------
// Name: LoadTextureFile()
// Desc: Loads the list of textures file.
//-----------------------------------------------------------------------------
bool Textures::LoadTextureFile( LPDIRECT3DDEVICE9 pd3dDevice, const char *TextureFile )
{
	FILES Files;
	FILE *fp = fopen( TextureFile, "rb" );

	if ( fp == NULL )
	{
		return false;
	}

	fgets( Files.Header, 30, fp );
	char sizetmp[5];
	fgets( sizetmp, 5, fp );
	Files.Size = atoi( sizetmp );
	m_Textures.reserve( Files.Size );
	int j = 0;			// ERROR WITH THIS VAR
	while ( !feof( fp ) )
	{
		// Check the current path too?
		fgets( Files.FileName, 128, fp );
		if ( char *cr1 = strchr( Files.FileName, '\n' ) )
			*cr1 = '\0';
		if ( char *cr2 = strchr( Files.FileName, '\r' ) )
			*cr2 = '\0';
		if ( SUCCEEDED( D3DXCreateTextureFromFile( pd3dDevice, Files.FileName, &m_Textures[j] ) ) )
		{
			j++;
		}
		else
		{
			return false;
		}
	}

	m_VectorDimension = j;
	fclose( fp ) ;

	return true;
}




//-----------------------------------------------------------------------------
// Name: SetTextureState()
// Desc: 
//-----------------------------------------------------------------------------
void Textures::SetTextureState( LPDIRECT3DDEVICE9 pd3dDevice, int Indice )
{
    // Setup our texture. Using textures introduces the texture stage states,
    // which govern how textures get blended together (in the case of multiple
    // textures) and lighting information. In this case, we are modulating
    // (blending) our texture with the diffuse color of the vertices.
	pd3dDevice->SetTexture( 0, m_Textures[Indice] );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
    pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
}

And my problem is when m_VectorDimension is equalled to j, the m_VectorDimension gets stupid values (should be 5). I've tried to do loads of things none of them succeeded, so I am asking your help. Thanks in advance for your time.

Share this post


Link to post
Share on other sites
Advertisement
One major problem is that you call reserve() when you should be calling resize(). resize() sets the size of the vector, reserve() does something else.

There are potential problems, too. One problem is that if the number of files is greater than the number specified in the file, then the vector is going to overflow. To avoid this you should use push_back() to append elements to the vector (and then reserve() would be appropriate).

Also, your texture file is a text file not binary, so you should open it using "r" instead of "rb".

Also, you don't really need m_VectorDimension since size() returns the number of elements in the vector.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!