1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
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))
{
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)*AlphaValue/255;
Pixels[PosY*Width + PosX].rgbGreen =GetGValue(BackColor)*AlphaValue/255;
Pixels[PosY*Width + PosX].rgbBlue =GetBValue(BackColor)*AlphaValue/255;
}
}
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));
BLENDFUNCTION bf;
bf.AlphaFormat = 0;
bf.BlendFlags = 0;
bf.BlendOp = AC_SRC_OVER;
bf.SourceConstantAlpha = AlphaValue;
AlphaBlend(HDCDestination, PosX, PosY, Width, Height,HDCBitmap, 0,0,Width, Height, bf);
}
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);
}
};
| |