Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switch/case within a switch/case

The following appears in my WinProc:

if(message == WM_CREATE)
{
//Do WM_CREATE stuff
}

else
{
    switch(message)
    {
        case WM_KEYDOWN:
        {
            switch(wParam)
            {
                case VK_LEFT:
                {
                //declare new variable here
                D2D1_RECT_F bounds;
                HRESULT hr = pDemoApp->mpGeometryGroup->GetBounds(pDemoApp->mTransform, &bounds);
                }
             }
         }
    }
}

Is there any problem with declaring and using variables this way?

I set up a breakpoint after I declare and use bounds (still within the scope) but I can't seem to find it in the 'Locals' window in the debugger. What is wrong?

I didn't want to spam the post with a bunch of unrelated code, but here is the full WinProc.

LRESULT CALLBACK DemoApp::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT result = 0;

if (message == WM_CREATE)
{
    LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
    DemoApp *pDemoApp = (DemoApp *)pcs->lpCreateParams;

    ::SetWindowLongPtrW(
        hwnd,
        GWLP_USERDATA,
        PtrToUlong(pDemoApp)
        );

    result = 1;
}
else
{
    DemoApp *pDemoApp = reinterpret_cast<DemoApp *>(static_cast<LONG_PTR>(
        ::GetWindowLongPtrW(
        hwnd,
        GWLP_USERDATA
        )));

    bool wasHandled = false;

    if (pDemoApp)
    {
        switch (message)
        {
        case WM_SIZE:
            {
                UINT width = LOWORD(lParam);
                UINT height = HIWORD(lParam);
                pDemoApp->OnResize(width, height);
            }
            result = 0;
            wasHandled = true;
            break;

        case WM_DISPLAYCHANGE:
            {
                InvalidateRect(hwnd, NULL, FALSE);
            }
            result = 0;
            wasHandled = true;
            break;

        case WM_PAINT:
            {
                pDemoApp->OnRender();
                ValidateRect(hwnd, NULL);
            }
            result = 0;
            wasHandled = true;
            break;

        case WM_KEYDOWN:
            {
                D2D1_SIZE_F rtSize = pDemoApp->mpRenderTarget->GetSize();
                static float angle = 0.0f;

                switch(wParam)
                {
                case VK_LEFT:
                    {

                        angle -= 90;

                        if(angle < -360)
                            angle = 0;

                        D2D1_RECT_F bounds;
                        HRESULT hr = pDemoApp->mpGeometryGroup->GetBounds(pDemoApp->mTransform, &bounds);

                            pDemoApp->mTransform = D2D1::Matrix3x2F::Rotation(
                            angle,
                            D2D1::Point2F((bounds.right + bounds.left)/2, (bounds.bottom + bounds.top)/2)
                            );

                            hr = hr;


                        InvalidateRect(hwnd, NULL, FALSE);
                        break;
                        }

                case VK_RIGHT:
                    {
                        angle += 90;

                        if(angle > 360)
                            angle = 0;

                        D2D1_RECT_F bounds;
                        pDemoApp->mpGeometryGroup->GetBounds(pDemoApp->mTransform, &bounds);

                            pDemoApp->mTransform = D2D1::Matrix3x2F::Rotation(
                            angle,
                            D2D1::Point2F((bounds.right + bounds.left)/2, (bounds.bottom + bounds.top)/2)
                            );


                        InvalidateRect(hwnd, NULL, FALSE);
                        break;
                    }

                case VK_DOWN:
                    {
                        pDemoApp->mTransform = pDemoApp->mTransform * D2D1::Matrix3x2F::Translation(
                            0.0f,
                            5.0f);

                        InvalidateRect(hwnd, NULL, FALSE);
                        break;
                    }
                }
            }
            result = 0;
            wasHandled = true;
            break;

        case WM_LBUTTONDOWN:
            {
                FLOAT xPos, yPos;

                xPos = LOWORD(lParam);
                yPos = HIWORD(lParam);

                BOOL contains = false;

                pDemoApp->mpGeometryGroup->FillContainsPoint(
                    D2D1::Point2F(xPos, yPos),
                    pDemoApp->mTransform,
                    &contains);

                if(contains)
                    MessageBoxA(hwnd, "Hooray!", NULL, NULL);

                D2D1_GEOMETRY_RELATION relation;

                pDemoApp->mpGeometryGroup->CompareWithGeometry(
                    pDemoApp->mpSecondGeometryGroup,
                    pDemoApp->mTransform,
                    0.001f,
                    &relation);

                if(relation == D2D1_GEOMETRY_RELATION_CONTAINS ||
                    relation == D2D1_GEOMETRY_RELATION_IS_CONTAINED ||
                    relation == D2D1_GEOMETRY_RELATION_OVERLAP)
                {
                    MessageBoxA(hwnd, "overlap or contains.", 0, 0);
                }


            }
            result = 0;
            wasHandled = true;
            break;

        case WM_DESTROY:
            {
                PostQuitMessage(0);
            }
            result = 1;
            wasHandled = true;
            break;
        }
    }

    if (!wasHandled)
    {
        result = DefWindowProc(hwnd, message, wParam, lParam);
    }
}

return result;

}

like image 967
Kyle Avatar asked Nov 14 '22 16:11

Kyle


1 Answers

There is no problem in declaring variables that way, since you have specified a new scope for the VK_LEFT case. If you weren't declaring a separated scope, then variables would be visible yet possibly non-initialized which would be a problem. Note that you missed a few breaks by the way.

like image 80
K-ballo Avatar answered Dec 30 '22 13:12

K-ballo