开发环境:VC++6.0 + DirectX8.0
首先创建一个Win32空工程DX8,新建一个CPP文件DX8,代码如下:
#pragma comment(lib,"d3d8.lib")
#pragma comment(lib,"d3dx8.lib")
LPDIRECT3D8 g_pD3D = NULL;
LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;
HRESULT InitialiseD3D(HWND hWnd)
{
// First of all, create the main D3D object. If it is created successfully we
// should get a pointer to an IDirect3D8 interface.
g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
if (g_pD3D == NULL)
{
return E_FAIL;
}
// Get the current display mode
D3DDISPLAYMODE d3ddm;
if (FAILED(g_pD3D -> GetAdapterDisplayMode(D3DADAPTER_DEFAULT, & d3ddm)))
{
return E_FAIL;
}
// Create a structure to hold the settings for our device
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( & d3dpp, sizeof (d3dpp));
// Fill the structure.
// We want our program to be windowed, and set the back buffer to a format
// that matches our current display mode
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
d3dpp.BackBufferFormat = d3ddm.Format;
// Create a Direct3D device.
if (FAILED(g_pD3D -> CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, & d3dpp,
& g_pD3DDevice)))
{
return E_FAIL;
}
return S_OK;
}
void Render()
{
if (g_pD3DDevice == NULL)
{
return ;
}
// Clear the back buffer to a green colour
g_pD3DDevice -> Clear( 0 , NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0 , 0 , 0 ), 1.0f , 0 );
// Begin the scene
g_pD3DDevice -> BeginScene();
// Rendering of our game objects will go here
// End the scene
g_pD3DDevice -> EndScene();
// Filp the back and front buffers so that whatever has been rendered on the
// back buffer will now be visible on screen (front buffer).
g_pD3DDevice -> Present(NULL, NULL, NULL, NULL);
}
void CleanUp()
{
if (g_pD3DDevice != NULL)
{
g_pD3DDevice -> Release();
g_pD3DDevice = NULL;
}
if (g_pD3D != NULL)
{
g_pD3D -> Release();
g_pD3D = NULL;
}
}
void GameLoop()
{
// Enter the game loop
MSG msg;
BOOL fMessage;
PeekMessage( & msg, NULL, 0U , 0U , PM_NOREMOVE);
while (msg.message != WM_QUIT)
{
fMessage = PeekMessage( & msg, NULL, 0U , 0U , PM_REMOVE);
if (fMessage)
{
// Process message
TranslateMessage( & msg);
DispatchMessage( & msg);
}
else
{
// No message to process, so render the current scene
Render();
}
}
}
// The windows message handler
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage( 0 );
return 0 ;
break ;
case WM_KEYUP:
switch (wParam)
{
case VK_ESCAPE:
// User has pressed the escape key, so quit
DestroyWindow(hWnd);
return 0 ;
break ;
}
break ;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
// Application entry point
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
{
// Register the window class
WNDCLASSEX wc = { sizeof (WNDCLASSEX), CS_CLASSDC, WinProc, 0L , 0L ,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
" DX Project 1 " , NULL};
RegisterClassEx( & wc);
// Create the application's window
HWND hWnd = CreateWindow( " DX Project 1 " , " www.andypike.com: Tutorial 1 " ,
WS_OVERLAPPEDWINDOW, 50 , 50 , 500 , 500 ,
GetDesktopWindow(), NULL, wc.hInstance, NULL);
// Initialize Direct3D
if (SUCCEEDED(InitialiseD3D(hWnd)))
{
// Show our window
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);
// Start game running: Enter the game loop
GameLoop();
}
CleanUp();
UnregisterClass( " DX Project 1 " , wc.hInstance);
return 0 ;
}
结果是一个简单的窗口,不过我竟然花了整整一天才调试出来。一方面是开发环境的问题(我在一台内存128M的烂机子上装了VC++6.0三次;VS2005二次;DirectX9三次;DirectX8一次。。。)一方面还是自己水平的问题——对微软的IDE极不熟悉(个人感觉VS系列的IDE友好性很差,而且VC语法晦涩,很难掌握,比Java难的不是一星半点~本人的Java还是有一定基础的)
下面是网上找到的解决方案:
Q:连接一个DX8工程时出现错误error LNK2001: unresolved external symbol _Direct3DCreate8@4
A:你搞错了DX sdk的版本了
A:D3DXSaveSurfaceToFile是dx8.1才支持的,8不支持
你需要找8.1的sdk去,8.0的不可以
Q:我用DX9 SDK替换掉DX8 SDK,还是不行
A:如果是DX9的话
#include "d3d9.h"
#include "d3dx9tex.h"
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
A:这两个头文件只是保证你那两个函数可以用.如果你还要建模,设置变换矩阵,灯光什么的,最好再加上
#include "d3dx9math.h"
A:DX9当然也是不行的
呵呵
这个就是Directx和openGL相比不好的地方
在DX sdk的各个版本之间接口函数写法差距很大
你没有办法把不同版本的代码原封不动的编译成功(个别除外)
dx8.1以后支持D3DXSaveSurfaceToFile这个函数
但是并不意味着,DX9当中对接口的用法和DX8.1的能够兼容的
你只有两种解决方案
1.找到DX8.1的sdk可以保证,你不用改代码直接编译成功
2.找DX9的sdk,找到相应的编译出问题的函数,按照DX9的sdk文档当中的描述进行修改
A:DX sdk每个版本之间在使用上都有差异,而且升级的速度比较快,基本上一年不到就是一个新版本出来了,谁也不知道微软会在新的dxsdk当中和上一个在使用方式上有多大的差异。这就要求你必须跟得上时代,而openGL就不同,从最开始到现在版本号码也不过是在小数点后面变化了几个位置而已,从1.0仅仅升级到了1.4而且基本上都是可以兼容的,1.0的代码在1.4版本上照用。初学者如果没有合适的指导人的话,还是学习openGL方便。至少在调试时候轻易不会重新启动电脑,网络上相应的学习资源也很多,尤其是在各个校园的内部网络。
Q:OK问题解决~