#define WIN32_LEAN_AND_MEAN #include #include #include #include struct SGlobal { HWND m_hMainWindow; LPDIRECT3D8 m_pD3D; LPDIRECT3DDEVICE8 m_pDevice; int m_wide; int m_tall; D3DPRESENT_PARAMETERS m_SavedPresentationParameters; } G; int ShowError( const char* zStr, int retcode = E_FAIL) { OutputDebugString( zStr); OutputDebugString( "\n"); return retcode; } long DoPaint( HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT PaintStruct; HDC hDC; hDC = BeginPaint( hWnd, &PaintStruct); // Do Paint stuff vvvvvvv // Done with Paint stuff ^^^^^^^ EndPaint( hWnd, &PaintStruct); return 0; } long CALLBACK WindowProc( HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam) { switch( uMessage) { case WM_CREATE: return 0; case WM_PAINT: ValidateRect( hWnd, NULL); // may windows think I did the update; return 0; case WM_DESTROY: PostQuitMessage(0); return 0; default: return DefWindowProc( hWnd, uMessage, wParam, lParam); } } int InitDirect3DDevice( HWND hWndTarget, int wide, int tall, BOOL fWindowed, D3DFORMAT FullScreenFormat, LPDIRECT3D8 pD3D, LPDIRECT3DDEVICE8 *ppDevice) { HRESULT hr; D3DDISPLAYMODE d3ddm; D3DPRESENT_PARAMETERS d3dpp; if ( *ppDevice) (*ppDevice)->Release(); ZeroMemory( &d3dpp, sizeof( D3DPRESENT_PARAMETERS)); hr = G.m_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm); if (FAILED(hr)) return ShowError( "Could not get display adapter information"); d3dpp.BackBufferWidth = wide; d3dpp.BackBufferHeight = tall; d3dpp.BackBufferFormat = fWindowed ? d3ddm.Format : FullScreenFormat; d3dpp.BackBufferCount = 1; d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.hDeviceWindow = hWndTarget; d3dpp.Windowed = fWindowed; d3dpp.EnableAutoDepthStencil = TRUE; d3dpp.AutoDepthStencilFormat = D3DFMT_D16; d3dpp.FullScreen_RefreshRateInHz = 0; // use default d3dpp.FullScreen_PresentationInterval = fWindowed ? 0 : D3DPRESENT_INTERVAL_IMMEDIATE; d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER; hr = G.m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWndTarget, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, ppDevice ); if (FAILED(hr)) return ShowError( "Could not create render device"); G.m_wide = wide; G.m_tall = tall; G.m_SavedPresentationParameters = d3dpp; return S_OK; } int D3DInit() { HRESULT hr; G.m_pD3D = Direct3DCreate8( D3D_SDK_VERSION ); if ( !G.m_pD3D) { return E_FAIL; } hr = InitDirect3DDevice( G.m_hMainWindow, 640, 480, TRUE, D3DFMT_X8R8G8B8, G.m_pD3D, &G.m_pDevice ); if (FAILED(hr)) return ShowError( "Initialization of the device failed"); return S_OK; } void SetPixel32( int x, int y, DWORD color, int pitch, DWORD* pData) { if ( (x >= G.m_wide) || (x < 0) ) return; if ( (y >= G.m_tall) || (y < 0) ) return; pData[ (((pitch/4) * y) + x) ] = color; } int Render() { HRESULT hr; D3DLOCKED_RECT lockedRect; LPDIRECT3DSURFACE8 pBackSurf; if ( !G.m_pDevice) return ShowError( "Cannot render, there is no device"); #if 1 G.m_pDevice->Clear( 0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 0), 1.0f, 0 ); #endif hr = G.m_pDevice->GetBackBuffer( 0, D3DBACKBUFFER_TYPE_MONO, &pBackSurf ); if (FAILED(hr)) return ShowError( "Could NOT get back buffer"); hr = pBackSurf->LockRect( &lockedRect, NULL, 0); if (FAILED(hr)) return ShowError( "Could NOT lock the back buffer"); DWORD *pData = (DWORD*)(lockedRect.pBits); // Draw into pData buffer Starts vvvvv for ( int i = 0; i < 50000; i++) { SetPixel32( rand()%(G.m_wide-1), rand()%(G.m_tall), D3DCOLOR_XRGB( 255, 0, 0), lockedRect.Pitch, pData ); } // Draw into pData buffer Ends ^^^^^ pBackSurf->UnlockRect(); pBackSurf->Release(); G.m_pDevice->Present( NULL, NULL, NULL, NULL); return S_OK; } void D3DIdleProc() { Render(); } void D3DShutdown() { } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; WNDCLASSEX wc; static char strAppName[] = "DirectDrawSimple"; wc.cbSize = sizeof( WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.lpfnWndProc = WindowProc; wc.hInstance = hInstance; wc.hbrBackground = (HBRUSH) GetStockObject( DKGRAY_BRUSH); wc.hIcon = LoadIcon( NULL, IDI_APPLICATION); wc.hIconSm = LoadIcon( NULL, IDI_HAND); wc.hCursor = LoadCursor( NULL, IDC_CROSS); wc.lpszMenuName = NULL; wc.lpszClassName = strAppName; RegisterClassEx( &wc); G.m_hMainWindow = CreateWindowEx( NULL, strAppName, strAppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, // x pos CW_USEDEFAULT, // y pos 512, // wide 512, // tall NULL, NULL, // Handle to Menu hInstance, NULL ); ShowWindow( G.m_hMainWindow, nCmdShow); UpdateWindow( G.m_hMainWindow); if ( FAILED(D3DInit()) ) { return E_FAIL; } for(;;) { if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE) ) { if ( msg.message == WM_QUIT ) { break; } TranslateMessage( &msg); DispatchMessage( &msg); } else { D3DIdleProc(); } } D3DShutdown(); return msg.wParam; }