• Advertisement
Sign in to follow this  

This topic is 4219 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

im trying to use d3d9 to create a 2d game. the following code is to try and set up sprite animation. i have cut out some of the code, hopefully havnt cut out anything important, when compiling i get the error Unhandled exception at 0x4fe5798b in startingD3D9.exe: 0xC0000005: Access violation writing location 0xababab5b. i roughly understand the problem being with pointers and memory but i dont know how to fix the issue. i know this is a common issue and i looked on msdn and it used the example int foo; int * ip = NULL; foo = *ip; //crash! i just dont know how to apply it. thanks mark

//loop through images
	int nImage = 0 ;

.....
// some code removed which wasnt relevant
.....
			//create texture
			m_pDestinationDevice->CreateTexture ( nTextureWidth , nTextureHeight , 1 , 0 , Format , D3DPOOL_DEFAULT , &m_pTextures [ nImage ], NULL ) ;

			//grab texture surface
			IDirect3DSurface9* pTextureSurface ;
			m_pTextures [ nImage ]->GetSurfaceLevel ( 0 , &pTextureSurface ) ;

			//source and destination
			POINT ptDst ;
			RECT rcSrc ;
			ptDst.x = 0 ;
			ptDst.y = 0 ;
			SetRect ( &rcSrc , 0 , 0 , nTextureWidth , nTextureHeight ) ;

			//copy rectangle
			m_pDestinationDevice->UpdateSurface ( pSurface , &rcSrc , pTextureSurface , &ptDst ) ;

			//release texture surface
			pTextureSurface->Release ( ) ;

			//offset rectangle
			OffsetRect ( &rcImage , -ptCenter.x , -ptCenter.y ) ;  //breaks here due to ptCenter not beign defined

			//set up vertices
			m_Vertices [ nImage * 4 ].x = rcImage.left ;
			m_Vertices [ nImage * 4 ].y = rcImage.top ;
			m_Vertices [ nImage * 4 ].z = 0.0f ;
			m_Vertices [ nImage * 4 ].rhw = 1.0f ;
			m_Vertices [ nImage * 4 ].diffuse = D3DCOLOR_XRGB ( 255 , 255 , 255 ) ;
			m_Vertices [ nImage * 4 ].u = 0.0f ;
			m_Vertices [ nImage * 4 ].v = 0.0f ;
			m_Vertices [ nImage * 4 + 1 ].x = rcImage.right ;
			m_Vertices [ nImage * 4 + 1 ].y = rcImage.top ;
			m_Vertices [ nImage * 4 + 1 ].z = 0.0f ;
			m_Vertices [ nImage * 4 + 1 ].rhw = 1.0f ;
			m_Vertices [ nImage * 4 + 1 ].diffuse = D3DCOLOR_XRGB ( 255 , 255 , 255 ) ;
			m_Vertices [ nImage * 4 + 1 ].u = ( float ) nSpriteWidth / ( float ) nTextureWidth ;
			m_Vertices [ nImage * 4 + 1 ].v = 0.0f ;
			m_Vertices [ nImage * 4 + 2 ].x = rcImage.left ;
			m_Vertices [ nImage * 4 + 2 ].y = rcImage.bottom ;
			m_Vertices [ nImage * 4 + 2 ].z = 0.0f ;
			m_Vertices [ nImage * 4 + 2 ].rhw = 1.0f ;
			m_Vertices [ nImage * 4 + 2 ].diffuse = D3DCOLOR_XRGB ( 255 , 255 , 255 ) ;
			m_Vertices [ nImage * 4 + 2 ].u = 0.0f ;
			m_Vertices [ nImage * 4 + 2 ].v = ( float ) nSpriteHeight / ( float ) nTextureHeight ;
			m_Vertices [ nImage * 4 + 3 ].x = rcImage.right ;
			m_Vertices [ nImage * 4 + 3 ].y = rcImage.bottom ;
			m_Vertices [ nImage * 4 + 3 ].z = 0.0f ;
			m_Vertices [ nImage * 4 + 3 ].rhw = 1.0f ;
			m_Vertices [ nImage * 4 + 3 ].diffuse = D3DCOLOR_XRGB ( 255 , 255 , 255 ) ;
			m_Vertices [ nImage * 4 + 3 ].u = ( float ) nSpriteWidth / ( float ) nTextureWidth ;
			m_Vertices [ nImage * 4 + 3 ].v = ( float ) nSpriteHeight / ( float ) nTextureHeight ;

			//increment image number
			nImage ++ ;
.....
// some code removed which wasnt relevant
.....
//retrieve information
IDirect3DTexture9* CSpriteSet::GetTexture ( int nImage ) 
{
	//return the texture
	return ( m_pTextures [ nImage ] ) ;
}

SpriteSetVertex* CSpriteSet::GetVertex ( int nImage , int nVertex ) 
{
	//return pointer to the vertex
	return ( &m_Vertices [ nImage * 4 + nVertex ] ) ;
}

//render
void CSpriteSet::PutSprite ( int nImage , float x , float y , float angle , float scalex , float scaley ) 
{
.....
// some code removed which wasnt relevant
.....
	//set the texture
	m_pDestinationDevice->SetTexture ( 0 , m_pTextures[nImage] ) ;

	//render
	m_pDestinationDevice->DrawPrimitiveUP ( D3DPT_TRIANGLESTRIP , 2 , &vert , sizeof ( SpriteSetVertex ) ) ;



Share this post


Link to post
Share on other sites
Advertisement
your example shows how deferencing a null pointer is bad.

basically, you are asking for memory that does not belong to you. The operating system did not give you permission to read/write at that location (in this case it is 0 ) and it is terminating the application because of it.

Frankly, if you don't understand pointers, or why that example is relevant, you have much further to go before making your own game, much less doing so with DirectX 9.

Do a google on pointers. Try downloading "Pointer Fun With Binky" and watch that. Thats how I learned.

Share this post


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

  • Advertisement