Code Snippet
-
Upload
barcamp-kerala -
Category
Entertainment & Humor
-
view
563 -
download
4
description
Transcript of Code Snippet
//////////////////////////////////////////////// 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 ....>}
////////////////////////////////////////////////// Ctor//
CdrawSurface::CDrawSurface( int x , int y ){ m_x = x; m_y = y; m_size = m_x*m_y; pixels = new UINT[ m_size ]; m_init_val = true;}
/////////////////////////////////////////////// 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++;
} }
//////////////////////////////////////////////////////////////// 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; }
///////////////////////////////////////////////////// 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; }
//////////////////////////////////////////// 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);
}
//////////////////////////////////////////// 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);
}
PART 2
class Matrix2D {private :
double m11,m12,m13;double m21,m22,m23;double m31,m32,m33;
};
Scale
void Matrix2D::Scale( double xscale , double yscale ){ Matrix2D *mat = new Matrix2D();
mat->SetIdentity ();mat->m11=xscale;mat->m22=yscale;PreMultiply(*mat);delete mat;
}
Translate
void Matrix2D::Translate( double xtrans , double ytrans ){
Matrix2D *mat = new Matrix2D();mat->SetIdentity ();
mat->m13=xtrans;mat->m23=ytrans;PreMultiply(*mat);delete mat;
}
Rotate
void Matrix2D::Rotate( double rot ){
double angle = rot*3.14159/180.0;Matrix2D *mat = new Matrix2D();mat->SetIdentity ();
mat->m11=cos(angle);mat->m12=-sin(angle);mat->m21=sin(angle);mat->m22=cos(angle);PreMultiply (*mat);
delete mat;
}
class CTransformDrawSurface : public CDrawSurface{
private: Matrix2D tr_mat;
public:
void Scale( double xscale , double yscale ) { tr_mat.Scale (xscale,yscale);} void Rotate( double rot ){
tr_mat.Rotate( rot); }
void Translate( double xtrans , double ytrans ) { tr_mat.Translate(xtrans,ytrans);}
void Transform( double& x , double& y ){ tr_mat.Transform ( x,y);}
};
void TestPolyFill( HDC dc , CTransformDrawSurface& dr , COLOR *col){
POINT2D Poly[] = { { 0,0 } , { 0 , 150 },{ 150,150 },{ 100,100}};
dr.ResetTransform ();dr.Rotate(-90);dr.Scale(1,-1);dr.Translate(200,200);for( int i = 0; i<sizeof(Poly)/sizeof(Poly[0]);i++){
double x = Poly[i].X; double y = Poly[i].Y;
dr.Transform ( x,y ); Poly[i].Y = y;
Poly[i].X = x;}
POLYGON2D rs ;rs.Length = sizeof(Poly)/ sizeof(POINT2D);rs.Points = Poly;dr.FillPolygon ( &rs , col );
return;}
//////////////////////////////////////////// A Simple Demo to Show Filled Polygon with Transformation//void Demo4( HDC hdc , int width,int height ){ CTransformDrawSurface ds(width,height);
COLOR clr;clr.a = 255;clr.b = 0;clr.g = 0;clr.r = 0;ds.Clear(&clr);clr.r = 127;
TestPolyFill(hdc,ds,&clr);ds.Render(hdc);
}
void TestQuad(HDC dc , CTransformDrawSurface& dr , COLOR *col){
POINT2D Quad[] = { { 0,0},{0,100},{100,100},{100,0} };
dr.ResetTransform ();dr.Scale ( 1,-1);dr.Translate(dr.m_x /2,dr.m_y /2);
for( int i = 0; i<sizeof(Quad)/sizeof(POINT2D);i++){
double x = Quad[i].X;double y = Quad[i].Y;
dr.Transform ( x,y ); Quad[i].Y = y;
Quad[i].X = x;}
POLYGON2D rs ; rs.Length = sizeof(Quad)/ sizeof(POINT2D);
rs.Points = Quad;col->g = 255;dr.FillPolygon ( &rs , col );
}
//////////////////////////////////////////// A Simple Demo to Show Quad Transformation//void Demo5( HDC hdc , int width,int height ){ CTransformDrawSurface ds(width,height);
COLOR clr;clr.a = 255;clr.b = 0;clr.g = 0;clr.r = 0;ds.Clear(&clr);clr.r = 127;
TestQuad(hdc,ds,&clr);ds.Render(hdc);
}
Bresenham's Line Algorithm
Bresenham's Circle algorithm