问一个 傻问题
控制台 程序 能 建立 D3D设备吗 也就是说 创建 Device 时 传入的 窗口句柄 是空的 能创建成功吗。
[解决办法]
控制台窗口也有句柄,不过空的传入是不可能创建D3D的
[解决办法]
另外,给你一个参考代码:
// ------------------------------------//§ File: Main.cpp//§ Desc: Main//§ //§ Date: 2011/3/11//§ Copyright(c) xychzh// ------------------------------------#include "stdafx.h"#include "DDSConvert.h"#include <iostream>using namespace std;struct SD3DINFO{public: SD3DINFO() : m_pD3D(NULL), m_pD3DDevice(NULL), m_bWindowed(true) {} ~SD3DINFO() { SAFE_RELEASE(m_pD3D); SAFE_RELEASE(m_pD3DDevice); } IDirect3D9* m_pD3D; // D3D9接口 IDirect3DDevice9* m_pD3DDevice; // D3D设备对象 D3DPRESENT_PARAMETERS* m_pD3DParam; // 指向当前使用的Param D3DPRESENT_PARAMETERS m_D3DParamWnd; // 窗口模式的Param D3DPRESENT_PARAMETERS m_D3DParamFull; // 全屏模式的Param D3DCAPS9 m_D3DCaps; // 设备信息 D3DCOLOR m_BackGroundColor; // 背景清除色 int m_RenderWidth; // 后台缓存宽度 int m_RenderHeight; // 后台缓存高度 HWND m_hWnd; // 窗口句柄 bool m_bNonePowOf2; // 显卡是否支持非2的次幂的图像 bool m_bWindowed; // 是否窗口化} g_D3DInfo;void InitD3D(HWND hWnd);int main(){ InitD3D(GetConsoleWindow()); system("pause"); return 0;}void InitD3D(HWND hWnd){ g_D3DInfo.m_hWnd = hWnd; g_D3DInfo.m_RenderWidth = 800; g_D3DInfo.m_RenderHeight = 600; g_D3DInfo.m_pD3D = Direct3DCreate9(D3D_SDK_VERSION); if (!g_D3DInfo.m_pD3D) return; D3DADAPTER_IDENTIFIER9 AdID; // 用于获得显卡信息 g_D3DInfo.m_pD3D->GetAdapterIdentifier(D3DADAPTER_DEFAULT, 0, &AdID); D3DDISPLAYMODE DisplayMode; if (FAILED(g_D3DInfo.m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &DisplayMode))) return; UINT nModes = 0; UINT i = 0; D3DFORMAT Formatfs = D3DFMT_UNKNOWN; // 获得显卡数量 nModes = g_D3DInfo.m_pD3D->GetAdapterModeCount(D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8); for(UINT i=0; i<nModes; i++) { g_D3DInfo.m_pD3D->EnumAdapterModes(D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8, i, &DisplayMode); if (DisplayMode.Width != (UINT)g_D3DInfo.m_RenderWidth || DisplayMode.Height != (UINT)g_D3DInfo.m_RenderHeight) continue; Formatfs = DisplayMode.Format; break; } if (Formatfs == D3DFMT_UNKNOWN) { if (!g_D3DInfo.m_bWindowed) return; } ZeroMemory(&g_D3DInfo.m_D3DParamWnd, sizeof(g_D3DInfo.m_D3DParamWnd)); g_D3DInfo.m_D3DParamWnd.BackBufferWidth = g_D3DInfo.m_RenderWidth; g_D3DInfo.m_D3DParamWnd.BackBufferHeight = g_D3DInfo.m_RenderHeight; g_D3DInfo.m_D3DParamWnd.BackBufferFormat = DisplayMode.Format; g_D3DInfo.m_D3DParamWnd.BackBufferCount = 1; g_D3DInfo.m_D3DParamWnd.MultiSampleType = D3DMULTISAMPLE_NONE; // D3DMULTISAMPLE_4_SAMPLES g_D3DInfo.m_D3DParamWnd.hDeviceWindow = hWnd; g_D3DInfo.m_D3DParamWnd.Windowed = TRUE; g_D3DInfo.m_D3DParamWnd.SwapEffect = D3DSWAPEFFECT_DISCARD; g_D3DInfo.m_D3DParamWnd.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;//D3DPRESENT_INTERVAL_DEFAULT; ZeroMemory(&g_D3DInfo.m_D3DParamFull, sizeof(g_D3DInfo.m_D3DParamFull)); g_D3DInfo.m_D3DParamFull.BackBufferWidth = g_D3DInfo.m_RenderWidth; g_D3DInfo.m_D3DParamFull.BackBufferHeight = g_D3DInfo.m_RenderHeight; g_D3DInfo.m_D3DParamFull.BackBufferFormat = Formatfs; g_D3DInfo.m_D3DParamFull.BackBufferCount = 1; g_D3DInfo.m_D3DParamFull.MultiSampleType = D3DMULTISAMPLE_NONE; // D3DMULTISAMPLE_4_SAMPLES g_D3DInfo.m_D3DParamFull.hDeviceWindow = hWnd; g_D3DInfo.m_D3DParamFull.Windowed = FALSE; g_D3DInfo.m_D3DParamFull.SwapEffect = D3DSWAPEFFECT_DISCARD; g_D3DInfo.m_D3DParamFull.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; g_D3DInfo.m_D3DParamFull.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; if (g_D3DInfo.m_bWindowed) { g_D3DInfo.m_pD3DParam = &g_D3DInfo.m_D3DParamWnd; } else { g_D3DInfo.m_pD3DParam = &g_D3DInfo.m_D3DParamFull; } ZeroMemory(&g_D3DInfo.m_D3DCaps, sizeof(g_D3DInfo.m_D3DCaps)); if (FAILED(g_D3DInfo.m_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &g_D3DInfo.m_D3DCaps))) return; // 显卡是否支持非2次幂大小的平面纹理 g_D3DInfo.m_bNonePowOf2 = (g_D3DInfo.m_D3DCaps.TextureCaps & D3DPTEXTURECAPS_POW2)? false : true; HRESULT hr; if (g_D3DInfo.m_D3DCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) { // 创建硬件顶点运算 hr = g_D3DInfo.m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, g_D3DInfo.m_pD3DParam, &g_D3DInfo.m_pD3DDevice); } else { // 创建软件顶点运算 hr = g_D3DInfo.m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, g_D3DInfo.m_pD3DParam, &g_D3DInfo.m_pD3DDevice); } if (FAILED(hr)) { return; }}
[解决办法]
DX9不可以,DX10和11可以。