   Need some help with D3DXLoadSurfaceFromFile
  Hi!I'm pretty new to DirectX, and I have some problems...First of all:I have three files.initdirect.hinitdirec.cppwinmain.cppinitdirect.h#include <windows.h>struct spriteStruct{RECT srcRect;// positionint posX; // the X coordinateint posY; // the Y coordinate};class initdirect{public:static void addItemToList(std::string item);static void render(void);static bool initWindow(HINSTANCE hInstance);static bool initDirect3D(void);static void cleanUp();static void flushVector();static LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM);static IDirect3DSurface9* getSurfaceFromBitmap(std::string filename);};initdirect.cpp#include <windows.h> #include <string>#include <d3d9.h>#include <d3dx9.h>#include <vector>#include <fstream>#include "initdirect.h"#pragma comment(lib, "d3d9.lib")#pragma comment(lib, "d3dx9.lib")#define SPRITE_WIDTH 48#define SPRITE_HEIGHT 48#define SCRN_WIDTH 800#define SCRN_HEIGHT 600HINSTANCE hInst; HWND wndHandle; using namespace std;char *message = "HELLO WORLD";int i;WCHAR * LoadFile;WCHAR * szTitle;WCHAR * szWindowClass;WCHAR * szFileName;LPDIRECT3D9 pD3D;LPDIRECT3DDEVICE9 pd3dDevice;std::vector<std::string> adapterDetails;IDirect3DSurface9* surface;spriteStruct sstruct[10];bool initdirect::initWindow( HINSTANCE hInstance ){WNDCLASSEX wcex;szWindowClass = (WCHAR*)L"DirectXExample";szTitle = (WCHAR*)L"DirectXExample";wcex.cbSize = sizeof(WNDCLASSEX);wcex.style = CS_HREDRAW | CS_VREDRAW;wcex.lpfnWndProc = (WNDPROC)(initdirect::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 = szTitle; wcex.hIconSm = 0; RegisterClassEx(&wcex);  wndHandle = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);if (!wndHandle)return false;ShowWindow(wndHandle, SW_SHOW);UpdateWindow(wndHandle);return true;}LRESULT CALLBACK initdirect::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){switch (message){case WM_DESTROY:PostQuitMessage(0);break;}return DefWindowProc(hWnd, message, wParam, lParam);}bool initdirect::initDirect3D(void){pD3D = NULL;pd3dDevice = NULL;if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ){return false;}HRESULT hResult;D3DADAPTER_IDENTIFIER9 ident;pD3D->GetAdapterIdentifier(D3DADAPTER_DEFAULT, 0, &ident);D3DPRESENT_PARAMETERS d3dpp;ZeroMemory( &d3dpp, sizeof( d3dpp ) );d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;d3dpp.Windowed = TRUE;d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;d3dpp.BackBufferCount = 1;d3dpp.BackBufferHeight = 800;d3dpp.BackBufferWidth = 600;d3dpp.hDeviceWindow = wndHandle;if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, wndHandle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice ) ) ){return false;}surface = initdirect::getSurfaceFromBitmap("sprite.bmp");if (surface == NULL) return false;return true;}void initdirect::render(void){IDirect3DSurface9* backbuffer = NULL;if( NULL == pd3dDevice )return;pd3dDevice->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 < 10; i++ ){RECT destRect; // Create a temporary destination RECTdestRect.left = sstruct.posX;destRect.top = sstruct.posY;destRect.bottom = destRect.top + SPRITE_HEIGHT;destRect.right = destRect.left + SPRITE_WIDTH;// Draw the sprite to the back bufferpd3dDevice->StretchRect( surface, &sstruct->srcRect, backbuffer, &destRect, D3DTEXF_NONE);}// Present the back buffer contents to the displaypd3dDevice->Present( NULL, NULL, NULL, NULL );}void initdirect::cleanUp (void){if( pd3dDevice != NULL )pd3dDevice->Release( );if( pD3D != NULL )pD3D->Release( );}IDirect3DSurface9* initdirect::getSurfaceFromBitmap(std::string filename){HRESULT hResult;IDirect3DSurface9* surface = NULL;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) return NULL;// Create the offscreen surface that will hold the bitmaphResult = pd3dDevice->CreateOffscreenPlainSurface( imageInfo.Width, imageInfo.Height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL );// Make sure that this function call did not fail; if it did,// exit this functionif ( FAILED( hResult ) ) return NULL;// Load the bitmap into the surface that was created earlierhResult = D3DXLoadSurfaceFromFile( surface, NULL, NULL, filename.c_str(), NULL, D3DX_DEFAULT, 0, NULL );if ( FAILED( hResult ) ) return NULL;return surface;}bool initSprites(void){// Loop through 10 sprite structures and initialize themfor (int i = 0; i < 10; i++ ){sstruct.srcRect.top = 0;sstruct.srcRect.left = i * SPRITE_WIDTH;sstruct.srcRect.right = sstruct.srcRect.left + SPRITE_WIDTH;sstruct.srcRect.bottom = SPRITE_HEIGHT;sstruct.posX = rand()% SCRN_WIDTH - SPRITE_WIDTH;sstruct.posY = rand()% SCRN_HEIGHT - SPRITE_HEIGHT;}return true;}winmain.cpp#include <windows.h> #include <string>#include <d3d9.h>#include <d3dx9.h>#include <vector>#include <fstream>#include "initdirect.h"#ifdef _DEBUG# pragma comment( lib, "d3dx9d.lib" )#else# pragma comment( lib, "d3dx9.lib" )#endifusing namespace std;INT WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){if ( !initdirect::initWindow (hInstance) ) return false;if ( !initdirect::initDirect3D()) return false;MSG msg;ZeroMemory( &msg, sizeof (msg) );while( msg.message!=WM_QUIT ){if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ){TranslateMessage ( &msg );DispatchMessage ( &msg );}else{initdirect::render();}}initdirect::cleanUp();return (int) msg.wParam;}The problem is two sided... First of all, I get these two error messages, and this warning:Error2error C2664: 'D3DXGetImageInfoFromFileW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'Error3error C2664: 'D3DXLoadSurfaceFromFileW' : cannot convert parameter 4 from 'const char *' to 'LPCWSTR' Warning1warning C4101: 'hResult' : unreferenced local variable(Line 157, 166 and 89 in initdirect.cpp)If I cast the LPCWSTR like this:hResult = D3DXGetImageInfoFromFile((LPCWSTR)filename.c_str(), &imageInfo);hResult = D3DXLoadSurfaceFromFile( surface, NULL, NULL, (LPCWSTR)filename.c_str(), NULL, D3DX_DEFAULT, 0, NULL );then the surface gets back with a NULL.So, do you have any tips, what I'm doing wrong? Because it's a bit irritating, I try to correct this for at least 20 hours... Please, I really need the help...   
  The problem is that the function expects a wide character string (a string of wchar_t) and you're sending it a string of char's.  You might want to have a look at this.   ;  Well, it looks quite good... I made some changes:hResult = D3DXGetImageInfoFromFile((wchar_t*)filename.c_str(), &imageInfo);hResult = D3DXLoadSurfaceFromFile( surface, NULL, NULL, (wchar_t*)filename.c_str(), NULL, D3DX_DEFAULT, 0, NULL );But... The getSurfaceFromBitmap still returns with a NULL... It looks like it's first hResult didn't get something...That's the questionable part:hResult = D3DXGetImageInfoFromFile((wchar_t*)filename.c_str(), &imageInfo);if FAILED (hResult) return NULL;;  BUMP...Sorry for it, it's pretty important to me, and I can't find any solution...   ;  Did you read what I linked you to? This is not a problem that can be solved with casting. That function is expecting something: a string of WCHAR's. You giving it a string of CHAR's and saying "no trust me, it's actually WCHAR's".  This will never work, because the function will attempt to read that data as if there were actually WCHAR's in that location (but there really isn't).You have three main options:1.  Send that function a string of WCHAR's.  This means you'll want to use std::wstring instead of std::string, and also prefix your string literals with an "L".2.  Directly call the ANSI version of that function, which is named "D3DXLoadSurfaceFromFileA".  That function takes a string of CHAR's. 3.  Change your Character Set in your VC++ project properties, so that it uses multi-byte characters instead of Unicode.  This will cause "D3DXLoadSurfaceFromFile" to resolve to "D3DXLoadSurfaceFromFileA", instead of "D3DXLoadSurfaceFromFileW".   ;  I have chosen the secound version, and it's kind of working... Now it shows a black screen in the window... I know, I'm kind of annoying, but it's almost midnight at me...It looks like the PeekMessage comes back with this:0x0015f748 {msg=0x0000c03b wp=0x00000011 lp=0x00000000}And it says the hwnd is unused, "Error: expression cannot be evaluated"...   ;  Ok... Then if there's nobody, who knows how to solve, or anything, could somebody give some tips for other sprite using methods? I want to make some kind of remake for the first "Sid Meier's Civilization"... And any tips would be great. What am I thinking:- Other sprite loading methods- Other rendering methodsPlease, if you know something, tell me...Forgot to tell: Using Visual Studio 2008...[Edited by - Kichigai on June 16, 2009 2:20:45 AM]