   Can anyone see what is wrong with the following code for my tile system
#include "windows.h"// include directx9#include &lt;d3d9.h&gt;#include &lt;d3dx9tex.h&gt;#include &lt;dxerr9.h&gt;// Constants.const COLUMNSINTILEFILE = 2;const TILESIZE = 32;const MAPCOLUMNCOUNT = 6;const MAPROWCOUNT = 6;const XOFFSET = 220;const YOFFSET = 120;HINSTANCE hInst; //Global Handle to hold the application instanceHWND wndHandle;  //Global variable to hold the window handleLPDIRECT3D9   pD3D;// the Direct3D ObjectLPDIRECT3DDEVICE9  pd3dDevice;// the Direct3D Device//forward declarationsbool initWindow(HINSTANCE hInstance);LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM);// DirectX functionsbool initDirect3D();void render(void);void CleanUpDirect3D();void PaintBackground();int ColumnRow2Sector(int c, int r, int numCols);HRESULT PlaceTile(IDirect3DSurface9* pBackBuffer,  IDirect3DSurface9* pTileSurface, int TileNumber,  int DstCol, int DstRow, int numTileCols, int numMapCols,  int tileSize, int xOffset, int yOffset);int TileNumber2SourceX(int TileNumber, int numCols, int tileSize);int TileNumber2SourceY(int TileNumber, int numCols, int tileSize);int Column2X(int col, int tileSize, int numCols);int Row2Y(int r, int tileSize);// Global variables.//HWND g_hWnd;IDirect3D9* g_pDirect3D = NULL;IDirect3DDevice9* g_pDirect3DDevice = NULL;IDirect3DSurface9* g_pBitmapSurface = NULL;HRESULT g_hResult = D3D_OK;char g_szErrorMsg[256];IDirect3DSurface9* g_pBackBuffer = NULL;char g_Sectors[] ={0,0,0,0,0,0,0,2,2,2,2,0,0,2,1,1,2,0,0,2,1,1,2,0,0,2,0,2,2,0,0,0,0,0,0,3};/********************************************************************** WinMain the main entry point for windows applications*********************************************************************/int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){//initialize the windowif (!initWindow(hInstance)){MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);return false;}if (!initDirect3D()){MessageBox(NULL, "Unable to init Direct3D", "ERROR", MB_OK);return false;}// Main 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{render();} }// release the device and the direct3D objectif( pd3dDevice != NULL) pd3dDevice-&gt;Release();if( pD3D != NULL)pD3D-&gt;Release();//CleanUpDirect3D();return(int)msg.wParam;}bool initWindow(HINSTANCE hInstance){WNDCLASSEX wcex;//fill in the WNDCLASS structure. this describes how the window will look to the systemwcex.cbSize = sizeof(WNDCLASSEX); // the size of the structurewcex.style = CS_HREDRAW | CS_VREDRAW; // the class stylewcex.lpfnWndProc = (WNDPROC) WndProc; // The window procedure callbackwcex.cbClsExtra = 0; //extra bytes to allocate for this classwcex.cbWndExtra = 0; //extra bytes to allocate for this instancewcex.hInstance = hInstance; //Handle the application to the instancewcex.hIcon = 0; //icon to associate with the applicationwcex.hCursor = LoadCursor(NULL, IDC_ARROW); // the default cursorwcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // the background colorwcex.lpszMenuName = NULL;wcex.lpszClassName = "PacMan2D"; //The Class name being createdwcex.hIconSm = 0; //the handle to the small iconRegisterClassEx(&wcex);//Create the windowwndHandle = CreateWindow("PacMan2D",//The window class to use"PacMan2D",//The TitleBar textWS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,//The Window styleCW_USEDEFAULT,//The starting xCoordinateCW_USEDEFAULT,//The starting yCoordinate640,//The pixel width of the window480,//The pixel height of the windowNULL,//The parent window. Null for desktopNULL,//The menu from the pop up applicationhInstance,//The handle to the application instanceNULL);//No values passed to the window//make sure that the window handle that is created is validif(!wndHandle)return false;//Display the window on the screenShowWindow(wndHandle, SW_SHOW);UpdateWindow(wndHandle);return true;}/********************************************************************** WndProc*********************************************************************/LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){//Check any available message from the queueswitch (message) {case WM_DESTROY:PostQuitMessage(0);break;case WM_PAINT:ValidateRect(hWnd, NULL);return 0;case WM_KEYDOWN:if( wParam == VK_ESCAPE ) {PostQuitMessage(0);break;}}//Always return the message to the default windowreturn DefWindowProc(hWnd, message, wParam, lParam);}/********************************************************************** initDirect3D* initializes direct3D*********************************************************************/bool initDirect3D(){pD3D = NULL;pd3dDevice = NULL;// create the directX objectif( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ){return false;}// fill the presentation parameters structureD3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) );d3dpp.Windowed = FALSE;d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;d3dpp.BackBufferCount  = 1;d3dpp.BackBufferHeight = 480;d3dpp.BackBufferWidth  = 640;d3dpp.hDeviceWindow    = wndHandle;// create a default directx deviceif( FAILED( pD3D-&gt;CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, wndHandle,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp, &pd3dDevice ) ) ){return false;}g_hResult = g_pDirect3DDevice-&gt;CreateOffscreenPlainSurface(64, 64,D3DFMT_X8R8G8B8,D3DPOOL_SYSTEMMEM,&g_pBitmapSurface,NULL);if (FAILED(g_hResult)){strcpy(g_szErrorMsg, "Error creating bitmap surface.");PostQuitMessage(WM_QUIT);}g_hResult = D3DXLoadSurfaceFromFile(g_pBitmapSurface, NULL, NULL,"image.bmp", NULL, D3DX_DEFAULT, 0, NULL);if (FAILED(g_hResult)){strcpy(g_szErrorMsg, "Couldn’t load bitmap file.");PostQuitMessage(WM_QUIT);}g_hResult = g_pDirect3DDevice-&gt;GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO, &g_pBackBuffer);if (FAILED(g_hResult)){strcpy(g_szErrorMsg, "Couldn’t get back buffer.");PostQuitMessage(WM_QUIT);}return true;}//////////////////////////////////////////////////////// CleanUpDirect3D()//////////////////////////////////////////////////////void CleanUpDirect3D(){if (g_pBackBuffer)g_pBackBuffer-&gt;Release();if (g_pBitmapSurface)g_pBitmapSurface-&gt;Release();if (g_pDirect3DDevice)g_pDirect3DDevice-&gt;Release();if (g_pDirect3D)g_pDirect3D-&gt;Release();}/********************************************************************** render*********************************************************************/void render(void){// check to make sure we have a valid Direct3D Deviceif( NULL == pd3dDevice )return;// Clear the backbuffer to a blue colorpd3dDevice-&gt;Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );PaintBackground();// Present the backbuffer contents to the displaypd3dDevice-&gt;Present( NULL, NULL, NULL, NULL );}//////////////////////////////////////////////////////// PaintBackground()//////////////////////////////////////////////////////void PaintBackground(){g_pDirect3DDevice-&gt;Clear(0, NULL, D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0), 1.0f, 0);for (int Row = 0; Row &lt; MAPROWCOUNT; ++Row){for (int Col = 0; Col &lt; MAPCOLUMNCOUNT; ++Col){int iSector = ColumnRow2Sector(Col, Row, MAPCOLUMNCOUNT);int TileNumber = g_Sectors[iSector];g_hResult = PlaceTile(g_pBackBuffer, g_pBitmapSurface,TileNumber, Col, Row, COLUMNSINTILEFILE, MAPCOLUMNCOUNT,TILESIZE, XOFFSET, YOFFSET);if (FAILED(g_hResult)){strcpy(g_szErrorMsg, "Error drawing tiles.");PostQuitMessage(WM_QUIT);}}}}//////////////////////////////////////////////////////// PlaceTile()//////////////////////////////////////////////////////HRESULT PlaceTile(IDirect3DSurface9* pBackBuffer,  IDirect3DSurface9* pTileSurface, int TileNumber,  int DstCol, int DstRow, int numTileCols, int numMapCols,  int tileSize, int xOffset, int yOffset){RECT SrcTileRect;POINT DstPoint;SrcTileRect.left = TileNumber2SourceX(TileNumber,numTileCols, tileSize);SrcTileRect.right = SrcTileRect.left + tileSize;SrcTileRect.top = TileNumber2SourceY(TileNumber,numTileCols, tileSize);SrcTileRect.bottom = SrcTileRect.top + tileSize;DstPoint.x = Column2X(DstCol, tileSize, numMapCols) + xOffset;DstPoint.y = Row2Y(DstRow, tileSize) + yOffset;HRESULT hResult = g_pDirect3DDevice-&gt;UpdateSurface(g_pBitmapSurface, &SrcTileRect, pBackBuffer, &DstPoint);return hResult;}//////////////////////////////////////////////////////// ColumnRow2Sector()//////////////////////////////////////////////////////int ColumnRow2Sector(int c, int r, int numCols){return r * numCols + c;}//////////////////////////////////////////////////////// TileNumber2SourceX()//////////////////////////////////////////////////////int TileNumber2SourceX(int TileNumber, int numCols, int tileSize){return (TileNumber - ((int)(TileNumber / numCols))* numCols) * tileSize;}//////////////////////////////////////////////////////// TileNumber2SourceY()//////////////////////////////////////////////////////int TileNumber2SourceY(int TileNumber, int numCols, int tileSize){return ((int)(TileNumber / numCols)) * tileSize;}//////////////////////////////////////////////////////// Column2X()//////////////////////////////////////////////////////int Column2X(int col, int tileSize, int numCols){return (col - ((int)((col / numCols)) *numCols)) * tileSize;}//////////////////////////////////////////////////////// Row2Y()//////////////////////////////////////////////////////int Row2Y(int r, int tileSize){return r * tileSize;}
  I edited your post to make it use the [ source ] tags.   ;  I was very tempted to post "No", but I resisted.What's wrong with it? Any debug output? Does it compile? Does it link? Any crashes? Are you using the debug DX runtimes? What SDK version? What compiler? Does it seem to work and you're asking is if we see any problems with it?   ;  thank you for encapsulating the code, i am using visual 6, and it builds fine no errors what so ever, when i try to lrun the program i just get a black screen that hangs there, not the tile images that i want?   ;  Put a breakpoint in your place tile routine and step through it to see what happens.I don't know how Direct3D's present routine works, but you may wish to check the arguments for it.   