   DirextX9 Displaying Bitmap Problem
  Hello I bought the book Beginning DirextX 9 by Wendy Jones and am wanting to display a bitmap image. I loaded up the example for it but it does not work here is the code:[SOURCE]#include &lt;windows.h&gt;#include &lt;d3d9.h&gt;#include &lt;d3dx9tex.h&gt;#include &lt;string&gt;HINSTANCE hInst;HWND wndHandle;bool initWindow(HINSTANCE hInstance);LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);// DirectX functions and global variablesHRESULT  lastResult;LPDIRECT3D9   pD3D;LPDIRECT3DDEVICE9  pd3dDevice;IDirect3DSurface9*bkgrd;bool initDirect3D(HWND hwnd);IDirect3DSurface9* getSurfaceFromBitmap(std::string filename);void Render(void);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){if (!initWindow(hInstance))return false;if (!initDirect3D(wndHandle))return false;bkgrd = getSurfaceFromBitmap("background.bmp");// Main message loop:// Enter the message loop    MSG msg;ZeroMemory( &msg, sizeof(msg) );    while( msg.message!=WM_QUIT )    {if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )   {TranslateMessage( &msg );  DispatchMessage( &msg );   }else{// call our render functionRender();}    }if( pd3dDevice != NULL) {   pd3dDevice-&gt;Release();pd3dDevice = NULL;}    if( pD3D != NULL){   pD3D-&gt;Release();pD3D = NULL;}return (int) msg.wParam;}bool initWindow(HINSTANCE hInstance){WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX);wcex.style= CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc= (WNDPROC)WndProc;wcex.cbClsExtra= 0;wcex.cbWndExtra= 0;wcex.hInstance= hInstance;wcex.hIcon= 0;wcex.hCursor= LoadCursor(NULL, IDC_ARROW);wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName= NULL;wcex.lpszClassName= "DirectXExample";wcex.hIconSm= 0;RegisterClassEx(&wcex);wndHandle = CreateWindow("DirectXExample",  "DirectXExample",  WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,  CW_USEDEFAULT,  640,  480,  NULL,  NULL,  hInstance,  NULL);   if (!wndHandle) return false; ShowWindow(wndHandle, SW_SHOW);   UpdateWindow(wndHandle);   return true;}LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){switch (message) {case WM_DESTROY:PostQuitMessage(0);break;}return DefWindowProc(hWnd, message, wParam, lParam);}bool initDirect3D(HWND hwnd){if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ){lastResult = E_FAIL;return false;}D3DPRESENT_PARAMETERS d3dpp;ZeroMemory( &d3dpp, sizeof(d3dpp) );    d3dpp.Windowed = TRUE;    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;d3dpp.BackBufferCount  = 1;d3dpp.BackBufferHeight = 480;d3dpp.BackBufferWidth  = 640;d3dpp.hDeviceWindow    = hwnd;    if( FAILED( pD3D-&gt;CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hwnd,   D3DCREATE_SOFTWARE_VERTEXPROCESSING,   &d3dpp, &pd3dDevice ) ) )    {lastResult = E_FAIL;   return false;    }return true;}IDirect3DSurface9* getSurfaceFromBitmap(std::string filename){HRESULT hResult;IDirect3DSurface9* surface = NULL;D3DXIMAGE_INFO imageInfo;// Get the width and height info from this bitmaphResult = D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo);if FAILED (hResult)return NULL;hResult = pd3dDevice-&gt;CreateOffscreenPlainSurface(imageInfo.Width, imageInfo.Height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);if (FAILED(hResult))return NULL;hResult = D3DXLoadSurfaceFromFile(surface, NULL, NULL, filename.c_str(), NULL, D3DX_DEFAULT, 0, NULL);if (FAILED(hResult))return NULL;return surface;}void Render(void){// This will hold the back bufferIDirect3DSurface9* backbuffer = NULL//Check to make sure you have a valid Direct3D deviceif( NULL == pd3dDevice )   return;    // Clear the backbuffer to a blue color    pd3dDevice-&gt;Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );// Get the back bufferpd3dDevice-&gt;GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO, &backbuffer);// Copy the offscreen surface to the back buffer// Note the use of NULL values for the source and distination RECTs// This ensures a copy of the entire surface to the back bufferpd3dDevice-&gt;StretchRect(srcSurface, NULL, backbuffer, NULL, D3DTEXF_NONE);//Present the back buffer contents to the displaypd3dDevice-&gt;Present (NULL, NULL, NULL, NULL);}[/SOURCE]Thanks for the help[Edited by - ApollosReign on February 23, 2009 8:13:25 AM]
  What is the error message?  What do you mean, "it wont work"?  Does it compile but displays nothing, does it display a white square?Also, but your code in [ SOURCE ] [ / SOURCE] tags, with the spaces removed.   ;  It does not compile. All I need it to do is set the background to a bitmap but have no knowledge of directX and the book is outdated so I do not know how to change the code.The error message is:error C2065: 'srcSurface' : undeclared identifierThanks   ;Quote:Original post by ApollosReignIt does not compile. All I need it to do is set the background to a bitmap but have no knowledge of directX and the book is outdated so I do not know how to change the code.The error message is:error C2065: 'srcSurface' : undeclared identifierThanksLooks like a typo in the book. Try replacing 'srcSurface' with 'bkgrd' in your StretchRect() call.   ;  Hey thanks that worked but now it runs and crashes instantly.   ;Quote:Original post by ApollosReignHey thanks that worked but now it runs and crashes instantly.Then something else is wrong too.You'll need to tell us how it "crashes". Does your computer burst into flames? Blue screen of death? Access violation? What does the debugger tell you? What do the debug runtimes tell you?   ;  It flashes a white screen and closes in a second or less.It seems like this returns false:if (!initWindow(hInstance))return false;Thanks   ;Quote:Original post by ApollosReignIt flashes a white screen and closes in a second or less.It seems like this returns false:if (!initWindow(hInstance))return false;ThanksWhat do the debug runtimes tell you?When you trace through the code using the debugger, why does that function return false? You need to find the lowest level call that's failing, and then find out why (Although we can help with that).Also, unrelated, there's a bug in that CreateWindow call, since you never call AdjustWindowRect. You're creating a window with a client area of something like 634x462, but a DirectX backbuffer of 640x480, so the backbuffer will be shrunk to fit on the window, which will cause some odd artifacts.   ;  Ok I found what it is failing it wasn't actually in the initWindow it was in the Direct3D call and this is what is failing:[Source]    if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hwnd,   D3DCREATE_SOFTWARE_VERTEXPROCESSING,   &d3dpp, &pd3dDevice ) ) )    {lastResult = E_FAIL;   return false;    }    [/Source];Quote:Original post by ApollosReignOk I found what it is failing it wasn't actually in the initWindow it was in the Direct3D call and this is what is failing:   *** Source Snippet Removed ***[/source]And what do the debug runtimes tell you the problem is?   