Advertisement

help!... "new" operator

Started by January 12, 2001 12:37 AM
2 comments, last by HyprLogik 24 years ago
Is the following code ok? I'm having trouble trying to figure out using my own classes. I keep getting errors in my program (of course, this isn't all the code, but this is the part that causes errors). They're not comilation/link errors, but rather debug/runtime errors. I'm using VC++ 6.0. By the way, this is using some of NeHe's code. I'm including the class definition below. If anyone can help me, thanks... hyprlogik@mail.utexas.edu
    

CTexture*    texture[3];

for( int i = 0; i < 3; i++ )					
{
    texture[i] = new CTexture[1];				
    texture[i]->filename = "data/wall.bmp";
    
    if( !(texture[i]->initTexture()) ) return false;
}

  
        

//class definition...


class CTexture
{
public:
	CTexture(){}	//		constructor


	~CTexture(){}

	char*  filename; //	file used as texture image

	bool   initTexture( void );  /*initializes the texture 
                                     by loading the file and 
                                setting aside available memory */

	void TexParameteri(   //sets filter/wrap parameters
			GLenum,
			GLenum,
			GLint );
	void  setCurrent( GLenum );   /*activates the texture  
        for use in subsequent texture routines*/
private:

	AUX_RGBImageRec* LoadBMP( void ); //loads a bitmap image	
	AUX_RGBImageRec* textureImage[1]; /*storage space for 
        the texture (holds actual data)*/

	int status; // status marker for initialization 
	GLuint textureRef[1]; // reference to texture
};

/*
_____________________________________________________________________________________

CTexture Members
_____________________________________________________________________________________

*/

void CTexture::TexParameteri( GLenum target, /*	sets filter/wrap 
                                             parameters*/
                              GLenum pname, GLint  param )
{ 
	glBindTexture(	target, textureRef[0] );/*select the 
                                 texture for use on target*/
	glTexParameteri( target, pname, param ); /*sets 
                         filter/wrap parameters*/
	glTexImage2D(target, 0, 3, /*set the texture properties*/
		     textureImage[0]->sizeX,
		     textureImage[0]->sizeY,
		     0, GL_RGB,
		     GL_UNSIGNED_BYTE,
		     textureImage[0]->data );
	gluBuild2DMipmaps( target, 3, //build the mipmaps
			textureImage[0]->sizeX,
			textureImage[0]->sizeY,
			GL_RGB,
			GL_UNSIGNED_BYTE,
			textureImage[0]->data );
	
	if( textureImage[0] )//		if texture exists..
	{
	    if( textureImage[0]->data )				//		if texture image exists..
		{
			free( textureImage[0]->data );		//		free the texture image memory
		}

		free( textureImage[0] );				//		free the image stucture
	}
}

void CTexture::setCurrent( GLenum target ) /*activates the 
                                           texture for use in 
                                           subsequent texture 
                                           routines*/
{
	glBindTexture( target, textureRef[0] ); /*activates the 
                                                 texture for use 
                                                 in subsequent 
                                               texture routines*/
}

bool CTexture::initTexture( void ) /*initializes the texture by 
                                    loading the file and setting 
                                    aside available memory*/
{
	if( !filename ) return false;

	memset( textureImage, 0,					//		set aside available memory
			sizeof( void* ) * 1 );
	
	if( !(textureImage[0] = LoadBMP()) ) /*load the bitmap 
                                             into memory*/
	{
		MessageBox( NULL, "BMP load failed.", "ERROR", 
                            MB_OK | MB_ICONEXCLAMATION );
		status = false;							//		set status to false


		return false;							//		if load failed, return false

	}

	glGenTextures( 1, &textureRef[0] );			//		generate the texture


	return true;								//		everything went ok

}

AUX_RGBImageRec* CTexture::LoadBMP( void ) //loads a bitmap image

{
	FILE* file = NULL;							//		temporary file to work with


	if( !filename )								//		make sure a filename was given

	{
		return NULL;							//		if not return NULL

	}

	file = fopen( filename, "r" );				//		check to see if the file exists


	if( file )									//		does the file exist?

	{
		fclose( file );							//		close the handle

		return auxDIBImageLoad( filename );		//		load the bitmap and return a pointer

	}

	return NULL;								//		if load failed return NULL

}

    
|-|A[k3R$Z R \/\/|-|AT |V|Ak3 t|-|3 \/\/0R|_D g0 R0|_||\|D!! Edited by - hyprlogik on January 12, 2001 1:47:30 AM
|-|A[k3R$Z R //|-|AT |V|Ak3 t|-|3 //0R|_D g0 R0|_|||D!!
OK, there''s a couple of things I''d like to say about your code :-)

Each element of array texture is a pointer to a CTexture object, so you only have to write
  texture[i] = new CTexture;  

Actually, I''ve noticed that you use the array notation for single data items, e.g
  AUX_RGBImageRec* textureImage[1];  

There''s no need to do this.

Also, be careful with your use of char pointers. It''s good practice to allocate memory for a string, e.g
  texture[i]->filename = new char[strlen("data/wall.bmp")+1];strcpy(texture[i]->filename,"data/wall.bmp");  

Then your object has it''s own personal copy of the string. But remember to delete this memory when the object is destroyed!

In your fopen call, you should set the translation mode as binary as the default is text mode, e.g
  file = fopen( filename, "rb" );	  

But, perhaps the auxDIBImageLoad function takes care of this anyway?

Hope this helps?

Andy.

Advertisement
Hi,



Is the following code ok? I''m having trouble trying to figure out using my own classes. I keep getting errors in my program (of course, this isn''t all the code, but this is the part that causes errors). They''re not comilation/link errors, but rather debug/runtime errors. I''m using VC++ 6.0. By the way, this is using some of NeHe''s code. I''m including the class definition below. If anyone can help me, thanks...

OK, i''ll try ...

CTexture * texture[3];

for( int i = 0; i < 3; i++ )
{
texture = new CTexture[1];
texture->filename = "data/wall.bmp";<br> <br> if( !(texture->initTexture()) ) return false;<br>}<br><br>I do not excactly understand what you do there but what''s about this: <br> <br>// A vector of (3) pointers to Texture class<br>CTexture * texture[3];<br><br>// you could use sizeof(texture) instead of 3 <br>for( int i = 0; i < 3; i++ ) <br>{<br> // hook a texture class instance to every pointer <br> // by calling a constructor and not CTexture[1].<br> texture = new CTexture();<br> <br> // could it be that you mean : "data\\wall.bmp"<br> texture->filename = "data/wall.bmp";<br> <br> if( !(texture->initTexture()) )return false;<br><br>}<br><br>It''s just an assumption since you did not give enough details what is going to happen with your codes during runtime. <br><br>Could you give us some more details ? <br><br>cu <br><br>Peter <br><br><br><br><br> </i>
HPH
Also don''t forget to check for NULL pointers after all dynamic allocations.

CTexture* pTexture = new CTexture;
if( NULL == pTexture )
{
//''new'' failed - out of memory
//Handle error here
}

You should also make a SetFileName method or have you init method take a file name as a parameter. That way you can allocate the correct amout of memory for your string. Similiar to the previous post. I personally would create you file name buffer off of the stack. Stack allocations don''t fail. Dynamic ones can. The only problem with stack allocations is that you have to pick a size.



This topic is closed to new replies.

Advertisement