//
#include"mrWindow.h"
//
#include<d3d8.h>
#include<math.h>
class D3DWindow:public mrWindow
{
//Direct3D 接口
LPDIRECT3D8 m_pD3D;
LPDIRECT3DDEVICE8 m_pD3DDevice;
//Direct3D表明接口
//LPDIRECT3DSURFACE8 m_pD3DSurface;
//Direct3D当前格式
//mrUInt32 m_iD3DFormat;
public:
//
D3DWindow(void){};
~D3DWindow(void){};
//初始化和关闭Direct3D
HRESULT SetupDirect3D(void);
HRESULT KillDirect3D(void);
//
mrBool32 Frame(void);
};
//初始化Direct3D
HRESULT D3DWindow::SetupDirect3D(void)
{
//创建Direct3D对象,Direct3DCreate8返回指向IDirect3D(LPDIRECT3D8)接口的指针
if(NULL == (m_pD3D = Direct3DCreate8(D3D_SDK_VERSION)))
{
return E_FAIL;//Direct3D定义的用来描述一个未确定错误的值
}
//为获得色深信息需要获取当前显示
D3DDISPLAYMODE d3ddm;
if(FAILED(m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm)))
{
return E_FAIL;
}
//填充显示参数
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp,sizeof(d3dpp)); //把指定缓冲区置0
//采用窗口模式(即不是全屏)
d3dpp.Windowed = TRUE;
//翻转缓冲区时不改变后备缓冲区
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
//设置的格式与当前格式相同
//调用m_pD3D->GetAdapterDisplayMode方法得到当前格式
d3dpp.BackBufferFormat = d3ddm.Format;
//创建设备
if(FAILED(m_pD3D ->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,m_hWindow,D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,&m_pD3DDevice)))
{
return E_FAIL;
}
/*/创建表面
if(FAILED(m_pD3DDevice->CreateImageSurface(256,256,(D3DFORMAT)m_iD3DFormat,&m_pD3DSurface)))
{
return E_FAIL;
}
//锁定表面
D3DLOCKED_RECT kLockedRect;
m_pD3DSurface->LockRect(&kLockedRect,NULL,0);
//将指针强制类型转换,以使指针指向首像素
DWORD *piSurfaceBuffer = (DWORD *)kLockedRect.pBits;
//填充表面
int iX,iY;
for(iY = 0;iY < 256;iY++)
{
for(iX = 0;iX < 256;iX++)
{
int iPower = (int)(sin(iY * iX) * 128 + cos(iY * - iX) * 128);
piSurfaceBuffer[iX + iY * (kLockedRect.Pitch >>2)] = D3DCOLOR_XRGB(iPower,iPower,iPower);
}
}
//解锁表面
m_pD3DSurface->UnlockRect();
*/ return D3D_OK;
}
HRESULT D3DWindow::KillDirect3D(void)
{
//只要存在Direct3D对象,就将其释放
if(NULL == m_pD3D)
{
m_pD3D->Release();
}
if(NULL != m_pD3DDevice)
{
m_pD3DDevice->Release();
}
//if(NULL != m_pD3DSurface)
//{
//m_pD3DSurface->Release();
//} return D3D_OK;
}
//将屏幕设置为蓝色
mrBool32 D3DWindow::Frame(void)
{
//将窗口设置为蓝色
m_pD3DDevice ->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0);
//得到后备缓冲区
//LPDIRECT3DSURFACE8 pBackBuffer;
//m_pD3DDevice->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&pBackBuffer);
//开始渲染
m_pD3DDevice->BeginScene();
//将表面拷贝到屏幕
//m_pD3DDevice->CopyRects(m_pD3DSurface,NULL,0,pBackBuffer,NULL); //结束渲染
m_pD3DDevice->EndScene();
//释放后背缓冲
//pBackBuffer->Release(); //将渲染的场景显示到屏幕上
m_pD3DDevice->Present(NULL,NULL,NULL,NULL);
return mrTrue;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
D3DWindow kWindow;
kWindow.Create(hInstance,"D3D Demo");
//设置DirectX3D
kWindow.SetupDirect3D();
//进入消息循环
kWindow.Run();
kWindow.KillDirect3D();
return 0;
}
这个程序是在先在已有框架的文件下修改的。
代码里没有红色的是把背景色设为蓝色,但是添加了红色的代码后,在运行的时候提示内存不能为”Read“,找不出是那里出问题,诚心希望高手帮下忙。
谢谢了!
经过不同工具调试发现//m_pD3DDevice->CopyRects(m_pD3DSurface,NULL,0,pBackBuffer,NULL);这个位置的m_pD3DSurface的地址是0x00000000
都告诉你m_pD3DSurface的地址为0,这不就是问题了,继续调一下m_pD3DSurface被创建的地方是否出错了