Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdexcept>
- #include <exception>
- #include <d3d9.h>
- #include <windows.h>
- #include <windowsx.h>
- #include <ctime>
- #include <cstdio>
- #include <cstring>
- #include <cinttypes>
- #define LOG( ... ) ( void )( printf( __VA_ARGS__ ), printf( "\n" ) )
- static wchar_t const* ClassName = L"OurFancyWindow";
- static wchar_t const* WindowCaption = L"LOOK AT ME";
- static DWORD const WindowStyle =
- WS_OVERLAPPED |
- WS_CAPTION |
- WS_SYSMENU |
- WS_BORDER |
- WS_MINIMIZEBOX;
- static int const Window_defaultsize[] = { 800, 600 };
- WNDCLASSW Window_wndclass;
- HWND Window_hwnd;
- bool Window_terminated = false;
- D3DPRESENT_PARAMETERS Display_presentparameters = {
- 0,
- 0,
- D3DFMT_A8R8G8B8,
- 0,
- D3DMULTISAMPLE_NONE,
- 0,
- D3DSWAPEFFECT_DISCARD,
- 0,
- true,
- true,
- D3DFMT_D24S8,
- 0,
- 0,
- D3DPRESENT_INTERVAL_IMMEDIATE };
- IDirect3D9* Display_direct3d;
- IDirect3DDevice9* Display_device;
- float Display_projectionmatrix[] = {
- 1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, 0, 0, 1,
- 0, 0, 0, 1,
- };
- IDirect3DVertexDeclaration9* Shape_vertexdeclaration;
- IDirect3DVertexBuffer9* Shape_vertexbuffer;
- IDirect3DIndexBuffer9* Shape_indexbuffer;
- struct Vertex {
- float pos_x;
- float pos_y;
- float pos_z;
- uint32_t color;
- };
- Vertex Shape_vertexbufferdata[] = {
- { -0.5, -0.5, -0.5, 0x80000000 },
- { 0.5, -0.5, -0.5, 0x80ff0000 },
- { -0.5, 0.5, -0.5, 0x8000ff00 },
- { 0.5, 0.5, -0.5, 0x80ffff00 },
- { -0.5, -0.5, 0.5, 0x800000ff },
- { 0.5, -0.5, 0.5, 0x80ff00ff },
- { -0.5, 0.5, 0.5, 0x8000ffff },
- { 0.5, 0.5, 0.5, 0x80ffffff },
- };
- /*
- 6 7
- 2 3
- 4 5
- 0 1
- */
- uint16_t Shape_indexbufferdata[] = {
- // 0, 1, 3, 0, 3, 2,
- // 1, 5, 7, 1, 7, 3,
- // 2, 3, 7, 2, 7, 6,
- 5, 4, 6, 5, 6, 7,
- 4, 0, 1, 4, 1, 5,
- 4, 0, 2, 4, 2, 6,
- };
- float Shape_worldmatrix1[] = {
- 0.173, 0.1, 0, 0,
- -0.1, 0.173, 0, 0,
- 0, 0, 0.2, 0,
- 0.4, 0, 0, 1,
- };
- float Shape_worldmatrix2[] = {
- 0.5, 0, 0, 0,
- 0, 0.5, 0, 0,
- 0, 0, 0.5, 0,
- 0.5, 0, 0, 1,
- };
- void CheckWinError( char const* file, char const* function, int line ) {
- DWORD LastError = GetLastError();
- if( !LastError ) {
- return;
- }
- char MsgBuffer[ 1024 ];
- FormatMessage(
- FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK,
- 0,
- LastError,
- MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US ),
- MsgBuffer,
- sizeof( MsgBuffer ) - 1,
- 0 );
- static char ErrorBuffer[ 1024 ];
- snprintf( ErrorBuffer, sizeof( ErrorBuffer ), "[%s : %s @ %i] %s", file, function, line, MsgBuffer );
- throw std::runtime_error( ErrorBuffer );
- }
- #define CheckWinError() CheckWinError( __FILE__, __FUNCTION__, __LINE__ )
- void Display_checkerror_pos( char const* filename, char const* function, int line, HRESULT hr ) {
- if( hr ) {
- static char buffer[ 1024 ];
- snprintf( buffer, sizeof( buffer ), "Direct3D error: %#x", uint32_t( hr ) );
- throw std::runtime_error( buffer );
- }
- }
- #define Display_checkerror( hr ) Display_checkerror_pos( __FILE__, __FUNCTION__, __LINE__, hr )
- #define RELEASE( ref ) ( void )( ref ? ( ref->Release(), ref = 0 ) : 0 )
- LRESULT CALLBACK WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
- void Window_initialize( HINSTANCE hInstance, int nCmdShow ) {
- Window_wndclass.style = CS_VREDRAW | CS_HREDRAW;
- Window_wndclass.lpfnWndProc = &WindowProc;
- Window_wndclass.cbClsExtra = 0;
- Window_wndclass.cbWndExtra = 0;
- Window_wndclass.hInstance = hInstance;
- Window_wndclass.hIcon = LoadIcon( 0, IDI_APPLICATION );
- Window_wndclass.hCursor = LoadCursor( 0, IDC_ARROW );
- Window_wndclass.hbrBackground = 0;
- Window_wndclass.lpszMenuName = 0;
- Window_wndclass.lpszClassName = ClassName;
- if( !RegisterClassW( &Window_wndclass ) ) {
- CheckWinError();
- }
- RECT WindowRect = {
- 0,
- 0,
- Window_defaultsize[ 0 ] ,
- Window_defaultsize[ 1 ] };
- if( !AdjustWindowRect( &WindowRect, WindowStyle, false ) ) {
- CheckWinError();
- }
- Window_hwnd = CreateWindowW(
- ClassName,
- WindowCaption,
- WindowStyle,
- 20,
- 20,
- WindowRect.right - WindowRect.left,
- WindowRect.bottom - WindowRect.top,
- 0,
- 0,
- hInstance,
- 0 );
- if( !Window_hwnd ) {
- CheckWinError();
- }
- ShowWindow( Window_hwnd, nCmdShow );
- Display_direct3d = Direct3DCreate9( D3D_SDK_VERSION );
- if( !Display_direct3d ) {
- throw std::runtime_error( "Direct3DCreate9 failed" );
- }
- Display_checkerror( Display_direct3d->CreateDevice(
- D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Window_hwnd,
- D3DCREATE_FPU_PRESERVE | D3DCREATE_SOFTWARE_VERTEXPROCESSING,
- &Display_presentparameters, &Display_device ) );
- Display_checkerror( Display_device->SetRenderState(
- D3DRS_CULLMODE, D3DCULL_NONE ) );
- Display_checkerror( Display_device->SetRenderState(
- D3DRS_LIGHTING, false ) );
- Display_checkerror( Display_device->SetRenderState(
- D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ) );
- Display_checkerror( Display_device->SetRenderState(
- D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ) );
- Display_checkerror( Display_device->SetRenderState(
- D3DRS_ALPHABLENDENABLE, true ) );
- UpdateWindow( Window_hwnd );
- }
- void Window_finalize() {
- RELEASE( Shape_indexbuffer );
- RELEASE( Shape_vertexbuffer );
- RELEASE( Shape_vertexdeclaration );
- RELEASE( Display_device );
- RELEASE( Display_direct3d );
- }
- void Window_paint() {
- if( !Display_device ) {
- return;
- }
- Display_checkerror( Display_device->Clear( 0, 0, D3DCLEAR_TARGET, 0xff000000, 0, 0 ) );
- if( !Shape_vertexdeclaration ) {
- static D3DVERTEXELEMENT9 const VDElements[] = {
- { 0, offsetof( Vertex, pos_x ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
- { 0, offsetof( Vertex, color ), D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
- D3DDECL_END()
- };
- Display_checkerror( Display_device->CreateVertexDeclaration(
- VDElements,
- &Shape_vertexdeclaration ) );
- };
- if( !Shape_vertexbuffer ) {
- Display_checkerror( Display_device->CreateVertexBuffer(
- sizeof( Shape_vertexbufferdata ),
- D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY,
- 0,
- D3DPOOL_DEFAULT,
- &Shape_vertexbuffer,
- 0 ) );
- Display_checkerror( Display_device->CreateIndexBuffer(
- sizeof( Shape_indexbufferdata ),
- D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY,
- D3DFMT_INDEX16,
- D3DPOOL_DEFAULT,
- &Shape_indexbuffer,
- 0 ) );
- void* buffer;
- Display_checkerror( Shape_vertexbuffer->Lock( 0, 0, &buffer, D3DLOCK_DISCARD | D3DLOCK_NOSYSLOCK ) );
- try {
- memcpy( buffer, Shape_vertexbufferdata, sizeof( Shape_vertexbufferdata ) );
- } catch( ... ) {
- Shape_vertexbuffer->Unlock();
- throw;
- }
- Display_checkerror( Shape_indexbuffer->Lock( 0, 0, &buffer, D3DLOCK_DISCARD | D3DLOCK_NOSYSLOCK ) );
- try {
- memcpy( buffer, Shape_indexbufferdata, sizeof( Shape_indexbufferdata ) );
- } catch( ... ) {
- Shape_indexbuffer->Unlock();
- throw;
- }
- }
- Display_checkerror( Display_device->BeginScene() );
- Display_checkerror( Display_device->SetTransform( D3DTS_PROJECTION, ( D3DMATRIX const* )Display_projectionmatrix ) );
- Display_checkerror( Display_device->SetTextureStageState(
- 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ) );
- Display_checkerror( Display_device->SetTextureStageState(
- 0, D3DTSS_COLORARG1, D3DTA_DIFFUSE ) );
- Display_checkerror( Display_device->SetTextureStageState(
- 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ) );
- Display_checkerror( Display_device->SetTextureStageState(
- 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE ) );
- Display_checkerror( Display_device->SetTextureStageState(
- 1, D3DTSS_COLOROP, D3DTOP_DISABLE ) );
- Display_checkerror( Display_device->SetTextureStageState(
- 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE ) );
- Display_checkerror( Display_device->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD ) );
- Display_checkerror( Display_device->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ) );
- Display_checkerror( Display_device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ) );
- Display_checkerror( Display_device->SetStreamSource( 0, Shape_vertexbuffer, 0, sizeof( Vertex ) ) );
- Display_checkerror( Display_device->SetVertexDeclaration( Shape_vertexdeclaration ) );
- Display_checkerror( Display_device->SetIndices( Shape_indexbuffer ) );
- Display_checkerror( Display_device->SetTransform( D3DTS_WORLD, ( D3DMATRIX const* )Shape_worldmatrix1 ) );
- Display_checkerror( Display_device->DrawIndexedPrimitive(
- D3DPT_TRIANGLELIST, 0, 0,
- sizeof( Shape_vertexbufferdata ) / sizeof( Vertex ), 0,
- sizeof( Shape_indexbufferdata ) / ( sizeof( uint16_t ) * 3 ) ) );
- Display_checkerror( Display_device->SetTransform( D3DTS_WORLD, ( D3DMATRIX const* )Shape_worldmatrix2 ) );
- Display_checkerror( Display_device->DrawIndexedPrimitive(
- D3DPT_TRIANGLELIST, 0, 0,
- sizeof( Shape_vertexbufferdata ) / sizeof( Vertex ), 0,
- sizeof( Shape_indexbufferdata ) / ( sizeof( uint16_t ) * 3 ) ) );
- Display_checkerror( Display_device->EndScene() );
- Display_checkerror( Display_device->Present( 0, 0, 0, 0 ) );
- }
- LRESULT CALLBACK WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
- if( Window_hwnd != hwnd ) {
- return DefWindowProcW( hwnd, uMsg, wParam, lParam );
- }
- switch( uMsg ) {
- case WM_DESTROY:
- Window_finalize();
- Window_terminated = true;
- PostQuitMessage( 0 );
- return 0;
- case WM_PAINT:
- Window_paint();
- return 0;
- default:
- return DefWindowProcW( hwnd, uMsg, wParam, lParam );
- }
- }
- void MainLoop() {
- MSG message = { 0, 0, 0, 0 };
- while( !Window_terminated ) {
- if( PeekMessageW( &message, Window_hwnd, 0, 0, PM_REMOVE ) ) {
- TranslateMessage( &message );
- DispatchMessageW( &message );
- }
- }
- }
- int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
- LOG( "~ Application start" );
- try {
- Window_initialize( hInstance, nCmdShow );
- MainLoop();
- Window_finalize();
- } catch( const std::exception& e ) {
- LOG( "! Critical error: %s", e.what() );
- }
- LOG( "~ Application end" );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement