📄 t3dlib1.cpp
字号:
lpddpal->Release();
// release the secondary surface
if (lpddsback)
lpddsback->Release();
// release the primary surface
if (lpddsprimary)
lpddsprimary->Release();
// finally, the main dd object
if (lpdd)
lpdd->Release();
// return success
return(1);
} // end DDraw_Shutdown
///////////////////////////////////////////////////////////
LPDIRECTDRAWCLIPPER DDraw_Attach_Clipper(LPDIRECTDRAWSURFACE7 lpdds,
int num_rects,
LPRECT clip_list)
{
// this function creates a clipper from the sent clip list and attaches
// it to the sent surface
int index; // looping var
LPDIRECTDRAWCLIPPER lpddclipper; // pointer to the newly created dd clipper
LPRGNDATA region_data; // pointer to the region data that contains
// the header and clip list
// first create the direct draw clipper
if (FAILED(lpdd->CreateClipper(0,&lpddclipper,NULL)))
return(NULL);
// now create the clip list from the sent data
// first allocate memory for region data
region_data = (LPRGNDATA)malloc(sizeof(RGNDATAHEADER)+num_rects*sizeof(RECT));
// now copy the rects into region data
memcpy(region_data->Buffer, clip_list, sizeof(RECT)*num_rects);
// set up fields of header
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 of all clipping regions
for (index=0; index<num_rects; index++)
{
// test if the next rectangle unioned with the current bound is larger
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;
} // end for index
// now we have computed the bounding rectangle region and set up the data
// now let's set the clipping list
if (FAILED(lpddclipper->SetClipList(region_data, 0)))
{
// release memory and return error
free(region_data);
return(NULL);
} // end if
// now attach the clipper to the surface
if (FAILED(lpdds->SetClipper(lpddclipper)))
{
// release memory and return error
free(region_data);
return(NULL);
} // end if
// all is well, so release memory and send back the pointer to the new clipper
free(region_data);
return(lpddclipper);
} // end DDraw_Attach_Clipper
///////////////////////////////////////////////////////////
LPDIRECTDRAWSURFACE7 DDraw_Create_Surface(int width,
int height,
int mem_flags,
USHORT color_key_value)
{
// this function creates an offscreen plain surface
DDSURFACEDESC2 ddsd; // working description
LPDIRECTDRAWSURFACE7 lpdds; // temporary surface
// set to access caps, width, and height
memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
// set dimensions of the new bitmap surface
ddsd.dwWidth = width;
ddsd.dwHeight = height;
// set surface to offscreen plain
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | mem_flags;
// create the surface
if (FAILED(lpdd->CreateSurface(&ddsd,&lpdds,NULL)))
return(NULL);
// set color key to default color 000
// note that if this is a 8bit bob then palette index 0 will be
// transparent by default
// note that if this is a 16bit bob then RGB value 000 will be
// transparent
DDCOLORKEY color_key; // used to set color key
color_key.dwColorSpaceLowValue = color_key_value;
color_key.dwColorSpaceHighValue = color_key_value;
// now set the color key for source blitting
lpdds->SetColorKey(DDCKEY_SRCBLT, &color_key);
// return surface
return(lpdds);
} // end DDraw_Create_Surface
///////////////////////////////////////////////////////////
int DDraw_Flip(void)
{
// this function flip the primary surface with the secondary surface
// test if either of the buffers are locked
if (primary_buffer || back_buffer)
return(0);
// flip pages
if (!screen_windowed)
while(FAILED(lpddsprimary->Flip(NULL, DDFLIP_WAIT)));
else
{
RECT dest_rect; // used to compute destination rectangle
// get the window's rectangle in screen coordinates
GetWindowRect(main_window_handle, &dest_rect);
// compute the destination rectangle
dest_rect.left +=window_client_x0;
dest_rect.top +=window_client_y0;
dest_rect.right =dest_rect.left+screen_width-1;
dest_rect.bottom =dest_rect.top +screen_height-1;
// clip the screen coords
// blit the entire back surface to the primary
if (FAILED(lpddsprimary->Blt(&dest_rect, lpddsback,NULL,DDBLT_WAIT,NULL)))
return(0);
} // end if
// return success
return(1);
} // end DDraw_Flip
///////////////////////////////////////////////////////////
int DDraw_Wait_For_Vsync(void)
{
// this function waits for a vertical blank to begin
lpdd->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN,0);
// return success
return(1);
} // end DDraw_Wait_For_Vsync
///////////////////////////////////////////////////////////
int DDraw_Fill_Surface(LPDIRECTDRAWSURFACE7 lpdds, USHORT color, RECT *client)
{
DDBLTFX ddbltfx; // this contains the DDBLTFX structure
// clear out the structure and set the size field
DDRAW_INIT_STRUCT(ddbltfx);
// set the dwfillcolor field to the desired color
ddbltfx.dwFillColor = color;
// ready to blt to surface
lpdds->Blt(client, // ptr to dest rectangle
NULL, // ptr to source surface, NA
NULL, // ptr to source rectangle, NA
DDBLT_COLORFILL | DDBLT_WAIT, // fill and wait
&ddbltfx); // ptr to DDBLTFX structure
// return success
return(1);
} // end DDraw_Fill_Surface
///////////////////////////////////////////////////////////
UCHAR *DDraw_Lock_Surface(LPDIRECTDRAWSURFACE7 lpdds, int *lpitch)
{
// this function locks the sent surface and returns a pointer to it
// is this surface valid
if (!lpdds)
return(NULL);
// lock the surface
DDRAW_INIT_STRUCT(ddsd);
lpdds->Lock(NULL,&ddsd,DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,NULL);
// set the memory pitch
if (lpitch)
*lpitch = ddsd.lPitch;
// return pointer to surface
return((UCHAR *)ddsd.lpSurface);
} // end DDraw_Lock_Surface
///////////////////////////////////////////////////////////
int DDraw_Unlock_Surface(LPDIRECTDRAWSURFACE7 lpdds)
{
// this unlocks a general surface
// is this surface valid
if (!lpdds)
return(0);
// unlock the surface memory
lpdds->Unlock(NULL);
// return success
return(1);
} // end DDraw_Unlock_Surface
///////////////////////////////////////////////////////////
UCHAR *DDraw_Lock_Primary_Surface(void)
{
// this function locks the priamary surface and returns a pointer to it
// and updates the global variables primary_buffer, and primary_lpitch
// is this surface already locked
if (primary_buffer)
{
// return to current lock
return(primary_buffer);
} // end if
// lock the primary surface
DDRAW_INIT_STRUCT(ddsd);
lpddsprimary->Lock(NULL,&ddsd,DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,NULL);
// set globals
primary_buffer = (UCHAR *)ddsd.lpSurface;
primary_lpitch = ddsd.lPitch;
// return pointer to surface
return(primary_buffer);
} // end DDraw_Lock_Primary_Surface
///////////////////////////////////////////////////////////
int DDraw_Unlock_Primary_Surface(void)
{
// this unlocks the primary
// is this surface valid
if (!primary_buffer)
return(0);
// unlock the primary surface
lpddsprimary->Unlock(NULL);
// reset the primary surface
primary_buffer = NULL;
primary_lpitch = 0;
// return success
return(1);
} // end DDraw_Unlock_Primary_Surface
//////////////////////////////////////////////////////////
UCHAR *DDraw_Lock_Back_Surface(void)
{
// this function locks the secondary back surface and returns a pointer to it
// and updates the global variables secondary buffer, and back_lpitch
// is this surface already locked
if (back_buffer)
{
// return to current lock
return(back_buffer);
} // end if
// lock the primary surface
DDRAW_INIT_STRUCT(ddsd);
lpddsback->Lock(NULL,&ddsd,DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,NULL);
// set globals
back_buffer = (UCHAR *)ddsd.lpSurface;
back_lpitch = ddsd.lPitch;
// return pointer to surface
return(back_buffer);
} // end DDraw_Lock_Back_Surface
///////////////////////////////////////////////////////////
int DDraw_Unlock_Back_Surface(void)
{
// this unlocks the secondary
// is this surface valid
if (!back_buffer)
return(0);
// unlock the secondary surface
lpddsback->Unlock(NULL);
// reset the secondary surface
back_buffer = NULL;
back_lpitch = 0;
// return success
return(1);
} // end DDraw_Unlock_Back_Surface
///////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -