// 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); |