1 //D3D初始化
2 bool HGE_Impl::_GfxInit()
3 {
4 static const char *szFormats[]={"UNKNOWN", "R5G6B5", "X1R5G5B5", "A1R5G5B5", "X8R8G8B8", "A8R8G8B8"}; //颜色格式串
5 D3DADAPTER_IDENTIFIER8 AdID; //D3D设备ID
6 D3DDISPLAYMODE Mode; //显示模式
7 D3DFORMAT Format=D3DFMT_UNKNOWN; //颜色格式
8 UINT nModes, i;
9
10 // Init D3D
11 //创建dx8对象
12 pD3D=Direct3DCreate8(120); // D3D_SDK_VERSION
13 if(pD3D==NULL)
14 {
15 _PostError("Can't create D3D interface");
16 return false;
17 }
18
19 // Get adapter info
20 //取显卡信息
21 pD3D->GetAdapterIdentifier(D3DADAPTER_DEFAULT, D3DENUM_NO_WHQL_LEVEL, &AdID);
22 System_Log("D3D Driver: %s",AdID.Driver);
23 System_Log("Description: %s",AdID.Description);
24 System_Log("Version: %d.%d.%d.%d",
25 HIWORD(AdID.DriverVersion.HighPart),
26 LOWORD(AdID.DriverVersion.HighPart),
27 HIWORD(AdID.DriverVersion.LowPart),
28 LOWORD(AdID.DriverVersion.LowPart));
29
30 // Set up Windowed presentation parameters
31 //设置窗口模式参数
32 //取显卡模式
33 if(FAILED(pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Mode)) || Mode.Format==D3DFMT_UNKNOWN)
34 {
35 //如果pD3D->GetAdapterDisplayMode被成功调用后,会设置Mode,所以Mode.Format==D3DFMT_UNKNOWN是有效的。
36 //如果取模式失败,或者取得模式后,得到的格式为D3DFMT_UNKNOWN,则失败。
37 _PostError("Can't determine desktop video mode");
38 if(bWindowed) return false;
39 }
40
41 ZeroMemory(&d3dppW, sizeof(d3dppW));
42 //设置后台缓冲
43 d3dppW.BackBufferWidth = nScreenWidth; //后台缓存的宽度,像素为单位
44 d3dppW.BackBufferHeight = nScreenHeight; //后台缓存的高度,像素为单位
45 d3dppW.BackBufferFormat = Mode.Format; //像素格式
46 d3dppW.BackBufferCount = 1; //后台缓存的个数
47
48 d3dppW.MultiSampleType = D3DMULTISAMPLE_NONE; //指定多重采样类型,用于图型抗锯齿,在这里表示不抗锯齿
49 d3dppW.hDeviceWindow = hwnd; //指定的绘图窗口
50 d3dppW.Windowed = TRUE; //显示方式,TRUE表示以窗口方式,FALSE表示以全屏方式
51
52 //d3dppW.SwapEffect 设置后台到前台的方式
53 if(nHGEFPS==HGEFPS_VSYNC) d3dppW.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC; //D3DSWAPEFFECT_COPY_VSYNC时值为4用来说明只有一个后缓冲区,并执行COPY操作,该值在DX9中,已经没有了
54 else d3dppW.SwapEffect = D3DSWAPEFFECT_COPY; //当后台缓存为1时有用,将后台复制到前台,后台不变
55
56 if(bZBuffer) //如果使用深度缓存
57 {
58 d3dppW.EnableAutoDepthStencil = TRUE; //创建并管理一个深度缓存
59 d3dppW.AutoDepthStencilFormat = D3DFMT_D16; //表示深度值为16位二进制表示
60 }
61
62 // Set up Full Screen presentation parameters
63 //下面是设置全屏模式
64 nModes=pD3D->GetAdapterModeCount(D3DADAPTER_DEFAULT); //取设备模式的个数
65
66 for(i=0; i<nModes; i++) //依次取出所有模式,并找出Mode.Format的最高值,并放到Format中
67 {
68 pD3D->EnumAdapterModes(D3DADAPTER_DEFAULT, i, &Mode); //取得第I个模式的信息
69 if(Mode.Width != (UINT)nScreenWidth || Mode.Height != (UINT)nScreenHeight) continue;
70 if(nScreenBPP==16 && (_format_id(Mode.Format) > _format_id(D3DFMT_A1R5G5B5))) continue;
71 if(_format_id(Mode.Format) > _format_id(Format)) Format=Mode.Format;
72 }
73
74 if(Format == D3DFMT_UNKNOWN)
75 {
76 _PostError("Can't find appropriate full screen video mode");
77 if(!bWindowed) return false;
78 }
79
80 ZeroMemory(&d3dppFS, sizeof(d3dppFS));
81
82 d3dppFS.BackBufferWidth = nScreenWidth; //后台缓存的宽度,像素为单位
83 d3dppFS.BackBufferHeight = nScreenHeight;//后台缓存的高度,像素为单位
84 d3dppFS.BackBufferFormat = Format; //像素格式
85 d3dppFS.BackBufferCount = 1; //后台缓存的个数
86 d3dppFS.MultiSampleType = D3DMULTISAMPLE_NONE; //指定多重采样类型,用于图型抗锯齿,在这里表示不抗锯齿
87 d3dppFS.hDeviceWindow = hwnd; //指定的绘图窗口
88 d3dppFS.Windowed = FALSE; //显示方式,TRUE表示以窗口方式,FALSE表示以全屏方式
89
90 d3dppFS.SwapEffect = D3DSWAPEFFECT_FLIP; //后台复制到前台,缓存不变。缓存数大于1时使用。这里好像有点不对
91 d3dppFS.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; //屏幕屏新频率,一般使用显示器默认的就可以了。
92
93 if(nHGEFPS==HGEFPS_VSYNC) d3dppFS.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_ONE; //前台与后台缓存交换频率
94 else d3dppFS.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
95
96 if(bZBuffer)
97 {
98 d3dppFS.EnableAutoDepthStencil = TRUE; //创建并管理一个深度缓存
99 d3dppFS.AutoDepthStencilFormat = D3DFMT_D16; //表示深度值为16位二进制表示
100 }
101
102 d3dpp = bWindowed ? &d3dppW : &d3dppFS; //根据窗口选择d3dpp
103
104 if(_format_id(d3dpp->BackBufferFormat) < 4) nScreenBPP=16; //Screen Bitdepth,在窗口模式下,该值无效。将使用系统自带的
105 else nScreenBPP=32;
106
107 // Create D3D Device
108 //创建设备
109 if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
110 D3DCREATE_SOFTWARE_VERTEXPROCESSING,
111 d3dpp, &pD3DDevice ) ) )
112 {
113 _PostError("Can't create D3D device");
114 return false;
115 }
116 //调整窗口
117 _AdjustWindow();
118
119 System_Log("Mode: %d x %d x %s\n",nScreenWidth,nScreenHeight,szFormats[_format_id(Format)]);
120
121 // Create vertex batch buffer
122
123 VertArray=0; //批量顶点缓冲区指针,将会在Gfx_BeginScene末尾Lock该值。
124 textures=0;
125
126 // Init all stuff that can be lost
127 //设置投影Matrix
128 _SetProjectionMatrix(nScreenWidth, nScreenHeight);
129 //创建一个单位Matrix
130 D3DXMatrixIdentity(&matView);
131 //初始化缓冲区,_init_lost也在设备、资源丢失的时候,使用。
132 if(!_init_lost()) return false;
133 Gfx_Clear(0); //清屏到指定颜色,在这里是黑色。
134 return true;
135 }