Home
Tutorials
Code Snippets
Code Samples
Downloads
Links

The Blog
Our Projects
About
Contact

::Add RageStorm to Favorites!::

The Blog | Our Projects | Guest Book | About | Contact

 
Code Snippet - DIB Graphics
Author:Arkon
Category:Win32API
File Size:~ 3.05 KB
Uploaded at:27-Nov-02 07:26:28 pm
Description:
  Use DIB for quick and direct access to a back buffer where you use it for your graphics.
  
// Need speed? Don't wanna use any special APIS(OGL, DX, ..), then there you go
// well don't expect too much! :)

//Here we go, creating a DIB section and a palette, then writing something onto the buffer
//and blitting it !

#define WIDTH 640 // The width of your window
#define HEIGHT 480 // The height of your window

struct pBITMAPINFO
{
 BITMAPINFOHEADER bmiHeader;
 RGBQUAD bmiColors[256];
}BMInfo;

struct pLOGPALETTE
{
 WORD palVersion;
 WORD palNumEntries;
 PALETTEENTRY palPalEntry[256];
}PalInfo;

HBITMAP hBM;
unsigned char* double_buffer = NULL;


// This goes for the initialization of the DIB section and the palatte 
 int i;
 HDC hDC = GetDC(g_hWnd);
 HPALETTE hPal;
 RGBQUAD palette[256];

 BMInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
 BMInfo.bmiHeader.biWidth = WIDTH;
 BMInfo.bmiHeader.biHeight = -abs(HEIGHT); // The minus is for top to bottom origin
 BMInfo.bmiHeader.biPlanes = 1;
 BMInfo.bmiHeader.biBitCount = 8; // 8 bits color depth
 BMInfo.bmiHeader.biCompression = BI_RGB; // RGB - Uncompressed
 BMInfo.bmiHeader.biSizeImage = 0;
 BMInfo.bmiHeader.biXPelsPerMeter = 0;
 BMInfo.bmiHeader.biYPelsPerMeter = 0;
 BMInfo.bmiHeader.biClrUsed = 256; // We have 256 quads
 BMInfo.bmiHeader.biClrImportant = 256; // Use all 256 quads

 SetPaletteTable(palette); // Set the palette table as you wish

 for(i = 0; i < 256; i++)
  BMInfo.bmiColors[i] = palette[i];

 PalInfo.palVersion = 0x300;
 PalInfo.palNumEntries = 256;
 for(i = 0; i < 256; i++)
 {
  PalInfo.palPalEntry[i].peRed = palette[i].rgbRed;
  PalInfo.palPalEntry[i].peGreen = palette[i].rgbGreen;
  PalInfo.palPalEntry[i].peBlue = palette[i].rgbBlue;
  PalInfo.palPalEntry[i].peFlags = PC_NOCOLLAPSE;
 }

 //Create the palette
 hPal = CreatePalette((LOGPALETTE*)&PalInfo);
 //Select it into the DC
 HPALETTE hOldpal = SelectPalette(hDC, hPal, FALSE);
 // Realize the palette on that DC
 RealizePalette(hDC);
 //Delete palette handler
 SelectPalette(hDC, hOldpal, FALSE);
 DeleteObject(hPal);

 hBM = CreateDIBSection(hDC, (BITMAPINFO*)&BMInfo, DIB_RGB_COLORS, (void**)&double_buffer, 0, 0);
 ReleaseDC(g_hWnd, hDC);


//Now you can have something like the good ol' dos days!!
//I leave the optimizations up to you
void PutPixel(int x, int y, unsigned char c)
{
 double_buffer[y * WIDTH + x] = c;
}

// This is the code to blit the buffer onto the dc.

 RECT Dest;
 HDC hDC, Context;
 HBITMAP DefaultBitmap;

 hDC = GetDC(g_hWnd);
 GetClientRect(g_hWnd, &Dest);
 Context = CreateCompatibleDC(NULL);

 DefaultBitmap = (HBITMAP)SelectObject(Context, hBM);
 BitBlt(hDC, 0, 0, Dest.right, Dest.bottom, Context, 0, 0, SRCCOPY);
 SelectObject(Context, DefaultBitmap);
 
 DeleteDC(Context);
 ReleaseDC(g_hWnd, hDC);


//The SetPaletteTable should look something like this:
// Note that this creates a grey-scale palette
void SetPaletteTable(RGBQUAD* pPal)
{
 for (int i = 0; i < 256; i++)
 {
  pPal[i].rgbRed = i;
  pPal[i].rgbGreen = i;
  pPal[i].rgbBlue = i;
  pPal[i].rgbReserved = 0;
 }
}

// Clean up!!
 DeleteObject(hBM);
NOTE:
Comments that will hurt anyone in any way will be deleted.
Don't ask for features, advertise or curse.
If you want to leave a message to the author use the contacts,
if you have any question in relation to your comments please use the forum.
Comments which violate any of these requests will be deleted without further
notice. Use the comment system decently.

Post your comment:
Name:
email:
Comment:
::Top 5 Tutorials::
Embedded Python[117052]
2D Rotated Rectangles Collision Detection[88980]
Keyboard Hook[77319]
UDP[65823]
HTTP Proxy[41212]

::Top 5 Samples::
2D ColDet Rotated Rectangles[11561]
PS2 Mouse Driver[6958]
Wave Format Player[5792]
Reading FAT12[5620]
CodeGuru[5359]


All rights reserved to RageStorm © 2009