Juan Mar
Juan Mar

Reputation: 59

Tic Tac Toe: Winapi painting

I am having trouble painting in my client area whenever I turn a bool true, trying to make a Tic Tac Toe, and once I get the initial painting properly I can keep going.

The main problem is that it is not painting the lower rows properly, or they are painted after a 3 or 4 click, At the moment I don't care about circles, just want to know what I am have wrong.

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        static BOOL fState[DIVISIONS][DIVISIONS];
        bool  XorO = false;
        static int  cxBlock, cyBlock;`

        HDC         hdc;
        int         x, y, iPosX , iPosY;
        PAINTSTRUCT ps;
        RECT        rect;

        static const int grid = 1000;
        static const int block = grid / DIVISIONS;
        static const int width = 1200;
        static const int height =1200;

        switch (message)
        {
        case WM_SIZE:

                cxBlock = LOWORD (lParam) / DIVISIONS ;
                cyBlock = HIWORD (lParam) / DIVISIONS ;

            return 0;

        case WM_LBUTTONDOWN:
            x = LOWORD(lParam)/ cxBlock;
            y = HIWORD(lParam)/cyBlock;

            if (x < DIVISIONS && y < DIVISIONS)
            {
                //Click in the first cuadrant x = 0 and y = 0 , so both false
                //second x = 1, y = 0
                //third x 2 , y = 0 and so on.
                fState[x][y] ^= 1;
                rect.left = x * block;
                rect.top = y * block;
                rect.right = (x + 1) * block;
                rect.bottom = (y + 1) * block;

                InvalidateRect(hwnd, &rect, FALSE);

            }
            else
                MessageBeep(0); 

            return 0;

        case WM_PAINT:

            hdc = BeginPaint(hwnd, &ps);


            //paint X or ellipses here
            for (x = 0; x < DIVISIONS; x++)
                for (y = 0; y < DIVISIONS; y++)
                {

                    if (fState[x][y] )
                    {
                        MoveToEx(hdc, x     * block, y       *  block, NULL);
                        LineTo(hdc,  (x + 1) * block, (y + 1) *  block);
                        MoveToEx(hdc, x     * block, (y + 1) *  block, NULL);
                        LineTo(hdc,  (x + 1) * block, y       *  block);
                    }
                }


            //main grid
            for (int i = block; i < grid - 1; i += block) {
                MoveToEx(hdc, i, 0, NULL); 
                LineTo(hdc, i, grid);
                MoveToEx(hdc, 0, i, NULL);
                LineTo(hdc, grid, i);


            }

            EndPaint(hwnd, &ps);
            return 0;

        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        }
        return DefWindowProc(hwnd, message, wParam, lParam);
    }

I even tried to make the painting more direct and primitive by making a single if statement for each scenario but, once the program runs , it does not paint correctly on the second and third line. or if I click space 2,2 (the last one on the grid) it does not paint until i click somewhere else...

Upvotes: 0

Views: 404

Answers (1)

Joel Spolsky
Joel Spolsky

Reputation: 33667

You need to call SelectObject to select a suitable pen into the DC or else LineTo will randomly use whatever pen was leftover there (which might be invisible or something).

Upvotes: 2

Related Questions