|
|
首先,对那些对知识共享没兴趣的老鸟表示不满(也只能不满而已)。
其次,对自己没好好学英文表示不满,同时,对金山词霸表示感谢(虽然不怎么好用)。
再次,我很累,但很兴奋。熬夜学习象做爱一样刺激我的神经。
最后,明天,我会继续体验熬夜给我带来的快感。
以下,是今天晚上的成果,很多地方弄不清,希望有高手指点,感激不尽!
SDK自带的例题1蓝色窗口,在金山词霸和GOOGLE两位老师指点下写的注释。
//-----------------------------------------------------------------------------
// File: CreateDevice.cpp
//
// Desc: This is the first tutorial for using Direct3D. In this tutorial, all
// we are doing is creating a Direct3D device and using it to clear the
// window.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include <d3d9.h>
#include <strsafe.h>//?晕,不懂
//安全的C语言字符串处理函数库
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
//接口指针
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
//设备指针
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object, which is needed to create the D3DDevice.
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
// Set up the structure used to create the D3DDevice. Most parameters are
// zeroed out. We set Windowed to TRUE, since we want to do D3D in a
// window, and then set the SwapEffect to "discard", which is the most
// efficient method of presenting the back buffer to the display. And
// we request a back buffer format that matches the current desktop display
// format.
D3DPRESENT_PARAMETERS d3dpp; //创建适合Device的结构体
ZeroMemory( &d3dpp, sizeof(d3dpp) );//将结构体清零
d3dpp.Windowed = TRUE;//创建窗口模式
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;//?晕,交换实现
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;//后台缓冲的格式保持与当前基础画面模式一致
// Create the Direct3D device. Here we are using the default adapter (most
// systems only have one, unless they have multiple graphics hardware cards
// installed) and requesting the HAL (which is saying we want the hardware
// device rather than a software one). Software vertex processing is
// specified since we know it will work on all cards. On cards that support
// hardware vertex processing, though, we would see a big performance gain
// by specifying hardware vertex processing.
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
/************************CreatDevice()函数传递的参数********************
Adaptr: 要创建的Device的顺序编号
DeviceType 决定输出Device的种类。HAL是支持硬件加速的Device。REF为软件。
hfocuusWindow Device要输出的Window的句柄。
BehaviorFlags ?控制所创建设备行为的集合
PpreseationParameters 前面声明的结构体的指针
PpReturnedDeviceinterface 得到设备指针
***********************************************************************/
{
return E_FAIL;
}
// Device state would normally be set here
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects清除初始化对象
//-----------------------------------------------------------------------------
VOID Cleanup()//后创建的先清除,感觉跟析构函数差不多的说
{
if( g_pd3dDevice != NULL)
g_pd3dDevice->Release();
if( g_pD3D != NULL)
g_pD3D->Release();
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
if( NULL == g_pd3dDevice )
return;
// Clear the backbuffer to a blue color用蓝色清除后台缓冲
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
// Begin the scene开始绘制
if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
// Rendering of scene objects can happen here在这里放置Rendering命令
// End the scene绘制结束
g_pd3dDevice->EndScene();
}
// Present the backbuffer contents to the display能看到后台缓冲的画面
g_pd3dDevice-> resent( NULL, NULL, NULL, NULL );
}
//-----------------------------------------------------------------------------
// Name: MsgProc()回调函数,消息句柄。没找到帮助,看返回值应该是传递都WinMain()
//中的消息循环中去了。
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
case WM_PAINT:
Render();
ValidateRect( hWnd, NULL );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// Register the window class
//定义窗口类
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"窗口类名", NULL };
//注册窗口类
RegisterClassEx( &wc );
// Create the application's window
//创建窗口
HWND hWnd = CreateWindow( "窗口类名", "窗口的名字(标题栏)",
WS_OVERLAPPEDWINDOW,//窗口风格
100, 100,// ? x,y(帮助上这么写的,没看明白。不过
//经修改测试后看,应该与窗口的位置有关)
300, 300,//窗口的宽,高
GetDesktopWindow(), NULL, wc.hInstance, NULL );
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Show the window
ShowWindow( hWnd, SW_SHOWDEFAULT );//显示窗口
UpdateWindow( hWnd );//强制显示
// Enter the message loop
MSG msg; //消息循环
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );//解释消息
DispatchMessage( &msg );//传递消息
}
}
UnregisterClass( "D3D Tutorial", wc.hInstance );//注销窗口类
return 0;
}
|
|