在Mobile上使用Direct3D

好久以前写的,想做个比较漂亮的主界面,但后来因为用D3D太卡,所以只学了一点,后来就没再学

就是显示一个立方体,按方向键可以旋转

main.bmp就是那个唯一的资源(拿SDK里面的D3D的例子改的)

 

直接贴代码了:

  1. #pragma comment(linker, "/nodefaultlib:oldnames.lib")
  2. #include <windows.h>
  3. #include <d3dm.h>
  4. #include <d3dmx.h>
  5. #include <aygshell.h>
  6. #include <d3dmtypes.h>
  7. #define IDB_BANANA                     101
  8. //-----------------------------------------------------------------------------
  9. // Global variables
  10. //-----------------------------------------------------------------------------
  11. LPDIRECT3DMOBILE        g_pD3DM       = NULL;  // Used to create the D3DMDevice
  12. LPDIRECT3DMOBILEDEVICE  g_pd3dmDevice = NULL;  // Our rendering device
  13. HMODULE                 g_hRefDLL     = NULL;  // DLL handle for d3dmref.dll
  14. bool                    g_bUseRef     = false// Flag denoting use of d3dmref
  15. LPDIRECT3DMOBILEVERTEXBUFFER g_pVB    = NULL; // Buffer to hold vertices
  16. LPDIRECT3DMOBILETEXTURE     g_pTexture   = NULL;
  17. // A structure for our custom vertex type
  18. struct CUSTOMVERTEX
  19. {
  20.     FLOAT x, y, z; // The transformed position for the vertex
  21.     DWORD color;        // The vertex color
  22.     FLOAT tu,tv;        //texture point
  23. };
  24. // Our custom FVF, which describes our custom vertex structure
  25. #define D3DMFVF_CUSTOMVERTEX (D3DMFVF_XYZ_FLOAT | D3DMFVF_DIFFUSE | D3DMFVF_TEX1 | D3DMFVF_TEXCOORDSIZE2(0) | D3DMFVF_TEXCOORDFLOAT(0))
  26. //-----------------------------------------------------------------------------
  27. // Name: IsScreenRotated()
  28. // Desc: Currently the D3DM runtime does not support a rotated screen. If the 
  29. //       screen is rotated, we should inform the user this is not supported.
  30. //       If this routing returns TRUE the caller should cleanup and exit the application
  31. //-----------------------------------------------------------------------------
  32. BOOL IsScreenRotated()
  33. {
  34.     DEVMODE devMode  = {0};
  35.     devMode.dmSize   = sizeof(DEVMODE);
  36.     devMode.dmFields = DM_DISPLAYORIENTATION;
  37.     ChangeDisplaySettingsEx(NULL, &devMode, NULL, CDS_TEST, NULL);
  38.     if (devMode.dmDisplayOrientation != DMDO_0)
  39.     {
  40.         MessageBox(
  41.             NULL, 
  42.             TEXT("This D3DM sample will not work on a rotated screen./nThe application will now exit."),
  43.             TEXT("Notice"),
  44.             MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND
  45.             );
  46.         return TRUE;
  47.     }
  48.     return FALSE;
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Name: InitD3DM()
  52. // Desc: Initializes Direct3D Mobile
  53. //-----------------------------------------------------------------------------
  54. HRESULT InitD3DM( HWND hWnd )
  55. {
  56.     // Create the D3DM object, which is needed to create the D3DMDevice.
  57.     if( NULL == ( g_pD3DM = Direct3DMobileCreate( D3DM_SDK_VERSION ) ) )
  58.         return E_FAIL;
  59.     // Set up the structure used to create the D3DMDevice. Most parameters are
  60.     // zeroed out. We set Windowed to TRUE, since we want to do D3DM in a
  61.     // window, and then set the SwapEffect to "discard", which is the most
  62.     // efficient method of presenting the back buffer to the display.  And 
  63.     // we request a back buffer format that matches the current desktop display 
  64.     // format.
  65.     D3DMPRESENT_PARAMETERS d3dmpp; 
  66.     memset( &d3dmpp, 0, sizeof(d3dmpp) );
  67.     d3dmpp.Windowed = TRUE;
  68.     d3dmpp.SwapEffect = D3DMSWAPEFFECT_DISCARD;
  69.     d3dmpp.BackBufferFormat = D3DMFMT_UNKNOWN;
  70.     d3dmpp.EnableAutoDepthStencil = TRUE;
  71.     d3dmpp.AutoDepthStencilFormat = D3DMFMT_D16;
  72.     // Create the Direct3D Mobile device.
  73.     UINT uAdapter;
  74.     
  75.     if (g_bUseRef)
  76.     {
  77.         // Load the D3DM reference driver DLL
  78.         g_hRefDLL = (HMODULE)LoadLibrary(TEXT("d3dmref.dll"));
  79.         if (NULL == g_hRefDLL)
  80.         {
  81.             OutputDebugString(TEXT("Unable to load D3DM reference driver DLL./n"));
  82.             return E_FAIL;
  83.         }
  84.         // Get the reference driver's entry point
  85.         void* pfnD3DMInit = GetProcAddress(g_hRefDLL, TEXT("D3DM_Initialize"));   
  86.         if( NULL == pfnD3DMInit )
  87.         {
  88.             OutputDebugString(TEXT("Unable to retrieve D3DM reference driver entry point./n"));
  89.             return E_FAIL;
  90.         }
  91.         // Register the software device
  92.         if( FAILED( g_pD3DM->RegisterSoftwareDevice(pfnD3DMInit) ) )
  93.         {
  94.             OutputDebugString(TEXT("Unable to register D3DM reference driver./n"));
  95.             return E_FAIL;
  96.         }
  97.         
  98.         uAdapter = D3DMADAPTER_REGISTERED_DEVICE;
  99.     }
  100.     else
  101.     {
  102.         // Use the default system D3DM driver    
  103.         uAdapter = D3DMADAPTER_DEFAULT;
  104.     }
  105.     if( FAILED( g_pD3DM->CreateDevice( uAdapter, 
  106.                                     D3DMDEVTYPE_DEFAULT,
  107.                                     hWnd, 0,
  108.                                     &d3dmpp, &g_pd3dmDevice ) ) )
  109.     {
  110.         OutputDebugString(TEXT("Unable to create a D3DM device./n"));
  111.         return E_FAIL;
  112.     }
  113.     
  114.     // Device state would normally be set here
  115.     
  116.     // Turn off culling
  117.     g_pd3dmDevice->SetRenderState( D3DMRS_CULLMODE, D3DMCULL_NONE );
  118.     // Turn off D3D lighting
  119.     g_pd3dmDevice->SetRenderState( D3DMRS_LIGHTING, FALSE );
  120.     // Turn on the zbuffer
  121.     g_pd3dmDevice->SetRenderState( D3DMRS_ZENABLE, TRUE );
  122.     return S_OK;
  123. }
  124. //-----------------------------------------------------------------------------
  125. // Name: InitVB()
  126. // Desc: Creates a vertex buffer and fills it with our vertices. The vertex
  127. //       buffer is basically just a chuck of memory that holds vertices. After
  128. //       creating it, we must Lock()/Unlock() it to fill it. For indices, D3DM
  129. //       also uses index buffers. 
  130. //-----------------------------------------------------------------------------
  131. HRESULT InitVB()
  132. {
  133.     // Initialize three vertices for rendering a triangle
  134.     CUSTOMVERTEX vertices[] =
  135.     {
  136.         {-1.0f, -1.0f, -1.0f, 0xff000000, 0.0f, 0.5f},      //0
  137.         {1.0f, -1.0f, -1.0f, 0xff000000, 1.0f/3,0.5f},      //1
  138.         {-1.0f, 1.0f, -1.0f, 0xff000000, 0.0f, 0.0f},       //3
  139.         {1.0f, -1.0f, -1.0f, 0xff000000, 1.0f/3,0.5f},      //1
  140.         {-1.0f, 1.0f, -1.0f, 0xff000000, 0.0f, 0.0f},       //3
  141.         {1.0f, 1.0f, -1.0f, 0xff000000, 1.0f/3, 0.0f},      //2
  142.         {-1.0f, 1.0f, -1.0f, 0xff000000, 0.0f, 1.0f},       //3
  143.         {1.0f, 1.0f, -1.0f, 0xff000000, 1.0f/3, 1.0f},      //2
  144.         {-1.0f, 1.0f, 1.0f, 0xff000000, 0.0f, 0.5f},        //4
  145.         {1.0f, 1.0f, -1.0f, 0xff000000, 1.0f/3, 1.0f},      //2
  146.         {-1.0f, 1.0f, 1.0f, 0xff000000, 0.0f, 0.5f},        //4
  147.         {1.0f, 1.0f, 1.0f, 0xff000000, 1.0f/3, 0.5f},       //5
  148.         {-1.0f, 1.0f, 1.0f, 0xff000000, 1.0f/3, 0.5f},      //4
  149.         {1.0f, 1.0f, 1.0f, 0xff000000, 2.0f/3, 0.5f},       //5
  150.         {-1.0f, -1.0f, 1.0f, 0xff000000, 1.0f/3, 0.0f},     //7
  151.         {1.0f, 1.0f, 1.0f, 0xff000000, 2.0f/3, 0.5f},       //5
  152.         {-1.0f, -1.0f, 1.0f, 0xff000000, 1.0f/3, 0.0f},     //7
  153.         {1.0f, -1.0f, 1.0f, 0xff000000, 2.0f/3, 0.0f},      //6
  154.         {-1.0f, -1.0f, 1.0f, 0xff000000, 1.0f/3, 0.5f},     //7
  155.         {1.0f, -1.0f, 1.0f, 0xff000000, 2.0f/3, 1.0f},      //6
  156.         {-1.0f, -1.0f, -1.0f, 0xff000000, 1.0f/3, 0.5f},    //0
  157.         
  158.         {-1.0f, -1.0f, -1.0f, 0xff000000, 1.0f/3, 0.5f},    //0
  159.         {1.0f, -1.0f, -1.0f, 0xff000000, 2.0f/3,0.5f},      //1
  160.         {1.0f, -1.0f, 1.0f, 0xff000000, 2.0f/3, 1.0f},      //6
  161.         {1.0f, -1.0f, -1.0f, 0xff000000, 2.0f/3,0.5f},      //1
  162.         {1.0f, 1.0f, -1.0f, 0xff000000, 2.0f/3, 0.0f},      //2
  163.         {1.0f, -1.0f, 1.0f, 0xff000000, 1.0f, 0.5f},        //6
  164.         {1.0f, 1.0f, -1.0f, 0xff000000, 2.0f/3, 0.0f},      //2
  165.         {1.0f, -1.0f, 1.0f, 0xff000000, 1.0f, 0.5f},        //6
  166.         {1.0f, 1.0f, 1.0f, 0xff000000, 1.0f, 0.0f},         //5
  167.         {-1.0f, -1.0f, 1.0f, 0xff000000, 2.0f/3, 1.0f},     //7
  168.         {-1.0f, 1.0f, 1.0f, 0xff000000, 2.0f/3, 0.5f},      //4
  169.         {-1.0f, -1.0f, -1.0f, 0xff000000, 1.0f, 1.0f},      //0
  170.         {-1.0f, 1.0f, 1.0f, 0xff000000, 2.0f/3, 0.5f},      //4
  171.         {-1.0f, -1.0f, -1.0f, 0xff000000, 1.0f, 1.0f},      //0
  172.         {-1.0f, 1.0f, -1.0f, 0xff000000, 1.0f, 0.5f},       //3
  173.     };
  174.     // Determine if the device can create vertex buffers in video memory
  175.     // by testing the device caps bits.
  176.     D3DMCAPS caps;
  177.     if( FAILED( g_pd3dmDevice->GetDeviceCaps(&caps) ) )
  178.     {
  179.         return E_FAIL;
  180.     }
  181.     D3DMPOOL texpool;
  182.     if (caps.SurfaceCaps & D3DMSURFCAPS_VIDTEXTURE)
  183.     {
  184.         texpool = D3DMPOOL_VIDEOMEM;
  185.     }
  186.     else
  187.     {
  188.         texpool = D3DMPOOL_SYSTEMMEM;
  189.     }
  190.     // Load the texture map resource (banana.bmp)
  191.      if( FAILED( D3DMXCreateTextureFromResourceEx( g_pd3dmDevice, GetModuleHandle(NULL), 
  192.         MAKEINTRESOURCE(IDB_BANANA), D3DMX_DEFAULT, D3DMX_DEFAULT, 1, 0, 
  193.         D3DMFMT_UNKNOWN, texpool, D3DMX_FILTER_POINT, D3DMX_FILTER_POINT, 
  194.         0, NULL, NULL, &g_pTexture ) ) )
  195.     {
  196.         return E_FAIL;
  197.     }      
  198.     D3DMPOOL pool;
  199.     if (caps.SurfaceCaps & D3DMSURFCAPS_VIDVERTEXBUFFER)
  200.     {
  201.         pool = D3DMPOOL_VIDEOMEM;
  202.     }
  203.     else
  204.     {
  205.         pool = D3DMPOOL_SYSTEMMEM;
  206.     }        
  207.     // Create the vertex buffer. Here we are allocating enough memory
  208.     // (from the default pool) to hold all our 3 custom vertices. We also
  209.     // specify the FVF, so the vertex buffer knows what data it contains.
  210.     if( FAILED( g_pd3dmDevice->CreateVertexBuffer( 36*sizeof(CUSTOMVERTEX),
  211.                                                   0, D3DMFVF_CUSTOMVERTEX,
  212.                                                   pool, &g_pVB ) ) )
  213.     {
  214.         return E_FAIL;
  215.     }
  216.     // Now we fill the vertex buffer. To do this, we need to Lock() the VB to
  217.     // gain access to the vertices. This mechanism is required becuase vertex
  218.     // buffers may be in device memory.
  219.     void* pVertices;
  220.     if( FAILED( g_pVB->Lock( 0, sizeof(vertices), &pVertices, 0 ) ) )
  221.         return E_FAIL;
  222.     memcpy( pVertices, vertices, sizeof(vertices) );
  223.     g_pVB->Unlock();
  224.     return S_OK;
  225. }
  226. //-----------------------------------------------------------------------------
  227. // Name: Cleanup()
  228. // Desc: Releases all previously initialized objects
  229. //-----------------------------------------------------------------------------
  230. VOID Cleanup()
  231. {
  232.     if( g_pVB != NULL )        
  233.         g_pVB->Release();
  234.     if( g_pd3dmDevice != NULL) 
  235.         g_pd3dmDevice->Release();
  236.     if( g_pD3DM != NULL)
  237.     {
  238.         if (g_hRefDLL)
  239.         {
  240.             g_pD3DM->RegisterSoftwareDevice(NULL);
  241.             FreeLibrary(g_hRefDLL);
  242.         }
  243.     
  244.         g_pD3DM->Release();
  245.     }
  246.     if (g_pTexture != NULL)
  247.         g_pTexture->Release();
  248. }
  249. VOID SetupMatrices()
  250. {
  251.     // For our world matrix, we will just leave it as the identity
  252.     D3DMXMATRIX matWorld;
  253.     D3DMXMatrixIdentity( &matWorld );
  254.     D3DMXMatrixRotationY( &matWorld, GetTickCount()/5000.0f );
  255.     g_pd3dmDevice->SetTransform( D3DMTS_WORLD, (D3DMMATRIX*)&matWorld, D3DMFMT_D3DMVALUE_FLOAT );
  256.     // Set up our view matrix. A view matrix can be defined given an eye point,
  257.     // a point to lookat, and a direction for which way is up. Here, we set the
  258.     // eye five units back along the z-axis and up three units, look at the
  259.     // origin, and define "up" to be in the y-direction.
  260.     D3DMXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );
  261.     D3DMXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
  262.     D3DMXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
  263.     D3DMXMATRIX matView;
  264.     D3DMXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
  265.     g_pd3dmDevice->SetTransform( D3DMTS_VIEW, (D3DMMATRIX*)&matView, D3DMFMT_D3DMVALUE_FLOAT );
  266.     // For the projection matrix, we set up a perspective transform (which
  267.     // transforms geometry from 3D view space to 2D viewport space, with
  268.     // a perspective divide making objects smaller in the distance). To build
  269.     // a perpsective transform, we need the field of view (1/4 pi is common),
  270.     // the aspect ratio, and the near and far clipping planes (which define at
  271.     // what distances geometry should be no longer be rendered).
  272.     D3DMXMATRIX matProj;
  273.     D3DMXMatrixPerspectiveFovLH( &matProj, D3DMX_PI/4.0f, 1.0f, 1.0f, 100.0f );
  274.     g_pd3dmDevice->SetTransform( D3DMTS_PROJECTION, (D3DMMATRIX*)&matProj, D3DMFMT_D3DMVALUE_FLOAT );
  275. }
  276. //-----------------------------------------------------------------------------
  277. // Name: Render()
  278. // Desc: Draws the scene
  279. //-----------------------------------------------------------------------------
  280. VOID Render()
  281. {
  282.     if( NULL == g_pd3dmDevice )
  283.         return;
  284.     // Clear the backbuffer to a blue color
  285.     g_pd3dmDevice->Clear( 0, NULL, D3DMCLEAR_TARGET | D3DMCLEAR_ZBUFFER, D3DMCOLOR_XRGB(0,0,255), 1.0f, 0 );
  286.     
  287.     // Begin the scene
  288.     if( SUCCEEDED( g_pd3dmDevice->BeginScene() ) )
  289.     {
  290.         SetupMatrices();
  291.         // Draw the triangles in the vertex buffer. This is broken into a few
  292.         // steps. We are passing the vertices down a "stream", so first we need
  293.         // to specify the source of that stream, which is our vertex buffer. Then,
  294.         // we call DrawPrimitive() which does the actual rendering of our geometry
  295.         // (in this case, just one triangle).
  296.         g_pd3dmDevice->SetTexture( 0, g_pTexture );
  297.         g_pd3dmDevice->SetTextureStageState( 0, D3DMTSS_COLOROP,   D3DMTOP_SELECTARG1);//D3DMTOP_MODULATE );
  298.         g_pd3dmDevice->SetTextureStageState( 0, D3DMTSS_COLORARG1, D3DMTA_TEXTURE );
  299.         g_pd3dmDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );
  300.         //g_pd3dmDevice->SetFVF( D3DMFVF_CUSTOMVERTEX );
  301.         g_pd3dmDevice->DrawPrimitive( D3DMPT_TRIANGLELIST, 0, 12 );
  302.         // End the scene
  303.         g_pd3dmDevice->EndScene();
  304.     }
  305.     // Present the backbuffer contents to the display
  306.     g_pd3dmDevice->Present( NULL, NULL, NULL, NULL );
  307. }
  308. //-----------------------------------------------------------------------------
  309. // Name: MsgProc()
  310. // Desc: The window's message handler
  311. //-----------------------------------------------------------------------------
  312. LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  313. {
  314.     switch( msg )
  315.     {
  316.     case WM_LBUTTONUP: 
  317.         PostMessage(hWnd, WM_CLOSE, 0, 0);
  318.         break;
  319.     case WM_KEYDOWN:
  320.         if (VK_ESCAPE == wParam)
  321.         {
  322.             PostMessage(hWnd, WM_CLOSE, 0, 0);
  323.         }
  324.         break;
  325.     case WM_CLOSE:
  326.         Cleanup();
  327.         break;
  328.     case WM_DESTROY:
  329.         PostQuitMessage( 0 );
  330.         return 0;
  331.     case WM_SETTINGCHANGE:
  332.         //we don't support screen rotation
  333.         if (IsScreenRotated())
  334.         {
  335.             PostMessage(hWnd, WM_CLOSE, 0, 0);
  336.         }
  337.         break;
  338.     }
  339.     return DefWindowProc( hWnd, msg, wParam, lParam );
  340. }
  341. //-----------------------------------------------------------------------------
  342. // Name: WinMain()
  343. // Desc: The application's entry point
  344. //-----------------------------------------------------------------------------
  345. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCELPTSTR szCmd, INT )
  346. {
  347.     // Parse command line to determine if user wants to use
  348.     // the D3DM reference driver instead of the default system driver
  349.     if (0 == lstrcmp(szCmd, TEXT("-ref")))
  350.         g_bUseRef = true;
  351.     //we don't support a rotated screen
  352.     if (IsScreenRotated())
  353.     {
  354.         return 0;
  355.     }
  356.     
  357.     // Register the window class
  358.     WNDCLASS wc = { 0L, MsgProc, 0L, 0L, 
  359.                       hInst, NULL, NULL, NULL, NULL,
  360.                       TEXT("D3DM Tutorial") };
  361.     RegisterClass( &wc );
  362.     int iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
  363.     int iScreenHeight = GetSystemMetrics(SM_CYSCREEN);
  364.     // Create the application's window
  365.     HWND hWnd = CreateWindow( TEXT("D3DM Tutorial"), 
  366.                               TEXT("D3DM Tutorial 02: Vertices"), 
  367.                               WS_VISIBLE, 
  368.                               0, 0, iScreenWidth, iScreenHeight,
  369.                               NULL, NULL, wc.hInstance, NULL );
  370.     //SHFullScreen(hWnd, SHFS_HIDESIPBUTTON | SHFS_HIDETASKBAR);
  371.     // Initialize Direct3D Mobile
  372.     if( SUCCEEDED( InitD3DM( hWnd ) ) )
  373.     { 
  374.         // Create the vertex buffer
  375.         if( SUCCEEDED( InitVB() ) )
  376.         {
  377.             // Show the window
  378.             ShowWindow( hWnd, SW_SHOWNORMAL );
  379.             UpdateWindow( hWnd );
  380.             // Enter the message loop
  381.             MSG msg;
  382.             memset( &msg, 0, sizeof(msg) );
  383.             while( msg.message!=WM_QUIT )
  384.             {
  385.                 if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
  386.                 {
  387.                     TranslateMessage( &msg );
  388.                     DispatchMessage( &msg );
  389.                 }
  390.                 else
  391.                     Render();
  392.             }
  393.         }
  394.     }
  395.     return 0;
  396. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值