   Direct X 9, strechRect() is failing!
  Right, learning how to use sprites from a book... book is not so good hence it cost £2.50. I think what I've done is right but nothing was showing on the screen so I put this around strechRect:if(FAILED(strechRect(...))){MessageBox(NULL, "...", "...", 0)and that's getting the message up, the only return value from strechRect is INVALID_CALL so I'm guessing that's what it is.Here be my code:/*********************** Direct X Tutorial ***********************//*---------------------------------------------------------------*//**************************** Includes ***************************/#include <windows.h>#include <d3d9.h>#include <d3dx9.h>#include <d3dx9tex.h>#include <string>#include <iostream>using namespace std;// include the Direct3D Library file#pragma comment (lib, "d3d9.lib")#pragma comment (lib, "d3dx9.lib")/*---------------------------------------------------------------*//***************************** Structs ****************************/struct {RECT srcRect;// positionint posX; // the X coordinateint posY; // the Y coordinate}spriteStruct[7];/*---------------------------------------------------------------*//********************* Variable Declerations ********************/// WindowHINSTANCE hInst; // Handle to Hold Window InstanceHWND wndHandle; // Hold Window Handle// Direct 3DLPDIRECT3D9 pD3D; // the Direct3D objectLPDIRECT3DDEVICE9 pd3dDevice; // the Direct3D deviceIDirect3DSurface9* surface = NULL;IDirect3DSurface9* backbuffer = NULL;int SPRITE_WIDTH = 100;int SPRITE_HEIGHT = 100;int SCRN_WIDTH = 800;int SCRN_HEIGHT = 600;/*---------------------------------------------------------------*//********************** Function Prototypes **********************/// Window Functionsbool initWindow ( HINSTANCE hInstance ); // window creationLRESULT CALLBACK WndProc ( HWND, UINT, WPARAM, LPARAM); // message handling// Direct 3d Functionsbool initDirect3D( void );void render ( void );void cleanUp ( void );IDirect3DSurface9* getSurfaceFromBitmap(std::wstring filename);bool initSprites(void);/*---------------------------------------------------------------*//*****************************************************************//************************ Main Entry Point ***********************//*****************************************************************/int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){// Initialize window:if ( !initWindow( hInstance ) ){MessageBox ( NULL, L"Window Initialization Failed!", L"Error!", 0);return false;}// Initialize Direct 3Dif ( !initDirect3D( ) ){MessageBox ( NULL, L"D3D Initialization Failed!", L"Error!", 0);return false;}initSprites();// Message loop:MSG msg;ZeroMemory( &msg, sizeof( msg ) );// Loop until quitwhile ( msg.message != WM_QUIT ){if ( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ){TranslateMessage( &msg );DispatchMessage( &msg ); //Send to message event handler ( wndProc )}else{render();}}// Clean up resourcescleanUp();// Exit the application with exit valuereturn (int) msg.wParam;}/*****************************************************************//********************** Window initialisation ********************//*****************************************************************/bool initWindow( HINSTANCE hInstance ){// Setting up the Window ClassWNDCLASSEX wcex;wcex.cbSize=sizeof( WNDCLASSEX );// Size of the structurewcex.style=CS_HREDRAW | CS_VREDRAW;// Window Stylewcex.lpfnWndProc=(WNDPROC) WndProc;// Window Callback Procedurewcex.cbClsExtra=0;// extra bytes for class ??wcex.cbWndExtra=0;// Extra bytes for instance ??wcex.hInstance=hInstance;// Handle to instancewcex.hIcon=0;// Icon to use with appwcex.hCursor=LoadCursor( NULL, IDC_ARROW);// Default arrow cursorwcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);// Background colourwcex.lpszMenuName=NULL;// menu resource name+wcex.lpszClassName=L"Direct X Example";// Class name to createwcex.hIconSm=0;// Handle to small IconRegisterClassEx(&wcex);// Creating the windowwndHandle = CreateWindow (L"Direct X Example",// Window Class to useL"Direct X Example",// Name of the window ( Title Bar Text )WS_EX_TOPMOST | WS_VISIBLE,// Style of the window WS_POPUP | 0, 0,// Window x y position800, 600,// Window width and heightNULL,// No parent window, NULL for desktopNULL,// Menu for the app, NULL = NonehInstance,// Handle to app InstanceNULL );// Mesages passed into the app, None// Check that hWnd creation was successfulif ( !wndHandle ) {MessageBox( NULL, L"OMG creating Window Handle failed!", L"OMGOMG WTF", 0 );return false;}// Display the windowShowWindow( wndHandle, SW_SHOW );UpdateWindow( wndHandle );return true;}/*****************************************************************//************************ Message Handling ***********************//*****************************************************************/LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){// Check any available messages from the queueswitch (message){case WM_DESTROY:PostQuitMessage(0);break;case WM_CLOSE:PostQuitMessage(0);break;}// Always return the message to the default window// procedure for further processingreturn DefWindowProc(hWnd, message, wParam, lParam);}/*****************************************************************//******************** Direct X Initialize ************************//*****************************************************************/bool initDirect3D( void ) {pD3D = NULL;pd3dDevice = NULL;// Create D3D Objectif ( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) {MessageBox( NULL, L"D3D Object Creation Failed", L"Error!", 0);return false;}//screenMode = MessageBox( NULL, L"Run in full screen?", L"Full Screen?", 4);//if ( screenMode == 0 ) MessageBox( NULL, L"Answer is 0", L"Hmm", 0);//if ( screenMode == 1 ) MessageBox( NULL, L"Answer is 1", L"Hmm", 0);//if ( screenMode == 2 ) MessageBox( NULL, L"Answer is 2", L"Hmm", 0);//if ( screenMode == 3 ) MessageBox( NULL, L"Answer is 3", L"Hmm", 0);D3DPRESENT_PARAMETERS d3dpp;ZeroMemory( &d3dpp, sizeof( d3dpp ) );d3dpp.Windowed = TRUE;d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;d3dpp.BackBufferCount = 1;d3dpp.BackBufferHeight = SCRN_HEIGHT;d3dpp.BackBufferWidth = SCRN_WIDTH;d3dpp.hDeviceWindow = wndHandle;// create the d3d deviceif( FAILED (pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,wndHandle,D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dpp,&pd3dDevice ))){MessageBox( NULL, L"Failed to create D3D Device", L"Error!", 0);return false;}surface = getSurfaceFromBitmap(L"sprites.bmp");if (surface == NULL){MessageBox( NULL, L"Failed to create Surface", L"Error!", 0);return false;}return true;}/****************************************************************************** Render(void)*****************************************************************************/void render(void){// This will hold the back bufferIDirect3DSurface9* backbuffer = NULL;//if( NULL == pd3dDevice )return;// Clear the back buffer to a black colorpd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );// Retrieve a pointer to the back bufferpd3dDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO, &backbuffer);// Loop through all the spritesfor ( int i = 0; i < 7; i++ ){RECT src;src = spriteStruct.srcRect;RECT destRect; // Create a temporary destination RECT// Fill the temporary RECT with data from// the current sprite structuredestRect.left = 100;//destRect.left = spriteStruct.posX;destRect.top = 100;//destRect.top = spriteStruct.posY;destRect.left = 200;//destRect.bottom = destRect.top + SPRITE_HEIGHT;destRect.right = 200;//destRect.right = destRect.left + SPRITE_WIDTH;// Draw the sprite to the back bufferif(FAILED(pd3dDevice->StretchRect( surface, &src, backbuffer, &destRect, D3DTEXF_NONE))){MessageBox( NULL, L"StretchRect Failed", L"Error!", 0);}}// Present the back buffer contents to the displaypd3dDevice->Present( NULL, NULL, NULL, NULL );}/*****************************************************************//****************** Clean Up (Release) Resources *****************//*****************************************************************/void cleanUp ( void ) {if( pd3dDevice != NULL ) pd3dDevice->Release( );if( pD3D != NULL ) pD3D->Release( );return;}/*********************************************************** getSurfaceFromBitmap**********************************************************/IDirect3DSurface9* getSurfaceFromBitmap(std::wstring filename){int height;int width;IDirect3DSurface9* newSurface = NULL;HRESULT hResult;D3DXIMAGE_INFO imageInfo; // holds details concerning this bitmap// Get the width and height info from this bitmaphResult = D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo);// Make sure that the call to D3DXGetImageInfoFromFile succeededif FAILED (hResult){MessageBox( NULL, L"Get info from file failed", L"Error!", 0);return NULL;}height = imageInfo.Height;width = imageInfo.Width;cout << height << "\n" << width;// Create the offscreen surface that will hold the bitmaphResult = pd3dDevice->CreateOffscreenPlainSurface(width, height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &newSurface,NULL );// Make sure that this function call did not fail; if it did,// exit this functionif ( FAILED( hResult ) ){MessageBox( NULL, L"Create offscreen surface failed", L"Error!", 0);return NULL;}// Load the bitmap into the surface that was created earlierhResult = D3DXLoadSurfaceFromFile( newSurface, NULL, NULL, filename.c_str( ), NULL, D3DX_DEFAULT, 0, NULL );if ( FAILED( hResult ) ) {MessageBox( NULL, L"Create surface from file", L"Error!", 0);return NULL;}return newSurface;}/************************************************************************************** bool initSprites(void)*************************************************************************************/bool initSprites(void){// Loop through 10 sprite structures and initialize themfor (int i = 0; i < 7; i++ ){spriteStruct.srcRect.top = 0;spriteStruct.srcRect.left = i * SPRITE_WIDTH;spriteStruct.srcRect.right = spriteStruct.srcRect.left + SPRITE_WIDTH;spriteStruct.srcRect.bottom = SPRITE_HEIGHT;spriteStruct.posX = rand()% SCRN_WIDTH - SPRITE_WIDTH;spriteStruct.posY = rand()% SCRN_HEIGHT - SPRITE_HEIGHT;}return true;}Thanks Malb :)   
  Your first problem is that you never call pd3dDevice->BeginScene() and pd3dDevice()->EndScene(). You need to do this before calling any code that uses the device to draw.Try that and see if it fixes your problem.   ;  Ahh I remember that now from a different tutorial, thanks I'll try it nowEDIT:Nope that hasn't fixed it. Doesn't make much sense does it :(   ;  The input for the function is probably wrong. I think your surfaces are invalid.From the documentation:Quote:StretchRect RestrictionsDriver support varies. See the section on driver support (below) to see which drivers support which source and destination formats. The source and destination surfaces must be created in the default memory pool. If filtering is specified, you must set the appropriate filter caps (see StretchRectFilterCaps in D3DCAPS9). Stretching is not supported between source and destination rectangles on the same surface. Stretching is not supported if the destination surface is an off-screen plain surface but the source is not. You many not stretch between source and destination rectangles if either surface is in a compressed format (see Using Compressed Textures (Direct3D 9)). Stretching supports color-space conversion from YUV to high-precision RGBA only. Since color conversion support is not supported by software emulation, use IDirect3D9::CheckDeviceFormatConversion to test the hardware for color conversion support.I don't know if you can access the backbuffer like that. Need some more experts in here x).   ;  The book shows to do strechRect like this:pd3dDevice->StretchRect( surface, src, backbuffer, destRect, D3DTEXF_NONEBut I realized that I needed to add the ampersands like this because the function asks for CONST RECT *sourceRect & *destRect:pd3dDevice->StretchRect( surface, &src, backbuffer, &destRect, D3DTEXF_NONE Could there be a problem because of that?? Won't work without the ampersands, the book must have been made whilst using a different version of VS C++.   ;  I figured it out!!! Silly mistake! If you look in the loop here:for ( int i = 0; i < 7; i++ ){RECT src;src = spriteStruct.srcRect;RECT destRect; // Create a temporary destination RECT// Fill the temporary RECT with data from// the current sprite structuredestRect.left = 100;//destRect.left = spriteStruct.posX;destRect.top = 100;//destRect.top = spriteStruct.posY;destRect.left = 200;//<------------------- LEFT INSTEAD OF BOTTOM!destRect.right = 200;//destRect.right = destRect.left + SPRITE_WIDTH;// Draw the sprite to the back bufferif(FAILED(pd3dDevice->StretchRect( surface, &src, backbuffer, &destRect, D3DTEXF_NONE))){MessageBox( NULL, L"StretchRect Failed", L"Error!", 0);}}When fiddling trying to resolve my first error I must have put in left instead of bottom hence bottom had no value!! Seems to have stopped the error. Now to figure out why its just showing a blank white screen!Thanks :)   ;  YAY! Got the images on the screen and even got them constantly changing position :D happy now. Shall probably be back soon :P   