   Direct 3D Textures.
  Hello, I could do with a little advice.I have a texture which is set up like this: ->CreateTexture(width, height, 1, 0, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, &AnimationTexture, NULL);This part works fine but I need to lock this texture and write to it each frame. I have managed to patch together the following method:   D3DLOCKED_RECT LockedRect;if(AnimationTexture){   AnimationTexture->LockRect(0, &LockedRect, NULL, D3DLOCK_DISCARD);   RGBQUAD* Texture = static_cast<RGBQUAD *>(LockedRect.pBits);   for(int i = 0;i < width * height; x++)   { *Texture = whatever... Texture++;   }}However when I attempt to write to Texture (Texture = whatever...) I get the following error?..The instruction at "0x00402e3f" referenced memory at "0x01b99000". The memory could not be 'written'...Could anybody help?Thanks in advance.[edited by - mrmrcoleman on February 19, 2004 9:56:27 AM]
  yo i didn''t look at you prob much, but right off the bat you have an infinite loop x++. you''re writing off into no mans land.  ;  Sorry, that was a typo when entering the post. The loop actually looks like this. D3DLOCKED_RECT LockedRect;if(AnimationTexture){    AnimationTexture->LockRect(0, &LockedRect, NULL, D3DLOCK_DISCARD);    RGBQUAD* Texture = static_cast<RGBQUAD *>(LockedRect.pBits);    for(int i = 0;i < width * height; i++)    {  *Texture = whatever...  Texture++;    }}Any other probs??   [edited by - mrmrcoleman on February 19, 2004 9:24:35 AM];  You need to take the pitch in account. ;  What exactly are you writing at*Texture = whatever...  If you are doing something as simple as Texture->rgbBlue = colourB;Texture->rgbGreen = colourG;Texture->rgbRed = colourR;  or similar, then there shouldnt be a problem.  I tried that out myself and it worked fine (though you gotta be careful, if your texture is 256*256+ then thats 65536 or more times through that loop)Posting the code that is causeing the write error would be usefulSpree ;  SpreeTree - the exact code that I am using is this. D3DLOCKED_RECT LockedRect;if(AnimationTexture){    AnimationTexture->LockRect(0, &LockedRect, NULL, D3DLOCK_DISCARD);    RGBQUAD* Texture = static_cast<RGBQUAD *>(LockedRect.pBits);    for(int i = 0;i < width * height; x++)    {   Texture->rgbBlue = 0;   Texture->rgbGreen = 0;   Texture->rgbRed = 0;   Texture->rgbReserved = 0;   Texture++;    }}I am just writing zero''s at the moment so that I can get this section working..?Endurion - Do you know how I would alter this loop to take into account the pitch?Thanks ;  The actual code you have got there should work fine, so I am guessing that it is something outside of that code that is causing the problem.What are your values for width and height?How are u declaring AnimationTexture?Spree ;  Spree - the width and height are both 200, so it shouldnt be a loop out of bounds problem. The AnimationTexture is declared as:LPDIRECT3DTEXTURE9 AnimationTexture;Is this correct?Endurion - I did notice that the pitch was -800? I was not aware that textures could go ''backwards'' in memory, perhaps this is my problem?Thanks again guys. ;  Declaring your texture as an LPDIRECT3DTEXTURE9 should be fine (the compiler would throw up errors if i wasnt I guess).The only thing I can think of is when you specify a 200*200 texture, directX will actually create a 128*128 or 256*256 (Im not sure if DX rounds up or down).  If it does round down, then your actual texture surface will be 128*128 NOT 200*200, meaning you would go out of the bounds of the surface during your for loop, hence the write error.Call IDirect3DTexture9::GetLevelDesc(0, &surfaceDesc) where surfaceDesc is a D3DSURFACE_DESC.  surfaceDesc.Height and surfaceDesc.Width will then give you the exact size of the texture surafec created.Hope that helpsSpree ;  Hey!! I managed to get it all working. Endurion was correct when he said that I needed to take the stride into account!Only one problem though... It is fairly slow ;(I will be placing the whole thing in another post if you are interested because I dont know much about optimzation Thanks for your help SpreeTree and Endurion.Mark Coleman 