fork(1) download
  1. //-----------------------------------------------------------------------------
  2. // File: CreateDevice.cpp
  3. //
  4. // Desc: This is the first tutorial for using Direct3D. In this tutorial, all
  5. // we are doing is creating a Direct3D device and using it to clear the
  6. // window.
  7. //
  8. // Copyright (c) Microsoft Corporation. All rights reserved.
  9. //-----------------------------------------------------------------------------
  10. #include "stdafx.h"
  11. #include <Windows.h>
  12. #include <mmsystem.h>
  13. #include <d3dx9.h>
  14. #include <string>
  15.  
  16.  
  17.  
  18. //-----------------------------------------------------------------------------
  19. // Global variables
  20. //-----------------------------------------------------------------------------
  21. LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
  22. LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
  23. LPD3DXFONT g_font = NULL;
  24.  
  25.  
  26.  
  27. //-----------------------------------------------------------------------------
  28. // Name: InitD3D()
  29. // Desc: Initializes Direct3D
  30. //-----------------------------------------------------------------------------
  31. HRESULT InitD3D( HWND hWnd )
  32. {
  33. // Create the D3D object, which is needed to create the D3DDevice.
  34. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
  35. return E_FAIL;
  36.  
  37. //Fill out the D3DPRESENT_PARAMETERS structure.
  38. D3DPRESENT_PARAMETERS d3dpp;
  39. ZeroMemory( &d3dpp, sizeof(d3dpp) );
  40. d3dpp.Windowed = TRUE;
  41. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  42. d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  43.  
  44. // Create the Direct3D device.
  45. if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
  46. D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  47. &d3dpp, &g_pd3dDevice ) ) )
  48. {
  49. return E_FAIL;
  50. }
  51.  
  52. // Device state would normally be set here
  53.  
  54. return S_OK;
  55. }
  56.  
  57.  
  58.  
  59.  
  60. //-----------------------------------------------------------------------------
  61. // Name: Cleanup()
  62. // Desc: Releases all previously initialized objects
  63. //-----------------------------------------------------------------------------
  64. VOID Cleanup()
  65. {
  66. if( g_pd3dDevice != NULL)
  67. g_pd3dDevice->Release();
  68.  
  69. if( g_pD3D != NULL)
  70. g_pD3D->Release();
  71.  
  72. if(g_font != NULL)
  73. g_font->Release();
  74. }
  75.  
  76.  
  77.  
  78.  
  79. //-----------------------------------------------------------------------------
  80. // Name: Render()
  81. // Desc: Draws the scene
  82. //-----------------------------------------------------------------------------
  83. VOID Render()
  84. {
  85. if( NULL == g_pd3dDevice )
  86. return;
  87.  
  88. // Clear the backbuffer to a blue color
  89. g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
  90.  
  91. // Begin the scene
  92. if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
  93. {
  94. // 填充D3DXFONT_DESC結構體
  95. D3DXFONT_DESC df;
  96. ZeroMemory(&df,sizeof(D3DXFONT_DESC));
  97. df.Height = 25;
  98. df.Width = 12;
  99. df.Weight = 100;
  100. df.MipLevels = D3DX_DEFAULT;
  101. df.Italic = false;
  102. df.CharSet = DEFAULT_CHARSET;
  103. df.Quality = 0;
  104. df.PitchAndFamily = 0;
  105. strcpy(df.FaceName,"Times New Roman");
  106. //創建ID3DXFont 接口對象
  107. D3DXCreateFontIndirect(g_pd3dDevice,&df,&g_font);
  108. //居中繪制文本
  109. RECT g_FontPosition = {0, 0, 300, 300};
  110. g_font->DrawText(NULL,"Hello World",-1,&g_FontPosition,
  111. DT_CENTER|DT_VCENTER,D3DCOLOR_XRGB(255,255,255));
  112. // End the scene
  113. g_pd3dDevice->EndScene();
  114. }
  115.  
  116. // Present the backbuffer contents to the display
  117. g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
  118. }
  119.  
  120.  
  121.  
  122.  
  123. //-----------------------------------------------------------------------------
  124. // Name: MsgProc()
  125. // Desc: The window's message handler
  126. //-----------------------------------------------------------------------------
  127. LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  128. {
  129. switch( msg )
  130. {
  131. case WM_DESTROY:
  132. Cleanup();
  133. PostQuitMessage( 0 );
  134. return 0;
  135.  
  136. case WM_PAINT:
  137. Render();
  138. ValidateRect( hWnd, NULL );
  139. return 0;
  140. }
  141.  
  142. return DefWindowProc( hWnd, msg, wParam, lParam );
  143. }
  144.  
  145.  
  146.  
  147.  
  148. //-----------------------------------------------------------------------------
  149. // Name: WinMain()
  150. // Desc: The application's entry point
  151. //-----------------------------------------------------------------------------
  152. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  153. {
  154. // Register the window class
  155. WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
  156. GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
  157. "D3D Tutorial", NULL };
  158. RegisterClassEx( &wc );
  159.  
  160. // Create the application's window
  161. HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 01: CreateDevice",
  162. WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
  163. GetDesktopWindow(), NULL, wc.hInstance, NULL );
  164.  
  165. // Initialize Direct3D
  166. if( SUCCEEDED( InitD3D( hWnd ) ) )
  167. {
  168. // Show the window
  169. ShowWindow( hWnd, SW_SHOWDEFAULT );
  170. UpdateWindow( hWnd );
  171.  
  172. // Enter the message loop
  173. MSG msg;
  174. while( GetMessage( &msg, NULL, 0, 0 ) )
  175. {
  176. TranslateMessage( &msg );
  177. DispatchMessage( &msg );
  178. }
  179. }
  180.  
  181. UnregisterClass( "D3D Tutorial", wc.hInstance );
  182. return 0;
  183. }
  184.  
Not running #stdin #stdout 0s 0KB
stdin
#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
#include <string>
 
 
 
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9             g_pD3D       = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9       g_pd3dDevice = NULL; // Our rendering device
LPD3DXFONT              g_font       = NULL;
 
 
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object, which is needed to create the D3DDevice.
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;
 
    //Fill out the D3DPRESENT_PARAMETERS structure.
        D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
 
    // Create the Direct3D device. 
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }
 
    // Device state would normally be set here
 
    return S_OK;
}
 
 
 
 
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
    if( g_pd3dDevice != NULL) 
        g_pd3dDevice->Release();
 
    if( g_pD3D != NULL)
        g_pD3D->Release();
 
        if(g_font != NULL)
                g_font->Release();
}
 
 
 
 
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
    if( NULL == g_pd3dDevice )
        return;
    // Clear the backbuffer to a blue color
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
    
    // Begin the scene
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
		

        // 填充D3DXFONT_DESC結構體
        D3DXFONT_DESC df;
                ZeroMemory(&df,sizeof(D3DXFONT_DESC));
        df.Height = 25;
                df.Width = 12;
                df.Weight = 100;
                df.MipLevels = D3DX_DEFAULT;
        df.Italic = false;
        df.CharSet = DEFAULT_CHARSET;
                df.Quality = 0;
                df.PitchAndFamily = 0;
				wcscpy(df.FaceName,L"Times New Roman");
        //創建ID3DXFont 接口對象
                D3DXCreateFontIndirect(g_pd3dDevice,&df,&g_font);
                //居中繪制文本
                RECT g_FontPosition = {0, 0, 300, 300};         
                g_font->DrawText(NULL,L"Hello World",-1,&g_FontPosition,
                                          DT_CENTER|DT_VCENTER,D3DCOLOR_XRGB(255,255,255));

        // End the scene
        g_pd3dDevice->EndScene();
    }
 
    // Present the backbuffer contents to the display
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
 
 void Resize(int width,int height)
{
	D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

	d3dpp.BackBufferWidth=width;
	d3dpp.BackBufferHeight=height;

	g_pd3dDevice->Reset(&d3dpp);
}
 
 
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return 0;
 
        case WM_PAINT:
            Render();
            ValidateRect( hWnd, NULL );
            return 0;

		case WM_SIZE:
			Resize(LOWORD(lParam),HIWORD(lParam));
			return 0;
    }
 
    return DefWindowProc( hWnd, msg, wParam, lParam );
}
 
 
 
 
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    // Register the window class
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, 
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      L"D3D Tutorial", NULL };
    RegisterClassEx( &wc );
 
    // Create the application's window
    HWND hWnd = CreateWindow(L"D3D Tutorial", L"D3D Tutorial 01: CreateDevice", 
                              WS_OVERLAPPEDWINDOW, 100, 100, 800, 600,
                              GetDesktopWindow(), NULL, wc.hInstance, NULL );
 
    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    { 
        // Show the window
        ShowWindow( hWnd, SW_SHOWDEFAULT );
        UpdateWindow( hWnd );
 
        // Enter the message loop
        MSG msg; 
        while( GetMessage( &msg, NULL, 0, 0 ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
    }
 
    UnregisterClass( L"D3D Tutorial", wc.hInstance );
    return 0;
}
stdout
Standard output is empty