I'm using the WinAPI directly for drawing a grid, and extrapolating a 32x32 bitmap image to fit inside of that grid. (Basically, it's a zoom function with a grid-overlay.) Anyways, I've been looking for alternative ways to do this that would be faster but I can't quite find any, so I'm here asking for help!
The code takes around 3-4 seconds to draw the "image." I would like it to be instant. I'm not sure how I can optimize the code, so, any suggestions/pointers in the right direction would be _wonderful!_ Thanks in advanced if anyone can help me:
(btw: I've tested the code with just drawing the grid and just drawing the bitmap, it appears to take roughly 3-4 seconds regardless of weather I render only the grid, only the bitmap, or both. It doesn't make any sense to me, seems like it should take longer to draw both together if it was the drawing code at fault, so I'm wondering if it's either my loops or something else, but I'm not sure how to optimize any of it :x)
EDIT: Oh, also CSize is a class that basically stores two ints, int x and int y. getX() returns that x, and getY() returns that y. (It's really basic, the prototype for the functions is pretty much simply void getX() { return x; } void getY() { return y; }) I doubt that's the slow-down...but maybe?
Code:
bool CCharEdit::renderGrid(HDC hdc)
{
// Stuff we need
RECT gridRect;
CSize gridRectSize;
HDC hdcMem;
HBITMAP hBmOld;
BITMAP bm;
// Create a Black color
HBRUSH hBr = CreateSolidBrush(RGB(0,0,0));
// find the size of each grid "pixel"
gridRectSize.setSize((m_gridZoom*m_gridSize.getX()), (m_gridZoom*m_gridSize.getY()));
// Set the upper-bounds for the grid
gridRect.top = 10;
gridRect.left = 10;
// Find the lower-bounds for the grid
gridRect.bottom = gridRect.top + gridRectSize.getY();
gridRect.right = gridRect.left + gridRectSize.getX();
// Initialize the bitmap
hdcMem = CreateCompatibleDC(hdc);
hBmOld = (HBITMAP)SelectObject(hdcMem, m_hBmp);
GetObject(m_hBmp, sizeof(bm), &bm);
// Draw the grid
for ( int i = 0; i < m_Size.getX(); ++i ) // the rows
{
for ( int j = 0; j < m_Size.getY(); ++j ) // the columns
{
// Draw the grid-square
MoveToEx(hdc, gridRect.left, gridRect.top, NULL);
LineTo(hdc, gridRect.left, gridRect.bottom);
LineTo(hdc, gridRect.right, gridRect.bottom);
LineTo(hdc, gridRect.right, gridRect.top);
LineTo(hdc, gridRect.left, gridRect.top);
// Render the portion of the model
// that corresponds to this portion
// of the grid
for ( int k = 1; k < m_gridZoom; ++k )
{
for ( int l = 1; l < m_gridZoom; ++l )
{
BitBlt(hdc, gridRect.left+k, gridRect.top+l, 1,
1, hdcMem, i, j, SRCCOPY);
}
}
// Translate on to the next grid-square
gridRect.top = gridRect.bottom;
gridRect.bottom = gridRect.top + gridRectSize.getY();
}
// Translate on to the next row
gridRect.left = gridRect.right;
gridRect.right = gridRect.left + gridRectSize.getX();
// Set the grid column back to the top
gridRect.top = 10;
gridRect.bottom = gridRect.top + gridRectSize.getY();
}
// Clean Up
DeleteObject(hBr);
SelectObject(hdcMem, hBmOld);
DeleteDC(hdcMem);
DeleteObject(hBmOld);
// return true for success
return true;
}