1. ////////////////////////////////////////////
//
// A Virtual FrameBuffer
//
//
class CDrawSurface
{
private:
int m_x; // Width
int m_y; // Height
int m_size; // Width*Height*4
bool m_init_val; // Has Allocated Buffer ?
UINT *pixels; // Array of Pixels
int m_curr_x; // Current Raster (Cursor) position - X
int m_curr_y; // Current Raster poisition – Y
<.... Rest of the Code ....>
}
3. ///////////////////////////////////////////
//
// COLOR data structure
// Red , Green , Blue and Alpha ( 255 opaque , 0 - transparent )
//
typedef struct
{
BYTE r;
BYTE g;
BYTE b;
BYTE a;
}COLOR;
void CdrawSurface::Clear( COLOR *col )
{
UINT Value = ( col->a << 24 | col->r << 16 | col->g << 8 | col->b);
UINT *p_pixels = pixels;
int i=0;
while ( i < m_size )
{
*p_pixels++ = Value;
i++;
}
}
4. ////////////////////////////////////////////////////////////
//
// Plot a Pixel at (x,y)
//
//
void CdrawSurface::PutPixel( int x , int y , COLOR *col )
{
if ( ( x < 0 || x > m_x ) || (y < 0 || y > m_y ) )
return;
int offset = y*m_y*4 + x*4;
UINT r = col->r;
UINT g = col->g;
UINT b = col->b;
char *rs =(char *)((char *)pixels + offset);
*rs++=b;
*rs++=g;
*rs++=r;
*rs++=0xFF;
}
5. /////////////////////////////////////////////////
//
// How do i transfer it into a Window ?
//
//
int CdrawSurface::Render( HDC dc )
{
BITMAPINFO bmi;
LPVOID pvBits;
ZeroMemory(&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = m_x;
bmi.bmiHeader.biHeight = -m_y;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32; // four 8-bit components
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = m_x * m_y * 4;
HBITMAP hbitmap = CreateDIBSection(dc, &bmi,
DIB_RGB_COLORS,
&pvBits, NULL, 0x0);
memcpy(pvBits,pixels,m_x*m_y*4); // Copy the pixels into DIB
StretchDIBits(dc,
// destination rectangle
0, 0, m_x, m_y,
// source rectangle
0, 0, m_x,m_y,
pvBits,
&bmi,
DIB_RGB_COLORS,
SRCCOPY);
return 1;
}
6. ////////////////////////////////////////
//
// A Simple Demo to Clear the Screen
//
void Demo2( HDC hdc , int width,int height )
{
CDrawSurface ds(width,height);
COLOR clr;
clr.a = 255;
clr.b = 0;
clr.g = 0;
clr.r = 255;
ds.Clear(&clr);
ds.Render(hdc);
}
7. ////////////////////////////////////////
//
// A Simple Demo to Show the primitives
//
void Demo3( HDC hdc , int width,int height )
{
CDrawSurface ds(width,height);
COLOR clr;
clr.a = 255;
clr.b = 0;
clr.g = 0;
clr.r = 0;
ds.Clear(&clr);
clr.a = 255;
clr.b=255;
clr.g=0;
clr.r=255;
ds.Line(10,10,100,100,&clr);
ds.Circle ( 100,100,150,&clr);
ds.FilledCircle ( 100,100,100,&clr);
ds.Render(hdc);
}