I am trying to make a Alphablended form that has an animated offsetting
texture. The problem is that I need more speed, the functions are to slow.
The AlphaBlend Function takes the alpha the width and height, the 2 canvases
then the destination to do the AlphaBlend. The BGTileOffset Function just
tiles a BitMap with and offset X,Y. The Offset must be positive and limited
to the size of the bitmap. If someone knows how to do this in a better way,
please let me know.
You Are Free To Use This Source If You Want!! (I would like to see what you
do with it though.. spi...@bigpond.net.au)
BELOW IS "AlphaBlend.cpp"
**********************************
//--------------------------------------------------------------------------
-
bool __fastcall AlphaBlend(int, int, int, TCanvas*, TCanvas*, void*);
bool __fastcall AlphaBlend(int, int, int, AnsiString, AnsiString, void*);
bool __fastcall AlphaBlend(int, int, int, TCanvas*, AnsiString, void*);
bool __fastcall AlphaBlend(int, int, int, AnsiString, TCanvas*, void*);
//--------------------------------------------------------------------------
-
bool __fastcall AlphaBlend(int Alpha, int Alpha_W,int Alpha_H, TCanvas
*BGround, TCanvas *FGround, void* Dest)
{
Graphics::TBitmap *FGrnd = new Graphics::TBitmap; Graphics::TBitmap
*BGrnd = new Graphics::TBitmap;
BGrnd->PixelFormat=pf24bit;
FGrnd->PixelFormat=pf24bit;
BGrnd->Width=Alpha_W;BGrnd->Height=Alpha_H;
FGrnd->Width=Alpha_W;FGrnd->Height=Alpha_H;
BGrnd->Canvas->CopyRect(Rect(0,0,Alpha_W,Alpha_H),BGround,Rect(0,0,Alpha_W,A
lpha_H));
FGrnd->Canvas->CopyRect(Rect(0,0,Alpha_W,Alpha_H),FGround,Rect(0,0,Alpha_W,A
lpha_H));
Graphics::TBitmap *Composite=new Graphics::TBitmap;
Composite->Width=Alpha_W;
Composite->Height=Alpha_H;
Composite->PixelFormat=pf24bit;
for(int Y=0;Y<=Alpha_H-1; Y++)
{
BYTE *CompositeScan=(BYTE*)Composite->ScanLine[Y];
BYTE *BGrndScan=(BYTE*)BGrnd->ScanLine[Y];
BYTE *FGrndScan=(BYTE*)FGrnd->ScanLine[Y];
for(int X=0; X<=Alpha_W-1;X++)
{
for(int RGB=0; RGB<3; RGB++)
{
int P=X*3+RGB;
CompositeScan[P]=(((FGrndScan[P]-BGrndScan[P])/255.0)*Alpha)+BGrndScan[P];
}
}
}
BitBlt(Dest,0,0,Alpha_W,Alpha_H,Composite->Canvas->Handle,0,0,SRCCOPY);
delete Composite, FGrnd, BGrnd;
return true;
bool __fastcall AlphaBlend(int Alpha, int Alpha_W,int Alpha_H, AnsiString
BGround, AnsiString FGround, void* Dest)
{
if(access(BGround.c_str(),0))return false;
if(access(FGround.c_str(),0))return false;
Graphics::TBitmap *FGnd = new Graphics::TBitmap; Graphics::TBitmap *BGnd
= new Graphics::TBitmap;
BGnd->PixelFormat=pf24bit;
FGnd->PixelFormat=pf24bit;
BGnd->Width=Alpha_W;BGnd->Height=Alpha_H;
FGnd->Width=Alpha_W;FGnd->Height=Alpha_H;
BGnd->LoadFromFile(BGround);FGnd->LoadFromFile(FGround);
AlphaBlend(Alpha,Alpha_W,Alpha_H,BGnd->Canvas,FGnd->Canvas,Dest);
delete FGnd, BGnd;
return true;
bool __fastcall AlphaBlend(int Alpha, int Alpha_W,int Alpha_H, TCanvas
*BGround, AnsiString FGround, void* Dest)
{
if(access(FGround.c_str(),0))return false;
Graphics::TBitmap *FGnd = new Graphics::TBitmap;
FGnd->PixelFormat=pf24bit;
FGnd->Width=Alpha_W;FGnd->Height=Alpha_H;
FGnd->LoadFromFile(FGround);
AlphaBlend(Alpha,Alpha_W,Alpha_H,BGround,FGnd->Canvas,Dest);
delete FGnd;
return true;
bool __fastcall AlphaBlend(int Alpha, int Alpha_W,int Alpha_H, AnsiString
BGround, TCanvas *FGround, void* Dest)
{
if(access(BGround.c_str(),0))return false;
Graphics::TBitmap *BGnd = new Graphics::TBitmap;
BGnd->PixelFormat=pf24bit;
BGnd->Width=Alpha_W;BGnd->Height=Alpha_H;
BGnd->LoadFromFile(BGround);
AlphaBlend(Alpha,Alpha_W,Alpha_H,BGnd->Canvas,FGround,Dest);
delete BGnd;
return true;
BELOW IS "BGTile.cpp"
**********************************
//--------------------------------------------------------------------------
-
#include <io>
bool __fastcall BGTile(void*,int,int,int,int,void*);
bool __fastcall BGTile(AnsiString,int,int,int,void*);
bool __fastcall BGTileOffSet(void*,int,int,int,int,int,void*);
bool __fastcall BGTileOffSet(AnsiString,int,int,int,int,void*);
#define BG_Tile 0
#define BG_Stretch 1
#define BG_Center 2
//--------------------------------------------------------------------------
-
bool __fastcall BGTile(void* BG_Name, int BG_Width, int BG_Height, int
BG_Display,
int BG_Tile_Width, int BG_Tile_Height, void* BG_Dest)
{
if(BG_Display==BG_Tile)
{
int BG_X,BG_Y;
for(BG_X=0;BG_X<=BG_Tile_Width*2;BG_X=BG_X+BG_Tile_Width){
for(BG_Y=0;BG_Y<=BG_Tile_Height*2;BG_Y=BG_Y+BG_Tile_Height)
{BitBlt(BG_Dest,BG_X,BG_Y,BG_Tile_Width,BG_Tile_Height,BG_Name,0,0,SRCCOPY);
}
if(BG_Display==BG_Stretch)
{
StretchBlt(BG_Dest,0,0,BG_Width,BG_Height,BG_Name,0,0,BG_Tile_Width,BG_Tile_
Height,SRCCOPY);
}
if(BG_Display==BG_Center)
{
int BG_X,BG_Y;
BG_X = (BG_Width/2)-(BG_Tile_Width/2);
BG_Y = (BG_Height/2)-(BG_Tile_Height/2);
BitBlt(BG_Dest,BG_X,BG_Y,BG_Width,BG_Height,BG_Name,0,0,SRCCOPY);
}
return true;
bool __fastcall BGTile(AnsiString BG_Name, int BG_Width, int BG_Height, int
BG_Display, void* BG_Dest)
{
if(access(BG_Name.c_str(),0))return false;
Graphics::TBitmap *BG_Temp = new Graphics::TBitmap;
BG_Temp->LoadFromFile(BG_Name);
BGTile(BG_Temp->Canvas->Handle,BG_Width,BG_Height,BG_Display,BG_Temp->Width,
BG_Temp->Height,BG_Dest);
delete BG_Temp;
bool __fastcall BGTileOffSet(void* BG_Name, int BG_Width, int BG_Height, int
BG_Tile_Width,
int BG_Tile_Height, int BG_Offset_X, int BG_Offset_Y,
void* BG_Dest)
{
int BG_X,BG_Y;
if(BG_Offset_X<=0 || BG_Offset_Y<=0)return false;
if(BG_Offset_X>=BG_Tile_Width || BG_Offset_Y>=BG_Tile_Height)return
false;
BitBlt(BG_Dest,0,0,BG_Offset_X,BG_Offset_Y,BG_Name,BG_Tile_Width-BG_Offset_X
,BG_Tile_Height-BG_Offset_Y,SRCCOPY);
for(BG_X=BG_Offset_X;BG_X<=BG_Tile_Width*3;BG_X=BG_X+BG_Tile_Width)
{
BitBlt(BG_Dest,BG_X,0,BG_Tile_Width,BG_Offset_Y,BG_Name,0,BG_Tile_Height-BG_
Offset_Y,SRCCOPY);
}
for(BG_Y=BG_Offset_Y;BG_Y<=BG_Tile_Height*2;BG_Y=BG_Y+BG_Tile_Height)
{
BitBlt(BG_Dest,0,BG_Y,BG_Offset_X,BG_Tile_Height,BG_Name,BG_Tile_Width-BG_Of
fset_X,0,SRCCOPY);
}
for(BG_X=BG_Offset_X;BG_X<=BG_Tile_Width*3;BG_X=BG_X+BG_Tile_Width){
for(BG_Y=BG_Offset_Y;BG_Y<=BG_Tile_Height*2;BG_Y=BG_Y+BG_Tile_Height)
{BitBlt(BG_Dest,BG_X,BG_Y,BG_Tile_Width,BG_Tile_Height,BG_Name,0,0,SRCCOPY);
bool __fastcall BGTileOffSet(AnsiString BG_Name, int BG_Width, int
BG_Height, int BG_Offset_X,
int BG_Offset_Y, void* BG_Dest)
{
if(access(BG_Name.c_str(),0))return false;
Graphics::TBitmap *BG_Temp = new Graphics::TBitmap;
BG_Temp->LoadFromFile(BG_Name);
BGTileOffSet(BG_Temp->Canvas->Handle,BG_Width,BG_Height,BG_Temp->Width,BG_Te
mp->Height,BG_Offset_X,BG_Offset_Y,BG_Dest);
delete BG_Temp;
return true;