永发信息网

DirectX 编程

答案:1  悬赏:40  手机版
解决时间 2021-04-29 10:36

#include <d3d8.h>
#include <windows.h>


LPDIRECT3D8 g_pD3D = NULL;
LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;
LPDIRECT3DVERTEXBUFFER8 g_pVertexBuffer = NULL; // Buffer to hold vertices

struct MCONOUTPOINT{
FLOAT x,y;
DWORD color;
};

#define D3DFVF_MCONOUTPOINT (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

#define SafeRelease(pObject) if(pObject != NULL) {pObject->Release(); pObject=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;
}

HRESULT InitialiseVertexBuffer()
{
VOID* pVertices;

//Store each point of the triangle together with it's colour
MCONOUTPOINT cvVertices[] =
{
//Vertex 1 - Red (250, 100)
{250.0f, 100.0f, D3DCOLOR_XRGB(255, 0, 0),},

//Vertex 2 - Green (400, 350)
{400.0f, 350.0f, D3DCOLOR_XRGB(0, 255, 0),},

//Vertex 3 - Blue (100, 350)
{100.0f, 350.0f, D3DCOLOR_XRGB(0, 0, 255),},
};

//Create the vertex buffer from our device
if(FAILED(g_pD3DDevice->CreateVertexBuffer(3 * sizeof(MCONOUTPOINT),
0, D3DFVF_MCONOUTPOINT,
D3DPOOL_DEFAULT, &g_pVertexBuffer)))
{
return E_FAIL;
}

//Get a pointer to the vertex buffer vertices and lock the vertex buffer
if(FAILED(g_pVertexBuffer->Lock(0, sizeof(cvVertices), (BYTE**)&pVertices, 0)))
{
return E_FAIL;
}

//Copy our stored vertices values into the vertex buffer
memcpy(pVertices, cvVertices, sizeof(cvVertices));

//Unlock the vertex buffer
g_pVertexBuffer->Unlock();

return S_OK;
}

void Render()
{
if(g_pD3DDevice == NULL)
{
return;
}

//Clear the backbuffer to black
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

//Begin the scene
g_pD3DDevice->BeginScene();

//Rendering our point
g_pD3DDevice->SetStreamSource(0, g_pVertexBuffer, sizeof(MCONOUTPOINT));
g_pD3DDevice->SetVertexShader(D3DFVF_MCONOUTPOINT);
g_pD3DDevice->DrawPrimitive(D3DPT_POINTLIST, 0, 1);


//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()
{
SafeRelease(g_pVertexBuffer);
SafeRelease(g_pD3DDevice);
SafeRelease(g_pD3D);
}

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 2", NULL};
RegisterClassEx(&wc);

//Create the application's window
HWND hWnd = CreateWindow("DX Project 2", "...",
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);

//Initialize Vertex Buffer
if(SUCCEEDED(InitialiseVertexBuffer()))
{
//Start game running: Enter the game loop
GameLoop();
}
}

CleanUp();

UnregisterClass("DX Project 2", wc.hInstance);

return 0;
}

我的目的是在窗口上显示三个颜色分别是R、G、B的点,但是编译过后却是:

问题出在哪?

最佳答案
首先你好象得清屏吧...
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
本机能ping通内网服务器,却登不上服务器系统
从装电脑系统
飞车为什么抽到的都是垃圾、
手臂很酸,怎么做才不酸
把手伸进潮湿的粮食堆里为什么会有热乎乎的感
初学者戴美瞳,求经验
九加一是什么意思,没搞明白
三GQQ的情侣空间一但建立电脑端的个人空间是
篮网下一场将对阵哪个球队?
摩尔庄园的鬼屋怎么弄?
基金定投 终止定投还是持有吗?
中国达人秀邓波儿出场的时间放的什么歌
八公山路/淠望中路(路口)地址在哪,我要去那
东西摧毁后还可统一得到吗
电脑开机读内存然后叫两短声,就今不了系统!
推荐资讯
哪位大哥有关于c#入门的基础性书籍?
金牛男和双子女能坚持多久
哪里有最新的3D贴图和模型购买
黔西大酒店我想知道这个在什么地方
女生为何吸烟?
摄像头驱动安装
我是1989年6月15日出生的,请占卜一下我的爱情
邓家大桥我想知道这个在什么地方
is里一个人为什么能带好多小号?
表示助人为乐的诗句,有关助人为乐的名言警句
QQ牧场可生产的动物怎么卖不了?
为什么用银行卡开会员开不了啊
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?