DirectDraw and double buffering help needed.

Started by
1 comment, last by UKSaiyan 16 years ago
Hey all, I know i'm using the outdated DirectDraw :) but i'm really hoping someone can help me with a problem i'm having... I'm trying to implement "double buffering" in 16 bit colour mode. (I can get it to work in 8bit colour mode with a palette and UCHAR data type but not in 16bit using the USHORT type.) my code: LPDIRECTDRAW7 lpdd7; DDSURFACEDESC2 ddsd; LPDIRECTDRAWSURFACE7 lpddsprimary; void GameInit() { DirectDrawCreateEx(NULL, (LPVOID *) &lpdd7,IID_IDirectDraw7, NULL); lpdd7->SetCooperativeLevel(hwnd1, DDSCL_NORMAL|DDSCL_ALLOWREBOOT); lpdd7->SetDisplayMode(640,480,16,0,0); memset((void *)&ddsd,0,sizeof(DDSURFACEDESC2)); ddsd.dwSize = sizeof(DDSURFACEDESC2); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; lpdd7->CreateSurface(&ddsd,&lpddsprimary,NULL); } void GameMain() { //DOUBLE BUFFERING WRITES: STARTS. USHORT * bufferSurface = new USHORT[640*480]; memset( (void *)bufferSurface, 0, 640*480); for (int i = 0; i<1000; i++) { int x = rand()%640; int y = rand()%480; bufferSurface[x+y*640] = RGB_16BIT565(rand()%256,rand()%256,rand()%256); } //DOUBLE BUFFERING WRITES: ENDS. lpddsprimary->Lock(NULL,&ddsd,DDLOCK_WAIT|DDLOCK_SURFACEMEMORYPTR,NULL); USHORT * primarySurface = (USHORT *) ddsd.lpSurface; if(ddsd.lPitch == 640){ memcpy((void *)primarySurface,(void *)bufferSurface,640*480); }else{ USHORT * srcPtr = bufferSurface; USHORT * desPtr = primarySurface; for (int y=0; y<480; y++) { memcpy( (void *)desPtr, (void *) srcPtr, 640); desPtr = desPtr + (ddsd.lPitch>>1); srcPtr = srcPtr + 640; } } lpddsprimary->Unlock(NULL); } ....... ........ But when I run it... Only half the screen is filled with pixels... I've been trying to do this for two days now... but i've gotten to the point where my palms have started sweating :P I can't find anything on google... and the closest I've gotten to an answer was.. http://www.gamedev.net/community/forums/topic.asp?topic_id=484173 Thanks for your time. UKSaiyan.
Advertisement
Quote:Original post by UKSaiyan
	lpddsprimary->Lock(NULL,&ddsd,DDLOCK_WAIT|DDLOCK_SURFACEMEMORYPTR,NULL);	USHORT * primarySurface = (USHORT *) ddsd.lpSurface;		if(ddsd.lPitch == 640){		memcpy((void *)primarySurface,(void *)bufferSurface,640*480);	}else{		USHORT * srcPtr = bufferSurface;		USHORT * desPtr = primarySurface;		for (int y=0; y<480; y++) {			memcpy( (void *)desPtr, (void *) srcPtr, 640);					desPtr = desPtr + (ddsd.lPitch>>1);			srcPtr = srcPtr + 640;		}	}	lpddsprimary->Unlock(NULL);}

Why are you dividing the pitch by two there? Also, that memcpy should be copying 640*bytes_per_pixel (2 for 16-bit) pixels, since the parameter is the number of bytes to copy, not pixels.
Likewise for the if(ddsd.lPitch == 640) part; it should check 640*bytes_per_pixel, and copy 640*480*bytes_per_pixel.

Also, just as a style thing, I'd get rid of the 640 and 480 and make them into constants or #define WIDTH and HEIGHT, just to make the code slightly easier to understand.
Thanks Alot for your reply Evil-Steve!... I followed your advice and I got it to work ! The coding is still a little dodgy though... but it'll do for now! :D

This topic is closed to new replies.

Advertisement