|
電腦遊戲製作開發設計論壇 任何可以在PC上跑的遊戲都可以討論,主要以遊戲之製作開發為主軸,希望讓台灣的遊戲人有個討論、交流、教學、經驗傳承的園地
|
上一篇主題 :: 下一篇主題 |
發表人 |
內容 |
npcgamer 時常出沒的會員
註冊時間: 2007-11-02 文章: 47
3.00 果凍幣
|
發表於: 2007-11-3, PM 8:45 星期六 文章主題: [D3D] 第一個視窗 |
|
|
底下是建立空白畫面的 Direct3D 視窗應用程式
基本準備
Visual C++ 2008 Express Edition Beta 2
DirectX SDK
Create a Window
在這裡是使用內建 "#32770" 的Dialog Box視窗類別來建立視窗。 代碼: | #include "stdafx.h"
LRESULT CALLBACK WindowProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefDlgProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
hWnd = CreateWindowEx(0, TEXT("#32770"), TEXT("Direct3D Sample"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
SetWindowLongPtr(hWnd, GWL_WNDPROC, (LONG_PTR)WindowProc);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
} |
Use Direct3D
Direct3D Header & Library Files 代碼: | #include <d3dx9.h>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib") |
代碼: | LPDIRECT3D9 pD3D;
pD3D = Direct3DCreate9(D3D_SDK_VERSION); |
Set Dircet3D Device Parameters
代碼: | D3DDISPLAYMODE d3ddm;
D3DPRESENT_PARAMETERS d3dpp;
pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferWidth = 0;
d3dpp.BackBufferHeight = 0;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.Windowed = true;
// d3dpp.FullScreen_RefreshRateInHz = d3ddm.RefreshRate;
// d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; |
代碼: | LPDIRECT3DDEVICE9 pD3DD;
pD3DD = Direct3DDeviceCreate9(pD3D, &d3dpp); |
代碼: | LPDIRECT3DDEVICE9 Direct3DDeviceCreate9(LPDIRECT3D9 pD3D, D3DPRESENT_PARAMETERS* pd3dpp)
{
LPDIRECT3DDEVICE9 pD3DD = NULL;
if (pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, D3DCREATE_HARDWARE_VERTEXPROCESSING, pd3dpp, &pD3DD) != D3D_OK)
if (pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, pd3dpp, &pD3DD) != D3D_OK)
if (pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, pd3dpp, &pD3DD) != D3D_OK)
{
// IDirect3D9::CreateDevice() failed
return NULL;
}
return pD3DD;
} |
代碼: | void render ( )
{
pD3DD->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0, 0);
pD3DD->BeginScene();
pD3DD->EndScene();
pD3DD->Present(NULL, NULL, NULL, NULL);
} |
代碼: | MSG msg = {0};
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
render();
}
} |
代碼: | if (pD3DD) pD3DD->Release();
if (pD3D) pD3D->Release(); |
Modify the Window Procedure 代碼: | LRESULT CALLBACK WindowProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
// return DefDlgProc(hWnd, uMsg, wParam, lParam);
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
} |
想像圖:
dx9_initialization.rar |
|
回頂端 |
|
|
npcgamer 時常出沒的會員
註冊時間: 2007-11-02 文章: 47
3.00 果凍幣
|
發表於: 2008-3-10, PM 8:09 星期一 文章主題: |
|
|
【更正】
建立視窗應使用自訂視窗類別,優點是它是唯一能自製擁有 IME 輸入法功能的視窗。
代碼: | LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
} |
代碼: | // 建立主視窗
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = WindowProc;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIconSm = wcex.hIcon;
wcex.hbrBackground = CreateSolidBrush(RGB(0, 0, 0));
wcex.lpszMenuName = NULL;
wcex.lpszClassName = L"MyWindowApplication";
if (!RegisterClassEx(&wcex))
{
return 0;
}
HWND hWnd;
hWnd = CreateWindowEx(0, L"MyWindowApplication", L"DirectX 應用程式", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return 0;
} |
|
|
回頂端 |
|
|
|
|
您 無法 在這個版面發表文章 您 無法 在這個版面回覆文章 您 無法 在這個版面編輯文章 您 無法 在這個版面刪除文章 您 無法 在這個版面進行投票 您 可以 在這個版面附加檔案 您 可以 在這個版面下載檔案
|
|