開発環境 |
Microsoft Visual C++ 2010 Express (SP1) |
実行環境 |
Microsoft Windows XP Home Edition (SP3) |
プロジェクトの種類 |
Win32 プロジェクト |
プロジェクト名 |
udxhello |
アプリケーションの種類 |
Windows アプリケーション |
追加のオプション |
空のプロジェクト |
文字セット |
マルチ バイト |
Unmanaged DirectXでhello, worldする。
ddraw.libが必要になるがDirectX SDK (June 2010)には含まれていない。
DirectX SDK (February 2010)には含まれているのでこれを使用する。
プロジェクトのプロパティにDirectX SDKのディレクトリを追加する。
構成プロパティ/VC++ ディレクトリ
- インクルード ディレクトリ:C:\Program Files\Microsoft DirectX SDK (February 2010)\Include
- ライブラリ ディレクトリ:C:\Program Files\Microsoft DirectX SDK (February 2010)\Lib\x86
追加後はそれぞれ以下のようになっているはずである。
- C:\Program Files\Microsoft DirectX SDK (February 2010)\Include;$(IncludePath)
- C:\Program Files\Microsoft DirectX SDK (February 2010)\Lib\x86;$(LibraryPath)
$(DXSDK_DIR)というマクロも使えるが、内容が変わる可能性があるので直接指定した方が確実である。
DXSDK_DIR=C:\Program Files\Microsoft DirectX SDK (February 2010)\
また、以下のファイルが必要になる。
- ddutil.cpp
- ddutil.h
- dxutil.h
ddutil.hを以下のように修正する。
//#include <d3d.h>
#include <d3d9.h>
参考
udxhello.cpp
// マルチバイト文字セット
#pragma comment(lib, "ddraw.lib")
#pragma comment(lib, "dxguid.lib") // IID_IDirectDraw7のため
#define STRICT
#include <Windows.h>
#include <ddraw.h>
#include "ddutil.h"
#define CLASS_NAME TEXT("Hello DirectX")
#define ERMSG(x) MessageBox(NULL, x, "DirectX9", MB_OK)
#define SAFE_DELETE(p) {if (p) {delete (p); (p) = NULL;} }
// 関数プロトタイプ宣言
HRESULT WinInit(HINSTANCE hInstance, int nCmdShow, HWND *phWnd);
HRESULT InitDirectDraw(HWND hWnd);
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HRESULT DisplayFrame();
// 外部変数
CDisplay *g_pDisplay = NULL;
CSurface *g_pTextSurface = NULL;
BOOL g_bActive = FALSE;
//==============================================================================
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
MSG msg;
if (FAILED(WinInit(hInstance, nCmdShow, &hWnd))) return 0;
if (FAILED(InitDirectDraw(hWnd))) {
if (g_pDisplay) {
g_pDisplay->GetDirectDraw()->SetCooperativeLevel(NULL, DDSCL_NORMAL);
}
ERMSG("DirectDraw init failed. The sample will now exit.");
return 0;
}
while (1) {
if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
if (GetMessage(&msg, NULL, 0, 0) == 0) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
} else {
if (g_bActive) {
if (FAILED(DisplayFrame())) {
SAFE_DELETE(g_pDisplay);
ERMSG("Displaying the next frame failed."
" The sample will now exit.");
return 0;
}
} else {
WaitMessage();
}
}
}
return msg.wParam;
}
//------------------------------------------------------------------------------
HRESULT WinInit(HINSTANCE hInstance, int nCmdShow, HWND *phWnd)
{
WNDCLASS wc;
HWND hWnd;
ZeroMemory(&wc, sizeof (WNDCLASS));
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.lpfnWndProc = MainWndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = CLASS_NAME;
if (RegisterClass(&wc) == 0) return E_FAIL;
hWnd = CreateWindowEx(0, CLASS_NAME, TEXT("DirectDraw TEXT View"), WS_POPUP,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
NULL, NULL, hInstance, NULL);
if (hWnd == NULL) return E_FAIL;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
*phWnd = hWnd;
return S_OK;
}
//------------------------------------------------------------------------------
HRESULT InitDirectDraw(HWND hWnd)
{
HRESULT hr;
g_pDisplay = new CDisplay();
hr = g_pDisplay->CreateWindowedDisplay(hWnd, 640, 480);
if (FAILED(hr)) {
ERMSG("Failed initializing DirectDraw.");
return hr;
}
hr = g_pDisplay->CreateSurfaceFromText(&g_pTextSurface, NULL, "Hello DirectX",
RGB(0,0,0), RGB(255,255,0));
if (FAILED(hr)) {
return hr;
}
return S_OK;
}
//------------------------------------------------------------------------------
VOID FreeDirectDraw()
{
SAFE_DELETE(g_pTextSurface);
SAFE_DELETE(g_pDisplay);
}
//------------------------------------------------------------------------------
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_KEYDOWN:
PostMessage(hWnd, WM_CLOSE, 0, 0);
return 0;
case WM_PAINT:
if (g_pDisplay) {
if (DisplayFrame() == DDERR_SURFACELOST) {
PostMessage(hWnd, WM_CLOSE, 0, 0);
}
}
break;
case WM_MOVE:
if (g_pDisplay) g_pDisplay->UpdateBounds();
return 0;
case WM_SIZE:
if (wParam == SIZE_MAXHIDE || wParam == SIZE_MINIMIZED) {
g_bActive = FALSE;
} else {
g_bActive = TRUE;
}
if (g_pDisplay) g_pDisplay->UpdateBounds();
break;
case WM_DESTROY:
FreeDirectDraw();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
//------------------------------------------------------------------------------
HRESULT DisplayFrame()
{
HRESULT hr;
g_pDisplay->Clear(RGB(120,140,160));
g_pDisplay->Blt(200, 100, g_pTextSurface, NULL);
hr = g_pDisplay->Present();
if (FAILED(hr)) return hr;
return S_OK;
}
最終更新:2012年09月20日 06:10