Advertisement
Delfigamer

Untitled

Aug 4th, 2015
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.75 KB | None | 0 0
  1. #include <stdexcept>
  2. #include <exception>
  3. #include <d3d9.h>
  4. #include <windows.h>
  5. #include <windowsx.h>
  6. #include <ctime>
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <cinttypes>
  10.  
  11. #define LOG( ... ) ( void )( printf( __VA_ARGS__ ), printf( "\n" ) )
  12.  
  13. static wchar_t const* ClassName = L"OurFancyWindow";
  14. static wchar_t const* WindowCaption = L"LOOK AT ME";
  15. static DWORD const WindowStyle =
  16.     WS_OVERLAPPED |
  17.     WS_CAPTION |
  18.     WS_SYSMENU |
  19.     WS_BORDER |
  20.     WS_MINIMIZEBOX;
  21. static int const Window_defaultsize[] = { 800, 600 };
  22. WNDCLASSW Window_wndclass;
  23. HWND Window_hwnd;
  24. bool Window_terminated = false;
  25. D3DPRESENT_PARAMETERS Display_presentparameters = {
  26.     0,
  27.     0,
  28.     D3DFMT_A8R8G8B8,
  29.     0,
  30.     D3DMULTISAMPLE_NONE,
  31.     0,
  32.     D3DSWAPEFFECT_DISCARD,
  33.     0,
  34.     true,
  35.     true,
  36.     D3DFMT_D24S8,
  37.     0,
  38.     0,
  39.     D3DPRESENT_INTERVAL_IMMEDIATE };
  40. IDirect3D9* Display_direct3d;
  41. IDirect3DDevice9* Display_device;
  42. float Display_projectionmatrix[] = {
  43.     1, 0, 0, 0,
  44.     0, 1, 0, 0,
  45.     0, 0, 0, 1,
  46.     0, 0, 0, 1,
  47. };
  48. IDirect3DVertexDeclaration9* Shape_vertexdeclaration;
  49. IDirect3DVertexBuffer9* Shape_vertexbuffer;
  50. IDirect3DIndexBuffer9* Shape_indexbuffer;
  51. struct Vertex {
  52.     float pos_x;
  53.     float pos_y;
  54.     float pos_z;
  55.     uint32_t color;
  56. };
  57. Vertex Shape_vertexbufferdata[] = {
  58.     { -0.5, -0.5, -0.5, 0x80000000 },
  59.     {  0.5, -0.5, -0.5, 0x80ff0000 },
  60.     { -0.5,  0.5, -0.5, 0x8000ff00 },
  61.     {  0.5,  0.5, -0.5, 0x80ffff00 },
  62.     { -0.5, -0.5,  0.5, 0x800000ff },
  63.     {  0.5, -0.5,  0.5, 0x80ff00ff },
  64.     { -0.5,  0.5,  0.5, 0x8000ffff },
  65.     {  0.5,  0.5,  0.5, 0x80ffffff },
  66. };
  67. /*
  68.     6   7
  69.    2   3
  70.     4   5
  71.     0   1
  72. */
  73. uint16_t Shape_indexbufferdata[] = {
  74.     // 0, 1, 3, 0, 3, 2,
  75.     // 1, 5, 7, 1, 7, 3,
  76.     // 2, 3, 7, 2, 7, 6,
  77.     5, 4, 6, 5, 6, 7,
  78.     4, 0, 1, 4, 1, 5,
  79.     4, 0, 2, 4, 2, 6,
  80. };
  81. float Shape_worldmatrix1[] = {
  82.     0.173, 0.1, 0, 0,
  83.     -0.1, 0.173, 0, 0,
  84.     0, 0, 0.2, 0,
  85.     0.4, 0, 0, 1,
  86. };
  87. float Shape_worldmatrix2[] = {
  88.     0.5, 0, 0, 0,
  89.     0, 0.5, 0, 0,
  90.     0, 0, 0.5, 0,
  91.     0.5, 0, 0, 1,
  92. };
  93.  
  94. void CheckWinError( char const* file, char const* function, int line ) {
  95.     DWORD LastError = GetLastError();
  96.     if( !LastError ) {
  97.         return;
  98.     }
  99.     char MsgBuffer[ 1024 ];
  100.     FormatMessage(
  101.         FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK,
  102.         0,
  103.         LastError,
  104.         MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US ),
  105.         MsgBuffer,
  106.         sizeof( MsgBuffer ) - 1,
  107.         0 );
  108.     static char ErrorBuffer[ 1024 ];
  109.     snprintf( ErrorBuffer, sizeof( ErrorBuffer ), "[%s : %s @ %i] %s", file, function, line, MsgBuffer );
  110.     throw std::runtime_error( ErrorBuffer );
  111. }
  112. #define CheckWinError() CheckWinError( __FILE__, __FUNCTION__, __LINE__ )
  113.  
  114. void Display_checkerror_pos( char const* filename, char const* function, int line, HRESULT hr ) {
  115.     if( hr ) {
  116.         static char buffer[ 1024 ];
  117.         snprintf( buffer, sizeof( buffer ), "Direct3D error: %#x", uint32_t( hr ) );
  118.         throw std::runtime_error( buffer );
  119.     }
  120. }
  121. #define Display_checkerror( hr ) Display_checkerror_pos( __FILE__, __FUNCTION__, __LINE__, hr )
  122.  
  123. #define RELEASE( ref ) ( void )( ref ? ( ref->Release(), ref = 0 ) : 0 )
  124.  
  125. LRESULT CALLBACK WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  126.  
  127. void Window_initialize( HINSTANCE hInstance, int nCmdShow ) {
  128.     Window_wndclass.style = CS_VREDRAW | CS_HREDRAW;
  129.     Window_wndclass.lpfnWndProc = &WindowProc;
  130.     Window_wndclass.cbClsExtra = 0;
  131.     Window_wndclass.cbWndExtra = 0;
  132.     Window_wndclass.hInstance = hInstance;
  133.     Window_wndclass.hIcon = LoadIcon( 0, IDI_APPLICATION );
  134.     Window_wndclass.hCursor = LoadCursor( 0, IDC_ARROW );
  135.     Window_wndclass.hbrBackground = 0;
  136.     Window_wndclass.lpszMenuName = 0;
  137.     Window_wndclass.lpszClassName = ClassName;
  138.     if( !RegisterClassW( &Window_wndclass ) ) {
  139.         CheckWinError();
  140.     }
  141.     RECT WindowRect = {
  142.         0,
  143.         0,
  144.         Window_defaultsize[ 0 ] ,
  145.         Window_defaultsize[ 1 ] };
  146.     if( !AdjustWindowRect( &WindowRect, WindowStyle, false ) ) {
  147.         CheckWinError();
  148.     }
  149.     Window_hwnd = CreateWindowW(
  150.         ClassName,
  151.         WindowCaption,
  152.         WindowStyle,
  153.         20,
  154.         20,
  155.         WindowRect.right - WindowRect.left,
  156.         WindowRect.bottom - WindowRect.top,
  157.         0,
  158.         0,
  159.         hInstance,
  160.         0 );
  161.     if( !Window_hwnd ) {
  162.         CheckWinError();
  163.     }
  164.     ShowWindow( Window_hwnd, nCmdShow );
  165.     Display_direct3d = Direct3DCreate9( D3D_SDK_VERSION );
  166.     if( !Display_direct3d ) {
  167.         throw std::runtime_error( "Direct3DCreate9 failed" );
  168.     }
  169.     Display_checkerror( Display_direct3d->CreateDevice(
  170.         D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Window_hwnd,
  171.         D3DCREATE_FPU_PRESERVE | D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  172.         &Display_presentparameters, &Display_device ) );
  173.     Display_checkerror( Display_device->SetRenderState(
  174.         D3DRS_CULLMODE, D3DCULL_NONE ) );
  175.     Display_checkerror( Display_device->SetRenderState(
  176.         D3DRS_LIGHTING, false ) );
  177.     Display_checkerror( Display_device->SetRenderState(
  178.         D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ) );
  179.     Display_checkerror( Display_device->SetRenderState(
  180.         D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ) );
  181.     Display_checkerror( Display_device->SetRenderState(
  182.         D3DRS_ALPHABLENDENABLE, true ) );
  183.     UpdateWindow( Window_hwnd );
  184. }
  185.  
  186. void Window_finalize() {
  187.     RELEASE( Shape_indexbuffer );
  188.     RELEASE( Shape_vertexbuffer );
  189.     RELEASE( Shape_vertexdeclaration );
  190.     RELEASE( Display_device );
  191.     RELEASE( Display_direct3d );
  192. }
  193.  
  194. void Window_paint() {
  195.     if( !Display_device ) {
  196.         return;
  197.     }
  198.     Display_checkerror( Display_device->Clear( 0, 0, D3DCLEAR_TARGET, 0xff000000, 0, 0 ) );
  199.     if( !Shape_vertexdeclaration ) {
  200.         static D3DVERTEXELEMENT9 const VDElements[] = {
  201.             { 0, offsetof( Vertex, pos_x ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
  202.             { 0, offsetof( Vertex, color ), D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
  203.             D3DDECL_END()
  204.         };
  205.         Display_checkerror( Display_device->CreateVertexDeclaration(
  206.             VDElements,
  207.             &Shape_vertexdeclaration ) );
  208.     };
  209.     if( !Shape_vertexbuffer ) {
  210.         Display_checkerror( Display_device->CreateVertexBuffer(
  211.             sizeof( Shape_vertexbufferdata ),
  212.             D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY,
  213.             0,
  214.             D3DPOOL_DEFAULT,
  215.             &Shape_vertexbuffer,
  216.             0 ) );
  217.         Display_checkerror( Display_device->CreateIndexBuffer(
  218.             sizeof( Shape_indexbufferdata ),
  219.             D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY,
  220.             D3DFMT_INDEX16,
  221.             D3DPOOL_DEFAULT,
  222.             &Shape_indexbuffer,
  223.             0 ) );
  224.         void* buffer;
  225.         Display_checkerror( Shape_vertexbuffer->Lock( 0, 0, &buffer, D3DLOCK_DISCARD | D3DLOCK_NOSYSLOCK ) );
  226.         try {
  227.             memcpy( buffer, Shape_vertexbufferdata, sizeof( Shape_vertexbufferdata ) );
  228.         } catch( ... ) {
  229.             Shape_vertexbuffer->Unlock();
  230.             throw;
  231.         }
  232.         Display_checkerror( Shape_indexbuffer->Lock( 0, 0, &buffer, D3DLOCK_DISCARD | D3DLOCK_NOSYSLOCK ) );
  233.         try {
  234.             memcpy( buffer, Shape_indexbufferdata, sizeof( Shape_indexbufferdata ) );
  235.         } catch( ... ) {
  236.             Shape_indexbuffer->Unlock();
  237.             throw;
  238.         }
  239.     }
  240.     Display_checkerror( Display_device->BeginScene() );
  241.     Display_checkerror( Display_device->SetTransform( D3DTS_PROJECTION, ( D3DMATRIX const* )Display_projectionmatrix ) );
  242.     Display_checkerror( Display_device->SetTextureStageState(
  243.         0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ) );
  244.     Display_checkerror( Display_device->SetTextureStageState(
  245.         0, D3DTSS_COLORARG1, D3DTA_DIFFUSE ) );
  246.     Display_checkerror( Display_device->SetTextureStageState(
  247.         0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ) );
  248.     Display_checkerror( Display_device->SetTextureStageState(
  249.         0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE ) );
  250.     Display_checkerror( Display_device->SetTextureStageState(
  251.         1, D3DTSS_COLOROP, D3DTOP_DISABLE ) );
  252.     Display_checkerror( Display_device->SetTextureStageState(
  253.         1, D3DTSS_ALPHAOP, D3DTOP_DISABLE ) );
  254.     Display_checkerror( Display_device->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD ) );
  255.     Display_checkerror( Display_device->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ) );
  256.     Display_checkerror( Display_device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ) );
  257.     Display_checkerror( Display_device->SetStreamSource( 0, Shape_vertexbuffer, 0, sizeof( Vertex ) ) );
  258.     Display_checkerror( Display_device->SetVertexDeclaration( Shape_vertexdeclaration ) );
  259.     Display_checkerror( Display_device->SetIndices( Shape_indexbuffer ) );
  260.     Display_checkerror( Display_device->SetTransform( D3DTS_WORLD, ( D3DMATRIX const* )Shape_worldmatrix1 ) );
  261.     Display_checkerror( Display_device->DrawIndexedPrimitive(
  262.         D3DPT_TRIANGLELIST, 0, 0,
  263.         sizeof( Shape_vertexbufferdata ) / sizeof( Vertex ), 0,
  264.         sizeof( Shape_indexbufferdata ) / ( sizeof( uint16_t ) * 3 ) ) );
  265.     Display_checkerror( Display_device->SetTransform( D3DTS_WORLD, ( D3DMATRIX const* )Shape_worldmatrix2 ) );
  266.     Display_checkerror( Display_device->DrawIndexedPrimitive(
  267.         D3DPT_TRIANGLELIST, 0, 0,
  268.         sizeof( Shape_vertexbufferdata ) / sizeof( Vertex ), 0,
  269.         sizeof( Shape_indexbufferdata ) / ( sizeof( uint16_t ) * 3 ) ) );
  270.     Display_checkerror( Display_device->EndScene() );
  271.     Display_checkerror( Display_device->Present( 0, 0, 0, 0 ) );
  272. }
  273.  
  274. LRESULT CALLBACK WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
  275.     if( Window_hwnd != hwnd ) {
  276.         return DefWindowProcW( hwnd, uMsg, wParam, lParam );
  277.     }
  278.     switch( uMsg ) {
  279.         case WM_DESTROY:
  280.             Window_finalize();
  281.             Window_terminated = true;
  282.             PostQuitMessage( 0 );
  283.             return 0;
  284.         case WM_PAINT:
  285.             Window_paint();
  286.             return 0;
  287.         default:
  288.             return DefWindowProcW( hwnd, uMsg, wParam, lParam );
  289.     }
  290. }
  291.  
  292. void MainLoop() {
  293.     MSG message = { 0, 0, 0, 0 };
  294.     while( !Window_terminated ) {
  295.         if( PeekMessageW( &message, Window_hwnd, 0, 0, PM_REMOVE ) ) {
  296.             TranslateMessage( &message );
  297.             DispatchMessageW( &message );
  298.         }
  299.     }
  300. }
  301.  
  302. int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
  303.     LOG( "~ Application start" );
  304.     try {
  305.         Window_initialize( hInstance, nCmdShow );
  306.         MainLoop();
  307.         Window_finalize();
  308.     } catch( const std::exception& e ) {
  309.         LOG( "! Critical error: %s", e.what() );
  310.     }
  311.     LOG( "~ Application end" );
  312.     return 0;
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement