directx9学习笔记之三:利用纹理,屏幕输出图片

/*
   directx9学习笔记之三:利用纹理,屏幕输出图片 源代码

   输出一张800*600的图片到屏幕。
*/
#include <stdio.h>
#include <windows.h>
#include "resource.h"//保护本地资源,程序图标
//保护d3d头文件和库文件
#include <d3d9.h>
#include <d3dx9.h>

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
//定义窗体类名称、窗体标题、窗体宽度和高度(这个窗体没有菜单,有菜单需要加上菜单高度hMenu=GetSystemMetrics(SM_CYMENU); )
#define WINDOW_CLASS "Agame"
#define WINDOW_NAME  "我的第一个游戏"
int WINDOW_WIDTH=800+GetSystemMetrics(SM_CXFRAME)*2;//设置窗体宽度=800(客户区宽度)+2个边框宽度
int WINDOW_HEIGHT=600+GetSystemMetrics(SM_CYCAPTION)+GetSystemMetrics(SM_CYFRAME)*2;//设置窗体高度=600(客户区高度)+标题栏高度+2个边框高度
#define WINDOW_TYPE WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX//层叠窗口、禁用最大化按钮

// 定义函数
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst,LPSTR cmdLine, int show);
bool InitializeD3D(HWND hWnd, bool fullscreen);
bool InitializeObjects();
void RenderScene();
void ReleaseD3D();


// 定义d3d接口、设备
LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice = NULL;

// 定义定点缓冲
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;
//定义纹理
LPDIRECT3DTEXTURE9 g_D3DTexture=NULL;

D3DXMATRIX g_projection;
D3DXMATRIX g_ViewMatrix;
// 定义顶点结构
struct stD3DVertex
{
    float x, y, z, rhw,u,v;//x,y,z坐标;rhw深度;u,v坐标
};

// 定义顶点格式标识符:x、y、z、深度、纹理
#define D3DFVF_VERTEX (D3DFVF_XYZRHW | D3DFVF_TEX1)

//消息处理函数
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
   switch(msg)
      {
         case WM_DESTROY:
            PostQuitMessage(0);
   ReleaseD3D();
            return 0;
            break;

         case WM_KEYUP:
            if(wp == VK_ESCAPE) PostQuitMessage(0);
            break;
      }

   return DefWindowProc(hWnd, msg, wp, lp);
}

//winmain函数
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst,
                   LPSTR cmdLine, int show)
{
   // 定义windows类,并赋值
 WNDCLASSEX wcex;

 wcex.cbSize=sizeof(wcex);
 wcex.style=CS_CLASSDC;
 wcex.lpfnWndProc=MsgProc;
 wcex.cbClsExtra=0;
 wcex.cbWndExtra=0;
 wcex.hInstance=hInst;
 wcex.hIcon=LoadIcon(hInst,MAKEINTRESOURCE(IDI_APP));
 wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
 wcex.hbrBackground=NULL;//(HBRUSH)(COLOR_WINDOW+1);
 wcex.lpszMenuName=NULL;
 wcex.lpszClassName=WINDOW_CLASS;
 wcex.hIconSm=LoadIcon(hInst,MAKEINTRESOURCE(IDI_APP));
   RegisterClassEx(&wcex);//注册windows类

   // 创建窗口
  

   HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME,WINDOW_TYPE, (GetSystemMetrics(SM_CXSCREEN)-800)/2,(GetSystemMetrics(SM_CYSCREEN)-600)/2,WINDOW_WIDTH, WINDOW_HEIGHT,NULL, NULL,hInst, NULL);

   // 如果初始化d3d成功,则显示窗口
   if(InitializeD3D(hWnd, false))
      {
         // 显示并更新窗口
         ShowWindow(hWnd, SW_NORMAL);
         UpdateWindow(hWnd);

         //进入消息循环
         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();
            }
      }

   // 释放d3d
   ReleaseD3D();

   // 取消windows类注册
   UnregisterClass(WINDOW_CLASS, wcex.hInstance);
   return 0;
}

//初始化d3d
bool InitializeD3D(HWND hWnd, bool fullscreen)
{
 // 设置显示参数、显示模式变量
   D3DPRESENT_PARAMETERS d3dpp;
   D3DDISPLAYMODE displayMode;

   // 创建d3d接口
   if((g_D3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
    return FALSE;


   // Get the desktop display mode.
   if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&displayMode)))
    return false;
 //D3DADAPTER_DEFAULT桌面窗口显示模式

  
  
   ZeroMemory(&d3dpp, sizeof(d3dpp));


   if(fullscreen)
   {
    d3dpp.Windowed = FALSE;
    d3dpp.BackBufferWidth = WINDOW_WIDTH;
    d3dpp.BackBufferHeight = WINDOW_HEIGHT;
   }
   else
   {
    d3dpp.Windowed = TRUE;
   }
   d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
   d3dpp.BackBufferFormat = displayMode.Format;
   // 创建d3d设备---D3DADAPTER_DEFAULT主显卡
   if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, hWnd,D3DCREATE_HARDWARE_VERTEXPROCESSING ,&d3dpp, &g_D3DDevice)))
    return false;


   // 输出D3DCREATE_SOFTWARE_VERTEXPROCESSING  D3DCREATE_HARDWARE_VERTEXPROCESSING
   if(!InitializeObjects()) return false;
   return true;
}

//
bool InitializeObjects()
{
 
   // 创建顶点数组
   //顶点数组:x坐标、y坐标、z坐标、深度、u坐标、v坐标---uv坐标设置错误也会导致图片不显示
   stD3DVertex objData[] =
      {
         { 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,0.0f},
         { 800.0f, 0.0f, 0.0f, 1.0f, 1.0f,0.0f},
         { 0.0f, 600.0f, 0.0f, 1.0f, 0.0f,1.0f},
         { 800.0f, 600.0f, 0.0f, 1.0f, 1.0f,1.0f}
      };

   // 创建顶点缓冲
   if(FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0,D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_VertexBuffer,NULL)))
    return false;

   // 填充顶点缓冲
   void *ptr;
   if(FAILED(g_VertexBuffer->Lock(0,sizeof(objData),(void**)&ptr, 0)))
    return false;

   memcpy(ptr, objData, sizeof(objData));

   g_VertexBuffer->Unlock();
   //从图片创建纹理
  
   if(D3DXCreateTextureFromFile(g_D3DDevice, "logo.jpg",&g_D3DTexture) != D3D_OK)
    return false;
 
   return true;
}

//渲染函数
void RenderScene()
{
#pragma region renderscene

    //清除d3d设备缓存.
 g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,D3DCOLOR_RGBA(0,0,0,255), 1.0f, 0);

   // 开始渲染
   g_D3DDevice->BeginScene();

      // 将顶点缓存设置为渲染流
      g_D3DDevice->SetStreamSource(0, g_VertexBuffer, 0,sizeof(stD3DVertex));
  
      g_D3DDevice->SetFVF(D3DFVF_VERTEX);//设置顶点渲染格式

   //设置纹理
   g_D3DDevice->SetTexture(0,g_D3DTexture);
      g_D3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);//绘制要渲染的图元
   /*
  D3DPT_POINTLIST 点
  D3DPT_LINELIST 线
  D3DPT_LINESTRIP 多条连续线段 1,2;2,3
  D3DPT_TRIANGLELIST 三角形
  D3DPT_TRIANGLESTRIP 多个三角形1,2,3;2,3,4 三角形带
  D3DPT_TRIANGLEFAN 1,2,3;1,3,4 三角形扇
   */
   g_D3DDevice->SetTexture(0,NULL);
   // 结束渲染
   g_D3DDevice->EndScene();

   // Display the scene.
   g_D3DDevice->Present(NULL, NULL, NULL, NULL);//在屏幕上显示渲染结果
   #pragma endregion renderscene
}


void ReleaseD3D()
{
#pragma region released3d

    if(g_D3DDevice != NULL) g_D3DDevice->Release();
    if(g_D3D != NULL) g_D3D->Release();
    if(g_VertexBuffer != NULL) g_VertexBuffer->Release();
 if (g_D3DTexture!=NULL) g_D3DTexture->Release();
 {
 }
  
    g_D3DDevice = NULL;
    g_D3D = NULL;
    g_VertexBuffer = NULL;
 g_D3DTexture=NULL;
#pragma endregion released3d
}

posted @ 2012-01-15 23:04  gwjtssy  阅读(1156)  评论(0编辑  收藏  举报