BOOL cTexture::loadFromResource ( cGraphics *p_pGraphics, TCHAR* pSrcResource, unsigned p_transparent /*= 0*/, unsigned p_width /*= D3DX_DEFAULT*/, unsigned p_height /*= D3DX_DEFAULT*/, D3DFORMAT p_format /*= D3DFMT_UNKNOWN*/ ) { UNREFERENCED_PARAMETER(p_transparent); if( (m_pGraphics = p_pGraphics) == NULL ) return false; D3DXCreateTextureFromResourceEx( m_pGraphics->getD3DDevice(), NULL,pSrcResource, p_width, p_height, D3DX_DEFAULT, 0, p_format, D3DPOOL_MANAGED, D3DX_FILTER_TRIANGLE, D3DX_FILTER_TRIANGLE, 0, NULL, NULL, &m_pTexture); D3DSURFACE_DESC d3dsd; if( FAILED(m_pTexture->GetLevelDesc(0, &d3dsd)) ) return false; m_width = d3dsd.Width; m_height = d3dsd.Height; m_D3DFormat = d3dsd.Format; return TRUE; }
//-------------------------------------------------------------------------------------- HRESULT DialogResourceManager::CreateTexture9( UINT iTexture ) { HRESULT hr = S_OK; TextureNode* pTextureNode = m_TextureCache.at( iTexture ); D3DXIMAGE_INFO info; if( !pTextureNode->bFileSource ) { if( pTextureNode->nResourceID == 0xFFFF && pTextureNode->hResourceModule == (HMODULE) 0xFFFF ) { // Refactoring : DXUT 독립적으로 교체 hr = CreateGUITextureFromInternalArray9( m_pd3d9Device, &pTextureNode->pTexture9, &info ); if( FAILED(hr) ) return hr; } else { LPCWSTR pID = pTextureNode->nResourceID ? (LPCWSTR)(size_t)pTextureNode->nResourceID : pTextureNode->strFilename; // Create texture from resource hr = D3DXCreateTextureFromResourceEx( m_pd3d9Device, pTextureNode->hResourceModule, pID, D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, &info, NULL, &pTextureNode->pTexture9 ); if( FAILED(hr) ) return hr; } } else { // Make sure there's a texture to create if( pTextureNode->strFilename[0] == 0 ) return S_OK; // Create texture from file hr = D3DXCreateTextureFromFileEx( m_pd3d9Device, pTextureNode->strFilename, D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, &info, NULL, &pTextureNode->pTexture9 ); if( FAILED(hr) ) { wchar_t szTemp[50]; swprintf(szTemp, L"TextureCreateFromFile Failed\n %s", pTextureNode->strFilename); MessageBox(NULL, szTemp, L"ResourceManager error", 0); return hr; } } // Store dimensions pTextureNode->dwWidth = info.Width; pTextureNode->dwHeight = info.Height; return S_OK; }
INT LcD3D_TexLoadRscBmp(PDTX* pTexture, PDEV pDev, INT nResourceId , DWORD dColor , DIMG* pSrcInf , DWORD Filter , DWORD MipFilter , D3DFORMAT d3Fmt , DWORD Pool) { HRESULT hr; HINSTANCE hInst = NULL; UINT uMip = D3DX_DEFAULT; if(D3DX_FILTER_NONE == Filter || D3DX_FILTER_NONE == MipFilter) uMip = 1; hInst = GetModuleHandle(NULL); // DirectX Help에는 이렇게 나와 있다. // The resource being loaded must be of type RT_BITMAP or RT_RCDATA. // Resource type RT_RCDATA is used to load formats other than bitmaps (such as .tga, .jpg, and .dds). // 하지만 LcTex_TextureLoadRscCustom처럼 하는 것이 더 낫다. // 왜냐 하면 D3DXCreateTextureFromResourceEx함수는 리소스 타입을 전달할 아무런 인자가 없다. // 이 함수는 비트맵만 사용할 수 있다. hr=D3DXCreateTextureFromResourceEx(pDev , hInst , MAKEINTRESOURCE(nResourceId) , D3DX_DEFAULT , D3DX_DEFAULT , uMip , 0 , d3Fmt , (D3DPOOL)Pool , Filter, MipFilter , dColor, pSrcInf , NULL , pTexture ); if(FAILED(hr)) { pTexture = 0; return hr; } return 0; }
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; }