Rechenzeit dieser Funktion minimieren für Performance



  • ich benutze diese funktion, um bitmaps zu zeichnen:

    void CXTGlobal::BltTransparentBitmap(CDC *cDC, CBitmap *bBitmap, int iPosX, int iPosY, int iWidth, int iHight, COLORREF cTransparent, UINT uFlags, int iBmpWidth, int iBmpHeight)
    {
    	COLORREF	crOldBack = cDC->SetBkColor(RGB(255, 255, 255)); 
    	COLORREF	crOldText = cDC->SetTextColor(RGB(0, 0, 0));
    	CDC			dcImage, dcTrans; 
    	CBitmap		*pOldBitmapImage, *pOldBitmapTrans, bitmapTrans;
    	CRect		rStretchPos;
    
    	// Create two memory dcs for the image and the mask 
    	dcImage.CreateCompatibleDC(cDC); 
    	dcTrans.CreateCompatibleDC(cDC); 
    
    	// Select the image into the appropriate dc 
    	pOldBitmapImage = dcImage.SelectObject(bBitmap);
    
    	// Build mask based on transparent colour 
    	dcImage.SetBkColor(cTransparent); 
    
    	if (uFlags & BTB_STRETCH)
    	{
    		if ((iBmpWidth == -1) && (iBmpHeight == -1))
    		{
    			iBmpWidth	= iWidth;
    			iBmpHeight	= iHight;
    		}
    
    		rStretchPos.left	= (uFlags & BTB_FLIP_HORIZONTAL) ? (iBmpWidth - 1) : (0);
    		rStretchPos.right	= (uFlags & BTB_FLIP_HORIZONTAL) ? (-iBmpWidth) : (iBmpWidth);
    		rStretchPos.top		= (uFlags & BTB_FLIP_VERTICAL) ? (iBmpHeight - 1) : (0);
    		rStretchPos.bottom	= (uFlags & BTB_FLIP_VERTICAL) ? (-iBmpHeight) : (iBmpHeight);
    	}
    
    	// Is the picture wanted to be smaller?
    	if ((uFlags & BTB_STRETCH) && (iBmpWidth != -1) && (iBmpHeight != -1) && ((iWidth < iBmpWidth) || (iHight < iBmpHeight)))
    	{
    		CRect rNormal;
    
    		// Mirror normal Size, too
    		rNormal.left	= (uFlags & BTB_FLIP_HORIZONTAL) ? (iWidth - 1) : (0);
    		rNormal.right	= (uFlags & BTB_FLIP_HORIZONTAL) ? (-iWidth) : (iWidth);
    		rNormal.top		= (uFlags & BTB_FLIP_VERTICAL) ? (iHight - 1) : (0);
    		rNormal.bottom	= (uFlags & BTB_FLIP_VERTICAL) ? (-iHight) : (iHight);
    
    		// Create the mask bitmap 
    		bitmapTrans.CreateBitmap(iBmpWidth, iBmpHeight, 1, 1, NULL); 
    
    		// Select the mask bitmap into the appropriate dc 
    		pOldBitmapTrans = dcTrans.SelectObject(&bitmapTrans); 
    
    		dcTrans.StretchBlt(0, 0, iWidth, iHight, &dcImage, 0, 0, iBmpWidth, iBmpHeight, SRCCOPY); 
    
    		cDC->StretchBlt(iPosX, iPosY, iWidth, iHight, &dcImage, rStretchPos.left, rStretchPos.top, rStretchPos.right, rStretchPos.bottom, SRCINVERT); 
    		cDC->StretchBlt(iPosX, iPosY, iWidth, iHight, &dcTrans, rNormal.left, rNormal.top, rNormal.right, rNormal.bottom, SRCAND);
    		cDC->StretchBlt(iPosX, iPosY, iWidth, iHight, &dcImage, rStretchPos.left, rStretchPos.top, rStretchPos.right, rStretchPos.bottom, SRCINVERT);
    	}
    	else
    	{
    		// Create the mask bitmap 
    		bitmapTrans.CreateBitmap(iWidth, iHight, 1, 1, NULL); 
    
    		// Select the mask bitmap into the appropriate dc 
    		pOldBitmapTrans = dcTrans.SelectObject(&bitmapTrans); 
    
    		dcTrans.BitBlt(0, 0, iWidth, iHight, &dcImage, 0, 0, SRCCOPY); 
    
    		if (uFlags & BTB_STRETCH)
    		{
    			cDC->StretchBlt(iPosX, iPosY, iWidth, iHight, &dcImage, rStretchPos.left, rStretchPos.top, rStretchPos.right, rStretchPos.bottom, SRCINVERT); 
    			cDC->StretchBlt(iPosX, iPosY, iWidth, iHight, &dcTrans, rStretchPos.left, rStretchPos.top, rStretchPos.right, rStretchPos.bottom, SRCAND);
    			cDC->StretchBlt(iPosX, iPosY, iWidth, iHight, &dcImage, rStretchPos.left, rStretchPos.top, rStretchPos.right, rStretchPos.bottom, SRCINVERT);
    		}
    		else
    		{
    			cDC->BitBlt(iPosX, iPosY, iWidth, iHight, &dcImage, 0, 0, SRCINVERT); 
    			cDC->BitBlt(iPosX, iPosY, iWidth, iHight, &dcTrans, 0, 0, SRCAND);
    			cDC->BitBlt(iPosX, iPosY, iWidth, iHight, &dcImage, 0, 0, SRCINVERT); 
    		}
    	}
    
    	// Restore settings 
    	dcImage.SelectObject(pOldBitmapImage); 
    	dcTrans.SelectObject(pOldBitmapTrans); 
    
    	cDC->SetBkColor(crOldBack); 
    	cDC->SetTextColor(crOldText);
    }
    

    genauer gesagt wird mit hilfe dieser funktion ein spielfeld gemalt (Texturen, natürlich nur bitmaps, und einige objekte für das spiel).

    das bild wird pro sekunge ca. 30 mal neugezeichnet, daher wird diese funktion sehr oft aufgerufen. logischer weise ist die auslastung meines rechners bei 90% und mehr.

    kann man diese funktion vereinfachen und performance zu gewinnen?

    Ich weis wenn ich ein spiel basteln will sollte ich auf DirectX umsteigen 😉 kann man da trotzdem was machen?


Anmelden zum Antworten