//============================================================== // <CImage>이미지를 로드하기: // 큰 표면을 작성하여 이미지를 로드해, // 이것을 256x256 픽셀씩 분할하여 텍스쳐 위에 복사함. // UpdateSurface나 StretchRect는 사용할 수 없으므로 // (D3DPOOL_DEFAULT의 텍스쳐에는 복사할 수 있으나 // 이 클래스에서는 D3DPOOL_MANAGED을 사용하기 때문에) // LockRect와 memcpy를 사용하여 복사함. void CImage::LoadFromFile(wstring file_name) { LPDIRECT3DSURFACE9 surface; D3DLOCKED_RECT slr; if (SUCCEEDED(Device->CreateOffscreenPlainSurface( XCount*256, YCount*256, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &surface, NULL)) ) { if (SUCCEEDED(D3DXLoadSurfaceFromFile( surface, NULL, NULL, file_name.c_str(), NULL, D3DX_FILTER_NONE, 0, NULL)) && SUCCEEDED(surface->LockRect( &slr, NULL, D3DLOCK_READONLY)) ) { for (int yc=0, i=0; yc<YCount; yc++) { for (int xc=0; xc<XCount; xc++, i++) { if (!Textures[i]) continue; D3DLOCKED_RECT tlr; if (SUCCEEDED(Textures[i]->LockRect(0, &tlr, NULL, 0))) { int x=xc*256, y=yc*256; for (int line=0; line<256; line++) { memcpy( (char*)tlr.pBits+tlr.Pitch*line, (char*)slr.pBits+x*4+slr.Pitch*(y+line), 256*4); } Textures[i]->UnlockRect(0); } } } surface->UnlockRect(); } surface->Release(); } }
int LoadBitmapToSurface(char* PathName, LPDIRECT3DSURFACE8* ppSurface, LPDIRECT3DDEVICE8 pDevice){ HRESULT r; HBITMAP hBitmap; BITMAP Bitmap; hBitmap = (HBITMAP)LoadImage(NULL, PathName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION); if(hBitmap == NULL){ SetError("Unable to load bitmap"); return E_FAIL; } GetObject(hBitmap, sizeof(BITMAP), &Bitmap); DeleteObject(hBitmap);//we only needed it for the header info to create a D3D surface //create surface for bitmap r=pDevice->CreateImageSurface(Bitmap.bmWidth, Bitmap.bmHeight, D3DFMT_X8R8G8B8, ppSurface); if(FAILED(r)){ SetError("Unable to create surface for bitmap load"); return E_FAIL; } //load bitmap onto surface r = D3DXLoadSurfaceFromFile(*ppSurface, NULL, NULL, PathName, NULL, D3DX_FILTER_NONE, 0, NULL); if(FAILED(r)){ SetError("Unable to laod file to surface"); return E_FAIL; } return S_OK; }
LPDIRECT3DSURFACE9 LoadSurface(char *filename, D3DCOLOR transcolor) { LPDIRECT3DSURFACE9 image = NULL; D3DXIMAGE_INFO info; HRESULT result; result = D3DXGetImageInfoFromFile(filename, &info); result = d3ddev->CreateOffscreenPlainSurface( info.Width, info.Height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &image, NULL); result = D3DXLoadSurfaceFromFile( image, NULL, NULL, filename, NULL, D3DX_DEFAULT, transcolor, NULL); return image; }
HRESULT InitD3D( HWND hWnd ) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) { MessageBoxA(NULL, "Create D3D9 object failed!", "Error", 0) ; return E_FAIL; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; // use window mode, not full screen d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Create device if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { MessageBoxA(NULL, "Create D3D9 device failed!", "Error", 0) ; return E_FAIL; } // Disable lighting, since we didn't specify color for vertex g_pd3dDevice->SetRenderState( D3DRS_LIGHTING , FALSE ); // Create teapot D3DXCreateTeapot(g_pd3dDevice, &g_pTeapotMesh, NULL) ; // wire frame g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME); // Load surface from file HRESULT hr = D3DXLoadSurfaceFromFile( g_pBackgroundSurface, NULL, NULL, "chessboard.jpg", NULL, D3DX_DEFAULT, 0, NULL ); if(FAILED(hr)) { //D3DERR_INVALIDCALL D3DXERR_INVALIDDATA MessageBox(NULL, "Create surface from file failed!", "Error", 0); return E_FAIL; } return S_OK; }
bool Surface::loadSurface(LPDIRECT3DDEVICE9 device, std::string filename) { imageScale = 100; //set our image scale to 100% //record the height and width HRESULT hResult; // Get the width and height of the image D3DXIMAGE_INFO imageInfo; hResult = D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo); if FAILED (hResult){ return false; } height = imageInfo.Height; width = imageInfo.Width; //create the Off Screen Surface hResult = device->CreateOffscreenPlainSurface(width, //surface width height, //surface height D3DFMT_A8R8G8B8, //surface format, D3DFMT_A8R8G8B8 is a 32 bit format with 8 alpha bits D3DPOOL_DEFAULT, //create it in the default memory pool &surface, //the pointer to our surface NULL ); if (FAILED(hResult)) return false; //load the surface from the a file hResult = D3DXLoadSurfaceFromFile(surface, //the surface we just created NULL, //palette entry, NULL for non 256 color formats NULL, //dest rect, NULL for the whole surface filename.c_str(), // the file we wish to load NULL, // Source Rect, NULL for the whole file D3DX_DEFAULT, //Filter 0, // Color key, color that should be used as transparent. NULL // pointer to a D3DXIMAGE_INFO structure, for holding info about the image. ); if (FAILED(hResult)) return false; //set rects destRect.left = 0; destRect.top = 0; destRect.bottom = destRect.top + height; destRect.right = destRect.left + width; srcRect.left = 0; srcRect.top = 0; srcRect.bottom = destRect.top + height; srcRect.right = destRect.left + width; return true; }
void Material::SetCubeTex(string filenamePX, string filenameNX, string filenamePY, string filenameNY, string filenamePZ, string filenameNZ) { if(cubeTex) SAFE_RELEASE(cubeTex); D3DDevice->CreateCubeTexture(cubeTexSize, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &cubeTex, NULL); IDirect3DSurface9 *cubeTexSurf; cubeTex->GetCubeMapSurface(D3DCUBEMAP_FACE_POSITIVE_X, 0, &cubeTexSurf); D3DXLoadSurfaceFromFile(cubeTexSurf, NULL, NULL, filenamePX.c_str(), NULL, D3DX_DEFAULT, 0, NULL); cubeTex->GetCubeMapSurface(D3DCUBEMAP_FACE_NEGATIVE_X, 0, &cubeTexSurf); D3DXLoadSurfaceFromFile(cubeTexSurf, NULL, NULL, filenameNX.c_str(), NULL, D3DX_DEFAULT, 0, NULL); cubeTex->GetCubeMapSurface(D3DCUBEMAP_FACE_POSITIVE_Y, 0, &cubeTexSurf); D3DXLoadSurfaceFromFile(cubeTexSurf, NULL, NULL, filenamePY.c_str(), NULL, D3DX_DEFAULT, 0, NULL); cubeTex->GetCubeMapSurface(D3DCUBEMAP_FACE_NEGATIVE_Y, 0, &cubeTexSurf); D3DXLoadSurfaceFromFile(cubeTexSurf, NULL, NULL, filenameNY.c_str(), NULL, D3DX_DEFAULT, 0, NULL); cubeTex->GetCubeMapSurface(D3DCUBEMAP_FACE_POSITIVE_Z, 0, &cubeTexSurf); D3DXLoadSurfaceFromFile(cubeTexSurf, NULL, NULL, filenamePZ.c_str(), NULL, D3DX_DEFAULT, 0, NULL); cubeTex->GetCubeMapSurface(D3DCUBEMAP_FACE_NEGATIVE_Z, 0, &cubeTexSurf); D3DXLoadSurfaceFromFile(cubeTexSurf, NULL, NULL, filenameNZ.c_str(), NULL, D3DX_DEFAULT, 0, NULL); SAFE_RELEASE(cubeTexSurf); }
void CDxtexDoc::OpenCubeFace(D3DCUBEMAP_FACES FaceType) { HRESULT hr; CString fileName; LPDIRECT3DSURFACE9 psurfOrig = NULL; LPDIRECT3DSURFACE9 psurfNew = NULL; if (!IsCubeMap()) return; hr = ((LPDIRECT3DCUBETEXTURE9)m_ptexOrig)->GetCubeMapSurface(FaceType, 0, &psurfOrig); if (m_ptexNew != NULL) hr = ((LPDIRECT3DCUBETEXTURE9)m_ptexNew)->GetCubeMapSurface(FaceType, 0, &psurfNew); if (!PromptForBmp(&fileName)) return; hr = D3DXLoadSurfaceFromFile(psurfOrig, NULL, NULL, fileName, NULL, D3DX_DEFAULT, 0, NULL); // Look for "foo_a.bmp" for alpha channel int i = fileName.ReverseFind('.'); fileName = fileName.Left(i) + "_a.bmp"; CFileStatus status; if (CFile::GetStatus(fileName, status)) { if (FAILED(hr = LoadAlphaIntoSurface(fileName, psurfOrig))) return; } if (m_numMips > 1) { hr = D3DXFilterCubeTexture((LPDIRECT3DCUBETEXTURE9)m_ptexOrig, NULL, 0, D3DX_DEFAULT); } if (psurfNew != NULL) { hr = D3DXLoadSurfaceFromSurface(psurfNew, NULL, NULL, psurfOrig, NULL, NULL, D3DX_DEFAULT, 0); if (m_numMips > 1) { hr = D3DXFilterCubeTexture((LPDIRECT3DCUBETEXTURE9)m_ptexNew, NULL, 0, D3DX_DEFAULT); } } ReleasePpo(&psurfOrig); ReleasePpo(&psurfNew); SetModifiedFlag(TRUE); UpdateAllViews(NULL, 1); }
bool CSurface::CreateFromFile(const char *Filename, FORMAT format) { // get the info from the file D3DXIMAGE_INFO info; if (S_OK != D3DXGetImageInfoFromFile(Filename, &info)) return false; if (!Create(info.Width, info.Height, format)) return false; bool success = (S_OK == D3DXLoadSurfaceFromFile(m_surface, NULL, NULL, Filename, NULL, D3DX_FILTER_NONE, 0, NULL)); ClampToEdge(); return success; }
void Bitmap::Load(char* file) { if (!surface) { D3DXGetImageInfoFromFile(file, &imageInfo); m_pd3dDevice->CreateOffscreenPlainSurface(imageInfo.Width, imageInfo.Height, m_d3dsdBackBuffer.Format, D3DPOOL_SYSTEMMEM, &surface, NULL); } if (SUCCEEDED(D3DXLoadSurfaceFromFile(surface, NULL, NULL, file, NULL, D3DX_FILTER_NONE, 0, NULL))) bitmapLoaded=true; assert(surface); assert(bitmapLoaded); }
HRESULT ccUnit::loadUnit() { // // Create surfaces. // HRESULT hr; IDirect3DSurface9 *ppBackBuffer; if (SUCCEEDED(hr = m_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &ppBackBuffer))) { D3DSURFACE_DESC d3dsDesc; ppBackBuffer->GetDesc(&d3dsDesc); char fileName[] = "..//units//armbulldog.bmp"; if (FAILED(hr = D3DXGetImageInfoFromFile( fileName, &m_srcInfo))) return hr; m_unitSize.cx = m_srcInfo.Width; m_unitSize.cy = m_srcInfo.Height; m_unitRect.right += m_unitSize.cx; m_unitRect.bottom += m_unitSize.cy; if (FAILED(hr = m_pd3dDevice->CreateOffscreenPlainSurface( m_srcInfo.Width, m_srcInfo.Height, d3dsDesc.Format, D3DPOOL_DEFAULT, &m_pUnitImage, NULL))) return hr; if (FAILED(hr = D3DXLoadSurfaceFromFile( m_pUnitImage, NULL, NULL, fileName, NULL, D3DX_FILTER_NONE, 0x7B97FF, &m_srcInfo ))) return hr; } SAFE_RELEASE(ppBackBuffer); return S_OK; }
bool DirectGraphicsSurface::LoadImage(const char *Filename, int xSize, int ySize) { HRESULT hresult; char Temp[140]; // Surface erstellen hresult = lpD3DDevice->CreateOffscreenPlainSurface(xSize, ySize, D3DFormat, D3DPOOL_DEFAULT, &itsSurface, NULL); // Fehler beim Surface erstellen ? if(hresult != D3D_OK) { strcpy_s(Temp, strlen("Fehler beim Surface-Erstellen für ") + 1, "Fehler beim Surface-Erstellen für "); strcat_s(Temp, strlen(Filename) + 1, Filename); strcat_s(Temp, 3, " !"); Protokoll.WriteText(Temp, true); return false; } // Bild laden hresult = D3DXLoadSurfaceFromFile(itsSurface, NULL, NULL, Filename, NULL, D3DX_FILTER_NONE, 0, NULL); // Fehler beim Laden ? if(hresult != D3D_OK) { strcpy_s(Temp, strlen("Fehler beim Laden von ") + 1, "Fehler beim Laden von "); strcat_s(Temp, strlen(Filename) + 1, Filename); strcat_s(Temp, 5, " !\n"); Protokoll.WriteText(Temp, true); return false; } // Grösse setzen itsRect.left = 0; itsRect.top = 0; itsRect.right = xSize; itsRect.bottom = ySize; // Bild korrekt geladen strcat_s(Temp, strlen(TextArray [TEXT_LADE_BITMAP]) + 1, TextArray [TEXT_LADE_BITMAP]); strcat_s(Temp, strlen(Filename) + 1, Filename); strcat_s(Temp, strlen(TextArray [TEXT_LADEN_ERFOLGREICH]) + 1, TextArray [TEXT_LADEN_ERFOLGREICH]); strcat_s(Temp, 3, "\n"); Protokoll.WriteText(Temp, false); return true; }
/** Load the given bitmap file into a surface; returns a pointer to said surface */ LPDIRECT3DSURFACE9 DirectXStuff::loadSurface(string bitmapName) { LPDIRECT3DSURFACE9 image = NULL; //Get width and height of bitmap file D3DXIMAGE_INFO info; HRESULT result = D3DXGetImageInfoFromFile(bitmapName.c_str(), &info); if (result != D3D_OK) { //Unsuccessful return NULL; } //Create a surface for the image result = d3ddev->CreateOffscreenPlainSurface( info.Width, //surface height info.Height, //surface width D3DFMT_X8R8G8B8, //image format D3DPOOL_DEFAULT, //memory pool to use &image, //pointer to surface NULL //reserved (always NULL) ); if (result != D3D_OK) { return NULL; } //load bitmap file into the surface we just created result = D3DXLoadSurfaceFromFile( image, //Destination surface NULL, //destination palette NULL, //destination rectangle bitmapName.c_str(), //source bitmap file name NULL, //source rectangle D3DX_DEFAULT, //controls how image is filtered D3DCOLOR_XRGB(0, 0, 0), //for transparancy (0 for none) NULL); //source image info (usually NULL) //make sure file was loaded okay if (result != D3D_OK) { return NULL; } return image; }
LPDIRECT3DSURFACE9 LoadSurface(char* fileName, D3DCOLOR transColor) { LPDIRECT3DSURFACE9 image = NULL; D3DXIMAGE_INFO info; HRESULT hr; hr = D3DXGetImageInfoFromFile(fileName, &info); if (hr != D3D_OK) return NULL; hr = d3dDev->CreateOffscreenPlainSurface(info.Width, info.Height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &image, NULL); if (hr != D3D_OK) return NULL; hr = D3DXLoadSurfaceFromFile(image, NULL, NULL, fileName, NULL, D3DX_DEFAULT, transColor, NULL); if (hr != D3D_OK) return NULL; return image; }
int CDirectXRenderLayer::createSurfaceFromFile(char* fileName, unsigned int width, unsigned int height, TRect& srcRect, unsigned long flags, SURFACE_REF& surface) { if (createSurface(width, height, flags, surface)) { mRect[0].left = srcRect.mX0; mRect[0].top = srcRect.mY0; mRect[0].right = srcRect.mX1; mRect[0].bottom = srcRect.mY1; if (SUCCEEDED(D3DXLoadSurfaceFromFile(mSurface[surface].mD3DSurface, NULL, NULL, fileName, &mRect[0], D3DX_FILTER_NONE, 0, NULL))) { return E_SUCCESS; } else { releaseSurface(surface); } } return E_FAILED; }
bool Background::load(LPDIRECT3DDEVICE9 Device, std::wstring filename) { imageScale = 100; HRESULT hr; D3DXIMAGE_INFO imageInfo; hr = D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo); if(FAILED(hr)) { return false; } height = imageInfo.Height; width = imageInfo.Width; hr = Device->CreateOffscreenPlainSurface(width, height, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &background, NULL); if(FAILED(hr)) { return false; } //load from file hr = D3DXLoadSurfaceFromFile(background, NULL, NULL, filename.c_str(), NULL, D3DX_DEFAULT, 0, NULL); if(FAILED(hr)) { return false; } //Set Rects scrRect.left = 0; scrRect.top = 0; scrRect.bottom = 0; scrRect.top = 0; destRect.left = 0; destRect.top = 0; destRect.bottom = 0; destRect.right = 0; return true; }
//======================================================================= bool DxSurface::createFromFile ( IDXDEVICE device, const tstring& filepath, const RECT* srcRect ) { DWORD colorKey = 0; D3DXIMAGE_INFO info; if ( !getInfoFromFile( filepath ) ) { Logger::message( _T("Error: Could not load file!\n") ); return false; } if ( !createEmpty( device, (srcRect ? ( srcRect->right - srcRect->left ): fileInfo().Width), (srcRect ? ( srcRect->bottom - srcRect->top ): fileInfo().Height) ) ) { return false; } HRESULT result = D3DXLoadSurfaceFromFile( mySurface, NULL, NULL, filepath.c_str(), srcRect, D3DX_DEFAULT, colorKey, &info ); return SUCCEEDED( result ); }
LPDIRECT3DSURFACE9 CreateSurfaceFromFile(LPDIRECT3DDEVICE9 d3ddv, LPWSTR FilePath) { D3DXIMAGE_INFO info; HRESULT result = D3DXGetImageInfoFromFile(FilePath,&info); if (result!=D3D_OK) { trace(L"[ERROR] Failed to get image info '%s'",FilePath); return NULL; } LPDIRECT3DSURFACE9 surface; d3ddv->CreateOffscreenPlainSurface( info.Width, // width info.Height, // height D3DFMT_X8R8G8B8, // format D3DPOOL_DEFAULT, &surface, NULL); result = D3DXLoadSurfaceFromFile( surface, // surface NULL, // destination palette NULL, // destination rectangle FilePath, NULL, // source rectangle D3DX_DEFAULT, // filter image D3DCOLOR_XRGB(0,0,0), // transparency (0 = none) NULL); // reserved if (result!=D3D_OK) { trace(L"[ERROR] D3DXLoadSurfaceFromFile() failed"); return NULL; } return surface; }
LPDIRECT3DSURFACE9 cD3DManager::getD3DSurfaceFromFile(LPCSTR theFilename) // create and load a surface with an image from file { HRESULT hResult; LPDIRECT3DSURFACE9 surface; // the Direct3D surface hResult = pd3dDevice->CreateOffscreenPlainSurface(800, // the width of the surface to create 600, // the height of the surface to create D3DFMT_X8R8G8B8, // the surface format D3DPOOL_SYSTEMMEM, // the memory pool to use *IMPORTANT* when dealing with surfaces &surface, // holds the resulting surface NULL); // reserved; should be NULL // Check the return value to make sure that this function call was successful if (FAILED(hResult)) return false; hResult = D3DXLoadSurfaceFromFile(surface,NULL,NULL,theFilename,NULL, D3DX_DEFAULT,0,NULL); if ( FAILED(hResult)) return false; return surface; }
bool DXBackground::load(LPCOMSTRING filename) { HRESULT hResult = D3DXGetImageInfoFromFile(filename, &imgInfo); if (FAILED(hResult)) return false; release(); hResult = device->CreateOffscreenPlainSurface(imgInfo.Width, imgInfo.Height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &image, NULL); if (FAILED(hResult)) return false; hResult = D3DXLoadSurfaceFromFile(image, NULL, NULL, filename, NULL, D3DX_DEFAULT, 0, NULL); if (FAILED(hResult)) return false; _isCreated = true; return true; }
void CubeTexture::CreateFromFile(const wchar_t* filePathPX, const wchar_t* filePathNX, const wchar_t* filePathPY, const wchar_t* filePathNY, const wchar_t* filePathPZ, const wchar_t* filePathNZ) { const int CUBE_TEX_SIZE = 512; _Assert(NULL == mD3DCubeTexture); gEngine->GetDriver()->GetD3DDevice()->CreateCubeTexture(CUBE_TEX_SIZE, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &mD3DCubeTexture, NULL); _Assert(NULL != mD3DCubeTexture); YString::Copy(mFilePath[POSITIVE_X], _countof(mFilePath[POSITIVE_X]), filePathPX); YString::Copy(mFilePath[NEGATIVE_X], _countof(mFilePath[NEGATIVE_X]), filePathNX); YString::Copy(mFilePath[POSITIVE_Y], _countof(mFilePath[POSITIVE_Y]), filePathPY); YString::Copy(mFilePath[NEGATIVE_Y], _countof(mFilePath[NEGATIVE_Y]), filePathNY); YString::Copy(mFilePath[POSITIVE_Z], _countof(mFilePath[POSITIVE_Z]), filePathPZ); YString::Copy(mFilePath[NEGATIVE_Z], _countof(mFilePath[NEGATIVE_Z]), filePathNZ); IDirect3DSurface9 *cubeTexSurf; mD3DCubeTexture->GetCubeMapSurface(D3DCUBEMAP_FACE_POSITIVE_X, 0, &cubeTexSurf); D3DXLoadSurfaceFromFile(cubeTexSurf, NULL, NULL, filePathPX, NULL, D3DX_DEFAULT, 0, NULL); SAFE_RELEASE(cubeTexSurf); mD3DCubeTexture->GetCubeMapSurface(D3DCUBEMAP_FACE_NEGATIVE_X, 0, &cubeTexSurf); D3DXLoadSurfaceFromFile(cubeTexSurf, NULL, NULL, filePathNX, NULL, D3DX_DEFAULT, 0, NULL); SAFE_RELEASE(cubeTexSurf); mD3DCubeTexture->GetCubeMapSurface(D3DCUBEMAP_FACE_POSITIVE_Y, 0, &cubeTexSurf); D3DXLoadSurfaceFromFile(cubeTexSurf, NULL, NULL, filePathPY, NULL, D3DX_DEFAULT, 0, NULL); SAFE_RELEASE(cubeTexSurf); mD3DCubeTexture->GetCubeMapSurface(D3DCUBEMAP_FACE_NEGATIVE_Y, 0, &cubeTexSurf); D3DXLoadSurfaceFromFile(cubeTexSurf, NULL, NULL, filePathNY, NULL, D3DX_DEFAULT, 0, NULL); SAFE_RELEASE(cubeTexSurf); mD3DCubeTexture->GetCubeMapSurface(D3DCUBEMAP_FACE_POSITIVE_Z, 0, &cubeTexSurf); D3DXLoadSurfaceFromFile(cubeTexSurf, NULL, NULL, filePathPZ, NULL, D3DX_DEFAULT, 0, NULL); SAFE_RELEASE(cubeTexSurf); mD3DCubeTexture->GetCubeMapSurface(D3DCUBEMAP_FACE_NEGATIVE_Z, 0, &cubeTexSurf); D3DXLoadSurfaceFromFile(cubeTexSurf, NULL, NULL, filePathNZ, NULL, D3DX_DEFAULT, 0, NULL); SAFE_RELEASE(cubeTexSurf); }
LPDIRECT3DSURFACE9 Game::createSurfaceFromFile(LPDIRECT3DDEVICE9 device, LPWSTR filePath) { D3DXIMAGE_INFO info; HRESULT result = D3DXGetImageInfoFromFile(filePath, &info); if (result != D3D_OK) { GAMELOG("[Error] Failed to get image info %s", filePath); return NULL; } LPDIRECT3DSURFACE9 surface; GameGlobal::GetCurrentDevice()->CreateOffscreenPlainSurface(info.Width, info.Height, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL); result = D3DXLoadSurfaceFromFile(surface, NULL, NULL, filePath, NULL, D3DX_DEFAULT, 0, NULL); if (result != D3D_OK) { GAMELOG("[Error] Failed to load image from %s", filePath); return NULL; } return surface; }
LPDIRECT3DSURFACE9 FrkContent::LoadSurface(string path) { LPDIRECT3DSURFACE9 surface = NULL; D3DXIMAGE_INFO info; HRESULT hr; ZeroMemory(&info, sizeof(info)); hr = D3DXGetImageInfoFromFile(path.c_str(), &info); if (hr != D3D_OK) return NULL; hr = this->m_hGame->GetDevice()->CreateOffscreenPlainSurface( info.Width, info.Height, D3DFMT_UNKNOWN,//Ch?n format t? ??ng D3DPOOL_DEFAULT,//Ch?n vùng nh? t? ??ng &surface,//Con tr? l?u surface ???c t?o ra NULL); if (hr != D3D_OK) return 0; //load hình lên surface ?ã t?o ra hr = D3DXLoadSurfaceFromFile(surface, NULL, NULL, path.c_str(), NULL, D3DX_DEFAULT, D3DCOLOR_XRGB(255, 255, 255), &info); if (hr != D3D_OK) return 0; return surface; }
void ConvertFile(const char* Dir, const char* Filename, double MaxMSE) { HRESULT hr; LPDIRECT3DSURFACE8 pSrcSurf = NULL; char OutFilename[52]; if (Dir) _snprintf(OutFilename, 52, "%s\\%s", Dir, Filename); else _snprintf(OutFilename, 52, "%s", Filename); OutFilename[51] = 0; printf("%s: ", OutFilename); TRACE1("%s:\n", OutFilename); int n = strlen(OutFilename); if (n < 40) printf("%*c", 40-n, ' '); if (pSrcSurf) pSrcSurf->Release(); pSrcSurf = NULL; // Load up the file D3DXIMAGE_INFO info; hr = D3DXGetImageInfoFromFile(Filename, &info); CheckHR(hr); PrintImageInfo(info); UINT Width = PadPow2(info.Width); UINT Height = PadPow2(info.Height); float Waste = 100.f * (float)(Width * Height - info.Width * info.Height) / (float)(Width * Height); UncompressedSize += Width * Height * 4; TotalSrcPixels += info.Width * info.Height; TotalDstPixels += Width * Height; // Special case for 256-colour files - just directly drop into a P8 xpr if (info.Format == D3DFMT_P8) { hr = pD3DDevice->CreateImageSurface(Width, Height, D3DFMT_A8R8G8B8, &pSrcSurf); CheckHR(hr); hr = D3DXLoadSurfaceFromFile(pSrcSurf, NULL, NULL, Filename, NULL, D3DX_FILTER_NONE, 0, NULL); CheckHR(hr); FixTransparency(pSrcSurf); if (Width * Height > 4096) { // DXT1 for P8s if lossless and more than 4k image LPDIRECT3DSURFACE8 pTempSurf; hr = pD3DDevice->CreateImageSurface(Width, Height, D3DFMT_A8R8G8B8, &pTempSurf); CheckHR(hr); hr = D3DXLoadSurfaceFromSurface(pTempSurf, NULL, NULL, pSrcSurf, NULL, NULL, D3DX_FILTER_NONE, 0); CheckHR(hr); double CMSE, AMSE; TRACE0(" Checking DXT1: "); if (!GetFormatMSE(info, pTempSurf, D3DFMT_DXT1, CMSE, AMSE)) { pTempSurf->Release(); return; } TRACE2("CMSE=%05.2f, AMSE=%07.2f\n", CMSE, AMSE); if (CMSE <= 1e-6 && AMSE <= 1e-6) { printf("DXT1 %4dx%-4d (%5.2f%% waste)\n", Width, Height, Waste); TRACE0(" Selected Format: DXT1\n"); WriteXPR(OutFilename, info, pTempSurf, XB_D3DFMT_DXT1, NULL); pTempSurf->Release(); return; } pTempSurf->Release(); } printf("P8 %4dx%-4d (%5.2f%% waste)\n", Width, Height, Waste); TRACE0(" Selected Format: P8\n"); LPDIRECT3DSURFACE8 pTempSurf; DWORD pal[256]; ConvertP8(pSrcSurf, pTempSurf, pal, info); WriteXPR(OutFilename, info, pTempSurf, XB_D3DFMT_P8, pal); pTempSurf->Release(); return; } // test linear format versus non-linear format // Linear format requires 64 pixel aligned width, whereas // Non-linear format requires power of 2 width and height bool useLinearFormat(false); UINT linearWidth = (info.Width + 0x3f) & ~0x3f; if (AllowLinear && linearWidth * info.Height < Width * Height) useLinearFormat = true; hr = pD3DDevice->CreateImageSurface(Width, Height, D3DFMT_A8R8G8B8, &pSrcSurf); CheckHR(hr); hr = D3DXLoadSurfaceFromFile(pSrcSurf, NULL, NULL, Filename, NULL, D3DX_FILTER_NONE, 0, NULL); CheckHR(hr); // create the linear version as well LPDIRECT3DSURFACE8 pLinearSrcSurf = NULL; if (useLinearFormat) { hr = pD3DDevice->CreateImageSurface(linearWidth, info.Height, D3DFMT_A8R8G8B8, &pLinearSrcSurf); CheckHR(hr); hr = D3DXLoadSurfaceFromFile(pLinearSrcSurf, NULL, NULL, Filename, NULL, D3DX_FILTER_NONE, 0, NULL); CheckHR(hr); } // special case for small files - all textures are alloced on page granularity so just output uncompressed // dxt is crap on small files anyway if (Width * Height <= 1024) { if (useLinearFormat) { // correct sizing amounts UncompressedSize -= Width * Height * 4; UncompressedSize += linearWidth * info.Height * 4; TotalDstPixels -= Width * Height; TotalDstPixels += linearWidth * info.Height; Waste = 100.f * (float)(linearWidth * info.Height - info.Width * info.Height) / (float)(linearWidth * info.Height); printf("LIN_A8R8G8B8 %4dx%-4d (%5.2f%% waste)\n", linearWidth, info.Height, Waste); TRACE0(" Selected Format: LIN_A8R8G8B8\n"); WriteXPR(OutFilename, info, pLinearSrcSurf, XB_D3DFMT_LIN_A8R8G8B8, NULL); } else { printf("A8R8G8B8 %4dx%-4d (%5.2f%% waste)\n", Width, Height, Waste); TRACE0(" Selected Format: A8R8G8B8\n"); WriteXPR(OutFilename, info, pSrcSurf, XB_D3DFMT_A8R8G8B8, NULL); } return; } FixTransparency(pSrcSurf); // Find the best format within specified tolerance double CMSE, AMSE[2]; // DXT1 is the preferred format as it's smallest TRACE0(" Checking DXT1: "); if (!GetFormatMSE(info, pSrcSurf, D3DFMT_DXT1, CMSE, AMSE[0])) return; TRACE2("CMSE=%05.2f, AMSE=%07.2f\n", CMSE, AMSE[0]); if (CMSE <= MaxMSE && AMSE[0] <= MaxMSE) { printf("DXT1 %4dx%-4d (%5.2f%% waste)\n", Width, Height, Waste); TRACE0(" Selected Format: DXT1\n"); WriteXPR(OutFilename, info, pSrcSurf, XB_D3DFMT_DXT1, NULL); return; } // Use P8 is possible as it's lossless LPDIRECT3DSURFACE8 pTempSurf; DWORD pal[256]; if (ConvertP8(pSrcSurf, pTempSurf, pal, info)) { printf("P8 %4dx%-4d (%5.2f%% waste)\n", Width, Height, Waste); TRACE0(" Selected Format: P8\n"); WriteXPR(OutFilename, info, pTempSurf, XB_D3DFMT_P8, pal); pTempSurf->Release(); return; } // DXT3/5 are the same size so use whichever is better if good enough // CMSE will be equal for both TRACE0(" Checking DXT3: "); if (!GetFormatMSE(info, pSrcSurf, D3DFMT_DXT3, CMSE, AMSE[0])) return; TRACE2("CMSE=%05.2f, AMSE=%07.2f\n", CMSE, AMSE[0]); TRACE0(" Checking DXT5: "); if (!GetFormatMSE(info, pSrcSurf, D3DFMT_DXT5, CMSE, AMSE[1])) return; TRACE2("CMSE=%05.2f, AMSE=%07.2f\n", CMSE, AMSE[1]); if (AMSE[0] <= AMSE[1]) { if (CMSE <= MaxMSE && AMSE[0] <= MaxMSE) { printf("DXT3 %4dx%-4d (%5.2f%% waste)\n", Width, Height, Waste); TRACE0(" Selected Format: DXT3\n"); WriteXPR(OutFilename, info, pSrcSurf, XB_D3DFMT_DXT3, NULL); return; } } else { if (CMSE <= MaxMSE && AMSE[1] <= MaxMSE) { printf("DXT5 %4dx%-4d (%5.2f%% waste)\n", Width, Height, Waste); TRACE0(" Selected Format: DXT5\n"); WriteXPR(OutFilename, info, pSrcSurf, XB_D3DFMT_DXT5, NULL); return; } } // No good compressed format so use uncompressed // A1R5G5B5 is worth a try I guess... TRACE0(" Checking A1R5G5B5: "); if (!GetFormatMSE(info, pSrcSurf, D3DFMT_A1R5G5B5, CMSE, AMSE[0])) return; TRACE2("CMSE=%05.2f, AMSE=%07.2f\n", CMSE, AMSE[0]); if (CMSE <= MaxMSE && AMSE[0] <= MaxMSE) { printf("A1R5G5B5 %4dx%-4d (%5.2f%% waste)\n", Width, Height, Waste); TRACE0(" Selected Format: A1R5G5B5\n"); LPDIRECT3DSURFACE8 pTempSurf; hr = pD3DDevice->CreateImageSurface(Width, Height, D3DFMT_A1R5G5B5, &pTempSurf); CheckHR(hr); hr = D3DXLoadSurfaceFromSurface(pTempSurf, NULL, NULL, pSrcSurf, NULL, NULL, D3DX_FILTER_NONE, 0); CheckHR(hr); WriteXPR(OutFilename, info, pTempSurf, XB_D3DFMT_A1R5G5B5, NULL); pTempSurf->Release(); return; } // Use A8R8G8B8 if (useLinearFormat) { // correct sizing information UncompressedSize -= Width * Height * 4; UncompressedSize += linearWidth * info.Height * 4; TotalDstPixels -= Width * Height; TotalDstPixels += linearWidth * info.Height; Waste = 100.f * (float)(linearWidth * info.Height - info.Width * info.Height) / (float)(linearWidth * info.Height); printf("LIN_A8R8G8B8 %4dx%-4d (%5.2f%% waste)\n", linearWidth, info.Height, Waste); TRACE0(" Selected Format: LIN_A8R8G8B8\n"); WriteXPR(OutFilename, info, pLinearSrcSurf, XB_D3DFMT_LIN_A8R8G8B8, NULL); } else { printf("A8R8G8B8 %4dx%-4d (%5.2f%% waste)\n", Width, Height, Waste); TRACE0(" Selected Format: A8R8G8B8\n"); WriteXPR(OutFilename, info, pSrcSurf, XB_D3DFMT_A8R8G8B8, NULL); } if (pSrcSurf) pSrcSurf->Release(); }
void CDxtexDoc::OpenSubsurface(D3DCUBEMAP_FACES FaceType, LONG lwMip, LONG lwSlice) { HRESULT hr; CString fileName; LPDIRECT3DDEVICE9 pd3ddev = PDxtexApp()->Pd3ddev(); LPDIRECT3DTEXTURE9 ptex = NULL; LPDIRECT3DSURFACE9 psurfOrig = NULL; LPDIRECT3DSURFACE9 psurfNew = NULL; if (!PromptForBmp(&fileName)) return; if (IsVolumeMap()) { hr = D3DXCreateTextureFromFile(pd3ddev, fileName, &ptex); hr = ptex->GetSurfaceLevel(0, &psurfOrig); } else if (IsCubeMap()) { hr = ((LPDIRECT3DCUBETEXTURE9)m_ptexOrig)->GetCubeMapSurface(FaceType, lwMip, &psurfOrig); if (m_ptexNew != NULL) hr = ((LPDIRECT3DCUBETEXTURE9)m_ptexNew)->GetCubeMapSurface(FaceType, lwMip, &psurfNew); hr = D3DXLoadSurfaceFromFile(psurfOrig, NULL, NULL, fileName, NULL, D3DX_DEFAULT, 0, NULL); } else { hr = ((LPDIRECT3DTEXTURE9)m_ptexOrig)->GetSurfaceLevel(lwMip, &psurfOrig); if (m_ptexNew != NULL) hr = ((LPDIRECT3DTEXTURE9)m_ptexNew)->GetSurfaceLevel(lwMip, &psurfNew); hr = D3DXLoadSurfaceFromFile(psurfOrig, NULL, NULL, fileName, NULL, D3DX_DEFAULT, 0, NULL); } // Look for "foo_a.bmp" for alpha channel int i = fileName.ReverseFind('.'); fileName = fileName.Left(i) + "_a.bmp"; CFileStatus status; if (CFile::GetStatus(fileName, status)) { if (FAILED(hr = LoadAlphaIntoSurface(fileName, psurfOrig))) return; } if (IsVolumeMap()) { LPDIRECT3DVOLUME9 pvol; hr = ((LPDIRECT3DVOLUMETEXTURE9)m_ptexOrig)->GetVolumeLevel(lwMip, &pvol); hr = LoadVolumeSliceFromSurface(pvol, lwSlice, psurfOrig); ReleasePpo(&pvol); if (m_ptexNew) { hr = ((LPDIRECT3DVOLUMETEXTURE9)m_ptexNew)->GetVolumeLevel(lwMip, &pvol); hr = LoadVolumeSliceFromSurface(pvol, lwSlice, psurfOrig); ReleasePpo(&pvol); } } else if (psurfNew != NULL) { hr = D3DXLoadSurfaceFromSurface(psurfNew, NULL, NULL, psurfOrig, NULL, NULL, D3DX_DEFAULT, 0); } ReleasePpo(&psurfOrig); ReleasePpo(&psurfNew); ReleasePpo(&ptex); SetModifiedFlag(TRUE); UpdateAllViews(NULL, 1); }
// ファイルから読み込み // カラーキーはアルファ未使用のときのみ有効 // カラーキーを使用したくない場合はcolorKeyを0に // カラーキーを有効にしたい場合は0xff******にする void CMglImage::CreateFromFile( LPCSTR szFileName, BOOL bRenderTarget, D3DCOLOR colorKey ) { _MGL_DEBUGLOG( "+ CMglImage::CreateFromFile( \"%s\", %d, 0x%08X )", szFileName, bRenderTarget, colorKey ); InitCheck(); // 初期化チェック // 二回目以降の呼び出しを考慮し一端Release Release(); ////////////////////////////////////////////////////////////////////////////// // // D3DXGetImageInfoFromFile()が無いので無理矢理サイズ取得です。あほくせぇ(´Д`) // IDirect3DTexture8* m_pTexture2; // テクスチャ D3DXIMAGE_INFO imgInfo; MyuAssert( D3DXCreateTextureFromFileEx( d3d, szFileName, 256, 256, D3DX_DEFAULT, 0, /* m_myudg->backBufferDesc.Format */ m_myudg->GetFormat(), D3DPOOL_SYSTEMMEM, D3DX_FILTER_POINT, D3DX_FILTER_POINT, colorKey, &imgInfo, NULL, &m_pTexture2 ), D3D_OK, "CMglImage::CreateFromFile() %s のイメージ情報取得に失敗。", szFileName ); int x = imgInfo.Width; int y = imgInfo.Height; SAFE_RELEASE( m_pTexture2 ); /////////////////////////////////////////////////////////////////// // // テクスチャを作成する // // D3DXCreateTextureFromFileEx()にて作成を行うが、 // これはあくまでカラーキーを設定するためだけ。 // なお、画像は引き伸ばされて飲まれるので、後々再読み込みする // if ( bRenderTarget == TRUE ) { DWORD r = D3DXCreateTextureFromFileEx( d3d, szFileName, x, y, D3DX_DEFAULT, D3DUSAGE_RENDERTARGET, /* m_myudg->backBufferDesc.Format */ m_myudg->GetFormat(), D3DPOOL_DEFAULT, D3DX_FILTER_POINT, D3DX_FILTER_POINT, colorKey, &imgInfo, NULL, &m_pTexture ); if ( r == E_OUTOFMEMORY ) MyuThrow2( r, 0x0201, "%s の読み込みに失敗。VRAMまたはメモリが不足しています。", szFileName ); else if ( r == D3DERR_OUTOFVIDEOMEMORY ) MyuThrow2( r, 0x0202, "%s の読み込みに失敗。VRAMが不足しています。", szFileName ); else if ( r != S_OK ) MyuThrow2( r, 0x0203, "CMglImage::CreateFromFile() D3DXCreateTextureFromFileEx(VRAM,%s)に失敗", szFileName ); } else { DWORD r = D3DXCreateTextureFromFileEx( d3d, szFileName, x, y, D3DX_DEFAULT, 0, /* m_myudg->backBufferDesc.Format */ m_myudg->GetFormat(), D3DPOOL_MANAGED, D3DX_FILTER_POINT, D3DX_FILTER_POINT, colorKey, &imgInfo, NULL, &m_pTexture ); if ( r == E_OUTOFMEMORY ) MyuThrow( r, "%s の読み込みに失敗。メモリが足りません。", szFileName ); else if ( r != S_OK ) MyuThrow( r, "CMglImage::CreateFromFile() D3DXCreateTextureFromFileEx(MANAGED,%s)に失敗", szFileName ); } m_nBmpSizeX = x; m_nBmpSizeY = y; int i; ZeroMemory( m_vertices, sizeof(MYU_VERTEX)*4 ); // 実サイズを算出(GetRealTexSizeを使う方法もあるが、一応テクスチャから取ってくる) D3DSURFACE_DESC texDesc; m_pTexture->GetLevelDesc( 0, &texDesc ); nRealSizeX = texDesc.Width; nRealSizeY = texDesc.Height; // 比率設定 fRealTexTu = (float)x / (float)nRealSizeX; fRealTexTv = (float)y / (float)nRealSizeY; // 消そうとも思ったが一応デフォルト値として入れておくか… // X,Yをまずは m_vertices[VERTEXNO_LT].x = m_vertices[VERTEXNO_LB].x = 0+X_ADJ; m_vertices[VERTEXNO_LT].y = m_vertices[VERTEXNO_RT].y = 0+Y_ADJ; m_vertices[VERTEXNO_RB].x = m_vertices[VERTEXNO_RT].x = x+X_ADJ; m_vertices[VERTEXNO_RB].y = m_vertices[VERTEXNO_LB].y = y+Y_ADJ; // U,Vを m_vertices[VERTEXNO_LT].tu = m_vertices[VERTEXNO_LB].tu = 0.0f; m_vertices[VERTEXNO_LT].tv = m_vertices[VERTEXNO_RT].tv = 0.0f; //m_vertices[VERTEXNO_RB].tu = m_vertices[VERTEXNO_RT].tu = 1.0f; //m_vertices[VERTEXNO_RB].tv = m_vertices[VERTEXNO_LB].tv = 1.0f; m_vertices[VERTEXNO_RT].tu = m_vertices[VERTEXNO_RB].tu = fRealTexTu; m_vertices[VERTEXNO_LB].tv = m_vertices[VERTEXNO_RB].tv = fRealTexTv; // その他共通 for ( i=0; i<4; i++ ) { m_vertices[i].z = 0.0f; m_vertices[i].rhw = 1.0f; m_vertices[i].color = 0xffffffff; } //SetGradation(); // テクスチャのサーフェスを取得する MyuAssert( m_pTexture->GetSurfaceLevel(0, &m_pSurface), D3D_OK, "CMglImage::Create() GetSurfaceLevel()に失敗" ); m_colorKey = colorKey; if ( m_colorKey != 0 ) // カラーキーを使用する場合は0xffを念のためつけておく m_colorKey |= 0xff000000; // スプライト作成 MyuAssert( D3DXCreateSprite( d3d, &this->m_pSprite ), D3D_OK, "CMglImage::Init D3DXCreateSprite()に失敗" ); ///////////////////////////////////////////////////////////////////////// // // イメージを再読み込みする // IDirect3DSurface8* m_pTempSurface; // 一時サーフェス MyuAssert( d3d->CreateImageSurface( x, y, m_myudg->GetFormat(), &m_pTempSurface ), D3D_OK, "CMglImage::CreateFromFile() CreateImageSurface()に失敗" ); MyuAssert( D3DXLoadSurfaceFromFile( m_pTempSurface, NULL, NULL, szFileName, NULL, D3DX_FILTER_POINT, 0xffff00ff, NULL ), D3D_OK, "CMglImage::CreateFromFile() D3DXLoadSurfaceFromFile()に失敗" ); //MyuAssert( d3d->CopyRects(m_pTempSurface, NULL, 0, m_pSurface, NULL), MyuAssert( d3d->CopyRects(m_pTempSurface, NULL, 0, m_pSurface, NULL), D3D_OK, "CMglImage::CreateFromFile() CopyRects()に失敗" ); SAFE_RELEASE( m_pTempSurface ); m_bRenderTarget = bRenderTarget; createFlg = TRUE; _MGL_DEBUGLOG( "- CMglImage::CreateFromFile( \"%s\" )", szFileName ); }
void BackBuffer() { IDirect3DSurface9 *ppBackBuffer, *ppOffScreen; HRESULT back; D3DXIMAGE_INFO pSrcInfo; if (SUCCEEDED(back = g_pd3dDevice->CreateOffscreenPlainSurface( 256, 256, D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM, &ppOffScreen, NULL))) { if (SUCCEEDED( back = D3DXLoadSurfaceFromFile( ppOffScreen, NULL, NULL, "bana.bmp", NULL, D3DX_FILTER_TRIANGLE, 0, &pSrcInfo))) { back = NULL; } else if (back == D3DERR_INVALIDCALL) { back = 0; } else if (back == D3DXERR_INVALIDDATA) { pSrcInfo.Width; back = 1; } else { back = 2; } } //if (SUCCEEDED(g_pd3dDevice->GetRenderTarget(0, &ppBackBuffer))) if (SUCCEEDED(g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &ppBackBuffer))) { D3DSURFACE_DESC pDesc; ppBackBuffer->GetDesc(&pDesc); POINT pDestinationPoint; for (int i=0; i<4; i++) { pDestinationPoint.x = i*pSrcInfo.Width; for (int j=0; j<4; j++) { pDestinationPoint.y = j*pSrcInfo.Height; if (SUCCEEDED(back = g_pd3dDevice->UpdateSurface(ppOffScreen, NULL, ppBackBuffer, &pDestinationPoint))) { back = 0; } } } } ppBackBuffer->Release(); ppOffScreen->Release(); /* if (SUCCEEDED(back = g_pd3dDevice->CreateRenderTarget( 256, 256, D3DFMT_R8G8B8, D3DMULTISAMPLE_NONE, 0, true, &ppBackBuffer, NULL))) */ /*if (SUCCEEDED(back = g_pd3dDevice->CreateDepthStencilSurface( 256, 256, D3DFMT_D16, D3DMULTISAMPLE_NONE, 0, true, &ppBackBuffer, NULL)))*/ //if (SUCCEEDED(g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &ppBackBuffer))) /* D3DLOCKED_RECT pLockedRect; if (SUCCEEDED(back = ppBackBuffer->LockRect(&pLockedRect, NULL, D3DLOCK_READONLY))) { back=1; } */ /* HDC phdc; if (SUCCEEDED(back = ppBackBuffer->GetDC(&phdc))) //if (SUCCEEDED(back = ppBackBuffer->LockRect(&pLockedRect, NULL, D3DLOCK_READONLY))) { BYTE* pSrcTopRow = (BYTE*)pLockedRect.pBits; DWORD dwSrcPitch = (DWORD)pLockedRect.Pitch; *pSrcTopRow =1; //for(int i=0;i<1;i++) // *(pSrcTopRow+(i*dwSrcPitch)) = 1; } else if (back == D3DERR_INVALIDCALL) { back = 0; } else if(back == D3DERR_WASSTILLDRAWING) { back = 0; } */ // ppBackBuffer->UnlockRect(); }
bool GameWindow::Init(HINSTANCE hInstance, int nCmdShow){ //initialize window settings WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)WinProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = NULL; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = kApplicationTitle; wc.hIconSm = NULL; RegisterClassEx(&wc); //create a new window m_Window = CreateWindow(kApplicationTitle, kApplicationTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, kScreenWidth, kScreenHeight, NULL, NULL, hInstance, NULL); //was there an error creating the window? if (!m_Window) return false; //display the window ShowWindow(m_Window, nCmdShow); UpdateWindow(m_Window); // TODO: should I call this every update? The name certainly suggests that... //initialize Direct3D m_d3d = Direct3DCreate9(D3D_SDK_VERSION); if (!m_d3d){ MessageBox(m_Window, L"Error initializing Direct3D", L"Error", MB_OK); return false; } //set Direct3D presentation parameters D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dpp.BackBufferCount = 1; d3dpp.BackBufferWidth = kScreenWidth; d3dpp.BackBufferHeight = kScreenHeight; d3dpp.hDeviceWindow = m_Window; //create Direct3D device m_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_Window, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &m_d3ddev); if (!m_d3ddev){ MessageBox(m_Window, L"Error creating Direct3D device", L"Error", MB_OK); return 0; } //clear the m_Backbuffer to black m_d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0); //create m_Surface HRESULT result = m_d3ddev->CreateOffscreenPlainSurface( kScreenWidth, //width of the m_Surface kScreenHeight, //height of the m_Surface D3DFMT_X8R8G8B8, //m_Surface format D3DPOOL_DEFAULT, //memory pool to use &m_Surface, //pointer to the m_Surface NULL); //reserved (always NULL) if (!SUCCEEDED(result)) return false; //load m_Surface from file into newly created m_Surface result = D3DXLoadSurfaceFromFile( m_Surface, //destination m_Surface NULL, //destination palette NULL, //destination rectangle kBackground, //source filename NULL, //source rectangle D3DX_DEFAULT, //controls how image is filtered 0, //for transparency (0 for none) NULL); //source image info (usually NULL) //make sure file was loaded okay if (!SUCCEEDED(result)) return false; //create sprite D3DXCreateSprite(m_d3ddev, &m_Sprite); //create font for scores D3DXCreateFont(m_d3ddev, 45, 0, FW_BOLD, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("OCR A Std"), &m_Font); //load startgame texture m_StartGameTexture = Load_Texture(kStartGame); //load hp texture m_HpTexture = Load_Texture(m_HpImg); //powerup texture- static image m_pPowerup.m_Texture = Load_Texture(m_pPowerup.m_Image); //for respawn rate m_Frame = 0; //create sound m_cSound = new CSoundManager(); m_cSound->Initialize(m_Window, DSSCL_PRIORITY); m_cSound->SetPrimaryBufferFormat(2, 22050, 16); //sound effects m_cSound->Create(&m_pJump, L"sounds\\jump.wav"); m_cSound->Create(&m_pKill, L"sounds\\kill.wav"); m_cSound->Create(&m_pDamaged, L"sounds\\damaged.wav"); m_cSound->Create(&m_pShoot, L"sounds\\shoot.wav"); m_cSound->Create(&m_pMusic, L"sounds\\music.wav"); m_cSound->Create(&m_pSelect, L"sounds\\select.wav"); m_cSound->Create(&m_pPickup, L"sounds\\pickup.wav"); //play music loop m_pMusic->Play(0, DSBPLAY_LOOPING); return true; }
// TexturedSquare TexturedSquare::TexturedSquare(LPDIRECT3DDEVICE9 device, const D3DXVECTOR3& vect, const D3DXVECTOR3& shift, float size, std::vector<LPCWSTR> mipmapFilenames) : TexturedObject(device) { std::vector<TEXTURE_VERTEX_WITH_NORMAL> vertices; D3DXVECTOR3 normal = vect; D3DXVec3Normalize(&normal, &normal); const int n = 10; const float min = -size / 2; const float max = size / 2; const float step = size / n; for (float u = min; u < max; u += step) { for (float v = min; v < max; v += step) { D3DXVECTOR3 u1, u2, u3, u4; if (normal.y != 0) { u1 = { u + shift.x, -(normal.x * u + normal.z * v) / normal.y + shift.y, v + shift.z }; u2 = { u + step + shift.x, -(normal.x * (u + step) + normal.z * v) / normal.y + shift.y, v + shift.z }; u3 = { u + shift.x, -(normal.x * u + normal.z * (v + step)) / normal.y + shift.y, v + step + shift.z }; u4 = { u + step + shift.x, -(normal.x * (u + step) + normal.z * (v + step)) / normal.y + shift.y, v + step + shift.z }; } else if (normal.z != 0) { u1 = { u + shift.x, v + shift.y, -(normal.x * u + normal.y * v) / normal.z + shift.z }; u2 = { u + step + shift.x, v + shift.y, -(normal.x * (u + step) + normal.y * v) / normal.z + shift.z }; u3 = { u + shift.x, v + step + shift.y, -(normal.x * u + normal.y * (v + step)) / normal.z + shift.z }; u4 = { u + step + shift.x, v + step + shift.y, -(normal.x * (u + step) + normal.y * (v + step)) / normal.z + shift.z }; } else if (normal.x != 0) { u1 = { -(normal.y * u + normal.z * v) / normal.x + shift.x, u + shift.y, v + shift.z }; u2 = { -(normal.y * (u + step) + normal.z * v) / normal.x + shift.x, u + step + shift.y, v + shift.z }; u3 = { -(normal.y * u + normal.z * (v + step)) / normal.x + shift.x, u + shift.y, v + step + shift.z }; u4 = { -(normal.y * (u + step) + normal.z * (v + step)) / normal.x + shift.x, u + step + shift.y, v + step + shift.z }; } TEXTURE_VERTEX_WITH_NORMAL v1 = { u1.x, u1.y, u1.z, normal, u / size + 0.5f, v / size + 0.5f}; TEXTURE_VERTEX_WITH_NORMAL v2 = { u2.x, u2.y, u2.z, normal, (u + step) / size + 0.5f, v / size + 0.5f }; TEXTURE_VERTEX_WITH_NORMAL v3 = { u3.x, u3.y, u3.z, normal, u / size + 0.5f, (v + step) / size + 0.5f }; TEXTURE_VERTEX_WITH_NORMAL v4 = { u4.x, u4.y, u4.z, normal, (u + step) / size + 0.5f, (v + step) / size + 0.5f }; vertices.push_back(v1); vertices.push_back(v2); vertices.push_back(v3); vertices.push_back(v3); vertices.push_back(v2); vertices.push_back(v4); } } const UINT width = D3DX_DEFAULT; const UINT height = D3DX_DEFAULT; IDirect3DTexture9* prevTexture = NULL; D3DXCreateTexture(d3dDevice, width, height, mipmapFilenames.size(), 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &this->pTexture); D3DXCreateTexture(d3dDevice, width, height, mipmapFilenames.size(), 0, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &prevTexture); for (int i = 0; i < (int) mipmapFilenames.size(); ++i) { IDirect3DSurface9 *pSurf; prevTexture->GetSurfaceLevel(i, &pSurf); D3DXLoadSurfaceFromFile(pSurf, NULL, NULL, mipmapFilenames[i], NULL, D3DX_DEFAULT, 0, NULL); pSurf->Release(); } d3dDevice->UpdateTexture(prevTexture, pTexture); prevTexture->Release(); setVertices(vertices); }