Lines演示程序

#include "stdafx.h"
#include "d3d9.h"
#include "d3dx9.h"

#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")

#define  WINDOW_CLASS "UGPDX"  //窗口类名称
#define  WINDOW_NAME "Template" //窗口类标题
#define  WINDOW_WIDTH 640
#define  WINDOW_HEIGHT 480

bool InitializeD3D(HWND hWnd,bool fullscreen);//用于在程序中设置和创建Direct3D
bool InitializeObjects();  //用于创建显示程序中要绘制在屏幕上的物体
void RenderScene();//用于在屏幕上渲染已经绘制好的图形
void Shutdown(); //用于在程序退出时进行一些销毁工作

//direct3D object and device
LPDIRECT3D9 g_D3D=NULL;
LPDIRECT3DDEVICE9 g_D3DDevice=NULL;

//Matrices
D3DXMATRIX g_projection;
D3DXMATRIX g_ViewMatrix;
D3DXMATRIX g_WorldMatrix;

//vertex buffer to hold the geometry
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer=NULL;//定义了一个顶点缓存对象

//A structure for our custom vertex type
//定义场景中单个3D点的结构
struct stD3Dvertex
{
   float x,y,z,rhw;//点的x,y,z坐标值
   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))
 {
       ShowWindow(hWnd,SW_SHOWDEFAULT);
    UpdateWindow(hWnd);

    //enter the message loop
    MSG msg;
    ZeroMemory(&msg,sizeof(msg));//宏用0来填充一块内存区域

       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();
   UnregisterClass("AppClass",wc.hInstance);//取消对窗口类的注册
      return 0;
}

//设置写入要绘制的场景数据
bool InitializeObjects()
{
 unsigned long col=D3DCOLOR_XRGB(255,255,255);//红,绿,蓝
 //Fill in our structure to draw an object
 //x,y,z,rhw,color
 //objData结构体数组:设置所有点的数据
 stD3Dvertex objData[]=
 {
  {420.0f,150.0f,0.5f,1.0f,col,},//第一个点的坐标及颜色
  {420.0f,350.0f,0.5f,1.0f,col,},//第二个点的坐标及颜色
  {220.0f,150.0f,0.5f,1.0f,col,},//第三个点的坐标及颜色
  {220.0f,350.0f,0.5f,1.0f,col,},//第四个点的坐标及颜色
 };
 
 //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 back buffer
 g_D3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,
  D3DCOLOR_XRGB(0,0,0),1.0f,0);
    //begin the scene start rendering
 g_D3DDevice->BeginScene();

 //render object
 //设置顶点数据流的输入源
 g_D3DDevice->SetStreamSource(0,g_VertexBuffer,0,
  sizeof(stD3Dvertex));
 g_D3DDevice->SetFVF(D3DFVF_VERTEX);//设置顶点格式
 g_D3DDevice->DrawPrimitive(D3DPT_LINELIST,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;
}
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 D3Device
 if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT,
  D3DDEVTYPE_HAL,hWnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,
  &d3dpp,&g_D3DDevice)))
  return false;

 //initialize any objects we will be displaying
 if(!InitializeObjects()) return false;
 return true;

}

 

posted @ 2013-07-26 15:54  露水上的青蛙  阅读(306)  评论(0编辑  收藏  举报