Code:
class Bitmap{
public:
unsigned int Width =0;
unsigned int Height =0;
RGBQUAD *Pixels=NULL;
HBITMAP HBitmap=NULL;
HBITMAP OldHBitmap =NULL;
HDC HDCBitmap;
BITMAPINFO BitInfo;
void *BufferMemory=NULL;
Bitmap(unsigned int SizeWidth, unsigned int SizeHeight, COLORREF BackColor=RGB(0,0,0))
{
New(SizeWidth,SizeHeight,BackColor);
}
void Disposed()
{
Pixels=NULL;
VirtualFree(BufferMemory, 0,MEM_RELEASE);
SelectObject(HDCBitmap, OldHBitmap);
DeleteDC(HDCBitmap);
DeleteObject(HBitmap);
}
void New(unsigned int SizeWidth, unsigned int SizeHeight, COLORREF BackColor=RGB(0,0,0))
{ //New HBITMAP:
int BufferSize = SizeWidth * SizeHeight * sizeof(RGBQUAD);
if(BufferMemory) Disposed();
BufferMemory = VirtualAlloc(0, BufferSize,MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
Pixels =static_cast<RGBQUAD *> (BufferMemory);
Width = SizeWidth;
Height = SizeHeight;
ZeroMemory(&BitInfo, sizeof(BITMAPINFO));
BitInfo.bmiHeader.biSize = sizeof(BitInfo.bmiHeader);
BitInfo.bmiHeader.biWidth = Width;
BitInfo.bmiHeader.biHeight = -Height;
BitInfo.bmiHeader.biPlanes = 1;
BitInfo.bmiHeader.biBitCount = 32;
BitInfo.bmiHeader.biCompression = BI_RGB;
//BitInfo.bmiHeader.biSizeImage = BitInfo.bmiHeader.biWidth * (-BitInfo.bmiHeader.biHeight)* sizeof(RGBQUAD);
HBitmap = CreateBitmap(Width, Height,1,32,Pixels);
HDCBitmap = CreateCompatibleDC(NULL);
OldHBitmap= (HBITMAP)SelectObject(HDCBitmap, HBitmap);
Clear(BackColor);
}
void Clear(COLORREF BackColor=RGB(0,0,0), BYTE AlphaValue=255)
{
if(!BufferMemory) return;
for(unsigned int PosY=0; PosY<Height; PosY++)
{
for(unsigned int PosX=0; PosX<Width; PosX++)
{
Pixels[PosY*Width + PosX].rgbRed =GetRValue(BackColor);
Pixels[PosY*Width + PosX].rgbGreen =GetGValue(BackColor);
Pixels[PosY*Width + PosX].rgbBlue =GetBValue(BackColor);
}
}
SetDIBits(HDCBitmap,HBitmap,0,Height,Pixels,&BitInfo,DIB_RGB_COLORS);
}
void Draw(HDC HDCDestination, int PosX, int PosY, BYTE AlphaValue=5 )
{
//AlphaBlendWithTransparentColor (HDCDestination,PosX, PosY, Width, Height,HDCBitmap,PosX, PosY, Width,Height, SourceConstantAlpha, RGB(0,0,0));
HBRUSH color = CreateSolidBrush(RGB(0,0,255));
SelectObject(HDCDestination,color);
Ellipse(HDCBitmap, 0,0,100,100);
BLENDFUNCTION bf;
bf.AlphaFormat = AC_SRC_ALPHA;
bf.BlendFlags = 0;
bf.BlendOp = AC_SRC_OVER;
bf.SourceConstantAlpha = AlphaValue;
AlphaBlend(HDCDestination, PosX, PosY, Width, Height,HDCBitmap, 0,0,Width, Height, bf);
DeleteObject(color);
}
void SetPixel(unsigned int PosX, unsigned int PosY, COLORREF Color)
{
if(BufferMemory)
{
if(PosX>=Width|| PosX<0) PosX = Width-1;
if(PosY>=Height || PosY<0) PosY = Height-1;
Pixels[PosY*Width + PosX].rgbRed =GetRValue(Color);
Pixels[PosY*Width + PosX].rgbGreen =GetGValue(Color);
Pixels[PosY*Width + PosX].rgbBlue =GetBValue(Color);
}
SetDIBits(HDCBitmap,HBitmap,0,Height,Pixels,&BitInfo,DIB_RGB_COLORS);
}
RGBQUAD GetPixel(unsigned int PosX, unsigned int PosY)
{
if(BufferMemory)
{
if(PosX>=Width|| PosX<0) PosX = Width-1;
if(PosY>=Height || PosY<0) PosY = Height-1;
return Pixels[PosY*Width + PosX];
}
}
void DrawLine3D(Position3D Origin, Position3D Destination, COLORREF Color, BYTE Alpha=0)
{
if(!BufferMemory) return;
std::vector<Position3D> GetLineDots = GetLinePoints(Origin, Destination);
for(unsigned int x=0; x<GetLineDots.size()-1; x++)
{
unsigned int PosX = GetLineDots[x].PosX;
unsigned int PosY = GetLineDots[x].PosY;
if(PosX>=Width|| PosX<0) PosX = Width-1;
if(PosY>=Height || PosY<0) PosY = Height-1;
Pixels[PosY*Width + PosX].rgbRed =GetRValue(Color)*Alpha/255;
Pixels[PosY*Width + PosX].rgbGreen =GetGValue(Color)*Alpha/255;
Pixels[PosY*Width + PosX].rgbBlue =GetBValue(Color)*Alpha/255;
}
SetDIBits(HDCBitmap,HBitmap,0,Height,Pixels,&BitInfo,DIB_RGB_COLORS);
}
};