Archived

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

Stibmit

Memcpy is givin' me troubles.. as always.

Recommended Posts

Alright, so far I''ve gotten a lot complete on my program, however I''ve come across yet another problem using memcpy. I hate that function with an undying passion.... but anyways, this is my problem: After intializing DirectDraw, loading a bitmap to memory and creating an offscreen surface to hold the bitmap, I''ve tried to copy the data from the bitmap onto the offscreen surface using memcpy. Here''s the code:
  
hr=lpddbackground->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL);
	DdErrorCheck(hr, "lpddbackground->Lock() FAILED");
	if(FAILED(hr)) return(false);

	Error("lpddbackground->Lock - SUCCESS");

	UCHAR *imagebuffer = (UCHAR *)ddsd.lpSurface;
	if(!(memcpy((void *)imagebuffer, (void *)bitmap.buffer, RES_WIDTH*RES_HEIGHT)))
	{
		Error("memcpy FAILED");
		return(false);
	} //end if

	
	Error("memcpy - SUCCESS");

	hr=lpddbackground->Unlock(NULL);
	DdErrorCheck(hr, "lpddbackground->Unlock() FAILED");
	if(FAILED(hr)) return(false);
  
After this piece of code is executed, the program crashes and it returns to windows. What the hell. I know it''s this piece of code because I''ve made message boxes popup after each function (for debug purposes, of course), and I get all of the previous functions working properly previous to this little doozy- I don''t get a success message after this code, instead, it crashes my program. All I need is a way to get the data from a bitmap (already loaded) onto an offscreen surface. Can anyone point me in the right direction? This is frustrating! I''ve been at it all day... bah!!!

Share this post


Link to post
Share on other sites
Not sure but shouldnt it be:-
memcpy((void *)imagebuffer, (void *)bitmap.buffer, RES_WIDTH*RES_HEIGHT*BYTES_PER_PIXEL)
?

Share this post


Link to post
Share on other sites
returns the pointer to the destination memory doesnt it? well it returns something I think. Though yet again not sure.

Share this post


Link to post
Share on other sites
Like Seaman said mamcpy returns adress to original destination memory and there is no way for checking did the function fail or not.

Share this post


Link to post
Share on other sites
I tried RES_WIDTH*RES_HEIGHT_*RES_BPP but that didn''t make any sort of difference at all. I took out the if(!(memcpy.. since, as you said, it doesn''t really have a return value that I can assess for errors. Ugh. Any other help?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Your not taking into acount the pitchs.
You should:

for height
memcopy Surfacept,Bitmappt,width
Surfacept+=sfPitch
Bitmappt+=bmPitch

This should prevent wierd memory access

Share this post


Link to post
Share on other sites
....................what?


See, this is why Andre is a crappy author. I've been using the DirectX SDK to learn all this DirectDraw programming and Andre's book for exercises and for certain functions. If he would have written his damn book right the first time, I wouldn't have this urge to kick his ass.

Edited by - Stibmit on July 31, 2001 10:09:27 PM

Share this post


Link to post
Share on other sites