memcpy Intermediate Tutorial 6

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Sumit
Posts: 40
Joined: July 29th, 2012, 11:32 am

memcpy Intermediate Tutorial 6

Post by Sumit » October 27th, 2012, 2:14 pm

Hi chili

In tutorial 6, you typecasted backRect.pBits to BYTE in the memcpy function.
I am a little confused, can the destination type be different from source type.
I saw it worked fine in your tutorial. But it didn't work the same in my case (when I typecast backRect.pBits to BYTE). I have attached the image(showing my output). Please scroll down to see the image.
But typecasting backRect.pBits to D3DCOLOR woks fine.
BYTE takes 1 byte where as D3DCOLOR takes 4 bytes in the memory. So my question is, can we copy address of one type to the address of another type?

Thanks
Attachments
Image.png
(277.93 KiB) Downloaded 216 times
Sumit

Sumit
Posts: 40
Joined: July 29th, 2012, 11:32 am

Re: memcpy Intermediate Tutorial 6

Post by Sumit » October 28th, 2012, 8:05 am

Can anyone help me in this.

Thanks
Sumit

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: memcpy Intermediate Tutorial 6

Post by LuX » October 28th, 2012, 9:21 am

To sum it ( get it? sum-it : -P ), What is the question? Simplified, I don't like guessing games.
ʕ •ᴥ•ʔ

Sumit
Posts: 40
Joined: July 29th, 2012, 11:32 am

Re: memcpy Intermediate Tutorial 6

Post by Sumit » October 28th, 2012, 10:04 am

LuX wrote:To sum it ( get it? sum-it : -P ), What is the question? Simplified, I don't like guessing games.

Hi LuX,

In intermediate tutorial 6, chili copied the pSysBuffer data to backrect.pBits using memcpy :-
memcpy( &((BYTE*)backRect.pBits)[index * SCREEN_WIDTH] , &pSysBuffer[index * SCREEN_WIDTH] , sizeof(D3DCOLOR) * SCREEN_WIDTH );
where, he typecasted backRect.pBits to BYTE.

Shouldn't backRect.pBits be typecasted to D3DCOLOR, as pSysBuffer is of D3DCOLOR type, like:-
memcpy( &((D3DCOLOR*)backRect.pBits)[index * SCREEN_WIDTH] , &pSysBuffer[index * SCREEN_WIDTH] , sizeof(D3DCOLOR) * SCREEN_WIDTH );
Sumit

Sumit
Posts: 40
Joined: July 29th, 2012, 11:32 am

Re: memcpy Intermediate Tutorial 6

Post by Sumit » October 29th, 2012, 7:55 am

Hi all,

In intermediate lesson 6 Mr Chili taught to store the information in system buffer.
And then in the EndFrame function copy the information from system buffer to graphics buffer.
When I try the following code, something different happened.
void D3DGraphics::EndFrame()
{
HRESULT result;

result = pBackBuffer->LockRect( &backRect,NULL,NULL );
assert( !FAILED( result ) );

for( int index = 0 ; index < SCREEN_HEIGHT ; index++ )
{
memcpy( &((BYTE*)backRect.pBits)[index * SCREEN_WIDTH] , &pSysBuffer[index * SCREEN_WIDTH] , sizeof(D3DCOLOR) * SCREEN_WIDTH );
}
result = pBackBuffer->UnlockRect();
assert( !FAILED( result ) );

result = pDevice->Present( NULL,NULL,NULL,NULL );
assert( !FAILED( result ) );
}

I had already attached an image earlier in this topic, which shows my faulty output.
Please Scroll to the top to view that image.


But when I try the following code, everything executes perfectly.
void D3DGraphics::EndFrame()
{
HRESULT result;

result = pBackBuffer->LockRect( &backRect,NULL,NULL );
assert( !FAILED( result ) );

for( int index = 0 ; index < SCREEN_HEIGHT ; index++ )
{
memcpy( &((D3DCOLOR*)backRect.pBits)[index * SCREEN_WIDTH] , &pSysBuffer[index * SCREEN_WIDTH] , sizeof(D3DCOLOR) * SCREEN_WIDTH );
}
result = pBackBuffer->UnlockRect();
assert( !FAILED( result ) );

result = pDevice->Present( NULL,NULL,NULL,NULL );
assert( !FAILED( result ) );
}

The only difference in the above code and this code is memcpy function.

The above code worked fine in Mr Chili's tutorial.
Can anyone please explain, why the above code is not working in my case.

Thanks
Sumit

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: memcpy Intermediate Tutorial 6

Post by LuX » October 29th, 2012, 2:31 pm

I'm not completely sure, but I would say in memcpy you don't really need to specify the type in the "_Dst" and "_Src" parts, but since pBits is void* you need to give it some size for the function to work, when you are actually just showing it the index to be copied to. The amount is then specified in the Size where you show how much is being copied.

Dunno for sure what is causing it.
ʕ •ᴥ•ʔ

Sumit
Posts: 40
Joined: July 29th, 2012, 11:32 am

Re: memcpy Intermediate Tutorial 6

Post by Sumit » October 29th, 2012, 5:01 pm

LuX wrote:I'm not completely sure, but I would say in memcpy you don't really need to specify the type in the "_Dst" and "_Src" parts, but since pBits is void* you need to give it some size for the function to work, when you are actually just showing it the index to be copied to. The amount is then specified in the Size where you show how much is being copied.

Dunno for sure what is causing it.


Thank you LuX for looking into the matter.
Sumit

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: memcpy Intermediate Tutorial 6

Post by chili » October 31st, 2012, 2:51 pm

Your problem is here: &((BYTE*)backRect.pBits)[index * SCREEN_WIDTH]

You need to think about the meaning of backRect.pitch.
Chili

Post Reply