/*
プロジェクトのプロパティ
[構成プロパティ]-[VC++ ディレクトリ]
[インクルード ディレクトリ]
C:\Program Files\Microsoft DirectX SDK (February 2010)\Include
[ライブラリ ディレクトリ]
C:\Program Files\Microsoft DirectX SDK (February 2010)\Lib\x86
Unicode
*/
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
#include <d3d9.h>
#include <d3dx9.h>
#include <stdio.h>
#include <time.h>
#define WIDTH(rect) ((rect).right - (rect).left)
#define HEIGHT(rect) ((rect).bottom - (rect).top)
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
#define APP_NAME L"dxbase"
// 関数プロトタイプ宣言
HRESULT InitD3D(HWND hWnd);
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void Render(void);
// 外部変数構造体
static struct {
LPDIRECT3D9 pD3D; // Direct3D9
LPDIRECT3DDEVICE9 pDev; // レンダリングデバイス
LPD3DXFONT pFont; // フォント
} g;
//==============================================================================
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR lpCmdLine, int nCmdShow)
{
// ウィンドウクラスの登録
WNDCLASSEX wcx;
ZeroMemory(&wcx, sizeof wcx);
wcx.cbSize = sizeof wcx;
// wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = WndProc;
wcx.hInstance = hInstance;
wcx.hCursor = LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
wcx.lpszClassName = APP_NAME;
if (RegisterClassEx(&wcx) == 0) {
return 0;
}
// ウィンドウサイズの計算
RECT rect;
SetRect(&rect, 0, 0, 640, 480);
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
// ウィンドウの作成
HWND hWnd = CreateWindow(
APP_NAME, APP_NAME, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, WIDTH(rect), HEIGHT(rect),
NULL, NULL, hInstance, NULL);
if (hWnd == NULL) {
return 0;
}
// Direct3Dの初期化
if (FAILED(InitD3D(hWnd))) {
return 0;
}
// ウィンドウ表示
ShowWindow(hWnd, nCmdShow);
// メッセージループ
MSG msg;
do {
Sleep(1);
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
} else {
Render();
}
} while (msg.message != WM_QUIT);
SAFE_RELEASE(g.pFont);
SAFE_RELEASE(g.pDev);
SAFE_RELEASE(g.pD3D);
return msg.wParam;
}
//------------------------------------------------------------------------------
HRESULT InitD3D(HWND hWnd)
{
// Direct3D
g.pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (g.pD3D == NULL) {
return E_FAIL;
}
// デバイス作成用のパラメタ
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof d3dpp);
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // 1
d3dpp.Windowed = TRUE;
// Direct3Dデバイスの作成
HRESULT hr = g.pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g.pDev);
if (SUCCEEDED(hr)) {
hr = D3DXCreateFont(g.pDev, 40, 0, FW_HEAVY, 1, FALSE,
SHIFTJIS_CHARSET, OUT_TT_ONLY_PRECIS, ANTIALIASED_QUALITY,
FF_DONTCARE, L"MS Pゴシック", &g.pFont);
}
return hr;
}
//------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
//------------------------------------------------------------------------------
void Render(void)
{
static int fps = 0;
static int frame = 0;
static time_t tmPrev = time(NULL);
// fps
time_t tmCurr = time(NULL);
if (tmCurr != tmPrev) {
fps = frame;
frame = 0;
tmPrev = tmCurr;
}
frame++;
// バックバッファのクリア
g.pDev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0x7f,0x7f,0xff), 1.0f, 0);
g.pDev->BeginScene();
// 描画
TCHAR szBuf[32];
RECT rect;
SetRect(&rect, 0, 0, 50, 20);
swprintf_s(szBuf, L"time=%lld fps=%d", tmCurr, fps);
g.pFont->DrawText(NULL, szBuf, -1, &rect, DT_LEFT | DT_NOCLIP,
D3DCOLOR_ARGB(0xff,0xff,0xff,0xff));
// バックバッファを表画面に反映
g.pDev->EndScene();
g.pDev->Present(NULL, NULL, NULL, NULL);
}