Jump to content
  • Advertisement

Archived

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

farhanx

DX7: Problem in page flipping :( what to do????

This topic is 5388 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi! i am new in Direct X and i am using DX7 but i have dx8 SDK, the problem i am facing is strange output , i am drawing some pixels on the secondary surface''s buffer and then flip it tothe primary surface but every time output comes half screen with pixels and half downward screen with some white and black lines ..... here is my code i am using one function of WinMain and everything is inside this function please tell me what iam doing wrong???? Thanks in advance // Set the display mode to 800x600 with 16 bits per pixel. if ( FAILED( lpDD->SetDisplayMode( 640, 480, 16, 0, 0 ) ) ) return FALSE; /////////////////////////////////////////////// ZeroMemory( &ddsd, sizeof( ddsd ) ); ddsd.dwSize=sizeof(ddsd); ddsd.dwFlags=DDSD_CAPS | DDSD_BACKBUFFERCOUNT; //////////////////////////////////////////////////////// ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; ddsd.dwBackBufferCount=1; ///////////////////////////////////////////////////////// lpDD->CreateSurface( &ddsd,&primaryS, NULL ) ; back.dwCaps = DDSCAPS_BACKBUFFER; primaryS->GetAttachedSurface(&back,&secondS); if ((video_buffer = (USHORT *)malloc(640*486))==NULL) return(0); int x=0,y=0; //Game loop while(TRUE) { USHORT *dest_ptr = NULL, *src_ptr= NULL; if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if (msg.message ==WM_QUIT) break; if (msg.message ==WM_CLOSE) break; //translate any accelerator keys if(KEY_UP(VK_ESCAPE)) //MessageBox(NULL,"HEY YOU","WHAT",MB_OK); break; TranslateMessage(&msg); //send the message to the window proc DispatchMessage(&msg); } // erase secondary back buffer memset(&ddsd,0,sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); secondS->Lock(NULL,&ddsd,DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL); video_buffer=(USHORT *)ddsd.lpSurface; if (ddsd.lPitch == 640) {memset(video_buffer,0,640*486);} else { // non-linear memory // make copy of video pointer dest_ptr = video_buffer; // clear out memory one line at a time for (int y=0; y<486; y++) { // clear nect line memset(dest_ptr,0,640); // advance pointer to next line dest_ptr+=ddsd.lPitch; } // end for y } // end else for(int a=0; a<100;a++){ x=rand()%640; y=rand()%480; video_buffer[x +(y*ddsd.lPitch>>1)]=(USHORT)RGB16BIT(rand()%255,rand()%255,rand()%255); } secondS->Unlock((tagRECT *)video_buffer); while(primaryS->Flip(NULL,DDFLIP_WAIT)!=DD_OK); Sleep(5); }//End while //ZeroMemory(&video_buffer,sizeof(video_buffer) ); secondS->Release(); secondS=NULL; primaryS->Release(); primaryS=NULL; lpDD->Release(); lpDD=NULL; return 0; }//End of Win Main

Share this post


Link to post
Share on other sites
Advertisement
The third paramter to memset is the number of bytes to fill and you''re setting it to 640, when your surface format contains 2 bytes per pixel.

Also, in a few places you''ve written code that assumes a 486 pixel surface height, when you''ve created a 480 pixel high surface.

Share this post


Link to post
Share on other sites
Thanks for reply , i did changed the mistake of height pixels into 480 and i also changed the way of memset function but still i am having the same output while now i am getting colourful lines instead of those black and white lines underhalf screen. Plz tell me what else could be the problem in my code?????????




if ( FAILED( lpDD->SetCooperativeLevel( hwnd,DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN |
DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT ) ) )
return FALSE;

// Set the display mode to 800x600 with 16 bits per pixel.
if ( FAILED( lpDD->SetDisplayMode( 640, 480, 16, 0, 0 ) ) )
return FALSE;



///////////////////////////////////////////////
ZeroMemory( &ddsd, sizeof( ddsd ) );
ddsd.dwSize=sizeof(ddsd);
ddsd.dwFlags=DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
////////////////////////////////////////////////////////

ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP |
DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount=1;
/////////////////////////////////////////////////////////

lpDD->CreateSurface( &ddsd,&primaryS, NULL ) ;

back.dwCaps = DDSCAPS_BACKBUFFER;
primaryS->GetAttachedSurface(&back,&secondS);

if ((video_buffer = (USHORT *)malloc(640*480))==NULL)
return(0);


int x=0,y=0;


while(TRUE)
{
USHORT *dest_ptr = NULL, *src_ptr= NULL;

if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{

if (msg.message ==WM_QUIT)
break;

if (msg.message ==WM_CLOSE)
break;
//translate any accelerator keys
if(KEY_UP(VK_ESCAPE))
//MessageBox(NULL,"HEY YOU","WHAT",MB_OK);
break;

TranslateMessage(&msg);
//send the message to the window proc
DispatchMessage(&msg);

}



// erase secondary back buffer
memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);



secondS->Lock(NULL,&ddsd,DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);
video_buffer=(USHORT *)ddsd.lpSurface;



if (ddsd.lPitch == 640)
{memset(video_buffer,0,sizeof(video_buffer));}
else
{
// non-linear memory

// make copy of video pointer
dest_ptr = video_buffer;

// clear out memory one line at a time
for (int y=0; y<480; y++)
{
// clear nect line
memset(dest_ptr,0,sizeof(dest_ptr));

// advance pointer to next line
dest_ptr+=ddsd.lPitch;

} // end for y

} // end else

for(int a=0; a<100;a++){
x=rand()%640;
y=rand()%480;
video_buffer[x +(y*ddsd.lPitch>>1)]=(USHORT)RGB16BIT(rand()%255,rand()%255,rand()%255);
}

secondS->Unlock((tagRECT *)video_buffer);

while(primaryS->Flip(NULL,DDFLIP_WAIT)!=DD_OK);

Sleep(5);


}//End while

Share this post


Link to post
Share on other sites

//globals

UCHAR *surface = NULL; //pointer to data of DDsurface

LPDIRECTDRAWSURFACE7 DDsurface = NULL; //a pointer to a Direct Draw Surface

int lpitch; //hard to explain :)


BOOL Lock(LPDIRECTDRAWSURFACE7 DDsurface)
{
DDSURFACEDESC2 ddsd;

Unlock(DDsurface);

if (DDsurface == NULL)
return FALSE;

memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);
DDsurface->Lock(NULL,&ddsd,DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,NULL);

lpitch = ddsd.lPitch;
surface = (UCHAR *)ddsd.lpSurface;

return TRUE;
}

BOOL Unlock()
{
if ((DDSurface==NULL)||(surface==NULL))
return FALSE;
m_pDDSurface->Unlock(NULL);
surface = NULL;
return TRUE;
}

void PlotPixel(int x,int y,DWORD color)
{
((WORD *)surface) [x+y*(lpitch>>1)] = (WORD)color;
}






in the source above, have DDsurface point to your DirectDrawSurface
have surface be a null pointer to unsighed char
1. call Unlock() this will set surface to point at the data area of your directdrawsurface pointed to by DDsurface
this will also place a value in lpitch used in plotting pixels
2. call PlotPixel(x,y,color) this will plot a pixel on DDsurface at x,y with color
3. when all plotting is done, call Unlock()
will unlock the directdrawsurface and set surface=NULL

btw This is set for working is 16 bpp (bits per pixel) mode.
I dont know how it would work in 24, 32, etc... might have to change (lpitch>>1) in the plotting function to use in different modes.
hope this helps

Share this post


Link to post
Share on other sites
You are doing memset with sizeof(video_buffer) and sizeof(dest_ptr). The size of those is 4. They are pointers. sizeof doesn''t know how much space it is actually pointing to. Instead of sizeof, you need to specify the number of bytes to set.

Also, you are calling Unlock() with your video_buffer as a parameter. That''s definitely not right. You should be passing NULL to Unlock().

Share this post


Link to post
Share on other sites
Thanks alot all of you guys this is the best place for having great programmers like you all, Now my code is working the way i want it. The problem was really inside the non liner algorithm
but i changed the memset value as
memset(dest_ptr,0,640*16) and now its working perfectly with smooth animation while when i put memset(dest_ptr,0,640*480) then it comes so extremely slow and bad animation.


if (ddsd.lPitch == 640)
{memset(video_buffer,0,640*480);}
else
{
// non-linear memory
// make copy of video pointer
dest_ptr = video_buffer;
// clear out memory one line at a time
for (int y=0; y<480; y++)
{
// clear nect line
memset(dest_ptr,0,640*16);
// advance pointer to next line
dest_ptr+=ddsd.lPitch;

} // end for y

} // end else

///////////////////////////////////////////



Things has been changed but the just the way it is....

Share this post


Link to post
Share on other sites
Thanks alot all of you guys this is the best place for having great programmers like you all, Now my code is working the way i want it. The problem was really inside the non liner algorithm
but i changed the memset value as
memset(dest_ptr,0,640*16) and now its working perfectly with smooth animation while when i put memset(dest_ptr,0,640*480) then it comes so extremely slow and bad animation.


if (ddsd.lPitch == 640)
{memset(video_buffer,0,640*480);}
else
{
// non-linear memory
// make copy of video pointer
dest_ptr = video_buffer;
// clear out memory one line at a time
for (int y=0; y<480; y++)
{
// clear nect line
memset(dest_ptr,0,640*16);
// advance pointer to next line
dest_ptr+=ddsd.lPitch;

} // end for y

} // end else

///////////////////////////////////////////



Things has been changed but the just the way it is....

Share this post


Link to post
Share on other sites
Thanks alot all of you guys this is the best place for having great programmers like you all, Now my code is working the way i want it. The problem was really inside the non liner algorithm
but i changed the memset value as
memset(dest_ptr,0,640*16) and now its working perfectly with smooth animation while when i put memset(dest_ptr,0,640*480) then it comes so extremely slow and bad animation.


if (ddsd.lPitch == 640)
{memset(video_buffer,0,640*480);}
else
{
// non-linear memory
// make copy of video pointer
dest_ptr = video_buffer;
// clear out memory one line at a time
for (int y=0; y<480; y++)
{
// clear nect line
memset(dest_ptr,0,640*16);
// advance pointer to next line
dest_ptr+=ddsd.lPitch;

} // end for y

} // end else

///////////////////////////////////////////



Things has been changed but the just the way it is....

Share this post


Link to post
Share on other sites
Thanks alot all of you guys this is the best place for having great programmers like you all, Now my code is working the way i want it. The problem was really inside the non liner algorithm
but i changed the memset value as
memset(dest_ptr,0,640*16) and now its working perfectly with smooth animation while when i put memset(dest_ptr,0,640*480) then it comes so extremely slow and bad animation.


if (ddsd.lPitch == 640)
{memset(video_buffer,0,640*480);}
else
{
// non-linear memory
// make copy of video pointer
dest_ptr = video_buffer;
// clear out memory one line at a time
for (int y=0; y<480; y++)
{
// clear nect line
memset(dest_ptr,0,640*16);
// advance pointer to next line
dest_ptr+=ddsd.lPitch;

} // end for y

} // end else

///////////////////////////////////////////



Things has been changed but the just the way it is....

Share this post


Link to post
Share on other sites
Thanks alot all of you guys this is the best place for having great programmers like you all, Now my code is working the way i want it. The problem was really inside the non liner algorithm
but i changed the memset value as
memset(dest_ptr,0,640*16) and now its working perfectly with smooth animation while when i put memset(dest_ptr,0,640*480) then it comes so extremely slow and bad animation.


if (ddsd.lPitch == 640)
{memset(video_buffer,0,640*480);}
else
{
// non-linear memory
// make copy of video pointer
dest_ptr = video_buffer;
// clear out memory one line at a time
for (int y=0; y<480; y++)
{
// clear nect line
memset(dest_ptr,0,640*16);
// advance pointer to next line
dest_ptr+=ddsd.lPitch;

} // end for y

} // end else

///////////////////////////////////////////



Things has been changed but the just the way it is....

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!