Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with Textues

Started by TheGangster Sep 4, 2005 at 3:18 PM 1 replies 950+ views
Original Post
TheGangster
TheGangster
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.
JohnBolton
JohnBolton
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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.