⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hdir.cpp

📁 windows游戏编程的用c++写的一个例子,从别处下载的
💻 CPP
字号:
#define WIN32_LEAN_AND_MEAN

#include<windows.h>
#include<windowsx.h>
#include<mmsystem.h>

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
#include<memory.h>
#include<string.h>
#include<stdarg.h>
#include<stdio.h>
#include<math.h>
#include<io.h>
#include<fcntl.h>

#include<ddraw.h>
#include "Hdir.h"


extern HWND main_window_handle;
extern HINSTANCE main_instance;

//全局
LPDIRECTDRAW7         lpdd         = NULL;   
LPDIRECTDRAWSURFACE7  lpddsprimary = NULL;   
LPDIRECTDRAWSURFACE7  lpddsback    = NULL;   
LPDIRECTDRAWPALETTE   lpddpal      = NULL;//调色版   
LPDIRECTDRAWCLIPPER   lpddclipper  = NULL;  
PALETTEENTRY          palette[256];          
PALETTEENTRY          save_palette[256];     
DDSURFACEDESC2        ddsd;                  
DDBLTFX               ddbltfx;              
DDSCAPS2              ddscaps;             
HRESULT               ddrval;                
DWORD                 start_clock_count = 0;

//裁减矩形范围
int min_clip_x = 0,                          
    max_clip_x = SCREEN_WIDTH-1,
    min_clip_y = 0,
    max_clip_y = SCREEN_HEIGHT-1;
//
int screen_width  = SCREEN_WIDTH,            
    screen_height = SCREEN_HEIGHT,           
    screen_bpp    = SCREEN_BPP; 

//函数实现
int DD_Init(int width, int height, int bpp)
{
// 出始化directdraw
int index; 

//创建directdraw对象,并检查错误
if (DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)!=DD_OK)
   return(0);

//设置协作等级
if (lpdd->SetCooperativeLevel(main_window_handle,
           DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN | 
           DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)!=DD_OK)
    return(0);

// 设置显示模式
if (lpdd->SetDisplayMode(width,height,bpp,0,0)!=DD_OK)
   return(0);

// 给外部变量赋值
screen_height = height;
screen_width  = width;
screen_bpp    = bpp;

//创建主表面
memset(&ddsd,0,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,&lpddsprimary,NULL);

//获取后台缓冲面
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
lpddsprimary->GetAttachedSurface(&ddscaps,&lpddsback);


//将palette清零
memset(palette,0,256*sizeof(PALETTEENTRY));

// 颜色索引
for (index=0; index < 256; index++)
    {
    
    if (index < 64) 
        palette[index].peRed = index*4; 
    else           
    if (index >= 64 && index < 128) 
        palette[index].peGreen = (index-64)*4;
    else          
    if (index >= 128 && index < 192) 
       palette[index].peBlue = (index-128)*4;
    else           
    if (index >= 192 && index < 256) 
        palette[index].peRed = palette[index].peGreen = 
        palette[index].peBlue = (index-192)*4;
    
    
    palette[index].peFlags = PC_NOCOLLAPSE;
    } 

// 生成调色版
if (lpdd->CreatePalette(DDPCAPS_8BIT | DDPCAPS_INITIALIZE | DDPCAPS_ALLOW256,
                         palette,&lpddpal,NULL)!=DD_OK)
   return(0);

//调色版载入主表面
if (lpddsprimary->SetPalette(lpddpal)!=DD_OK)
   return(0);


DD_Fill_Surface(lpddsprimary,0);
DD_Fill_Surface(lpddsback,0);

//使用裁剪
RECT screen_rect = {0,0,screen_width,screen_height};
lpddclipper = DD_Attach_Clipper(lpddsback,1,&screen_rect);


return(1);
}

int DD_Shutdown(void)
{

// 释放裁剪器
if (lpddclipper)
    lpddclipper->Release();

// 释放调色版
if (lpddpal)
   lpddpal->Release();

// 释放后台
if (lpddsback)
    lpddsback->Release();

// 释放主表面
if (lpddsprimary)
   lpddsprimary->Release();

// 最后释放对象
if (lpdd)
    lpdd->Release();
return(1);
} 


LPDIRECTDRAWCLIPPER DD_Attach_Clipper(LPDIRECTDRAWSURFACE7 lpdds,
                                      int num_rects,
                                      LPRECT clip_list)

{

int index;                         
LPDIRECTDRAWCLIPPER lpddclipper;  
LPRGNDATA region_data;            
                                   

// 生成裁剪器
if ((lpdd->CreateClipper(0,&lpddclipper,NULL))!=DD_OK)
   return(NULL);




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);


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;


for (index=0; index<num_rects; index++)
    {
    
    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;

    } 

// 

if ((lpddclipper->SetClipList(region_data, 0))!=DD_OK)
   {
   
   free(region_data);
   return(NULL);
   } 


if ((lpdds->SetClipper(lpddclipper))!=DD_OK)
   {
   
   free(region_data);
   return(NULL);
   }


free(region_data);
return(lpddclipper);

} 

int DD_Flip(void)
{

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



return(1);

} 



DWORD Start_Clock(void)
{

return(start_clock_count = Get_Clock());

} 

DWORD Get_Clock(void)
{

return(GetTickCount());

}


DWORD Wait_Clock(DWORD count)
{

while((Get_Clock() - start_clock_count) < count);
return(Get_Clock());

} 


int DD_Fill_Surface(LPDIRECTDRAWSURFACE7 lpdds,int color)
{
DDBLTFX ddbltfx; 


DD_INIT_STRUCT(ddbltfx);


ddbltfx.dwFillColor = color; 

//
lpdds->Blt(NULL,      
           NULL,                  
           NULL,       
           DDBLT_COLORFILL | DDBLT_WAIT | DDBLT_ASYNC,                    
           &ddbltfx);  

return(1);
} 

   

int Draw_Rectangle(int x1, int y1, int x2, int y2, int color,
                   LPDIRECTDRAWSURFACE7 lpdds)
{


DDBLTFX ddbltfx; 
RECT fill_area; //目标矩形

 
DD_INIT_STRUCT(ddbltfx);


ddbltfx.dwFillColor = color; 

// 填充目标矩形数据
fill_area.top    = y1;
fill_area.left   = x1;
fill_area.bottom = y2+1;
fill_area.right  = x2+1;


lpdds->Blt(&fill_area, 
           NULL,                   
           NULL,       
           DDBLT_COLORFILL | DDBLT_WAIT | DDBLT_ASYNC,                    
           &ddbltfx);  


return(1);

} 



int Draw_Text_GDI(char *text, int x,int y,int color, LPDIRECTDRAWSURFACE7 lpdds)
{


HDC xdc; 


if (lpdds->GetDC(&xdc)!=DD_OK)
   return(0);


SetTextColor(xdc,RGB(palette[color].peRed,palette[color].peGreen,palette[color].peBlue) );

SetBkMode(xdc, TRANSPARENT);


TextOut(xdc,x,y,text,strlen(text));


lpdds->ReleaseDC(xdc);


return(1);
} 






⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -