int Draw_Text_GDI(char *text, int x,int y,COLORREF color, LPDIRECTDRAWSURFACE7 lpdds) { // this function draws the sent text on the sent surface // using color index as the color in the palette HDC xdc; // the working dc // get the dc from surface if (FAILED(lpdds->GetDC(&xdc))) return(0); // set the colors for the text up SetTextColor(xdc,color); // set background mode to transparent so black isn't copied SetBkMode(xdc, TRANSPARENT); // draw the text a TextOut(xdc,x,y,text,strlen(text)); // release the dc lpdds->ReleaseDC(xdc); // return success return(1); } // end Draw_Text_GDI
//----------------------------------------------------------------------------- // Name: dx_CopieBitmap(...) // Desc: Copie le contenu d'un bitmap dans le buffer d'affichage //----------------------------------------------------------------------------- bool dx_CopieBitmap (HBITMAP pBitmap, uint16 pPosX, uint16 pTailleX, uint16 pPosY, uint16 pTailleY) { HDC hdcImage; HDC hdc; BITMAP bm; if (pBitmap == NULL) return false; SurfaceBack->Restore(); hdcImage = CreateCompatibleDC (NULL); SelectObject (hdcImage, pBitmap); GetObject (pBitmap, sizeof(bm), &bm); pTailleX = pTailleX == 0 ? bm.bmWidth : pTailleX; pTailleY = pTailleY == 0 ? bm.bmHeight : pTailleY; SurfaceBack->GetDC(&hdc); BitBlt (hdc, pPosX, pPosY, pTailleX, pTailleY, hdcImage, 0, 0, SRCCOPY); SurfaceBack->ReleaseDC(hdc); DeleteDC (hdcImage); return true; }
bool dx_NombreGDI (uint32 pNombre, uint16 pPosX, uint16 pPosY, COLORREF pCouleur) { // this function draws the sent text on the sent surface // using color index as the color in the palette HDC xdc; // the working dc // get the dc from surface if (FAILED (SurfaceBack->GetDC (&xdc))) return false; char pTexte[128]; _itoa (pNombre, pTexte, 10); WCHAR Texte[128] = {0}; MultiByteToWideChar (GetACP(), 0, pTexte, -1, Texte, 128); SetTextColor (xdc, pCouleur); // set the colors for the text SetBkMode (xdc, TRANSPARENT); // set background mode to transparent so black isn't copied TextOut (xdc, pPosX, pPosY, Texte, strlen (pTexte)); // draw the text SurfaceBack->ReleaseDC (xdc); // release the dc return true; }
////FUNCTIONS////////////////// //----------------------------------------------------------------------------- // Name: Main() // Desc: Whwere all work is done //----------------------------------------------------------------------------- bool main() { static SCCOLOR col; static UCHAR* back_buf; static HDC hDC; static int iFrames=0; static int x=0; col.b = col.r = col.g = 255; if(GetKeyStatus(VK_ESCAPE) == true) return(0); //Clear the surface if(!ClrS(lpddsSecondary,wndRect))//can't be within a lock-Unlock block or u can't blt return(0); //Write text to the Secondary surface remember to Release the DC ddReturnVal = lpddsSecondary->GetDC(&hDC); if (DDFailedCheck(ddReturnVal, "GetDC() failed", cpErrorBuf )) { if(ddReturnVal == DDERR_SURFACELOST) { lpddsSecondary->Restore(); lpddsSecondary->GetDC(&hDC); } else { MessageBox(main_window_handle, cpErrorBuf, "main()", MB_ICONEXCLAMATION); return(0); } } static char buffer[1024]; SetBkColor(hDC,RGB(0,0,0)); SetBkMode(hDC,OPAQUE); sprintf(buffer, "Time elapsed(s)= %d", (int)(gametimer.GetStartElapsedTime()*0.001) );//0.00001666666f static DWORD starttime=GetTickCount(); if( gametimer.Elapsed(starttime,(60/0.001)) ) sprintf(buffer,"TIME!"," 1 minute"); SetTextColor(hDC,RGB(255,255,255)); iFrames++; static int FramesPerSec=0; FramesPerSec = iFrames/( ((int)(gametimer.GetStartElapsedTime()*0.001)+1) ); sprintf(buffer, "Frames/s = %d", FramesPerSec); TextOut(hDC, 100,100, buffer, strlen(buffer)); sprintf(buffer, "X= %d", x); TextOut(hDC, 100,140, buffer, strlen(buffer)); ddReturnVal = lpddsSecondary->ReleaseDC(hDC); if (DDFailedCheck(ddReturnVal, "ReleaseDC() failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "main()", MB_ICONEXCLAMATION); return(0); } //Lock the surface for drawing if(!Lock(lpddsSecondary)) return(0); back_buf = (UCHAR*)ddsd.lpSurface; //test drawing pixels DrawPixel(ddsd,x+200,100,col); DrawPixel(ddsd,x+200,101,col); DrawPixel(ddsd,x+200,102,col); DrawPixel(ddsd,x+200,103,col); DrawPixel(ddsd,x+200,104,col); DrawPixel(ddsd,x+200,105,col); DrawPixel(ddsd,x+200,106,col); DrawPixel(ddsd,x+200,107,col); DrawPixel(ddsd,x+200,108,col); x++; if(x>780) x=0; //test bitmap class for (int y=0; y < bitmap1.GetHeight(); y++) { // copy the line memcpy(&back_buf[100+(y+200)*ddsd.lPitch], // dest address &bitmap1.GetImageData()[y*bitmap1.GetWidth()*2], // src address bitmap1.GetWidth()*2); // bytes to copy } // end for y //Unlock the surface if(!UnLock(lpddsSecondary)) return(0); if(!Lock(Temp)) return(0); back_buf = (UCHAR*)ddsd.lpSurface; for (int y=0; y < bitmap2.GetHeight(); y++) { // copy the line memcpy(&back_buf[y*ddsd.lPitch], // dest address &bitmap2.GetImageData()[y*bitmap2.GetWidth()*2], // src address bitmap2.GetWidth()*2); // bytes to copy } // end for y if(!UnLock(Temp)) return(0); static RECT temprect= {200,200,520,400}; static RECT sorc_rect= {0,0,320,200}; ddReturnVal = lpddsSecondary->Blt(&temprect/*dest rect*/,Temp, //pointer to source surface &sorc_rect, //pointer to the source rectangle DDBLT_WAIT | DDBLT_KEYSRC,NULL); if (DDFailedCheck(ddReturnVal, "Blt failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Main()", MB_ICONEXCLAMATION); return(0); } static RECT temprect2; temprect2.top=200; temprect2.left=500; temprect2.right=sprite.GetWidth()+500; temprect2.bottom=sprite.GetHeight()+200; static int i=0; ddReturnVal = lpddsSecondary->Blt(&temprect2/*dest rect*/,sprite.GetFrame(i), //pointer to source surface NULL, //pointer to the source rectangle DDBLT_WAIT | DDBLT_KEYSRC,NULL/*ddbltfx struct*/); if (DDFailedCheck(ddReturnVal, "Blt failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Main()", MB_ICONEXCLAMATION); return(0); } static DWORD start = GetTickCount(); if(gametimer.Elapsed(start,120)) { i++; start = GetTickCount(); } if(i>13) i=0; static int size=0; temprect2.top=500+size; temprect2.left=300+size; temprect2.bottom=500+myface.GetWidth()+size; temprect2.right=300+myface.GetWidth()+size; ddReturnVal = lpddsSecondary->Blt(&temprect2/*dest rect*/,myface.GetFrame(0), //pointer to source surface NULL, //pointer to the source rectangle DDBLT_WAIT | DDBLT_KEYSRC,NULL/*ddbltfx struct*/); if (DDFailedCheck(ddReturnVal, "Blt failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Main()", MB_ICONEXCLAMATION); return(0); } static int what=0; what++; size= sin(what)+cos(what+5); static int j=0; static DWORD start2=GetTickCount(); if(gametimer.Elapsed(start2,50)) { j++; start2=GetTickCount(); } if(j>=3)j=0; temprect2.top=500; temprect2.left=100; temprect2.bottom=500+ship.GetHeight(); temprect2.right=100+ship.GetWidth(); ddReturnVal = lpddsSecondary->Blt(&temprect2/*dest rect*/,ship.GetFrame(j), //pointer to source surface NULL, //pointer to the source rectangle DDBLT_WAIT | DDBLT_KEYSRC,NULL/*ddbltfx struct*/); if (DDFailedCheck(ddReturnVal, "Blt failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Main()", MB_ICONEXCLAMATION); return(0); } temprect2.top=400; temprect2.left=500; temprect2.bottom=400+recttest.GetHeight()+50; temprect2.right=500+recttest.GetWidth()+50; ddReturnVal = lpddsSecondary->Blt(&temprect2/*dest rect*/,recttest.GetFrame(0), //pointer to source surface &recttest.GetRect(), //pointer to the source rectangle DDBLT_WAIT | DDBLT_KEYSRC,NULL/*ddbltfx struct*/); if (DDFailedCheck(ddReturnVal, "Blt failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Main()", MB_ICONEXCLAMATION); return(0); } temprect2.top=485; temprect2.left=160; temprect2.bottom=485+ship2.GetHeight(); temprect2.right=160+ship2.GetWidth(); static int k=0; static DWORD start3; if(gametimer.Elapsed(start3,0)) { k++; start3=GetTickCount(); } if(k>=11)k=0; ddReturnVal = lpddsSecondary->Blt(&temprect2/*dest rect*/,ship2.GetFrame(0), //pointer to source surface &ship2.GetRect(), //pointer to the source rectangle DDBLT_WAIT | DDBLT_KEYSRC,NULL/*ddbltfx struct*/); if (DDFailedCheck(ddReturnVal, "Blt failed", cpErrorBuf )) { MessageBox(main_window_handle, cpErrorBuf, "Main()", MB_ICONEXCLAMATION); return(0); } Flip(); return(1); }
ALERROR CGImageCache::LoadBitmapImage (DWORD dwImageUNID, DWORD dwTransparencyUNID, DWORD dwDepthUNID, int *retiIndex) // LoadBitmapImage // // Loads an image into the cache. This should only be called once // when the cache is initialized or when an image is first requested. { ALERROR error; HRESULT result; HBITMAP hBitmap; DDSURFACEDESC ddsd; BITMAP bm; LPDIRECTDRAWSURFACE7 pSurface; HDC hDC, hBitmapDC; HBITMAP hOldBitmap; HBITMAP hDepth; CGChannelStruct *pTrans = NULL; // Load the bitmap from the image db if (error = m_pMediaDb->LoadBitmap(dwImageUNID, &hBitmap)) return error; // Get some info from bitmap GetObject(hBitmap, sizeof(bm), &bm); // Create a surface of the proper size utlMemSet(&ddsd, sizeof(ddsd), 0); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; ddsd.dwWidth = bm.bmWidth; ddsd.dwHeight = bm.bmHeight; if (m_pScreen->GetDD()->CreateSurface(&ddsd, &pSurface, NULL) != DD_OK) { DeleteObject(hBitmap); return ERR_FAIL; } if (result = pSurface->GetDC(&hDC) != DD_OK) { pSurface->Release(); DeleteObject(hBitmap); return ERR_FAIL; } // Blt the bitmap onto the surface hBitmapDC = CreateCompatibleDC(NULL); hOldBitmap = (HBITMAP)SelectObject(hBitmapDC, hBitmap); BitBlt(hDC, 0, 0, bm.bmWidth, bm.bmHeight, hBitmapDC, 0, 0, SRCCOPY); SelectObject(hBitmapDC, hOldBitmap); DeleteDC(hBitmapDC); DeleteObject(hBitmap); pSurface->ReleaseDC(hDC); // Load the transparency bitmap if (dwTransparencyUNID) { HBITMAP hTransparency; if (error = m_pMediaDb->LoadBitmap(dwTransparencyUNID, &hTransparency)) { pSurface->Release(); return error; } // Convert this bitmap into an 8-bit map error = ConvertToChannel(hTransparency, &pTrans); DeleteObject(hTransparency); if (error) { pSurface->Release(); return error; } } // Load the depth bitmap if (dwDepthUNID) { if (error = m_pMediaDb->LoadBitmap(dwDepthUNID, &hDepth)) { if (pTrans) MemFree(pTrans); pSurface->Release(); return error; } } else hDepth = NULL; // Add the surface to our cache if (error = AddImage(dwImageUNID, pSurface, pTrans, hDepth, retiIndex)) { if (hDepth) DeleteObject(hDepth); if (pTrans) MemFree(pTrans); pSurface->Release(); return ERR_FAIL; } return NOERROR; }
HRESULT CTextureHolder::CopyBitmapToSurface(){ // Get a DDraw object to create a temporary surface LPDIRECTDRAW7 pDD; m_pddsSurface->GetDDInterface( (VOID**)&pDD ); // Get the bitmap structure (to extract width, height, and bpp) BITMAP bm; GetObject( m_hbmBitmap, sizeof(BITMAP), &bm ); // Setup the new surface desc DDSURFACEDESC2 ddsd; ddsd.dwSize = sizeof(ddsd); m_pddsSurface->GetSurfaceDesc( &ddsd ); ddsd.dwFlags = DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT| DDSD_TEXTURESTAGE; ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE|DDSCAPS_SYSTEMMEMORY; ddsd.ddsCaps.dwCaps2 = 0L; ddsd.dwWidth = bm.bmWidth; ddsd.dwHeight = bm.bmHeight; // Create a new surface for the texture LPDIRECTDRAWSURFACE7 pddsTempSurface; HRESULT hr; if( FAILED( hr = pDD->CreateSurface( &ddsd, &pddsTempSurface, NULL ) ) ) { pDD->Release(); return hr; } // Get a DC for the bitmap HDC hdcBitmap = CreateCompatibleDC( NULL ); if( NULL == hdcBitmap ) { pddsTempSurface->Release(); pDD->Release(); return hr; // bug? return E_FAIL? } SelectObject( hdcBitmap, m_hbmBitmap ); // Handle palettized textures. Need to attach a palette if( ddsd.ddpfPixelFormat.dwRGBBitCount == 8 ) { LPDIRECTDRAWPALETTE pPalette; DWORD dwPaletteFlags = DDPCAPS_8BIT|DDPCAPS_ALLOW256; DWORD pe[256]; WORD wNumColors = GetDIBColorTable( hdcBitmap, 0, 256, (RGBQUAD*)pe ); // Create the color table for( WORD i=0; i<wNumColors; i++ ) { pe[i] = RGB( GetBValue(pe[i]), GetGValue(pe[i]), GetRValue(pe[i]) ); // Handle textures with transparent pixels if( m_dwFlags & (D3DTEXTR_TRANSPARENTWHITE|D3DTEXTR_TRANSPARENTBLACK) ) { // Set alpha for opaque pixels if( m_dwFlags & D3DTEXTR_TRANSPARENTBLACK ) { if( pe[i] != 0x00000000 ) pe[i] |= 0xff000000; } else if( m_dwFlags & D3DTEXTR_TRANSPARENTWHITE ) { if( pe[i] != 0x00ffffff ) pe[i] |= 0xff000000; } } } // Add DDPCAPS_ALPHA flag for textures with transparent pixels if( m_dwFlags & (D3DTEXTR_TRANSPARENTWHITE|D3DTEXTR_TRANSPARENTBLACK) ) dwPaletteFlags |= DDPCAPS_ALPHA; // Create & attach a palette pDD->CreatePalette( dwPaletteFlags, (PALETTEENTRY*)pe, &pPalette, NULL ); pddsTempSurface->SetPalette( pPalette ); m_pddsSurface->SetPalette( pPalette ); SAFE_RELEASE( pPalette ); } // Copy the bitmap image to the surface. HDC hdcSurface; if( SUCCEEDED( pddsTempSurface->GetDC( &hdcSurface ) ) ) { BitBlt( hdcSurface, 0, 0, bm.bmWidth, bm.bmHeight, hdcBitmap, 0, 0, SRCCOPY ); pddsTempSurface->ReleaseDC( hdcSurface ); } DeleteDC( hdcBitmap ); // Copy the temp surface to the real texture surface m_pddsSurface->Blt( NULL, pddsTempSurface, NULL, DDBLT_WAIT, NULL ); // Done with the temp surface pddsTempSurface->Release(); // For textures with real alpha (not palettized), set transparent bits if( ddsd.ddpfPixelFormat.dwRGBAlphaBitMask ) { if( m_dwFlags & (D3DTEXTR_TRANSPARENTWHITE|D3DTEXTR_TRANSPARENTBLACK) ) { // Lock the texture surface DDSURFACEDESC2 ddsd; ddsd.dwSize = sizeof(ddsd); while( m_pddsSurface->Lock( NULL, &ddsd, 0, NULL ) == DDERR_WASSTILLDRAWING ); DWORD dwAlphaMask = ddsd.ddpfPixelFormat.dwRGBAlphaBitMask; DWORD dwRGBMask = ( ddsd.ddpfPixelFormat.dwRBitMask | ddsd.ddpfPixelFormat.dwGBitMask | ddsd.ddpfPixelFormat.dwBBitMask ); DWORD dwColorkey = 0x00000000; // Colorkey on black if( m_dwFlags & D3DTEXTR_TRANSPARENTWHITE ) dwColorkey = dwRGBMask; // Colorkey on white // Add an opaque alpha value to each non-colorkeyed pixel for( DWORD y=0; y<ddsd.dwHeight; y++ ) { WORD* p16 = (WORD*)((BYTE*)ddsd.lpSurface + y*ddsd.lPitch); DWORD* p32 = (DWORD*)((BYTE*)ddsd.lpSurface + y*ddsd.lPitch); for( DWORD x=0; x<ddsd.dwWidth; x++ ) { if( ddsd.ddpfPixelFormat.dwRGBBitCount == 16 ) { if( ( *p16 &= dwRGBMask ) != dwColorkey ) *p16 |= dwAlphaMask; p16++; } if( ddsd.ddpfPixelFormat.dwRGBBitCount == 32 ) { if( ( *p32 &= dwRGBMask ) != dwColorkey ) *p32 |= dwAlphaMask; p32++; } } } m_pddsSurface->Unlock( NULL ); } else if( m_bHasMyAlpha ){ DDSURFACEDESC2 ddsd; ddsd.dwSize = sizeof(ddsd); while( m_pddsSurface->Lock( NULL, &ddsd, 0, NULL ) == DDERR_WASSTILLDRAWING ); DWORD dwRGBMask = ( ddsd.ddpfPixelFormat.dwRBitMask | ddsd.ddpfPixelFormat.dwGBitMask | ddsd.ddpfPixelFormat.dwBBitMask ); DWORD rMask = ddsd.ddpfPixelFormat.dwRBitMask; DWORD gMask = ddsd.ddpfPixelFormat.dwGBitMask; DWORD bMask = ddsd.ddpfPixelFormat.dwBBitMask; DWORD aMask = ddsd.ddpfPixelFormat.dwRGBAlphaBitMask; DWORD rShift = GetShift( rMask ); DWORD gShift = GetShift( gMask ); DWORD bShift = GetShift( bMask ); DWORD aShift = GetShift( aMask ); DWORD maxRVal = rMask >> rShift; DWORD maxGVal = gMask >> gShift; DWORD maxBVal = bMask >> bShift; DWORD maxAVal = aMask >> aShift; DWORD rVal, gVal, bVal, aVal; FLOAT min, max; // Add an opaque alpha value to each non-colorkeyed pixel for( DWORD y=0; y<ddsd.dwHeight; y++ ){ WORD* p16 = (WORD*)((BYTE*)ddsd.lpSurface + y*ddsd.lPitch); DWORD* p32 = (DWORD*)((BYTE*)ddsd.lpSurface + y*ddsd.lPitch); for( DWORD x=0; x<ddsd.dwWidth; x++ ){ if( ddsd.ddpfPixelFormat.dwRGBBitCount == 32 ){ *p32 &= dwRGBMask; // set alpha to zero if( *p32 == 0 ){} // black is 100% transparent, so leave alpha at 0% else if( *p32 == dwRGBMask ){ // white is opaque, so set alpha to 100% *p32 |= aMask; } else{ // set alpha to equal intensity of brightest hue rVal = ( *p32 & rMask ) >> rShift; gVal = ( *p32 & gMask ) >> gShift; bVal = ( *p32 & bMask ) >> bShift; max = max( (FLOAT)rVal / maxRVal, max( (FLOAT)gVal / maxGVal, (FLOAT)bVal / maxBVal ) ); // min = min( (FLOAT)rVal / maxRVal, min( (FLOAT)gVal / maxGVal, (FLOAT)bVal / maxBVal ) ); aVal = max * 255; //if( rVal == gVal && gVal == bVal ){ // white fading to black // *p32 = dwRGBMask; // set color to white //} // maximize luminosity and saturation rVal /= max; gVal /= max; bVal /= max; *p32 = ( aVal << aShift ) | ( rVal << rShift ) | ( gVal << gShift ) | ( bVal << bShift ); } p32++; } else if( ddsd.ddpfPixelFormat.dwRGBBitCount == 16 ){ *p16 &= dwRGBMask; // set alpha to zero if( *p16 == 0 ){} // black is 100% transparent, so leave alpha at 0% else if( *p16 == dwRGBMask ){ // white is opaque, so set alpha to 100% *p16 |= aMask; } else{ // set alpha to equal intensity of brightest hue rVal = ( *p16 & rMask ) >> rShift; gVal = ( *p16 & gMask ) >> gShift; bVal = ( *p16 & bMask ) >> bShift; aVal = STATSTEXTURE_ALPHA * max( (FLOAT)rVal / maxRVal, max( (FLOAT)gVal / maxGVal, (FLOAT)bVal / maxBVal ) ); if( aVal < STATSTEXTURE_ALPHA ){ // semi-tranparent white *p16 = dwRGBMask; } *p16 |= aVal << aShift; } p16++; } } }