# In game image memset function not working...

## Recommended Posts

Posted (edited)

In one of my games, I have a minimap. the question that has been buzzing around in my mind for a while is "Why wont this function work correctly?" I did some experimenting and I narrowed my selection down to a memset function. Here are the lines of code.

...

D3DLOCKED_RECT sRect;
sightTexture->LockRect(0, &sRect, NULL, NULL);
BYTE *bytes = (BYTE*)sRect.pBits;
memset(bytes, 0, sRect.Pitch*sRect.Pitch);

float intensity = 1.3f;
D3DXVECTOR2 center = D3DXVECTOR2(32.0f, 32.0f);

for(int y=0;y<64;y++)
for(int x=0;x<64;x++)
{
float d = D3DXVec2Length(&(center - D3DXVECTOR2((float)x, (float)y)));
int value = (int)(((32.0f - d) / 32.0f) * 255.0f * intensity);
if(value < 0)value = 0;
if(value > 255)value = 255;
bytes[x + y * sRect.Pitch] = value;
}
sightTexture->UnlockRect(0);

...

Where is the code going wrong?

I found this technique on an old programming website I used to use...

Without the memset function, only 1/3 of my minimap gets rendered...

Edited by rjhwinner03
Uncommented the code that I needed to.

##### Share on other sites

I don't see enough code here. How many bytes per pixel are you using?

##### Share on other sites

Well for a start, you don't appear to be locking the rect (it is commented out), so sRect contains garbage. That and you are memsetting with the pitch * pitch, which sounds unlikely?

##### Share on other sites
Posted (edited)

It still passes an exception if I uncomment the lock function...

Edited by rjhwinner03

##### Share on other sites
3 minutes ago, rjhwinner03 said:

It still passes an exception if I uncomment the lock function...

Well, yes, that will probably be using pitch * pitch as the size of the surface, which is incorrect, so you are memsetting off the edge of the surface and causing an exception.

Pitch is the size of an x line in bytes. The correct number of bytes to memset (if you need to do this) would presumably be the pitch * the height of the texture.

##### Share on other sites

The code runs, but I still get the issue that I would get if I commented out the memset function...

##### Share on other sites

I'm not too up to speed on D3D at the moment, but:

- Are you sure the texture is lockable?

- Have you tried checking the return value of LockRect()?

- Have you fixed the previously mentioned 'pitch * pitch' issue? (That might be working circumstantially, depending on the texture size and format, but it doesn't seem conceptually correct.)

- Are you sure the texture format is single-channel 8-bit?

As for the minimap only being partially rendered, I don't know that there's enough information here to diagnose that. It looks though like the texture in question is some sort of circular visibility region, so I can imagine that if the code you posted is incorrect, it might result in something like the behavior you describe.

##### Share on other sites

All of this stuff worked like a charm in Windows XP through Windows 8... I had no errors... Windows 10 is and has been a big obstacle for me in the past and the present...

##### Share on other sites

It seems to me that the same questions remain open (texture lockable? LockRect() return value? pitch * pitch? texture format?).

Regarding the change in development environment, although the problem could be related to Windows 10, I wouldn't assume that it is. I'm not saying this is necessarily the case here, but sometimes changes in environment can reveal problems that simply went unnoticed previously due to accident or circumstance.

Anyway, if you haven't solved it yet, maybe it would help take it a question at a time. So:

Have you fixed the 'pitch * pitch' issue? If not, is there any particular reason you're computing the size of the data that way?

##### Share on other sites

Attach the debugger and put a breakpoint at the line with the memset. What are the contents of sRect?

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 14
• 9
• 10
• 12
• 17
• ### Forum Statistics

• Total Topics
632905
• Total Posts
3009152
• ### Who's Online (See full list)

There are no registered users currently online

×