// COTD Entry submitted by Joshua Carmody [paladinoftheweb@hotmail.com] #include /*************************************************************************/ // drawingSurface class. Allows for very easy drawing to a window. /*************************************************************************/ class drawingSurface { public: drawingSurface(int width, int height); // Constructor ~drawingSurface(); // Destructor void setPixel(int x, int y, unsigned long color); // Set Pixel data in our pointer to the specified color void drawLine(int x1, int y1, int x2, int y2, unsigned long color); // Draw a line from x1,y1 to x2,y2 in the specified color void display(int x, int y, HWND windowHandle); // Copies window pixels from memory pointer to screen. void erase(unsigned long color); // Completely fills the bitmap with the specified color int getWidth(); // Return the bitmap's width. int getHeight(); // Return the bitmap's height. protected: HBITMAP scratchPadBitmap; // Temporary Bitmap unsigned long *windowContents; // Pointer to pixel data to display in the window int windowWidth, windowHeight; // Store the window's dimensions }; /*************************************************************************/ // drawingSurface constructor. Creates a "drawing surface" (DIB) for the // application to draw to. Takes two parameters, width, and height. /*************************************************************************/ drawingSurface::drawingSurface(int width, int height) { windowWidth = width; // Store the width windowHeight = height; // Store the height HANDLE heap = GetProcessHeap(); // Get a memory block from windows // Allocate memory for the BITMAPINFO and BITMAPINFOHEADER structures. // The BITMAPINFO structure is just a BITMAPINFOHEADER structure with // RGBQUADs at the end, so we use this code so both structures share the // same memory. BITMAPINFO *format = (BITMAPINFO *)HeapAlloc(heap, 0, sizeof(BITMAPINFOHEADER) + (sizeof(RGBQUAD) * 3)); BITMAPINFOHEADER *header = (BITMAPINFOHEADER*)format; header->biSize = sizeof(BITMAPINFOHEADER); // This structure is as big as it is. This is a stupid value required by windows header->biWidth = width; // Width of the bitmap header->biHeight = -height; // Height of the bitmap. Negative values indicate a top-down bitmap instead of a bottom-up header->biPlanes = 1; // 1 plane. Always 1 plane. Never changes. header->biBitCount = 32; // 32 bits per pixel, TrueColor header->biCompression = BI_RGB; // No compression header->biSizeImage = 0; // The size of the image, we can ommit this for a non-compressed image header->biXPelsPerMeter = 0; // number of pixels per horizontal meter (we don't care) header->biYPelsPerMeter = 0; // number of pixels per vertical meter (we don't care) header->biClrUsed = 0; // How many colors are used? 0 means maxiumum header->biClrImportant = 0; // How many colors are "important", 0 means all of 'em // Create the DIB scratchPadBitmap = CreateDIBSection( NULL, // DC for the DIB top be compatible with, doesn't matter for 32-bit images format, // Bitmap Information DIB_RGB_COLORS, // Direct RGB values, instead of paletted values (void**)&windowContents, // A pointer to receive a pointer to the bitmap's bytes NULL, // This parameter and the next one have to deal with file mapping, 0 // a subject I don't completely understand. ); HeapFree(heap,0,format); // Free previously allocated memory } /*************************************************************************/ // Destructor for drawingSurface, cleans up vairables and frees memory. /*************************************************************************/ drawingSurface::~drawingSurface() { DeleteObject(scratchPadBitmap); } /*************************************************************************/ // setPixel, sets the pixel at x, y to color inside the DIB /*************************************************************************/ void drawingSurface::setPixel(int x, int y, unsigned long color) { *(windowContents + (x + (y * windowWidth))) = color; } /*************************************************************************/ // drawLine draws a line from x1, y1 to x2, y2 in the specified color /*************************************************************************/ void drawingSurface::drawLine(int x1, int y1, int x2, int y2, unsigned long color) { int i, j, k, deltaX, deltaY, a, testX, testY; testX = deltaX = (x2 - x1); testY = deltaY = (y2 - y1); if(testX < 0) { testX *= -1; } if(testY < 0) { testY *= -1; } if(testX > testY) { for(i=0;isetPixel(x, y, color); } /*************************************************************************/ // Passes a request to draw a line on to the drawing surface. /*************************************************************************/ void bitmapWindow::drawLine(int x1, int y1, int x2, int y2, unsigned long color) { windowContents->drawLine(x1, y1, x2, y2, color); } /*************************************************************************/ // Passes a request to erase a line on to the drawingSurface. /*************************************************************************/ void bitmapWindow::erase(unsigned long color) { windowContents->erase(color); } /*************************************************************************/ // Display's this windows drawing surface /*************************************************************************/ void bitmapWindow::flipIntoView() { windowContents->display(0, 0, windowHandle); } /*************************************************************************/ // Registers the window class we use /*************************************************************************/ void bitmapWindow::registerWindowClass(HINSTANCE currentInstance) { WNDCLASS windowClass; windowClass.style = CS_OWNDC; // The style of the window. CS_OWNDC means every window has it's own DC windowClass.lpfnWndProc = bitmapWindowHandler; // The function to call when this window receives a message windowClass.cbClsExtra = 0; // Extra bytes to allocate for this class (none) windowClass.cbWndExtra = 0; // Extra bytes to allocate for each window (none) windowClass.hInstance = currentInstance; // This application's instance windowClass.hIcon = LoadIcon(currentInstance, IDI_APPLICATION); // A standard Icon windowClass.hCursor = LoadCursor(currentInstance, IDC_ARROW); // A standard cursor windowClass.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE; // A standard background windowClass.lpszMenuName = NULL; // No menus in this window windowClass.lpszClassName = "Library_BMP_Window"; // A name for this class RegisterClass(&windowClass); // Register the class classIsRegistered = 1; // Did we register the class? Yes, we did. }