Sign in to follow this  
ChocoboX

surface pitch equal to zero ????

Recommended Posts

I'm trying to write a really simple function that copies a surface onto another. blend_result->Lock(NULL, &srf_desc, DDLOCK_WAIT, NULL); bmp_stretch->Lock(NULL, &bmp_desc, DDLOCK_WAIT, NULL); final->Lock(NULL, &res_desc, DDLOCK_WAIT, NULL); srf_data = (WORD*)srf_desc.lpSurface; bmp_data = (WORD*)bmp_desc.lpSurface; res_data = (WORD*)res_desc.lpSurface; fprintf(logfile, "blend result pitch/2 = %d\n" "final pitch/2 = %d\n" "bmp_stretch pitch/2 = %d\n", (int)srf_desc.lPitch/2, (int)res_desc.lPitch/2, (int)bmp_desc.lPitch/2); log.txt : blend result pitch/2 = 0 final pitch/2 = 0 bmp_stretch pitch/2 = 0 blend_result and final are both filled with color, while bmp_stretch contains the pixels of a bitmap.All the surfaces are 1024 x 768 and so is my screen resolution.My screen bpp is set to 16-bit (565).blend_result and final are filled with color using this function : void clearSurface(LPDIRECTDRAWSURFACE7 surface, WORD color) { memset(&clr_color, 0, sizeof(DDBLTFX)); clr_color.dwSize = sizeof(DDBLTFX); clr_color.dwFillColor = color; surface->Blt(NULL,NULL,NULL,DDBLT_COLORFILL|DDBLT_WAIT, &clr_color); } Also, when I try : for(int y = 0; y < SCREEN_HEIGHT; y++) for(int x = 0; x < SCREEN_WIDTH; x++) srf_data[y * SCREEN_WIDTH + x] = res_data[y * SCREEN_WIDTH + x]; blend_result->Unlock(NULL); bmp_stretch->Unlock(NULL); final->Unlock(NULL); } The app hangs and comes back to the desktop.I know I have to use the surface pitch instead of my screen width for the pixel plotting formula, but since they are zero it's not of any use.Even if I use the pitch the app jumps back to the desktop again.Please help me out.

Share this post


Link to post
Share on other sites
Are you sure the Lock() calls are succeeding? Most of the DirectX functions return a HRESULT value to indicate success or failure. It is recommended to test the return values so you can track any error as soon as they occur. The can be tested using something like:


HRESULT hr = blend_result->Lock(NULL, &srf_desc, DDLOCK_WAIT, NULL);
if (FAILED(hr))
{
fprintf(logfile, "[Error] Failed to lock blend_result [0x%08x]!\n", hr);
return;
}

That could explain the crash and pitch being zero, if Lock() fails, you won't have a valid surface pointer so it will cause an access violation when trying to use it.

And yes, you must use the surface's pitch value and not the screen width or surface creation width since the graphic card can add padding to the scanlines thus making them larger what you would expect them to be.

Share this post


Link to post
Share on other sites
Indeed one of my surfaces isn't getting locked.I can't express my gratitude towards you.Thank you.I'll actually change the way I code.From now on not a single expression won't be error-checked.
But why isn't the lock succeeding? How can I fix this ?

Share this post


Link to post
Share on other sites
I've found the problem.When initliazing the surfaces I used the same DDSURFACEDESC three times.I didn't know that the Lock function reads data from the surface description when you pass it.It actually reads and writes to it.Thanks everybody, really. Thanks !!! This taught me a great lesson : always check for errors

Share this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this