博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
5. Quad
阅读量:6758 次
发布时间:2019-06-26

本文共 4728 字,大约阅读时间需要 15 分钟。

#include
#define WINDOW_CLASS "UGPDX" #define WINDOW_NAME "Drawing a Quad" // Function Prototypes... bool InitializeD3D(HWND hWnd, bool fullscreen); bool InitializeObjects(); void RenderScene(); void Shutdown(); // Direct3D object and device. LPDIRECT3D9 g_D3D = NULL; LPDIRECT3DDEVICE9 g_D3DDevice = NULL; // Vertex buffer to hold the geometry. LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL; // A structure for our custom vertex type struct stD3DVertex {
float x, y, z, rhw; unsigned long color; }; // Our custom FVF, which describes our custom vertex structure #define D3DFVF_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE) LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_DESTROY: PostQuitMessage(0); return 0; break; case WM_KEYUP: if(wParam == VK_ESCAPE) PostQuitMessage(0); break; } return DefWindowProc(hWnd, msg, wParam, lParam); } int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst, LPSTR cmdLine, int show) {
// Register the window class WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, WINDOW_CLASS, NULL }; RegisterClassEx(&wc); // Create the application's window HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW, 100, 100, 640, 480, GetDesktopWindow(), NULL, wc.hInstance, NULL); // Initialize Direct3D if(InitializeD3D(hWnd, false)) {
// Show the window ShowWindow(hWnd, SW_SHOWDEFAULT); UpdateWindow(hWnd); // Enter the message loop MSG msg; ZeroMemory(&msg, sizeof(msg)); while(msg.message != WM_QUIT) {
if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {
TranslateMessage(&msg); DispatchMessage(&msg); } else RenderScene(); } } // Release any and all resources. Shutdown(); // Unregister our window. UnregisterClass(WINDOW_CLASS, wc.hInstance); return 0; } bool InitializeD3D(HWND hWnd, bool fullscreen) {
D3DDISPLAYMODE displayMode; // Create the D3D object. g_D3D = Direct3DCreate9(D3D_SDK_VERSION); if(g_D3D == NULL) return false; // Get the desktop display mode. if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode))) return false; // Set up the structure used to create the D3DDevice D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); if(fullscreen) {
d3dpp.Windowed = FALSE; d3dpp.BackBufferWidth = 640; d3dpp.BackBufferHeight = 480; } else d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = displayMode.Format; // Create the D3DDevice if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_D3DDevice))) {
return false; } // Initialize any objects we will be displaying. if(!InitializeObjects()) return false; return true; } bool InitializeObjects() {
// Fill in our structure to draw an object. // x, y, z, rhw, color. stD3DVertex objData[] = {
{ 420, 150, 0.5f, 1, D3DCOLOR_XRGB(255, 255, 255), }, { 420, 350, 0.5f, 1, D3DCOLOR_XRGB(255, 255, 255), }, { 220, 150, 0.5f, 1, D3DCOLOR_XRGB(255, 255, 255), }, { 220, 350, 0.5f, 1, D3DCOLOR_XRGB(255, 255, 255), }, }; // Create the vertex buffer. if(FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0, D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_VertexBuffer, NULL))) return false; // Fill the vertex buffer. void *ptr; if(FAILED(g_VertexBuffer->Lock(0, sizeof(objData), (void**)&ptr, 0))) return false; memcpy(ptr, objData, sizeof(objData)); g_VertexBuffer->Unlock(); return true; } void RenderScene() {
// Clear the backbuffer. g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0); // Begin the scene. Start rendering. g_D3DDevice->BeginScene(); // Render quad. g_D3DDevice->SetStreamSource(0, g_VertexBuffer, 0, sizeof(stD3DVertex)); g_D3DDevice->SetFVF(D3DFVF_VERTEX); g_D3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); // End the scene. Stop rendering. g_D3DDevice->EndScene(); // Display the scene. g_D3DDevice->Present(NULL, NULL, NULL, NULL); } void Shutdown() {
if(g_D3DDevice != NULL) g_D3DDevice->Release(); if(g_D3D != NULL) g_D3D->Release(); if(g_VertexBuffer != NULL) g_VertexBuffer->Release(); g_D3DDevice = NULL; g_D3D = NULL; g_VertexBuffer = NULL; }

  

转载地址:http://gnweo.baihongyu.com/

你可能感兴趣的文章
Linux shell编程学习笔记-----第七章
查看>>
oracle 查询char类型的数据
查看>>
Vue项目碰到"‘webpack-dev-server’不是内部或外部命令,也不是可运行的程序或批处理文件"报错...
查看>>
Android zxing扫描二维码 为什么有些机型扫描不出来或者很慢?
查看>>
SQLHelp sql数据库的DAL
查看>>
Java集合--LinkedList
查看>>
进阶第二课 Python内置函数(补)及自定义函数
查看>>
Spell It Right
查看>>
Spring AOP术语解释
查看>>
(一)通过JAVA连接SAP (sapjco3.jar在Windows和MacOS上的配置)
查看>>
《王者荣耀》的英雄是怎么诞生的?有没有最厉害的英雄?
查看>>
公司常用几种请求
查看>>
python3 字符串格式化
查看>>
一个字符在字符串中出现最多的次数的打印
查看>>
图片的三级缓存
查看>>
js跨域问题解决方案
查看>>
(八)统一配置中心-Config
查看>>
I.MX6 Android CAN 命令行测试
查看>>
linux shell except tcl login ssh Automatic interaction
查看>>
iOS JSONModel解析数据成Model
查看>>