Reputation: 523
I am trying to draw a 4x4 grid of 100x100 rectangles with 50px between them for a 2048 game. I have followed a tutorial and tried to make a drawCases() function to be called on window creation. The window is created and everything works (except the rectangles are not drawn) if I comment out the following line:
FillRect(hdc,&rectangles[i][j],(HBRUSH)GetStockObject(LTGRAY_BRUSH));
Otherwise the program crashes. Here is the whole function:
void drawCases(HWND hwnd){
HDC hdc = GetDC(hwnd);
// Error Check
if(!hdc)
return;
RECT clientRect;
RECT rectangles[4][4];
GetClientRect(hwnd,&clientRect); // Get the window's client area RECT
FillRect(hdc,&clientRect,(HBRUSH)GetStockObject(BLACK_BRUSH));
int leftStart = (clientRect.right)/2 - 200;
int topStart = (clientRect.bottom)/2 - 200;
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; i++){
int k = j * 150;
int n = i * 150;
rectangles[i][j].left = k + leftStart;
rectangles[i][j].right = k + leftStart + 100;
rectangles[i][j].top = topStart + n;
rectangles[i][j].bottom = topStart + n + 100;
FillRect(hdc,&rectangles[i][j],(HBRUSH)GetStockObject(LTGRAY_BRUSH));
}
}
ReleaseDC(hwnd,hdc);
}
Thank you for your help!
Upvotes: 2
Views: 364
Reputation: 2776
Here is the possible mistake:
for (int j = 0; j < 4; i++){
Replace i++
with j++
Upvotes: 5