//=============================================================================
// d3dApp.h by Frank Luna (C) 2005 All Rights Reserved.
//=============================================================================
#include "d3dApp.h"
D3DApp* gd3dApp = 0;
IDirect3DDevice9* gd3dDevice = 0;
LRESULT CALLBACK
MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// Don't start processing messages until the application has been created.
if( gd3dApp != 0 )
return gd3dApp->msgProc(msg, wParam, lParam);
else
return DefWindowProc(hwnd, msg, wParam, lParam);
}
D3DApp::D3DApp(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP)
{
mMainWndCaption = winCaption;
mDevType = devType;
mRequestedVP = requestedVP;
mhAppInst = hInstance;
mhMainWnd = 0;
md3dObject = 0;
mAppPaused = false;
ZeroMemory(&md3dPP, sizeof(md3dPP));
initMainWindow();
initDirect3D();
}
D3DApp::~D3DApp()
{
ReleaseCOM(md3dObject);
ReleaseCOM(gd3dDevice);
}
HINSTANCE D3DApp::getAppInst()
{
return mhAppInst;
}
HWND D3DApp::getMainWnd()
{
return mhMainWnd;
}
void D3DApp::initMainWindow()
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = mhAppInst;
wc.hIcon = LoadIcon(0, IDI_APPLICATION);
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = "D3DWndClassName";
if( !RegisterClass(&wc) )
{
MessageBox(0, "RegisterClass FAILED", 0, 0);
PostQuitMessage(0);
}
// Default to a window with a client area rectangle of 800x600.
RECT R = {0, 0, 800, 600};
AdjustWindowRect(&R, WS_OVERLAPPEDWINDOW, false);
mhMainWnd = CreateWindow("D3DWndClassName", mMainWndCaption.c_str(),
WS_OVERLAPPEDWINDOW, 100, 100, R.right, R.bottom,
0, 0, mhAppInst, 0);
if( !mhMainWnd )
{
MessageBox(0, "CreateWindow FAILED", 0, 0);
PostQuitMessage(0);
}
ShowWindow(mhMainWnd, SW_SHOW);
UpdateWindow(mhMainWnd);
}
void D3DApp::initDirect3D()
{
// Step 1: Create the IDirect3D9 object.
md3dObject = Direct3DCreate9(D3D_SDK_VERSION);
if( !md3dObject )
{
MessageBox(0, "Direct3DCreate9 FAILED", 0, 0);
PostQuitMessage(0);
}
// Step 2: Verify hardware support for specified formats in windowed and full screen modes.
D3DDISPLAYMODE mode;
md3dObject->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &mode);
HR(md3dObject->CheckDeviceType(D3DADAPTER_DEFAULT, mDevType, mode.Format, mode.Format, true));
HR(md3dObject->CheckDeviceType(D3DADAPTER_DEFAULT, mDevType, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, false));
// Step 3: Check for requested vertex processing and pure device.
D3DCAPS9 caps;
HR(md3dObject->GetDeviceCaps(D3DADAPTER_DEFAULT, mDevType, &caps));
DWORD devBehaviorFlags = 0;
if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
devBehaviorFlags |= mRequestedVP;
else
devBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
// If pure device and HW T&L supported
if( caps.DevCaps & D3DDEVCAPS_PUREDEVICE &&
devBehaviorFlags & D3DCREATE_HARDWARE_VERTEXPROCESSING)
devBehaviorFlags |= D3DCREATE_PUREDEVICE;
// Step 4: Fill out the D3DPRESENT_PARAMETERS structure.
md3dPP.BackBufferWidth = 0;
md3dPP.BackBufferHeight = 0;
md3dPP.BackBufferFormat = D3DFMT_UNKNOWN;
md3dPP.BackBufferCount = 1;
md3dPP.MultiSampleType = D3DMULTISAMPLE_NONE;
md3dPP.MultiSampleQuality = 0;
md3dPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
md3dPP.hDeviceWindow = mhMainWnd;
md3dPP.Windowed = true;
md3dPP.EnableAutoDepthStencil = true;
md3dPP.AutoDepthStencilFormat = D3DFMT_D24S8;
md3dPP.Flags = 0;
md3dPP.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
md3dPP.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
// Step 5: Create the device.
HR(md3dObject->CreateDevice(
D3DADAPTER_DEFAULT, // primary adapter
mDevType, // device type
mhMainWnd, // window associated with device
devBehaviorFlags, // vertex processing
&md3dPP, // present parameters
&gd3dDevice)); // return created device
}
int D3DApp::run()
{
MSG msg;
msg.message = WM_NULL;
__int64 cntsPerSec = 0;
QueryPerformanceFrequency((LARGE_INTEGER*)&cntsPerSec);
float secsPerCnt = 1.0f / (float)cntsPerSec;
__int64 prevTimeStamp = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&prevTimeStamp);
while(msg.message != WM_QUIT)
{
// If there are Window messages then process them.
if(PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
// Otherwise, do animation/game stuff.
else
{
// If the application is paused then free some CPU cycles to other
// applications and then continue on to the next frame.
if( mAppPaused )
{
Sleep(20);
continue;
}
if( !isDeviceLost() )
{
__int64 currTimeStamp = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&currTimeStamp);
float dt = (currTimeStamp - prevTimeStamp)*secsPerCnt;
updateScene(dt);
drawScene();
// Prepare for next iteration: The current time stamp becomes
// the previous time stamp for the next iteration.
prevTimeStamp = currTimeStamp;
}
}
}
return (int)msg.wParam;
}
LRESULT D3DApp::msgProc(UINT msg, WPARAM wParam, LPARAM lParam)
{
// Is the application in a minimized or maximized state?
static bool minOrMaxed = false;
RECT clientRect = {0, 0, 0, 0};
switch( msg )
{
// WM_ACTIVE is sent when the window is activated or deactivated.
// We pause the game when the main window is deactivated and
// unpause it when it becomes active.
case WM_ACTIVATE:
if( LOWORD(wParam) == WA_INACTIVE )
mAppPaused = true;
else
mAppPaused = false;
return 0;
// WM_SIZE is sent when the user resizes the window.
case WM_SIZE:
if( gd3dDevice )
{
md3dPP.BackBufferWidth = LOWORD(lParam);
md3dPP.BackBufferHeight = HIWORD(lParam);
if( wParam == SIZE_MINIMIZED )
{
mAppPaused = true;
minOrMaxed = true;
}
else if( wParam == SIZE_MAXIMIZED )
{
mAppPaused = false;
minOrMaxed = true;
onLostDevice();
HR(gd3dDevice->Reset(&md3dPP));
onResetDevice();
}
// Restored is any resize that is not a minimize or maximize.
// For example, restoring the window to its default size
// after a minimize or maximize, or from dragging the resize
// bars.
else if( wParam == SIZE_RESTORED )
{
mAppPaused = false;
// Are we restoring from a mimimized or maximized state,
// and are in windowed mode? Do not execute this code if
// we are restoring to full screen mode.
if( minOrMaxed && md3dPP.Windowed )
{
onLostDevice();
HR(gd3dDevice->Reset(&md3dPP));
onResetDevice();
}
else
{
// No, which implies the user is resizing by dragging
// the resize bars. However, we do not reset the device
// here because as the user continuously drags the resize
// bars, a stream of WM_SIZE messages is sent to the window,
// and it would be pointless (and slow) to reset for each
// WM_SIZE message received from dragging the resize bars.
// So instead, we reset after the user is done resizing the
// window and releases the resize bars, which sends a
// WM_EXITSIZEMOVE message.
}
minOrMaxed = false;
}
}
return 0;
// WM_EXITSIZEMOVE is sent when the user releases the resize bars.
// Here we reset everything based on the new wind
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Part II Ch 4-8.ZIP (99个子文件)
Chapter 4
Hello Direct3D
Hello Direct3D.ncb 7.78MB
Hello Direct3D.vcproj 4KB
HelloDirect3D.cpp 3KB
Hello Direct3D.sln 892B
Hello Direct3D.vcproj.CRONUS-J1M2FQ5T.Cronus.user 1KB
d3dApp.cpp 10KB
d3dUtil.h 2KB
Hello Direct3D.suo 7KB
d3dApp.h 2KB
Chapter 5
PageFlipDemo
PageFlipDemo.cpp 5KB
PageFlipDemo.ncb 8.21MB
PageFlipDemo.sln 888B
PageFlipDemo.vcproj 4KB
PageFlipDemo.suo 7KB
GfxStats.h 1019B
d3dApp.cpp 11KB
DirectInput.cpp 2KB
d3dUtil.h 2KB
DirectInput.h 1KB
d3dApp.h 2KB
fireatlas.bmp 1MB
PageFlipDemo.vcproj.CRONUS-J1M2FQ5T.Cronus.user 1KB
GfxStats.cpp 3KB
SpriteDemo
bkgd1.bmp 768KB
SpriteDemo.sln 884B
SpriteDemo.cpp 11KB
SpriteDemo.ncb 8.36MB
bullet.bmp 16KB
SpriteDemo.vcproj.CRONUS-J1M2FQ5T.Cronus.user 1KB
SpriteDemo.vcproj 4KB
GfxStats.h 1019B
d3dApp.cpp 11KB
alienship.bmp 64KB
DirectInput.cpp 2KB
d3dUtil.h 2KB
SpriteDemo.suo 7KB
DirectInput.h 1KB
d3dApp.h 2KB
GfxStats.cpp 3KB
GfxStatsDemo
GfxStatsDemo.vcproj.CRONUS-J1M2FQ5T.Cronus.user 1KB
GfxStatsDemo.cpp 2KB
GfxStatsDemo.vcproj 4KB
GfxStatsDemo.suo 7KB
GfxStats.h 1019B
d3dApp.cpp 11KB
d3dUtil.h 2KB
GfxStatsDemo.sln 888B
GfxStatsDemo.ncb 7.82MB
d3dApp.h 2KB
GfxStats.cpp 3KB
Chapter 8
TriGridDemo
d3dUtil.cpp 2KB
Vertex.h 865B
TriGridDemo.cpp 7KB
transform.fx 2KB
TriGridDemo.vcproj 5KB
TriGridDemo.vcproj.CRONUS-J1M2FQ5T.Cronus.user 1KB
TriGridDemo.sln 886B
GfxStats.h 1019B
d3dApp.cpp 11KB
TriGridDemo.suo 7KB
Vertex.cpp 786B
DirectInput.cpp 2KB
d3dUtil.h 2KB
TriGridDemo.ncb 8.34MB
DirectInput.h 1KB
d3dApp.h 2KB
GfxStats.cpp 3KB
MeshDemo
d3dUtil.cpp 2KB
MeshDemo.suo 7KB
Vertex.h 865B
MeshDemo.ncb 8.34MB
transform.fx 2KB
MeshDemo.vcproj.CRONUS-J1M2FQ5T.Cronus.user 1KB
GfxStats.h 1019B
d3dApp.cpp 11KB
Vertex.cpp 786B
DirectInput.cpp 2KB
d3dUtil.h 2KB
MeshDemo.cpp 9KB
DirectInput.h 1KB
MeshDemo.vcproj 5KB
MeshDemo.sln 880B
d3dApp.h 2KB
GfxStats.cpp 3KB
Chapter 7
CubeDemo
CubeDemo.ncb 8.3MB
Vertex.h 865B
GfxStats.h 1019B
d3dApp.cpp 11KB
Vertex.cpp 786B
CubeDemo.suo 7KB
CubeDemo.cpp 7KB
DirectInput.cpp 2KB
CubeDemo.vcproj 5KB
CubeDemo.sln 880B
d3dUtil.h 2KB
DirectInput.h 1KB
d3dApp.h 2KB
CubeDemo.vcproj.CRONUS-J1M2FQ5T.Cronus.user 1KB
GfxStats.cpp 3KB
共 99 条
- 1
资源评论
- yyseeking2011-11-07我在亚马逊上想买这本书,可是怎么都没有货,国外的好贵,你的资源太好了,整个网上就只有你上传了代码
- _大田2012-06-30这个代码不用下,只有4-8章,而且中间包含了很多垃圾文件. 不如下载上面那个第三部分(说是第三部分其实是第二部分,只有4-13章).
- 陳約翰2014-07-25代碼不全喔,不過還是謝謝了。
- knightzhuwei2014-02-24找了很多 这个至少确实有代码 多余文件删了就行了
liangneo
- 粉丝: 55
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功