• Advertisement
Sign in to follow this  

Clipping

This topic is 4831 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

Could anyone point me to a tutorial about how to set up the DirectDraw clipper? I have some code, but it doesn't work. At all. What so ever. :) When I try it, the first image blt works, but from then on, the blts fail, so I assume that my clipper set up is whacked. Cheers SIgma | | | \ / posted in the wrong form: [edit] whoops: Posted this in Game programming, not DX. Damn. Ok, I'll post over there, but don't freak for a double post ok! :)

Share this post


Link to post
Share on other sites
Advertisement

LPDIRECTDRAWCLIPPER lpClipper=NULL;//DirectDrawClipper

result = lpDD->SetDisplayMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, 0, 0);
result = lpDD->CreateSurface( &ddsd, &lpFront, NULL );
result = lpDD->CreateSurface(&ddsd, &lpBack, NULL);

// Create a clipper (used in IDirectDrawSurface::Blt call)
result = lpDD->CreateClipper(0, &lpClipper, NULL);
if( result!=DD_OK )
{
return false;
}

result = lpClipper->SetHWnd(0, g_hWindow); //Client area of the window
if( result!=DD_OK )
{
return false;
}

result = lpBack->SetClipper(lpClipper);
if( result!=DD_OK )
{
return false;
}




NOTE: Don't try BltFast() with clipper - only Blt() or Flip()

PS: That's quick cut out of the working app - hope it helps

Share this post


Link to post
Share on other sites
Well, i got my code to work. I was attaching the clipper BEFORE I initialized the surface! Oopps...

You seem to be doing the attaching in WAY less code than me tho....I'll try your method. thx man

Share this post


Link to post
Share on other sites
Actually it doesn't work 100% If I move off the screen at a diagonal angle, it crashes. Any idea what could cause this?

Share this post


Link to post
Share on other sites
first question: is it fullscreen app or windowed?
second question: do you attach clipper to front surface or to the back one?

Share this post


Link to post
Share on other sites
You'd better post some code of yours, where are
- Clipper creation and attachment
- Blitting/Flipping Back to Front surfaces
- Viewport movement

Share this post


Link to post
Share on other sites
clipping creation


LPDIRECTDRAWCLIPPER MercuryEngine::DDCrClipper(LPDIRECTDRAWSURFACE7 lpdds,
int num_rects,
LPRECT clip_list)
{
int index; //loop var
LPDIRECTDRAWCLIPPER DDclipper; // our clipper
LPRGNDATA region_data; //contains clip list

if(FAILED(lpdd->CreateClipper(0,&DDclipper,NULL)))
return(NULL);

//creat clip list from data sent
region_data = (LPRGNDATA)malloc((sizeof(RGNDATAHEADER) + num_rects * sizeof(RECT)));

//copy rects into region data
memcpy(region_data->Buffer, clip_list , sizeof(RECT)*num_rects);

//set up fields
region_data->rdh.dwSize = sizeof(RGNDATAHEADER);
region_data->rdh.iType = RDH_RECTANGLES;
region_data->rdh.nCount = num_rects;
region_data->rdh.nRgnSize = num_rects * sizeof(RECT);

region_data->rdh.rcBound.left = 64000;
region_data->rdh.rcBound.top = 64000;
region_data->rdh.rcBound.right = -64000;
region_data->rdh.rcBound.bottom = -64000;

//find bounds for clipping
for(index = 0; index < num_rects; ++index)
{
//test if the next rectangle union with the current bound is alrger
if (clip_list[index].left < region_data->rdh.rcBound.left)
region_data->rdh.rcBound.left = clip_list[index].left;

if (clip_list[index].right < region_data->rdh.rcBound.right)
region_data->rdh.rcBound.right = clip_list[index].right;

if (clip_list[index].top < region_data->rdh.rcBound.top)
region_data->rdh.rcBound.top = clip_list[index].top;

if (clip_list[index].bottom < region_data->rdh.rcBound.bottom)
region_data->rdh.rcBound.bottom = clip_list[index].bottom;

}

//now we have computedthe bounding rectangle region and set up the data
//now we deal with the clipping list

if(FAILED(DDclipper->SetClipList(region_data,0)));
{
delete(region_data);
//return(NULL);
}

if(FAILED(lpdds->SetClipper(DDclipper)));
{
delete(region_data);
//return(NULL);
}

//and now we are done
delete(region_data);
return(DDclipper);
}



Attach then Blt source

RECT clipper_rect[1] = {0,0,799,599}; //fullscreen clipping area
...
//attach a clipper to the backsurface
DDclipper = GameEngine.DDCrClipper(DDback,1,clipper_rect);

BLT HERE

//draw the back ground image
if(FAILED(DDback->Blt(&dest_rect,DDback_sprite,&src_rect,DDBLT_WAIT, NULL)))
{
Application->MessageBoxA("Error","Boom",MB_OK);
Application->Terminate();
}


//draw the BOB sprite
if(FAILED(DDback->Blt(&bob.dest_rect,bob.DDBOB,&bob.src_rect ,(DDBLT_WAIT | DDBLT_KEYSRC),NULL)))
{
//nail any errors that get through
hr = DDback->Blt(&bob.dest_rect,bob.DDBOB,&bob.src_rect ,DDBLT_WAIT,NULL);
//display said error
Application->MessageBoxA(buf,"Boom",MB_OK);
Application->Terminate();
}

//flip
while(FAILED(DDprimary->Flip(NULL,DDFLIP_WAIT)));



Share this post


Link to post
Share on other sites
I had once tried to use windows regions with clipper, but that failed, too (strange, perhaps it's a windows bug), so I've clipped to hWnd and Back surface and

lpClipper->SetHWnd(0, g_hWindow);
lpBack->SetClipper(lpClipper);

works fine for me in fullscreen and windowed modes

EDIT: Damn that tags!

Share this post


Link to post
Share on other sites
I don't really follow what you mean. What are you suggesting I change?

Cheers

Chris

Share this post


Link to post
Share on other sites
Quote:
Original post by _Sigma
I don't really follow what you mean. What are you suggesting I change?


Nope, I mean I have no solution on 'regions' you are using

Share this post


Link to post
Share on other sites
Ah, gotcha. Could you possibly post your clipping code that doesn't use regions? :D Pretty please?

Share this post


Link to post
Share on other sites
Ok, here it is. I've skipped return code checks and logging.


// cut from ddInit function that is called after main game window creation

LPDIRECTDRAW4 lpDD=NULL;
LPDIRECTDRAWCLIPPER lpClipper=NULL;
LPDIRECTDRAWSURFACE4 lpFront=NULL;
LPDIRECTDRAWSURFACE4 lpBack=NULL;

// ...Initialization and surface creation here...

// Create a clipper (used in IDirectDrawSurface::Blt call)
lpDD->CreateClipper(0, &lpClipper, NULL);
// Note: g_hWindow is main game window descriptor
lpClipper->SetHWnd(0, g_hWindow); //Client area of the window
lpBack->SetClipper(lpClipper);




// main game loop
while (true)
{
if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) )
{
if( !GetMessage( &msg, NULL, 0, 0 ) )
return msg.wParam;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
if (g_bAppIsActive)
{
UpdateFrame();
g_bLostSurfaces = false;
}
else
{
FPSCounter->GetFPS();
g_bLostSurfaces = true;
WaitMessage();
}
}
}





// UpdateFrame() in main game loop
void UpdateFrame()
{
if (g_bLostSurfaces)
RestoreSurfaces();

// ...All sprites plotting to lpBack (backbuffer) goes here...

// Begin mouse scroll
{
if ( SCREEN_MOUSE_X == 0 ) MapScrollLeft();
if ( SCREEN_MOUSE_Y == 0 ) MapScrollUp();
if ( SCREEN_MOUSE_X == SCREEN_WIDTH-1 ) MapScrollRight();
if ( SCREEN_MOUSE_Y == SCREEN_HEIGHT-1 ) MapScrollDown();
}
// End mouse scroll

// Displaying the framerate
sprintf(FPSString, "FPS=%3.1f", FPSCounter->GetFPS());
NuclearFont->PrintTextXY(FPSString, 0, 20);

// Draw the mouse cursor
g_pCursorSystem->m_pSprites[g_iCursorIndex].DrawSprite(SCREEN_MOUSE_X, SCREEN_MOUSE_Y, g_pCursorSystem->m_pSprites[g_iCursorIndex].CurrentFrame());

lpFront->Blt(&g_rcScreen, lpBack, &g_rcScreen, DDBLT_DONOTWAIT | DDBLT_ASYNC, NULL);

// Clear keyboard state
diKeyboardClear();

// Poll keyboard
diKeyboardProcess();
// Done with keyboard

// Poll mouse
diMouseProcess();
// Done with mouse

// process scripts
if (sc) processScript(sc);
}



EDIT: All above code is part of "Nuclear Wastelands" 2D Engine, which is property of FSB Development Team. The above code is for educational purposes only.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement