Archived

This topic is now archived and is closed to further replies.

johnnyBravo

DX9:vc++6.0:Getting Access violation, with creating sprites.

Recommended Posts

im trying to use the sprite functions that come with directx 9, to create a sprite. But I keep getting access violation errors, and i can't work it out. Heres the quote that should be relevant. declarations
quote:
#include <d3d9.h> #include <windows.h> #include <d3dx9.h> #include <d3dx9core.h> #include <mmsystem.h> const int ScreenWidth=400; const int ScreenHeight=400; LPDIRECT3D9 g_pD3D = NULL; LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; LPDIRECT3DVERTEXBUFFER9 g_pVB2D = NULL; LPDIRECT3DVERTEXBUFFER9 g_pVB3D = NULL; LPD3DXFONT g_pd3dxFont = NULL; LPDIRECT3DTEXTURE9 g_pTexture[2]={NULL}; LPD3DXSPRITE g_pDonutSprite = NULL; struct CUSTOMVERTEX2D { float x, y, z, rhw; DWORD color; float tu, tv; }; float g_fSpinX = 0.0f; float g_fSpinY = 0.0f; #define D3DFVF_CUSTOMVERTEX2D (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1) struct CUSTOMVERTEX3D { FLOAT x, y, z; // The untransformed, 3D position for the vertex DWORD color; // The vertex color float tu, tv; }; // Our custom FVF, which describes our custom vertex structure #define D3DFVF_CUSTOMVERTEX3D (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
part of D3D init
quote:
g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); //Textures D3DXCreateTextureFromFile( g_pd3dDevice, "woodfloor.bmp", &g_pTexture[0] ); D3DXCreateTextureFromFile( g_pd3dDevice, "woodfloor2.bmp", &g_pTexture[1] ); D3DXIMAGE_INFO d3dxImageInfo; D3DXCreateTextureFromFileEx( g_pd3dDevice, "donut.bmp", 320, // I had to set width manually. D3DPOOL_DEFAULT works for textures but causes problems for D3DXSPRITE. 384, // I had to set height manually. D3DPOOL_DEFAULT works for textures but causes problems for D3DXSPRITE. 1, // Don't create mip-maps when you plan on using D3DXSPRITE. It throws off the pixel math for sprite animation. D3DPOOL_DEFAULT, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f), &d3dxImageInfo, NULL, &g_pTexture[2] );
line that causes all the errors, doesn't matter where I call it, it'll cause the access violation.
quote:
D3DXCreateSprite( g_pd3dDevice, &g_pDonutSprite );
I havent even started trying to render the sprite, as I can't even get the creation of it done. Oh also this is the error message
quote:
First-chance exception in Dx9RefCPP.exe (D3D9.DLL): 0xC0000005: Access Violation.

Share this post


Link to post
Share on other sites
You only allocated space for 2 texture pointers with indexes 0 and 1. Then, you attempt to store your donut image into index 2. Thus stomping on memory (specifically the memory occupied by your g_pDonutSprite pointer.

If you want to store 3 textures, perhaps you should make your array large enough to hold 3 textures?

Share this post


Link to post
Share on other sites