ModelPtr XFileLoader::OpenFromResource( int resourceID,float scale ) { HRESULT hr = S_OK; m_fileName = _T(""); TCHAR path[MAX_PATH]; _tcscpy_s( path,MAX_PATH,m_fileName.c_str() ); PathRemoveFileSpec( path ); PathAddBackslash( path ); m_path = path; m_scale = scale; Graphics* graphics = Graphics::GetInstance(); IDirect3DDevice9Ptr pD3DDevice = graphics->GetDirect3DDevice(); m_pAdjacencyBuf = NULL; m_pMaterialBuf = NULL; m_pEffectInstancesBuf = NULL; m_Materials = 0; m_pD3DMesh = NULL; ModelPtr pModel; hr = D3DXLoadMeshFromXResource( NULL, (LPSTR)MAKEINTRESOURCE( resourceID ), (LPSTR)RT_RCDATA, D3DXMESH_SYSTEMMEM, pD3DDevice, &m_pAdjacencyBuf, &m_pMaterialBuf, &m_pEffectInstancesBuf, &m_Materials, &m_pD3DMesh ); if( FAILED(hr) ) { return ModelPtr(); } return ModelPtr( new Model( CreateMeshContainer() ) ); }
BOOL initD3dStuff(HWND hWnd) { void *tempVoid; LPD3DXBUFFER shipMB; D3DXMATERIAL *tempMaterials; int transparency = 127; CUSTOMVERTEX customVertex1[] = { // triangle {0.8f, -0.8f, 0.0f, 0, 0, 1, D3DCOLOR_ARGB(255, 255, 255, 255), 0, 0}, {0.0f, 0.8f, 0.0f, 0, 0, 1, D3DCOLOR_ARGB(255, 255, 255, 255), 1, 0}, {-0.8f, -0.8f, 0.0f, 0, 0, 1, D3DCOLOR_ARGB(255, 255, 255, 255), 1, 1} }; CUSTOMVERTEX customVertex2[] = { // side 1 {-3.0f, 3.0f, -3.0f, 0, 0, -1, D3DCOLOR_ARGB(transparency, 255, 255, 255), 0, 0}, {3.0f, 3.0f, -3.0f, 0, 0, -1, D3DCOLOR_ARGB(transparency, 255, 255, 255), 0, 1}, {-3.0f, -3.0f, -3.0f, 0, 0, -1, D3DCOLOR_ARGB(transparency, 255, 255, 255), 1, 0}, {3.0f, -3.0f, -3.0f, 0, 0, -1, D3DCOLOR_ARGB(transparency, 255, 255, 255), 1, 1}, // side 2 {-3.0f, 3.0f, 3.0f, 0, 0, 1, D3DCOLOR_ARGB(transparency, 255, 255, 255), 0, 0}, {-3.0f, -3.0f, 3.0f, 0, 0, 1, D3DCOLOR_ARGB(transparency, 255, 255, 255), 0, 1}, {3.0f, 3.0f, 3.0f, 0, 0, 1, D3DCOLOR_ARGB(transparency, 255, 255, 255), 1, 0}, {3.0f, -3.0f, 3.0f, 0, 0, 1, D3DCOLOR_ARGB(transparency, 255, 255, 255), 1, 1}, // side 3 {-3.0f, 3.0f, 3.0f, 0, 1, 0, D3DCOLOR_ARGB(transparency, 255, 255, 255), 0, 0}, {3.0f, 3.0f, 3.0f, 0, 1, 0, D3DCOLOR_ARGB(transparency, 255, 255, 255), 0, 1}, {-3.0f, 3.0f, -3.0f, 0, 1, 0, D3DCOLOR_ARGB(transparency, 255, 255, 255), 1, 0}, {3.0f, 3.0f, -3.0f, 0, 1, 0, D3DCOLOR_ARGB(transparency, 255, 255, 255), 1, 1}, // side 4 {-3.0f, -3.0f, 3.0f, 0, -1, 0, D3DCOLOR_ARGB(transparency, 255, 255, 255), 0, 0}, {-3.0f, -3.0f, -3.0f, 0, -1, 0, D3DCOLOR_ARGB(transparency, 255, 255, 255), 0, 1}, {3.0f, -3.0f, 3.0f, 0, -1, 0, D3DCOLOR_ARGB(transparency, 255, 255, 255), 1, 0}, {3.0f, -3.0f, -3.0f, 0, -1, 0, D3DCOLOR_ARGB(transparency, 255, 255, 255), 1, 1}, // side 5 {3.0f, 3.0f, -3.0f, 1, 0, 0, D3DCOLOR_ARGB(transparency, 255, 255, 255), 0, 0}, {3.0f, 3.0f, 3.0f, 1, 0, 0, D3DCOLOR_ARGB(transparency, 255, 255, 255), 0, 1}, {3.0f, -3.0f, -3.0f, 1, 0, 0, D3DCOLOR_ARGB(transparency, 255, 255, 255), 1, 0}, {3.0f, -3.0f, 3.0f, 1, 0, 0, D3DCOLOR_ARGB(transparency, 255, 255, 255), 1, 1}, // side 6 // I inverted the texture coords to get my image right {-3.0f, 3.0f, -3.0f, -1, 0, 0, D3DCOLOR_ARGB(transparency, 255, 255, 255), 0, 0}, {-3.0f, -3.0f, -3.0f, -1, 0, 0, D3DCOLOR_ARGB(transparency, 255, 255, 255), 0, 1}, {-3.0f, 3.0f, 3.0f, -1, 0, 0, D3DCOLOR_ARGB(transparency, 255, 255, 255), 1, 0}, {-3.0f, -3.0f, 3.0f, -1, 0, 0, D3DCOLOR_ARGB(transparency, 255, 255, 255), 1, 1} }; // predefined simple D3DX mesh objects D3DXCreateBox(d3d9dev, 7.0f, 3.0f, 3.0f, &mesh1, NULL); D3DXCreateSphere(d3d9dev, 0.3f, 20, 10, &mesh2, NULL); // one complete mesh loading (vertices, materials and textures) if (D3DXLoadMeshFromXResource(NULL, (LPCSTR)MAKEINTRESOURCE(IDR_MESHX1), "meshx", D3DXMESH_MANAGED, d3d9dev, NULL, &shipMB, NULL, &numMaterials, &mesh3) != D3D_OK) { MessageBox(hWnd, "craft.x could not be loaded", "Error", MB_ICONERROR); PostQuitMessage(0); return FALSE; } /* load from file if (D3DXLoadMeshFromX("craft.x", D3DXMESH_MANAGED, d3d9dev, NULL, &shipMB, NULL, &numMaterials, &mesh3) != D3D_OK) { MessageBox(hWnd, "craft.x file not found", "Error", MB_ICONERROR); PostQuitMessage(0); } */ tempMaterials = (D3DXMATERIAL *)shipMB->GetBufferPointer(); materialX = new D3DMATERIAL9[numMaterials]; planeTexture = new LPDIRECT3DTEXTURE9[numMaterials]; for (DWORD i = 0; i < numMaterials; i++) { materialX[i] = tempMaterials[i].MatD3D; materialX[i].Ambient = materialX[i].Diffuse; /* dont need the following cause I use resources USES_CONVERSION; // allows certain string conversions // if there is a texture to load, load it if(FAILED(D3DXCreateTextureFromFile(d3d9dev, CA2W(tempMaterials[i].pTextureFilename), &planeTexture[i]))) */ planeTexture[i] = NULL; // if there is no texture, set the texture to NULL } // load the textures (embedded in the executable) we will use if (D3DXCreateTextureFromResource(d3d9dev, NULL, (LPCSTR)MAKEINTRESOURCE(IDB_BITMAP1), &planeTexture[2]) != D3D_OK) { MessageBox(hWnd, "wings.bmp could not be loaded", "Error", MB_ICONERROR); PostQuitMessage(0); return FALSE; } if (D3DXCreateTextureFromResource(d3d9dev, NULL, (LPCSTR)MAKEINTRESOURCE(IDB_BITMAP2), &planeTexture[6]) != D3D_OK) { MessageBox(hWnd, "bihull.bmp could not be loaded", "Error", MB_ICONERROR); PostQuitMessage(0); return FALSE; } planeTexture[0] = planeTexture[1] = planeTexture[3] = planeTexture[4] = planeTexture[6]; if (D3DXCreateTextureFromResourceEx(d3d9dev, // the device pointer NULL, // module (executable containing the resource) (LPCSTR)MAKEINTRESOURCE(IDB_BITMAP3), // resource //"logo1.bmp", // the file name D3DX_DEFAULT, // default width D3DX_DEFAULT, // default height D3DX_DEFAULT, // no mip mapping NULL, // regular usage D3DFMT_A8R8G8B8, // 32-bit pixels with alpha D3DPOOL_MANAGED, // typical memory handling D3DX_DEFAULT, // no filtering D3DX_DEFAULT, // no mip filtering D3DCOLOR_ARGB(127, 255, 0, 255), // the hot-pink color key NULL, // no image info struct NULL, // not using 256 colors &texture1) != D3D_OK) { MessageBox(hWnd, "logo1.bmp could not be loaded", "Error", MB_ICONERROR); PostQuitMessage(0); return FALSE; } /* if (D3DXCreateTextureFromResource(d3d9dev, NULL, (LPCSTR)MAKEINTRESOURCE(IDB_BITMAP3), &texture1) != D3D_OK) { MessageBox(hWnd, "logo1.bmp could not be loaded", "Error", MB_ICONERROR); PostQuitMessage(0); return FALSE; } */ if (D3DXCreateTextureFromResourceEx(d3d9dev, // the device pointer NULL, // module (executable containing the resource) (LPCSTR)MAKEINTRESOURCE(IDR_LOGO2PNG), // resource //"logo2.png", // the file name D3DX_DEFAULT, // default width D3DX_DEFAULT, // default height D3DX_DEFAULT, // no mip mapping NULL, // regular usage D3DFMT_A8R8G8B8, // 32-bit pixels with alpha D3DPOOL_MANAGED, // typical memory handling D3DX_DEFAULT, // no filtering D3DX_DEFAULT, // no mip filtering D3DCOLOR_ARGB(127, 255, 0, 255), // the hot-pink color key NULL, // no image info struct NULL, // not using 256 colors &texture2) != D3D_OK) { MessageBox(hWnd, "logo2.png could not be loaded", "Error", MB_ICONERROR); PostQuitMessage(0); return FALSE; } /* if (D3DXCreateTextureFromResource(d3d9dev, NULL, (LPCSTR)MAKEINTRESOURCE(IDR_LOGO2PNG), &texture2) != D3D_OK) { MessageBox(hWnd, "logo2.png could not be loaded", "Error", MB_ICONERROR); PostQuitMessage(0); return FALSE; } */ if (D3DXCreateTextureFromResourceEx(d3d9dev, // the device pointer NULL, // module (executable containing the resource) (LPCSTR)MAKEINTRESOURCE(IDR_LOGO3PNG), // resource //"logo3.png", // the file name D3DX_DEFAULT, // default width D3DX_DEFAULT, // default height D3DX_DEFAULT, // no mip mapping NULL, // regular usage D3DFMT_A8R8G8B8, // 32-bit pixels with alpha D3DPOOL_MANAGED, // typical memory handling D3DX_DEFAULT, // no filtering D3DX_DEFAULT, // no mip filtering D3DCOLOR_ARGB(127, 255, 0, 255), // the hot-pink color key NULL, // no image info struct NULL, // not using 256 colors &texture3) != D3D_OK) { MessageBox(hWnd, "logo3.png could not be loaded", "Error", MB_ICONERROR); PostQuitMessage(0); return FALSE; } /* if (D3DXCreateTextureFromResource(d3d9dev, NULL, (LPCSTR)MAKEINTRESOURCE(IDR_LOGO3PNG), &texture3) != D3D_OK) { MessageBox(hWnd, "logo3.png could not be loaded", "Error", MB_ICONERROR); PostQuitMessage(0); return FALSE; } */ // sprite D3DXCreateSprite(d3d9dev, &sprite); // sprite texture if (D3DXCreateTextureFromResourceEx(d3d9dev, // the device pointer NULL, // module (executable containing the resource) (LPCSTR)MAKEINTRESOURCE(IDR_LEGENDPNG), // resource //"legend.png", // the file name D3DX_DEFAULT, // default width D3DX_DEFAULT, // default height D3DX_DEFAULT, // no mip mapping NULL, // regular usage D3DFMT_A8R8G8B8, // 32-bit pixels with alpha D3DPOOL_MANAGED, // typical memory handling D3DX_DEFAULT, // no filtering D3DX_DEFAULT, // no mip filtering D3DCOLOR_XRGB(255, 0, 255), // the hot-pink color key NULL, // no image info struct NULL, // not using 256 colors &legendTexture) != D3D_OK) { MessageBox(hWnd, "legend.png could not be loaded", "Error", MB_ICONERROR); PostQuitMessage(0); return FALSE; } // sprite legend D3DXCreateSprite(d3d9dev, &legendSprite); // its texture if (D3DXCreateTextureFromResourceEx(d3d9dev, // the device pointer NULL, (LPCSTR)MAKEINTRESOURCE(IDR_SPRITEPNG), //"sprite.png", // the file name D3DX_DEFAULT, // default width D3DX_DEFAULT, // default height D3DX_DEFAULT, // no mip mapping NULL, // regular usage D3DFMT_A8R8G8B8, // 32-bit pixels with alpha D3DPOOL_MANAGED, // typical memory handling D3DX_DEFAULT, // no filtering D3DX_DEFAULT, // no mip filtering D3DCOLOR_XRGB(255, 0, 255), // the hot-pink color key NULL, // no image info struct NULL, // not using 256 colors &spriteTexture) != D3D_OK) { MessageBox(hWnd, "panel.png could not be loaded", "Error", MB_ICONERROR); PostQuitMessage(0); return FALSE; } // triangle d3d9dev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &d3d9VertexBuffer1, NULL); d3d9VertexBuffer1->Lock(0, 0, &tempVoid, 0); if (memcpy(tempVoid, customVertex1, sizeof(customVertex1)) == NULL) MessageBox(NULL, "Error", "error", MB_OK); d3d9VertexBuffer1->Unlock(); // cube d3d9dev->CreateVertexBuffer(24*sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &d3d9VertexBuffer2, NULL); d3d9VertexBuffer2->Lock(0, 0, &tempVoid, 0); memcpy(tempVoid, customVertex2, sizeof(customVertex2)); d3d9VertexBuffer2->Unlock(); return TRUE; }