bool DisplaySurface::Create(const boost::any& _rConfig) { CreateInfo* pInfo = boost::any_cast<CreateInfo*>(_rConfig); FilePtr pFile = NULL; bool bResult = (NULL != pInfo); { pFile = FS::GetRoot()->OpenFile(pInfo->m_strPath, FS::EOpenMode_READBINARY); bResult = (NULL != pFile); } if (false != bResult) { int sSize = pFile->Size(); unsigned char* pBuffer = new unsigned char[sSize]; sSize = pFile->Read(pBuffer, sSize); bResult = SUCCEEDED(D3DXGetImageInfoFromFileInMemory(pBuffer, sSize, &m_oInfo)); if (false != bResult) { #pragma message(__FUNCTION__" : for better support some image formats must be converted into supported surface format. For example luminance image should be translated into paletted surface.") bResult = SUCCEEDED(m_rDisplay.GetDevicePtr()->CreateOffscreenPlainSurface(m_oInfo.Width, m_oInfo.Height, m_oInfo.Format, D3DPOOL_DEFAULT, &m_pSurface, NULL)); } if (false != bResult) { bResult = SUCCEEDED(D3DXLoadSurfaceFromFileInMemory(m_pSurface, NULL, NULL, pBuffer, sSize, NULL, D3DX_FILTER_NONE, 0xff000000, NULL)); } if (false != bResult) { m_uBPP = m_rDisplay.GetFormatBitsPerPixel(m_oInfo.Format); bResult = (0 != m_uBPP); } delete[] pBuffer; FS::GetRoot()->CloseFile(pFile); } return bResult; }
std::string CGameMap::initialize() { assert( !mCells ); /* Format of map data: string Tissue Name int32 Tissue Bitmap Length byte[] Tissue Bitmap (200*200) string Entites text file string Streams text file string Walls text files */ const BYTE* data; // // parse name mName = bu::receiveStr(); // TBD: workaround around Richard's funky stuff int lastSlash = mName.find_last_of( "\\//" ); if( lastSlash >= 0 ) mName = mName.substr( lastSlash+1, mName.length()-lastSlash ); CONS << "Game map name: " << mName << endl; // // read map bitmap int bmpSize = bu::receiveInt(); net::receiveChunk( data, bmpSize, true ); // compute CRC of the bitmap const char* bmpFile = (const char*)data; mCRC = boost::crc<32,0xFFFFFFFF,0,0,false,false>( bmpFile, bmpSize ); HRESULT hr; D3DXIMAGE_INFO bitmapInfo; hr = D3DXGetImageInfoFromFileInMemory( bmpFile, bmpSize, &bitmapInfo ); if( FAILED(hr) ) { return "Error in game map - incorrect tissue bitmap format"; } assert( bitmapInfo.Width > 10 && bitmapInfo.Height > 10 ); assert( bitmapInfo.Width * bitmapInfo.Height <= 256*256 ); if( bitmapInfo.Width < 10 || bitmapInfo.Height < 10 ) { return "Error in game map - map is too small"; } if( bitmapInfo.Width * bitmapInfo.Height > 256*256 ) { return "Error in game map - map is too large"; } mCellsX = bitmapInfo.Width; mCellsY = bitmapInfo.Height; mCells = new SCell[ mCellsX * mCellsY ]; CD3DDevice& dx = CD3DDevice::getInstance(); IDirect3DSurface9* surface = 0; hr = dx.getDevice().CreateOffscreenPlainSurface( bitmapInfo.Width, bitmapInfo.Height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &surface, NULL ); assert( SUCCEEDED(hr) ); hr = D3DXLoadSurfaceFromFileInMemory( surface, NULL, NULL, bmpFile, bmpSize, NULL, D3DX_DEFAULT, 0, NULL ); if( FAILED(hr) ) { return "Error in game map - incorrect cells map format"; } D3DLOCKED_RECT lr; surface->LockRect( &lr, NULL, D3DLOCK_READONLY ); const char* linePtr = (const char*)lr.pBits; for( int y = 0; y < mCellsY; ++y ) { const D3DCOLOR* p = (const D3DCOLOR*)linePtr; for( int x = 0; x < mCellsX; ++x ) { SCell& cell = mCells[y*mCellsX+x]; switch( *p ) { case 0xFFff0000: cell.type = CELL_BLOOD1; cell.color = CCOLOR_BLOOD; break; case 0xFF00ff00: cell.type = CELL_BLOOD2; cell.color = CCOLOR_BLOOD; break; case 0xFF0000ff: cell.type = CELL_BLOOD3; cell.color = CCOLOR_BLOOD; break; case 0xFF800000: cell.type = CELL_BLOOD1; cell.color = CCOLOR_BONE; break; case 0xFF008000: cell.type = CELL_BLOOD2; cell.color = CCOLOR_BONE; break; case 0xFF000080: cell.type = CELL_BLOOD3; cell.color = CCOLOR_BONE; break; case 0xFFC80000: cell.type = CELL_BLOOD1; cell.color = CCOLOR_NEURON; break; case 0xFF00C800: cell.type = CELL_BLOOD2; cell.color = CCOLOR_NEURON; break; case 0xFF0000C8: cell.type = CELL_BLOOD3; cell.color = CCOLOR_NEURON; break; default: cell.type = CELL_BONE; cell.color = CCOLOR_BLOOD; } cell.height = MIN_CELL_HEIGHT; cell.nearBone = true; ++p; } linePtr += lr.Pitch; } surface->UnlockRect(); surface->Release(); // check map validity if( !checkMapValidity() ) return "Tissue contains invalid topology (cells adjacent only diagonally)"; // // read entities std::string pts = bu::receiveStr(); char* ptsFile = (char*)pts.c_str(); // HACK const char* tokens = "\n\r"; const char* pline = strtok( ptsFile, tokens ); do { if( !pline ) break; int etype, eposx, eposy; int fread = sscanf( pline, "%i:%i:%i", &eposx, &eposy, &etype ); if( fread != 3 ) break; mPoints.push_back( SPoint(ePointType(etype), eposx, eposy ) ); } while( pline = strtok( NULL, tokens ) ); // // read streams std::string strms = bu::receiveStr(); // streams char* strmsFile = (char*)strms.c_str(); // HACK pline = strtok( strmsFile, tokens ); do { if( !pline ) break; int sx, sy, sw, sh, sdir; int fread = sscanf( pline, "%i:%i:%i:%i:%i", &sx, &sy, &sw, &sh, &sdir ); assert( sx >= 0 && sx < mCellsX ); assert( sy >= 0 && sy < mCellsY ); assert( sx+sw <= mCellsX ); assert( sy+sh <= mCellsY ); assert( sdir >= 0 && sdir <= 3 ); assert( fread == 5 ); if( fread != 5 ) break; SStream strm; strm.x = sx; strm.y = sy; strm.width = sw; strm.height = sh; switch( sdir ) { case 0: strm.deltaX = 0; strm.deltaY = 1; break; case 1: strm.deltaX = 0; strm.deltaY = -1; break; case 2: strm.deltaX = 1; strm.deltaY = 0; break; case 3: strm.deltaX = -1; strm.deltaY = 0; break; } mStreams.push_back( strm ); } while( pline = strtok( NULL, tokens ) ); // TBD: walls bu::receiveStr(); // walls // // all is loaded now // calculate cell heights calcCellHeights(); // add some decorative elements static int DECOR_TYPES_IN_TISSUE[DECOR_POINT_TYPE_COUNT] = { (1<<CCOLOR_BLOOD), (1<<CCOLOR_BLOOD), (1<<CCOLOR_BLOOD), (1<<CCOLOR_BONE), (1<<CCOLOR_BONE), (1<<CCOLOR_BONE) | (1<<CCOLOR_BLOOD), (1<<CCOLOR_NEURON), (1<<CCOLOR_NEURON), (1<<CCOLOR_NEURON), (1<<CCOLOR_NEURON), (1<<CCOLOR_NEURON), (1<<CCOLOR_NEURON)|(1<<CCOLOR_BLOOD)|(1<<CCOLOR_BONE), }; const int DECOR_PTS_COUNT = 125; const int MAX_TRY_COUNT = DECOR_PTS_COUNT * 10; int decPtsCounter = 0; int tryCounter = 0; while( decPtsCounter < DECOR_PTS_COUNT && tryCounter < MAX_TRY_COUNT ) { ++tryCounter; int x = gRandom.getUInt() % mCellsX; int y = gRandom.getUInt() % mCellsY; const SCell& cell = mCells[pos2index(x,y)]; if( !isBlood(cell.type) || cell.nearBone ) continue; if( cell.height < 2.0f ) continue; int ptType = gRandom.getUInt() % DECOR_POINT_TYPE_COUNT; if( !(DECOR_TYPES_IN_TISSUE[ptType] & (1<<cell.color)) ) continue; mPoints.push_back( SPoint(PT_DECORATIVE,x,y, ptType) ); decPtsCounter++; } // register level texture CSharedTextureBundle::getInstance().registerTexture( RID_TEX_LEVEL, *new CGameMapTextureCreator( *this ) ); return ""; // all ok! }
// // 関数: LoadTexture( char * , LPDIRECT3DDEVICE9 , RGBQUAD *pal, DWORD color_key ) // // 目的: テクスチャー読み込み // // パラメータ: // para1:ファイル名 // para2:D3Dデバイス // para3:パレットデータ(default NULL ) // para4:透明色(default 緑っぽい色 ) // // メモ: // 読み込める画像ファイル( .bmp, .jpg, .dds, .png など) // bool CTexture::LoadTexture( char *name , LPDIRECT3DDEVICE9 d3ddevice ,int vram, RGBQUAD *pal, DWORD color_key ) { HANDLE hReadFile ; DWORD nFileSize; LPDIRECT3DSURFACE9 pSurface = NULL; m_D3DDevice = d3ddevice; m_ColorKey = color_key; RELEASE_3D( pD3DTex ); // 画像情報読み込み strcpy( m_Filename, name ) ; D3DXGetImageInfoFromFile( m_Filename, &m_TexInfo ); // ファイルオープン hReadFile = NULL ; hReadFile = CreateFile( m_Filename ,GENERIC_READ,0,NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if( hReadFile == INVALID_HANDLE_VALUE ){ char a[255]; sprintf(a,"ファイルなし、%s",m_Filename); MessageBox(NULL,a,"file not found",MB_OK); return FALSE; } // ファイルサイズ取得 m_ImageSize = GetFileSize( hReadFile , NULL ); // 領域確保 m_ImageData = new unsigned char[ m_ImageSize ] ; // データ読み込み ReadFile( hReadFile , m_ImageData , m_ImageSize , &nFileSize , NULL ); CloseHandle( hReadFile ); DWORD res; /* if( D3D_OK != ( res = D3DXCreateTextureFromFileInMemoryEx( m_D3DDevice, m_ImageData, m_ImageSize, 0, 0, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_FILTER_NONE, m_ColorKey, NULL, NULL, &pD3DTex ) )){ delete m_ImageData; m_ImageData = NULL; return FALSE; } */ // パレットデータ割り当て if( pal != NULL ){ SetPalette( pal ); } // 空のテクスチャー作成 D3DPOOL pool; if( vram == 0 ){ pool = D3DPOOL_MANAGED; } else{ pool = D3DPOOL_DEFAULT; } if( FAILED( res = d3ddevice->CreateTexture( m_TexInfo.Width, m_TexInfo.Height, 1, 0,//D3DUSAGE_RENDERTARGET,D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8,pool, &pD3DTex ,NULL))){ delete m_ImageData; m_ImageData = NULL; return FALSE ; } // テクスチャーのサーフェス取得 pD3DTex->GetSurfaceLevel( 0, &pSurface ); // テクスチャーに画像割り当て if( D3D_OK != D3DXLoadSurfaceFromFileInMemory( pSurface, NULL, NULL, m_ImageData, m_ImageSize, NULL, D3DX_DEFAULT, color_key, NULL ) ){ RELEASE_3D( pSurface ); delete m_ImageData; m_ImageData = NULL; return FALSE; } RELEASE_3D( pSurface ); delete m_ImageData; m_ImageData = NULL; return TRUE; }